diff --git a/src/adsb_api/app.py b/src/adsb_api/app.py index 5de8520..64f65b2 100644 --- a/src/adsb_api/app.py +++ b/src/adsb_api/app.py @@ -202,10 +202,8 @@ async def metrics(): tags=["v0"], summary="Information about your receiver and global stats", ) -async def api_me( - x_original_forwarded_for: str | None = Header(default=None, include_in_schema=False) -): - client_ip = x_original_forwarded_for +async def api_me(request: Request): + client_ip = request.client.host my_beast_clients = provider.get_clients_per_client_ip(client_ip) mlat_clients = provider.mlat_clients_to_list(client_ip) @@ -231,58 +229,9 @@ async def api_me( return response -@app.get( - "/0/mylocalip/{ips}", - response_class=PrettyJSONResponse, - tags=["v0"], - include_in_schema=False, -) -async def mylocalip_put( - ips=str, - x_original_forwarded_for: str - | None = Header(default=None, include_in_schema=False), -): - client_ip = x_original_forwarded_for - # ips can be separated by , - # ensure each IP is also somewhat valid. - ips = [ip for ip in ips.split(",") if ipaddress.ip_address(ip)] - if not ips: - return {"error": "no valid IPs found"} - await redisVRS.redis.setex("mylocalip:" + client_ip, 60, ",".join(ips)) - return {"success": True, "ips": ips} - - -@app.get("/0/mylocalip", tags=["v0"], include_in_schema=False) -async def mylocalip_get( - request: Request, - x_original_forwarded_for: str - | None = Header(default=None, include_in_schema=False), -): - client_ip = x_original_forwarded_for - # this is the page the user loads if they want to see their local IPs - # if there is only one ip, redirect them to it - # if there are multiple, show them some clickable links for each - # if there are none, show them a message saying no IPs found - my_ips = await redisVRS.redis.get("mylocalip:" + client_ip) - if my_ips: - my_ips = my_ips.decode().split(",") - if len(my_ips) == 1: - return RedirectResponse(url="http://" + my_ips[0] + ":5000/") - else: - return templates.TemplateResponse( - "mylocalip.html", {"ips": my_ips, "request": request} - ) - else: - return templates.TemplateResponse( - "mylocalip.html", {"ips": [], "request": request} - ) - - @app.get("/api/0/my", tags=["v0"], summary="My Map redirect based on IP") -async def api_my( - x_original_forwarded_for: str | None = Header(default=None, include_in_schema=False) -): - client_ip = x_original_forwarded_for +async def api_my(request: Request): + client_ip = request.client.host my_beast_clients = provider.get_clients_per_client_ip(client_ip) uids = [] if len(my_beast_clients) == 0: diff --git a/src/adsb_api/utils/api_tar.py b/src/adsb_api/utils/api_tar.py index 2feba50..6f708cb 100644 --- a/src/adsb_api/utils/api_tar.py +++ b/src/adsb_api/utils/api_tar.py @@ -53,7 +53,9 @@ async def get_new_screenshot( min_lat, min_lon, max_lat, max_lon = False, False, False, False # get the min and max lat/lon from re-api async with aiohttp.ClientSession() as session: - async with session.get(f"{REAPI_ENDPOINT}/?find_hex={','.join(icaos)}") as response: + async with session.get( + f"{REAPI_ENDPOINT}/?find_hex={','.join(icaos)}" + ) as response: data = await response.json() for aircraft in data["aircraft"]: if not aircraft.get("lat") or not aircraft.get("lon"): diff --git a/src/adsb_api/utils/api_v2.py b/src/adsb_api/utils/api_v2.py index c76b60e..ea3da7e 100644 --- a/src/adsb_api/utils/api_v2.py +++ b/src/adsb_api/utils/api_v2.py @@ -1,4 +1,4 @@ -from fastapi import APIRouter, Header, Path +from fastapi import APIRouter, Request, Path from fastapi.responses import Response from fastapi_cache.decorator import cache @@ -19,14 +19,10 @@ description="Returns all aircraft with [PIA](https://nbaa.org/aircraft-operations/security/privacy/privacy-icao-address-pia/) addresses.", ) @cache(expire=REDIS_TTL) -async def v2_pia( - x_original_forwarded_for: str - | None = Header(default=None, include_in_schema=False), -) -> V2Response_Model: - client_ip = x_original_forwarded_for +async def v2_pia(request: Request) -> V2Response_Model: params = ["all", "filter_pia"] - res = await provider.ReAPI.request(params=params, client_ip=client_ip) + res = await provider.ReAPI.request(params=params, client_ip=request.client.host) return Response(res, media_type="application/json") @@ -36,14 +32,10 @@ async def v2_pia( description="Returns all military registered aircraft.", ) @cache(expire=REDIS_TTL) -async def v2_mil( - x_original_forwarded_for: str - | None = Header(default=None, include_in_schema=False), -) -> V2Response_Model: - client_ip = x_original_forwarded_for +async def v2_mil(request: Request) -> V2Response_Model: params = ["all", "filter_mil"] - res = await provider.ReAPI.request(params=params, client_ip=client_ip) + res = await provider.ReAPI.request(params=params, client_ip=request.client.host) return Response(res, media_type="application/json") @@ -54,13 +46,11 @@ async def v2_mil( ) @cache(expire=REDIS_TTL) async def v2_ladd( - x_original_forwarded_for: str - | None = Header(default=None, include_in_schema=False), + request: Request, ) -> V2Response_Model: - client_ip = x_original_forwarded_for params = ["all", "filter_ladd"] - res = await provider.ReAPI.request(params=params, client_ip=client_ip) + res = await provider.ReAPI.request(params=params, client_ip=request.client.host) return Response(res, media_type="application/json") @@ -77,14 +67,12 @@ async def v2_ladd( @cache(expire=REDIS_TTL) async def v2_squawk_filter( # Allow custom examples + request: Request, squawk: str = Path(default=..., example="1200"), - x_original_forwarded_for: str - | None = Header(default=None, include_in_schema=False), ) -> V2Response_Model: - client_ip = x_original_forwarded_for params = ["all", f"filter_squawk={squawk}"] - res = await provider.ReAPI.request(params=params, client_ip=client_ip) + res = await provider.ReAPI.request(params=params, client_ip=request.client.host) return Response(res, media_type="application/json") @@ -95,14 +83,12 @@ async def v2_squawk_filter( ) @cache(expire=REDIS_TTL) async def v2_type_filter( + request: Request, aircraft_type: str = Path(default=..., example="A320"), - x_original_forwarded_for: str - | None = Header(default=None, include_in_schema=False), ) -> V2Response_Model: - client_ip = x_original_forwarded_for params = [f"find_type={aircraft_type}"] - res = await provider.ReAPI.request(params=params, client_ip=client_ip) + res = await provider.ReAPI.request(params=params, client_ip=request.client.host) return Response(res, media_type="application/json") @@ -118,14 +104,12 @@ async def v2_type_filter( ) @cache(expire=REDIS_TTL) async def v2_reg_filter( + request: Request, registration: str = Path(default=..., example="G-KELS"), - x_original_forwarded_for: str - | None = Header(default=None, include_in_schema=False), ) -> V2Response_Model: - client_ip = x_original_forwarded_for params = [f"find_reg={registration}"] - res = await provider.ReAPI.request(params=params, client_ip=client_ip) + res = await provider.ReAPI.request(params=params, client_ip=request.client.host) return Response(res, media_type="application/json") @@ -141,14 +125,12 @@ async def v2_reg_filter( ) @cache(expire=REDIS_TTL) async def v2_hex_filter( + request: Request, icao_hex: str = Path(default=..., example="4CA87C"), - x_original_forwarded_for: str - | None = Header(default=None, include_in_schema=False), ) -> V2Response_Model: - client_ip = x_original_forwarded_for params = [f"find_hex={icao_hex}"] - res = await provider.ReAPI.request(params=params, client_ip=client_ip) + res = await provider.ReAPI.request(params=params, client_ip=request.client.host) return Response(res, media_type="application/json") @@ -159,14 +141,12 @@ async def v2_hex_filter( ) @cache(expire=REDIS_TTL) async def v2_callsign_filter( + request: Request, callsign: str = Path(default=..., example="JBU1942"), - x_original_forwarded_for: str - | None = Header(default=None, include_in_schema=False), ) -> V2Response_Model: - client_ip = x_original_forwarded_for params = [f"find_callsign={callsign}"] - res = await provider.ReAPI.request(params=params, client_ip=client_ip) + res = await provider.ReAPI.request(params=params, client_ip=request.client.host) return Response(res, media_type="application/json") @@ -182,17 +162,15 @@ async def v2_callsign_filter( ) @cache(expire=REDIS_TTL) async def v2_point( + request: Request, lat: float = Path(..., example=51.89508, ge=-90, le=90), lon: float = Path(..., example=2.79437, ge=-180, le=180), radius: int = Path(..., example=250, ge=0, le=250), - x_original_forwarded_for: str - | None = Header(default=None, include_in_schema=False), ) -> V2Response_Model: radius = min(radius, 250) - client_ip = x_original_forwarded_for res = await provider.ReAPI.request( - params=[f"circle={lat},{lon},{radius}"], client_ip=client_ip + params=[f"circle={lat},{lon},{radius}"], client_ip=request.client.host ) return Response(res, media_type="application/json") @@ -204,15 +182,12 @@ async def v2_point( description="Returns the closest aircraft to a point described by the latitude and longtidude within a radius up to 250nm.", ) async def v2_closest( + request: Request, lat: float = Path(..., example=51.89508, ge=-90, le=90), lon: float = Path(..., example=2.79437, ge=-180, le=180), radius: int = Path(..., example=250, ge=0, le=250), - x_original_forwarded_for: str - | None = Header(default=None, include_in_schema=False), ) -> V2Response_Model: - client_ip = x_original_forwarded_for - res = await provider.ReAPI.request( - params=[f"closest={lat},{lon},{radius}"], client_ip=client_ip + params=[f"closest={lat},{lon},{radius}"], client_ip=request.client.host ) return Response(res, media_type="application/json") diff --git a/src/adsb_api/utils/dependencies.py b/src/adsb_api/utils/dependencies.py index 03ee4de..2ea298c 100644 --- a/src/adsb_api/utils/dependencies.py +++ b/src/adsb_api/utils/dependencies.py @@ -2,7 +2,11 @@ from adsb_api.utils.provider import RedisVRS from adsb_api.utils.provider import FeederData from adsb_api.utils.settings import ENABLED_BG_TASKS -from adsb_api.utils.browser2 import BrowserTabPool, before_add_to_pool_cb, before_return_to_pool_cb +from adsb_api.utils.browser2 import ( + BrowserTabPool, + before_add_to_pool_cb, + before_return_to_pool_cb, +) provider = Provider(enabled_bg_tasks=ENABLED_BG_TASKS) redisVRS = RedisVRS() diff --git a/src/adsb_api/utils/plausible.py b/src/adsb_api/utils/plausible.py index 6d927d4..84f6c1e 100644 --- a/src/adsb_api/utils/plausible.py +++ b/src/adsb_api/utils/plausible.py @@ -23,9 +23,9 @@ def plausible( airportBLat, airportBLon, "50", - "20" + "20", ], capture_output=True, ) distance = orjson.loads(distanceResult.stdout) - return distance['withinThreshold'], distance['distAB'] + return distance["withinThreshold"], distance["distAB"] diff --git a/src/adsb_api/utils/provider.py b/src/adsb_api/utils/provider.py index 0df41ac..a4de4de 100644 --- a/src/adsb_api/utils/provider.py +++ b/src/adsb_api/utils/provider.py @@ -146,7 +146,9 @@ async def fetch_mlat(self): async with self.client_session.get( f"http://{server}:150/sync.json" ) as resp: - data_per_server[this] = self.anonymize_mlat_data(await resp.json()) + data_per_server[this] = self.anonymize_mlat_data( + await resp.json() + ) self.mlat_totalcount_json = { "UPDATED": datetime.now().strftime("%a %b %d %H:%M:%S UTC %Y"), } @@ -224,7 +226,7 @@ def mlat_clients_to_list(self, ip=None): # format of mlat_clients: # { "0A": {"user": {data}}, "0B": {"user": {data}} for server, data in self.mlat_clients.items(): - #for name, client in self.mlat_clients.items(): + # for name, client in self.mlat_clients.items(): for name, client in data.items(): if ip is not None and client["source_ip"] == ip: clients_list.append(