Update main.py
Browse files
main.py
CHANGED
|
@@ -17,7 +17,7 @@ logging.basicConfig(
|
|
| 17 |
)
|
| 18 |
TARGET_URL = os.getenv("TARGET_URL", "https://api.gmi-serving.com")
|
| 19 |
MAX_RETRIES = int(os.getenv("MAX_RETRIES", "15"))
|
| 20 |
-
DEFAULT_RETRY_CODES = "429,500,502,503,504"
|
| 21 |
RETRY_CODES_STR = os.getenv("RETRY_CODES", DEFAULT_RETRY_CODES)
|
| 22 |
try:
|
| 23 |
RETRY_STATUS_CODES = {int(code.strip()) for code in RETRY_CODES_STR.split(',')}
|
|
@@ -124,6 +124,16 @@ def get_random_sec_ch_ua_platform():
|
|
| 124 |
]
|
| 125 |
return random.choice(platforms)
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
# --- HTTPX Client Lifecycle Management ---
|
| 128 |
@asynccontextmanager
|
| 129 |
async def lifespan(app: FastAPI):
|
|
@@ -173,6 +183,7 @@ async def reverse_proxy_handler(request: Request):
|
|
| 173 |
"sec-ch-ua": get_random_sec_ch_ua(),
|
| 174 |
"sec-ch-ua-mobile": random.choice(["?0", "?1"]),
|
| 175 |
"sec-ch-ua-platform": get_random_sec_ch_ua_platform(),
|
|
|
|
| 176 |
"priority": random.choice(["u=0, i", "u=1", "u=2"]),
|
| 177 |
"x-forwarded-for": random_ip,
|
| 178 |
"x-real-ip": random_ip,
|
|
@@ -184,8 +195,8 @@ async def reverse_proxy_handler(request: Request):
|
|
| 184 |
"x-forwarded-proto": random.choice(["http", "https"]),
|
| 185 |
"client-ip": random_ip,
|
| 186 |
"x-client-ip": random_ip,
|
| 187 |
-
"
|
| 188 |
-
"
|
| 189 |
"forwarded": f"for={random_ip};proto=https",
|
| 190 |
}
|
| 191 |
request_headers.update(specific_headers)
|
|
@@ -234,7 +245,7 @@ async def reverse_proxy_handler(request: Request):
|
|
| 234 |
logging.warning(f"Attempt {attempt + 1}/{MAX_RETRIES} for {url.path} failed with connection error: {e}")
|
| 235 |
if attempt == MAX_RETRIES - 1:
|
| 236 |
break
|
| 237 |
-
await asyncio.sleep(2 ** attempt + random.uniform(0.
|
| 238 |
|
| 239 |
except Exception as e:
|
| 240 |
last_exception = e
|
|
|
|
| 17 |
)
|
| 18 |
TARGET_URL = os.getenv("TARGET_URL", "https://api.gmi-serving.com")
|
| 19 |
MAX_RETRIES = int(os.getenv("MAX_RETRIES", "15"))
|
| 20 |
+
DEFAULT_RETRY_CODES = "403,429,500,502,503,504"
|
| 21 |
RETRY_CODES_STR = os.getenv("RETRY_CODES", DEFAULT_RETRY_CODES)
|
| 22 |
try:
|
| 23 |
RETRY_STATUS_CODES = {int(code.strip()) for code in RETRY_CODES_STR.split(',')}
|
|
|
|
| 124 |
]
|
| 125 |
return random.choice(platforms)
|
| 126 |
|
| 127 |
+
def get_random_sec_ch_ua_full_version():
|
| 128 |
+
"""Returns a random sec-ch-ua-full-version header value."""
|
| 129 |
+
versions = [
|
| 130 |
+
"141.0.0.0",
|
| 131 |
+
"131.0.0",
|
| 132 |
+
"18.0.0",
|
| 133 |
+
"140.0.0.0",
|
| 134 |
+
]
|
| 135 |
+
return random.choice(versions)
|
| 136 |
+
|
| 137 |
# --- HTTPX Client Lifecycle Management ---
|
| 138 |
@asynccontextmanager
|
| 139 |
async def lifespan(app: FastAPI):
|
|
|
|
| 183 |
"sec-ch-ua": get_random_sec_ch_ua(),
|
| 184 |
"sec-ch-ua-mobile": random.choice(["?0", "?1"]),
|
| 185 |
"sec-ch-ua-platform": get_random_sec_ch_ua_platform(),
|
| 186 |
+
"sec-ch-ua-full-version": get_random_sec_ch_ua_full_version(),
|
| 187 |
"priority": random.choice(["u=0, i", "u=1", "u=2"]),
|
| 188 |
"x-forwarded-for": random_ip,
|
| 189 |
"x-real-ip": random_ip,
|
|
|
|
| 195 |
"x-forwarded-proto": random.choice(["http", "https"]),
|
| 196 |
"client-ip": random_ip,
|
| 197 |
"x-client-ip": random_ip,
|
| 198 |
+
"x-forwarded-client-ip": random_ip,
|
| 199 |
+
"via": f"1.1 {random_ip}",
|
| 200 |
"forwarded": f"for={random_ip};proto=https",
|
| 201 |
}
|
| 202 |
request_headers.update(specific_headers)
|
|
|
|
| 245 |
logging.warning(f"Attempt {attempt + 1}/{MAX_RETRIES} for {url.path} failed with connection error: {e}")
|
| 246 |
if attempt == MAX_RETRIES - 1:
|
| 247 |
break
|
| 248 |
+
await asyncio.sleep(0.5 + 2 ** attempt + random.uniform(0.2, 0.8)) # Base delay + exponential backoff with jitter
|
| 249 |
|
| 250 |
except Exception as e:
|
| 251 |
last_exception = e
|