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
76
ipserv.py
76
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,23 +101,25 @@ 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)
|
||||||
data = (await response.json())["data"]
|
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(5)) as session:
|
||||||
return {
|
async with session.get("https://ipinfo.io/widget/demo/" + ip) as response:
|
||||||
"ip": data["ip"],
|
data = (await response.json())["data"]
|
||||||
"city": data["city"],
|
return {
|
||||||
"country": data["region"],
|
"ip": data["ip"],
|
||||||
"countryCode": data["country"],
|
"city": data["city"],
|
||||||
"asn": int(data["asn"]["asn"][2:]),
|
"country": data["region"],
|
||||||
"isp": data["asn"]["name"],
|
"countryCode": data["country"],
|
||||||
"source": "ipinfo",
|
"asn": int(data["asn"]["asn"][2:]),
|
||||||
"lat": data["loc"].split(",")[0],
|
"isp": data["asn"]["name"],
|
||||||
"lon": data["loc"].split(",")[1],
|
"source": "ipinfo",
|
||||||
"hostname": data["hostname"],
|
"lat": data["loc"].split(",")[0],
|
||||||
"timezone": data["timezone"],
|
"lon": data["loc"].split(",")[1],
|
||||||
"subnet": data["abuse"]["network"],
|
"hostname": data["hostname"],
|
||||||
"abuse": data["abuse"]["email"]
|
"timezone": data["timezone"],
|
||||||
}
|
"subnet": data["abuse"]["network"],
|
||||||
|
"abuse": data["abuse"]["email"]
|
||||||
|
}
|
||||||
except json.JSONDecodeError as e:
|
except json.JSONDecodeError as e:
|
||||||
logging.error("Failed to parse data for %s: %s", ip, e, exc_info=True)
|
logging.error("Failed to parse data for %s: %s", ip, e, exc_info=True)
|
||||||
return HTTPException(500, "Failed to parse upstream response.")
|
return HTTPException(500, "Failed to parse upstream response.")
|
||||||
|
@ -126,21 +130,23 @@ 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)
|
||||||
data = await response.json()
|
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(5)) as session:
|
||||||
return {
|
async with session.get("https://ipinfo.io/%s?token=%s" % (ip, token)) as response:
|
||||||
"ip": data["ip"],
|
data = await response.json()
|
||||||
"city": data["city"],
|
return {
|
||||||
"country": data["region"],
|
"ip": data["ip"],
|
||||||
"countryCode": data["country"],
|
"city": data["city"],
|
||||||
"asn": int(data["org"].split()[0][2:]),
|
"country": data["region"],
|
||||||
"isp": data["org"].split(" ", 1)[1],
|
"countryCode": data["country"],
|
||||||
"source": "ipinfo",
|
"asn": int(data["org"].split()[0][2:]),
|
||||||
"lat": data["loc"].split(",")[0],
|
"isp": data["org"].split(" ", 1)[1],
|
||||||
"lon": data["loc"].split(",")[1],
|
"source": "ipinfo",
|
||||||
"hostname": data["hostname"],
|
"lat": data["loc"].split(",")[0],
|
||||||
"timezone": data["timezone"],
|
"lon": data["loc"].split(",")[1],
|
||||||
}
|
"hostname": data["hostname"],
|
||||||
|
"timezone": data["timezone"],
|
||||||
|
}
|
||||||
except json.JSONDecodeError as e:
|
except json.JSONDecodeError as e:
|
||||||
logging.error("Failed to parse data for %s: %s", ip, e, exc_info=True)
|
logging.error("Failed to parse data for %s: %s", ip, e, exc_info=True)
|
||||||
return HTTPException(500, "Failed to parse upstream response.")
|
return HTTPException(500, "Failed to parse upstream response.")
|
||||||
|
@ -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