2024-02-05 18:48:30 +08:00
|
|
|
"""
|
|
|
|
Proxy requests to avoid SSRF
|
|
|
|
"""
|
2024-07-25 13:10:39 +08:00
|
|
|
import logging
|
2024-02-05 18:48:30 +08:00
|
|
|
import os
|
2024-07-25 13:10:39 +08:00
|
|
|
import time
|
2024-02-05 18:48:30 +08:00
|
|
|
|
2024-06-24 16:14:59 +08:00
|
|
|
import httpx
|
2024-02-06 13:21:13 +08:00
|
|
|
|
2024-06-24 16:14:59 +08:00
|
|
|
SSRF_PROXY_ALL_URL = os.getenv('SSRF_PROXY_ALL_URL', '')
|
2024-02-05 18:48:30 +08:00
|
|
|
SSRF_PROXY_HTTP_URL = os.getenv('SSRF_PROXY_HTTP_URL', '')
|
|
|
|
SSRF_PROXY_HTTPS_URL = os.getenv('SSRF_PROXY_HTTPS_URL', '')
|
2024-07-25 13:10:39 +08:00
|
|
|
SSRF_DEFAULT_MAX_RETRIES = int(os.getenv('SSRF_DEFAULT_MAX_RETRIES', '3'))
|
2024-02-05 18:48:30 +08:00
|
|
|
|
2024-06-24 16:14:59 +08:00
|
|
|
proxies = {
|
2024-02-05 18:48:30 +08:00
|
|
|
'http://': SSRF_PROXY_HTTP_URL,
|
|
|
|
'https://': SSRF_PROXY_HTTPS_URL
|
|
|
|
} if SSRF_PROXY_HTTP_URL and SSRF_PROXY_HTTPS_URL else None
|
|
|
|
|
2024-07-25 13:10:39 +08:00
|
|
|
BACKOFF_FACTOR = 0.5
|
|
|
|
STATUS_FORCELIST = [429, 500, 502, 503, 504]
|
2024-06-24 16:14:59 +08:00
|
|
|
|
2024-07-25 13:10:39 +08:00
|
|
|
def make_request(method, url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs):
|
2024-07-25 20:50:26 +08:00
|
|
|
if "allow_redirects" in kwargs:
|
|
|
|
allow_redirects = kwargs.pop("allow_redirects")
|
|
|
|
if "follow_redirects" not in kwargs:
|
|
|
|
kwargs["follow_redirects"] = allow_redirects
|
|
|
|
|
2024-07-25 13:10:39 +08:00
|
|
|
retries = 0
|
|
|
|
while retries <= max_retries:
|
|
|
|
try:
|
|
|
|
if SSRF_PROXY_ALL_URL:
|
|
|
|
response = httpx.request(method=method, url=url, proxy=SSRF_PROXY_ALL_URL, **kwargs)
|
|
|
|
elif proxies:
|
|
|
|
response = httpx.request(method=method, url=url, proxies=proxies, **kwargs)
|
|
|
|
else:
|
|
|
|
response = httpx.request(method=method, url=url, **kwargs)
|
2024-06-24 16:14:59 +08:00
|
|
|
|
2024-07-25 13:10:39 +08:00
|
|
|
if response.status_code not in STATUS_FORCELIST:
|
|
|
|
return response
|
|
|
|
else:
|
|
|
|
logging.warning(f"Received status code {response.status_code} for URL {url} which is in the force list")
|
2024-06-24 16:14:59 +08:00
|
|
|
|
2024-07-25 13:10:39 +08:00
|
|
|
except httpx.RequestError as e:
|
|
|
|
logging.warning(f"Request to URL {url} failed on attempt {retries + 1}: {e}")
|
2024-06-24 16:14:59 +08:00
|
|
|
|
2024-07-25 13:10:39 +08:00
|
|
|
retries += 1
|
|
|
|
if retries <= max_retries:
|
|
|
|
time.sleep(BACKOFF_FACTOR * (2 ** (retries - 1)))
|
2024-06-24 16:14:59 +08:00
|
|
|
|
2024-07-25 13:10:39 +08:00
|
|
|
raise Exception(f"Reached maximum retries ({max_retries}) for URL {url}")
|
2024-06-24 16:14:59 +08:00
|
|
|
|
|
|
|
|
2024-07-25 13:10:39 +08:00
|
|
|
def get(url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs):
|
|
|
|
return make_request('GET', url, max_retries=max_retries, **kwargs)
|
2024-06-24 16:14:59 +08:00
|
|
|
|
|
|
|
|
2024-07-25 13:10:39 +08:00
|
|
|
def post(url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs):
|
|
|
|
return make_request('POST', url, max_retries=max_retries, **kwargs)
|
2024-06-24 16:14:59 +08:00
|
|
|
|
|
|
|
|
2024-07-25 13:10:39 +08:00
|
|
|
def put(url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs):
|
|
|
|
return make_request('PUT', url, max_retries=max_retries, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def patch(url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs):
|
|
|
|
return make_request('PATCH', url, max_retries=max_retries, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def delete(url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs):
|
|
|
|
return make_request('DELETE', url, max_retries=max_retries, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def head(url, max_retries=SSRF_DEFAULT_MAX_RETRIES, **kwargs):
|
|
|
|
return make_request('HEAD', url, max_retries=max_retries, **kwargs)
|