Add /imfeelinglucky and /raw
This commit is contained in:
parent
1568833853
commit
38eef0e33e
1 changed files with 38 additions and 10 deletions
48
ipserv.py
48
ipserv.py
|
@ -3,8 +3,9 @@ import json
|
||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
import random
|
||||||
from fastapi import FastAPI, Header, Request, Query, HTTPException
|
from fastapi import FastAPI, Header, Request, Query, HTTPException
|
||||||
from fastapi.responses import JSONResponse
|
from fastapi.responses import JSONResponse, PlainTextResponse
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
@ -26,6 +27,13 @@ app.state.cache = {}
|
||||||
|
|
||||||
|
|
||||||
async def make_request(ip: str, headers: dict[str, str]) -> dict | HTTPException:
|
async def make_request(ip: str, headers: dict[str, str]) -> dict | HTTPException:
|
||||||
|
if ip in app.state.cache:
|
||||||
|
data, timestamp = app.state.cache[ip]
|
||||||
|
if time.time() - timestamp < 3600:
|
||||||
|
logging.info("cache hit for %s", ip)
|
||||||
|
return data
|
||||||
|
logging.info("cache expired for %s", ip)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with app.state.session.get(f"/lookup?ip={ip}") as response:
|
async with app.state.session.get(f"/lookup?ip={ip}") as response:
|
||||||
data = await response.json()
|
data = await response.json()
|
||||||
|
@ -51,20 +59,13 @@ async def ip(
|
||||||
else:
|
else:
|
||||||
ip = request.client.host
|
ip = request.client.host
|
||||||
|
|
||||||
if ip in app.state.cache:
|
|
||||||
data, timestamp = app.state.cache[ip]
|
|
||||||
if time.time() - timestamp < 3600:
|
|
||||||
logging.info("cache hit for %s", ip)
|
|
||||||
return JSONResponse(data)
|
|
||||||
logging.info("cache expired for %s", ip)
|
|
||||||
|
|
||||||
logging.info("looking up IP info for %s", ip)
|
logging.info("looking up IP info for %s", ip)
|
||||||
data = await make_request(
|
data = await make_request(
|
||||||
ip,
|
ip,
|
||||||
{"User-Agent": User_Agent}
|
{"User-Agent": User_Agent}
|
||||||
)
|
)
|
||||||
if isinstance(data, HTTPException):
|
if isinstance(data, HTTPException):
|
||||||
return data
|
raise data
|
||||||
data["ip"] = ip
|
data["ip"] = ip
|
||||||
data.pop("legalese", None)
|
data.pop("legalese", None)
|
||||||
data.pop("source", None)
|
data.pop("source", None)
|
||||||
|
@ -89,7 +90,7 @@ async def lookup(ip: str = Query(...), User_Agent: str = Header("Mozilla/5.0")):
|
||||||
{"User-Agent": User_Agent}
|
{"User-Agent": User_Agent}
|
||||||
)
|
)
|
||||||
if isinstance(data, HTTPException):
|
if isinstance(data, HTTPException):
|
||||||
return data
|
raise data
|
||||||
data["ip"] = ip
|
data["ip"] = ip
|
||||||
data.pop("legalese", None)
|
data.pop("legalese", None)
|
||||||
data.pop("source", None)
|
data.pop("source", None)
|
||||||
|
@ -97,3 +98,30 @@ async def lookup(ip: str = Query(...), User_Agent: str = Header("Mozilla/5.0")):
|
||||||
logging.info("%s -> %r", ip, data)
|
logging.info("%s -> %r", ip, data)
|
||||||
app.state.cache[ip] = [data, time.time()]
|
app.state.cache[ip] = [data, time.time()]
|
||||||
return JSONResponse(data)
|
return JSONResponse(data)
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/imfeelinglucky")
|
||||||
|
async def im_feeling_lucky(req: Request):
|
||||||
|
host = req.client.host
|
||||||
|
if host.count(".") != 3:
|
||||||
|
raise HTTPException(400, "IPv4 only endpoint.")
|
||||||
|
|
||||||
|
data = await make_request(host, {"User-Agent": "Mozilla/5.0 Nex/19.04.2024"})
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
raise data
|
||||||
|
data = data.copy()
|
||||||
|
|
||||||
|
parts = list(map(int, host.split(".")))
|
||||||
|
n = random.randint(1, 1000)
|
||||||
|
if n in range(50, 54):
|
||||||
|
if n > 400:
|
||||||
|
parts[n % 4] += 1
|
||||||
|
else:
|
||||||
|
parts[n % 4] -= 1
|
||||||
|
data["ip"] = ".".join(map(str, parts))
|
||||||
|
return JSONResponse(data)
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/raw")
|
||||||
|
def get_raw(req: Request):
|
||||||
|
return PlainTextResponse(req.client.host)
|
||||||
|
|
Reference in a new issue