This commit is contained in:
+38
-12
@@ -1327,8 +1327,12 @@ def register_web_routes(app, dependencies):
|
||||
@app.post("/manual/scan")
|
||||
async def api_manual_scan(request: Request):
|
||||
"""Proxy manual scan requests to core container"""
|
||||
import httpx
|
||||
import urllib.request
|
||||
import urllib.parse
|
||||
import urllib.error
|
||||
import json
|
||||
import os
|
||||
import socket
|
||||
|
||||
# Get request body
|
||||
try:
|
||||
@@ -1342,21 +1346,38 @@ def register_web_routes(app, dependencies):
|
||||
core_url = f"http://{core_host}:{core_port}/manual/scan"
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=30.0) as client:
|
||||
response = await client.post(core_url, json=body)
|
||||
return response.json()
|
||||
except httpx.TimeoutException:
|
||||
raise HTTPException(status_code=504, detail="Core container request timed out")
|
||||
except httpx.RequestError as e:
|
||||
# Prepare request data
|
||||
data = json.dumps(body).encode('utf-8')
|
||||
|
||||
# Create request with timeout
|
||||
req = urllib.request.Request(
|
||||
core_url,
|
||||
data=data,
|
||||
headers={'Content-Type': 'application/json'}
|
||||
)
|
||||
|
||||
# Make request with timeout
|
||||
with urllib.request.urlopen(req, timeout=30) as response:
|
||||
response_data = response.read().decode('utf-8')
|
||||
return json.loads(response_data)
|
||||
|
||||
except urllib.error.HTTPError as e:
|
||||
raise HTTPException(status_code=e.code, detail=f"Core container HTTP error: {e.reason}")
|
||||
except urllib.error.URLError as e:
|
||||
raise HTTPException(status_code=503, detail=f"Could not connect to core container: {str(e)}")
|
||||
except socket.timeout:
|
||||
raise HTTPException(status_code=504, detail="Core container request timed out")
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Manual scan request failed: {str(e)}")
|
||||
|
||||
@app.get("/api/scan/status")
|
||||
async def api_scan_status():
|
||||
"""Proxy scan status requests to core container"""
|
||||
import httpx
|
||||
import urllib.request
|
||||
import urllib.error
|
||||
import json
|
||||
import os
|
||||
import socket
|
||||
|
||||
# Get core container URL
|
||||
core_host = os.environ.get("CORE_API_HOST", "nfoguard-core")
|
||||
@@ -1364,10 +1385,15 @@ def register_web_routes(app, dependencies):
|
||||
core_url = f"http://{core_host}:{core_port}/api/scan/status"
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=10.0) as client:
|
||||
response = await client.get(core_url)
|
||||
return response.json()
|
||||
except httpx.RequestError:
|
||||
# Create request with timeout
|
||||
req = urllib.request.Request(core_url)
|
||||
|
||||
# Make request with timeout
|
||||
with urllib.request.urlopen(req, timeout=10) as response:
|
||||
response_data = response.read().decode('utf-8')
|
||||
return json.loads(response_data)
|
||||
|
||||
except (urllib.error.URLError, socket.timeout):
|
||||
# If core is unreachable, return default status
|
||||
return {"scanning": False, "message": "Core container unavailable"}
|
||||
except Exception:
|
||||
|
||||
Reference in New Issue
Block a user