Update app.py
Browse files
app.py
CHANGED
|
@@ -12,37 +12,36 @@ app = FastAPI(title="Secure DuckDuckGo SearXNG Proxy")
|
|
| 12 |
def log(message: str):
|
| 13 |
print(f"[SEARCH-PROXY] {message}", file=sys.stdout, flush=True)
|
| 14 |
|
| 15 |
-
# Secure Token Configuration
|
| 16 |
-
# Priority: 1. Hugging Face Secret Variable (INTERNAL_SEARCH_KEY) | 2. Fallback string
|
| 17 |
AUTH_TOKEN = os.getenv("INTERNAL_SEARCH_KEY", "your-super-secure-search-password")
|
| 18 |
|
| 19 |
@app.get("/")
|
| 20 |
def home():
|
| 21 |
-
"""Public root landing page.
|
| 22 |
return {
|
| 23 |
"status": "healthy",
|
| 24 |
"engine": "DuckDuckGo-SearXNG-Proxy",
|
| 25 |
-
"security": "Enabled (
|
| 26 |
}
|
| 27 |
|
| 28 |
-
|
| 29 |
-
@app.get("/
|
|
|
|
| 30 |
def searxng_compat_endpoint(
|
| 31 |
token: str,
|
| 32 |
q: str = Query(None),
|
| 33 |
format: str = Query("json")
|
| 34 |
):
|
| 35 |
"""
|
| 36 |
-
|
| 37 |
-
to prevent unauthorized external scrapers from using your Space bandwidth.
|
| 38 |
"""
|
| 39 |
-
#
|
| 40 |
if token != AUTH_TOKEN:
|
| 41 |
-
log("Unauthorized search attempt rejected.
|
| 42 |
raise HTTPException(status_code=401, detail="Unauthorized: Invalid Token.")
|
| 43 |
|
| 44 |
if not q:
|
| 45 |
-
return {"results": [], "note": "Token verified.
|
| 46 |
|
| 47 |
log(f"Authorized search request received: '{q}'")
|
| 48 |
|
|
|
|
| 12 |
def log(message: str):
|
| 13 |
print(f"[SEARCH-PROXY] {message}", file=sys.stdout, flush=True)
|
| 14 |
|
| 15 |
+
# Secure Token Configuration from HF Secrets
|
|
|
|
| 16 |
AUTH_TOKEN = os.getenv("INTERNAL_SEARCH_KEY", "your-super-secure-search-password")
|
| 17 |
|
| 18 |
@app.get("/")
|
| 19 |
def home():
|
| 20 |
+
"""Public root landing page."""
|
| 21 |
return {
|
| 22 |
"status": "healthy",
|
| 23 |
"engine": "DuckDuckGo-SearXNG-Proxy",
|
| 24 |
+
"security": "Enabled (Root path locked)"
|
| 25 |
}
|
| 26 |
|
| 27 |
+
# CRITICAL FIX: Capture the token immediately after the root domain slash
|
| 28 |
+
@app.get("/{token}")
|
| 29 |
+
@app.get("/{token}/")
|
| 30 |
def searxng_compat_endpoint(
|
| 31 |
token: str,
|
| 32 |
q: str = Query(None),
|
| 33 |
format: str = Query("json")
|
| 34 |
):
|
| 35 |
"""
|
| 36 |
+
Catches Open WebUI's direct root-level password paths cleanly.
|
|
|
|
| 37 |
"""
|
| 38 |
+
# Validate the token directly
|
| 39 |
if token != AUTH_TOKEN:
|
| 40 |
+
log(f"Unauthorized search attempt rejected. Received token: '{token}' does not match.")
|
| 41 |
raise HTTPException(status_code=401, detail="Unauthorized: Invalid Token.")
|
| 42 |
|
| 43 |
if not q:
|
| 44 |
+
return {"results": [], "note": f"Token '{token}' verified successfully. Add 'q' parameter to search."}
|
| 45 |
|
| 46 |
log(f"Authorized search request received: '{q}'")
|
| 47 |
|