ssrf_proxy.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. """
  2. Proxy requests to avoid SSRF
  3. """
  4. from httpx import get as _get, post as _post, put as _put, patch as _patch, head as _head, options as _options
  5. from requests import delete as _delete
  6. import os
  7. SSRF_PROXY_HTTP_URL = os.getenv('SSRF_PROXY_HTTP_URL', '')
  8. SSRF_PROXY_HTTPS_URL = os.getenv('SSRF_PROXY_HTTPS_URL', '')
  9. requests_proxies = {
  10. 'http': SSRF_PROXY_HTTP_URL,
  11. 'https': SSRF_PROXY_HTTPS_URL
  12. } if SSRF_PROXY_HTTP_URL and SSRF_PROXY_HTTPS_URL else None
  13. httpx_proxies = {
  14. 'http://': SSRF_PROXY_HTTP_URL,
  15. 'https://': SSRF_PROXY_HTTPS_URL
  16. } if SSRF_PROXY_HTTP_URL and SSRF_PROXY_HTTPS_URL else None
  17. def get(url, *args, **kwargs):
  18. return _get(url=url, *args, proxies=httpx_proxies, **kwargs)
  19. def post(url, *args, **kwargs):
  20. return _post(url=url, *args, proxies=httpx_proxies, **kwargs)
  21. def put(url, *args, **kwargs):
  22. return _put(url=url, *args, proxies=httpx_proxies, **kwargs)
  23. def patch(url, *args, **kwargs):
  24. return _patch(url=url, *args, proxies=httpx_proxies, **kwargs)
  25. def delete(url, *args, **kwargs):
  26. return _delete(url=url, *args, proxies=requests_proxies, **kwargs)
  27. def head(url, *args, **kwargs):
  28. return _head(url=url, *args, proxies=httpx_proxies, **kwargs)
  29. def options(url, *args, **kwargs):
  30. return _options(url=url, *args, proxies=httpx_proxies, **kwargs)