mirror of
https://github.com/donaldzou/WGDashboard.git
synced 2026-10-30 01:12:07 +00:00
Fix: batch IP geolocation requests (limit 100), add deduplication and caching (#1232)
- Added batching of IP addresses into chunks of 100 to comply with ip-api limits - Implemented deduplication to avoid repeated geolocation requests for identical IPs - Added in-memory cache to reuse results across batches - Preserved original order and length of geolocation list in API response - Improved error handling around external API failures
This commit is contained in:
+30
-17
@@ -1140,23 +1140,36 @@ def API_GetPeerHistoricalEndpoints():
|
|||||||
if not configurationName or not id:
|
if not configurationName or not id:
|
||||||
return ResponseObject(False, "Please provide configurationName and id")
|
return ResponseObject(False, "Please provide configurationName and id")
|
||||||
fp, p = WireguardConfigurations.get(configurationName).searchPeer(id)
|
fp, p = WireguardConfigurations.get(configurationName).searchPeer(id)
|
||||||
if fp:
|
if not fp:
|
||||||
result = p.getEndpoints()
|
return ResponseObject(False, "Peer does not exist")
|
||||||
geo = {}
|
endpoints = p.getEndpoints()
|
||||||
try:
|
ips = [x['endpoint'] for x in endpoints]
|
||||||
r = requests.post(f"http://ip-api.com/batch?fields=city,country,lat,lon,query",
|
unique_ips = list(set(ips))
|
||||||
data=json.dumps([x['endpoint'] for x in result]))
|
def chunks(lst, size=100):
|
||||||
d = r.json()
|
for i in range(0, len(lst), size):
|
||||||
|
yield lst[i:i + size]
|
||||||
|
geo_cache = {}
|
||||||
except Exception as e:
|
try:
|
||||||
return ResponseObject(data=result, message="Failed to request IP address geolocation. " + str(e))
|
for batch in chunks(unique_ips, 100):
|
||||||
|
r = requests.post(
|
||||||
return ResponseObject(data={
|
"http://ip-api.com/batch?fields=city,country,lat,lon,query",
|
||||||
"endpoints": p.getEndpoints(),
|
data=json.dumps(batch),
|
||||||
"geolocation": d
|
timeout=5
|
||||||
})
|
)
|
||||||
return ResponseObject(False, "Peer does not exist")
|
r.raise_for_status()
|
||||||
|
data = r.json()
|
||||||
|
for item in data:
|
||||||
|
geo_cache[item["query"]] = item
|
||||||
|
geolocation = [geo_cache[ip] for ip in ips]
|
||||||
|
except Exception as e:
|
||||||
|
return ResponseObject(
|
||||||
|
data={"endpoints": endpoints},
|
||||||
|
message="Failed to request IP address geolocation. " + str(e)
|
||||||
|
)
|
||||||
|
return ResponseObject(data={
|
||||||
|
"endpoints": endpoints,
|
||||||
|
"geolocation": geolocation
|
||||||
|
})
|
||||||
|
|
||||||
@app.get(f'{APP_PREFIX}/api/getPeerSessions')
|
@app.get(f'{APP_PREFIX}/api/getPeerSessions')
|
||||||
def API_GetPeerSessions():
|
def API_GetPeerSessions():
|
||||||
|
|||||||
Reference in New Issue
Block a user