Spaces:
Sleeping

michdubai commited on
Commit
ae48344
·
verified ·
1 Parent(s): c22da75

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -11
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. Shows healthy status but hides the gateway."""
22
  return {
23
  "status": "healthy",
24
  "engine": "DuckDuckGo-SearXNG-Proxy",
25
- "security": "Enabled (Endpoint is locked)"
26
  }
27
 
28
- @app.get("/search/{token}")
29
- @app.get("/proxy/{token}")
 
30
  def searxng_compat_endpoint(
31
  token: str,
32
  q: str = Query(None),
33
  format: str = Query("json")
34
  ):
35
  """
36
- Secured search gateway. Requires your custom token inside the path string
37
- to prevent unauthorized external scrapers from using your Space bandwidth.
38
  """
39
- # 1. Guard rail validation verification against custom token entry
40
  if token != AUTH_TOKEN:
41
- log("Unauthorized search attempt rejected. Token mismatch or missing.")
42
  raise HTTPException(status_code=401, detail="Unauthorized: Invalid Token.")
43
 
44
  if not q:
45
- return {"results": [], "note": "Token verified. Provide a search query 'q'."}
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