Update main.py
Browse files
main.py
CHANGED
|
@@ -4,8 +4,13 @@ from starlette.responses import StreamingResponse
|
|
| 4 |
from starlette.background import BackgroundTask
|
| 5 |
import os
|
| 6 |
import random
|
|
|
|
| 7 |
from contextlib import asynccontextmanager
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# --- Configuration ---
|
| 10 |
# The target URL is configurable via an environment variable.
|
| 11 |
TARGET_URL = os.getenv("TARGET_URL", "https://api.gmi-serving.com/v1/chat")
|
|
@@ -40,7 +45,7 @@ app = FastAPI(docs_url=None, redoc_url=None, lifespan=lifespan)
|
|
| 40 |
async def _reverse_proxy(request: Request):
|
| 41 |
"""
|
| 42 |
Forwards a request to the target URL with retry logic and spoofed IP headers.
|
| 43 |
-
It allows for a user-provided Authorization header.
|
| 44 |
"""
|
| 45 |
client: httpx.AsyncClient = request.app.state.http_client
|
| 46 |
|
|
@@ -60,6 +65,10 @@ async def _reverse_proxy(request: Request):
|
|
| 60 |
|
| 61 |
# 3. Generate a random IP for spoofing headers.
|
| 62 |
random_ip = generate_random_ip()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
# 4. Set the specific, required headers for the target API.
|
| 65 |
# This will overwrite any conflicting headers from the original request.
|
|
|
|
| 4 |
from starlette.background import BackgroundTask
|
| 5 |
import os
|
| 6 |
import random
|
| 7 |
+
import logging
|
| 8 |
from contextlib import asynccontextmanager
|
| 9 |
|
| 10 |
+
# --- Logging Configuration ---
|
| 11 |
+
# Configure logging to display INFO level messages.
|
| 12 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 13 |
+
|
| 14 |
# --- Configuration ---
|
| 15 |
# The target URL is configurable via an environment variable.
|
| 16 |
TARGET_URL = os.getenv("TARGET_URL", "https://api.gmi-serving.com/v1/chat")
|
|
|
|
| 45 |
async def _reverse_proxy(request: Request):
|
| 46 |
"""
|
| 47 |
Forwards a request to the target URL with retry logic and spoofed IP headers.
|
| 48 |
+
It allows for a user-provided Authorization header and logs the spoofed IP.
|
| 49 |
"""
|
| 50 |
client: httpx.AsyncClient = request.app.state.http_client
|
| 51 |
|
|
|
|
| 65 |
|
| 66 |
# 3. Generate a random IP for spoofing headers.
|
| 67 |
random_ip = generate_random_ip()
|
| 68 |
+
|
| 69 |
+
# --- ADDED LOGGING ---
|
| 70 |
+
# Log the original client IP and the spoofed IP being used for the request.
|
| 71 |
+
logging.info(f"Client '{request.client.host}' is being proxied with spoofed IP: {random_ip}")
|
| 72 |
|
| 73 |
# 4. Set the specific, required headers for the target API.
|
| 74 |
# This will overwrite any conflicting headers from the original request.
|