|
4 | 4 | import tempfile |
5 | 5 | import zipfile |
6 | 6 | import platform |
| 7 | +import os |
| 8 | +import ssl |
7 | 9 | from pathlib import Path |
8 | 10 | from urllib.request import ( |
| 11 | + HTTPSHandler, |
9 | 12 | ProxyHandler, |
10 | 13 | Request, |
11 | 14 | build_opener, |
|
19 | 22 | logger = logging.getLogger(__name__) |
20 | 23 |
|
21 | 24 |
|
| 25 | +def standalone_upstream_proxy(): |
| 26 | + """Set upstream Proxy for urllib - standalone.""" |
| 27 | + upstream_proxy_enabled = bool(os.getenv('MOBSF_UPSTREAM_PROXY_ENABLED', '')) |
| 28 | + |
| 29 | + if upstream_proxy_enabled: |
| 30 | + upstream_proxy_username = os.getenv('MOBSF_UPSTREAM_PROXY_USERNAME', '') |
| 31 | + upstream_proxy_password = os.getenv('MOBSF_UPSTREAM_PROXY_PASSWORD', '') |
| 32 | + upstream_proxy_type = os.getenv('MOBSF_UPSTREAM_PROXY_TYPE', 'http') |
| 33 | + upstream_proxy_ip = os.getenv('MOBSF_UPSTREAM_PROXY_IP', '127.0.0.1') |
| 34 | + upstream_proxy_port = int(os.getenv('MOBSF_UPSTREAM_PROXY_PORT', '3128')) |
| 35 | + |
| 36 | + # Handle Docker proxy IP translation |
| 37 | + if os.getenv('MOBSF_PLATFORM') == 'docker': |
| 38 | + if (upstream_proxy_ip and upstream_proxy_ip.strip() in |
| 39 | + ('127.0.0.1', 'localhost')): |
| 40 | + upstream_proxy_ip = 'host.docker.internal' |
| 41 | + |
| 42 | + if not upstream_proxy_username: |
| 43 | + proxy_port = str(upstream_proxy_port) |
| 44 | + proxy_host = f'{upstream_proxy_type}://{upstream_proxy_ip}:{proxy_port}' |
| 45 | + else: |
| 46 | + proxy_port = str(upstream_proxy_port) |
| 47 | + proxy_host = (f'{upstream_proxy_type}://{upstream_proxy_username}:' |
| 48 | + f'{upstream_proxy_password}@{upstream_proxy_ip}:' |
| 49 | + f'{proxy_port}') |
| 50 | + |
| 51 | + # For urllib, we need to set both http and https proxies |
| 52 | + proxies = { |
| 53 | + 'http': proxy_host, |
| 54 | + 'https': proxy_host, |
| 55 | + } |
| 56 | + else: |
| 57 | + proxies = {} |
| 58 | + |
| 59 | + upstream_proxy_ssl_verify = os.getenv('MOBSF_UPSTREAM_PROXY_SSL_VERIFY', '1') |
| 60 | + verify = upstream_proxy_ssl_verify in ('1', '"1"') |
| 61 | + return proxies, verify |
| 62 | + |
| 63 | + |
22 | 64 | def download_file(url, file_path): |
23 | 65 | req = Request(url) |
| 66 | + |
| 67 | + # Check for system proxies first (http_proxy, https_proxy env vars) |
24 | 68 | system_proxies = getproxies() |
25 | | - proxy_handler = ProxyHandler(system_proxies) |
26 | | - opener = build_opener(proxy_handler) |
| 69 | + |
| 70 | + if system_proxies: |
| 71 | + proxies = system_proxies |
| 72 | + verify = True # Default to verify for system proxies |
| 73 | + logger.info('Using system proxies: %s (SSL verify: %s)', proxies, verify) |
| 74 | + else: |
| 75 | + # Check if MobSF upstream proxy is explicitly configured |
| 76 | + upstream_proxy_enabled = bool(os.getenv('MOBSF_UPSTREAM_PROXY_ENABLED', '')) |
| 77 | + |
| 78 | + if upstream_proxy_enabled: |
| 79 | + proxies, verify = standalone_upstream_proxy() |
| 80 | + logger.info('Using MobSF upstream proxies: %s (SSL verify: %s)', |
| 81 | + proxies, verify) |
| 82 | + else: |
| 83 | + # No proxy configuration - use direct connection |
| 84 | + proxies = {} |
| 85 | + verify = True |
| 86 | + |
| 87 | + proxy_handler = ProxyHandler(proxies) |
| 88 | + |
| 89 | + if verify: |
| 90 | + ssl_context = ssl.create_default_context() |
| 91 | + else: |
| 92 | + ssl_context = ssl._create_unverified_context() |
| 93 | + |
| 94 | + https_handler = HTTPSHandler(context=ssl_context) |
| 95 | + opener = build_opener(proxy_handler, https_handler) |
27 | 96 |
|
28 | 97 | with opener.open(req) as response: |
29 | 98 | if response.status == 200: |
|
0 commit comments