Skip to content

Commit

Permalink
Remove unused mylocalip functionality
Browse files Browse the repository at this point in the history
Originally made for adsb.im
  • Loading branch information
iakat committed Oct 5, 2023
1 parent 33fc9a8 commit 081f899
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 107 deletions.
59 changes: 4 additions & 55 deletions src/adsb_api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion src/adsb_api/utils/api_tar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
67 changes: 21 additions & 46 deletions src/adsb_api/utils/api_v2.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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")


Expand All @@ -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")


Expand All @@ -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")


Expand All @@ -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")


Expand All @@ -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")


Expand All @@ -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")


Expand All @@ -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")


Expand All @@ -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")


Expand All @@ -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")

Expand All @@ -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")
6 changes: 5 additions & 1 deletion src/adsb_api/utils/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/adsb_api/utils/plausible.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
6 changes: 4 additions & 2 deletions src/adsb_api/utils/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 081f899

Please sign in to comment.