httpd: improve api error handling

This commit is contained in:
Username
2025-12-21 23:37:49 +01:00
parent b88aa2a878
commit 901f2c1aee

View File

@@ -56,7 +56,7 @@ class ProxyAPIHandler(BaseHTTPServer.BaseHTTPRequestHandler):
"""Show available endpoints."""
endpoints = {
'endpoints': {
'/proxies': 'list working proxies (params: limit, proto, country)',
'/proxies': 'list working proxies (params: limit, proto, country, asn)',
'/proxies/count': 'count working proxies',
'/health': 'health check',
}
@@ -77,10 +77,11 @@ class ProxyAPIHandler(BaseHTTPServer.BaseHTTPRequestHandler):
limit = min(int(params.get('limit', 100)), 1000)
proto = params.get('proto', '')
country = params.get('country', '')
asn = params.get('asn', '')
format = params.get('format', 'json')
# Build query
sql = 'SELECT ip, port, proto, country FROM proxylist WHERE failed=0'
sql = 'SELECT ip, port, proto, country, asn FROM proxylist WHERE failed=0'
args = []
if proto:
@@ -89,6 +90,9 @@ class ProxyAPIHandler(BaseHTTPServer.BaseHTTPRequestHandler):
if country:
sql += ' AND country=?'
args.append(country.upper())
if asn:
sql += ' AND asn=?'
args.append(int(asn))
sql += ' ORDER BY tested DESC LIMIT ?'
args.append(limit)
@@ -109,7 +113,8 @@ class ProxyAPIHandler(BaseHTTPServer.BaseHTTPRequestHandler):
'ip': row[0],
'port': row[1],
'proto': row[2],
'country': row[3]
'country': row[3],
'asn': row[4]
})
self.send_json({'count': len(proxies), 'proxies': proxies})