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(
|
||||
"https://ip.shronk.tech",
|
||||
connector=aiohttp.TCPConnector(limit=2048),
|
||||
raise_for_status=True
|
||||
raise_for_status=True,
|
||||
timeout=aiohttp.ClientTimeout(5)
|
||||
) as client:
|
||||
_app.state.session = client
|
||||
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)
|
||||
|
||||
try:
|
||||
logging.info("Querying SHRoNKNet: %s", ip)
|
||||
async with app.state.session.get(f"/lookup?ip={ip}", headers=headers) as response:
|
||||
data = await response.json()
|
||||
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):
|
||||
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"]
|
||||
return {
|
||||
"ip": data["ip"],
|
||||
|
@ -126,7 +130,9 @@ async def _get_ipinfo_no_token(ip: str):
|
|||
|
||||
async def _get_ipinfo_token(ip: str, token: str):
|
||||
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()
|
||||
return {
|
||||
"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)
|
||||
|
||||
try:
|
||||
data = await get_ip_information(ip)
|
||||
data = await get_from_ipinfo(ip)
|
||||
except Exception as e:
|
||||
logging.error("Failed to contact ipinfo: %s", e, exc_info=True)
|
||||
else:
|
||||
|
@ -195,7 +201,7 @@ async def ip(
|
|||
ip = ".".join(map(str, (random.randint(0, 255) for _ in range(4))))
|
||||
|
||||
logging.info("looking up IP info for %s", ip)
|
||||
data = await make_request(ip)
|
||||
data = await get_ip_information(ip)
|
||||
if isinstance(data, HTTPException):
|
||||
raise data
|
||||
data["ip"] = ip
|
||||
|
|
Reference in a new issue