fixes
All checks were successful
Build and Publish ipserv / build_and_publish (push) Successful in 3m19s
All checks were successful
Build and Publish ipserv / build_and_publish (push) Successful in 3m19s
This commit is contained in:
parent
5a2c2e7ffc
commit
606e700f11
1 changed files with 41 additions and 35 deletions
16
ipserv.py
16
ipserv.py
|
@ -36,7 +36,8 @@ async def lifespan(_app: FastAPI):
|
||||||
async with aiohttp.ClientSession(
|
async with aiohttp.ClientSession(
|
||||||
"https://ip.shronk.tech",
|
"https://ip.shronk.tech",
|
||||||
connector=aiohttp.TCPConnector(limit=2048),
|
connector=aiohttp.TCPConnector(limit=2048),
|
||||||
raise_for_status=True
|
raise_for_status=True,
|
||||||
|
timeout=aiohttp.ClientTimeout(5)
|
||||||
) as client:
|
) as client:
|
||||||
_app.state.session = client
|
_app.state.session = client
|
||||||
yield
|
yield
|
||||||
|
@ -85,6 +86,7 @@ async def make_request(ip: str, headers: dict[str, str] = None) -> dict | HTTPEx
|
||||||
logging.info("cache expired for %s", ip)
|
logging.info("cache expired for %s", ip)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
logging.info("Querying SHRoNKNet: %s", ip)
|
||||||
async with app.state.session.get(f"/lookup?ip={ip}", headers=headers) as response:
|
async with app.state.session.get(f"/lookup?ip={ip}", headers=headers) as response:
|
||||||
data = await response.json()
|
data = await response.json()
|
||||||
data["source"] = "SHRoNKNet"
|
data["source"] = "SHRoNKNet"
|
||||||
|
@ -99,7 +101,9 @@ async def make_request(ip: str, headers: dict[str, str] = None) -> dict | HTTPEx
|
||||||
|
|
||||||
async def _get_ipinfo_no_token(ip: str):
|
async def _get_ipinfo_no_token(ip: str):
|
||||||
try:
|
try:
|
||||||
async with app.state.session.get("https://ipinfo.io/widget/demo/" + ip) as response:
|
logging.info("Querying IPInfo WITHOUT TOKEN: %s", ip)
|
||||||
|
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(5)) as session:
|
||||||
|
async with session.get("https://ipinfo.io/widget/demo/" + ip) as response:
|
||||||
data = (await response.json())["data"]
|
data = (await response.json())["data"]
|
||||||
return {
|
return {
|
||||||
"ip": data["ip"],
|
"ip": data["ip"],
|
||||||
|
@ -126,7 +130,9 @@ async def _get_ipinfo_no_token(ip: str):
|
||||||
|
|
||||||
async def _get_ipinfo_token(ip: str, token: str):
|
async def _get_ipinfo_token(ip: str, token: str):
|
||||||
try:
|
try:
|
||||||
async with app.state.session.get("https://ipinfo.io/%s?token=%s" % (ip, token)) as response:
|
logging.info("Querying IPInfo WITH token: %s", ip)
|
||||||
|
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(5)) as session:
|
||||||
|
async with session.get("https://ipinfo.io/%s?token=%s" % (ip, token)) as response:
|
||||||
data = await response.json()
|
data = await response.json()
|
||||||
return {
|
return {
|
||||||
"ip": data["ip"],
|
"ip": data["ip"],
|
||||||
|
@ -170,7 +176,7 @@ async def get_ip_information(ip: str) -> dict | HTTPException:
|
||||||
logging.error("Failed to contact shronk net ip: %s", e, exc_info=True)
|
logging.error("Failed to contact shronk net ip: %s", e, exc_info=True)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
data = await get_ip_information(ip)
|
data = await get_from_ipinfo(ip)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error("Failed to contact ipinfo: %s", e, exc_info=True)
|
logging.error("Failed to contact ipinfo: %s", e, exc_info=True)
|
||||||
else:
|
else:
|
||||||
|
@ -195,7 +201,7 @@ async def ip(
|
||||||
ip = ".".join(map(str, (random.randint(0, 255) for _ in range(4))))
|
ip = ".".join(map(str, (random.randint(0, 255) for _ in range(4))))
|
||||||
|
|
||||||
logging.info("looking up IP info for %s", ip)
|
logging.info("looking up IP info for %s", ip)
|
||||||
data = await make_request(ip)
|
data = await get_ip_information(ip)
|
||||||
if isinstance(data, HTTPException):
|
if isinstance(data, HTTPException):
|
||||||
raise data
|
raise data
|
||||||
data["ip"] = ip
|
data["ip"] = ip
|
||||||
|
|
Reference in a new issue