Add proxy support.

This commit is contained in:
0x80 2022-03-27 00:00:00 +00:00
parent b3991d0cf6
commit f673411ed2
Signed by: 0x80
GPG Key ID: 68368BCBC000EF51
2 changed files with 43 additions and 2 deletions

View File

@ -20,6 +20,21 @@ def download (platform, url, args, verbosity, return_http_code=False, follow_loc
c.setopt(pycurl.USERAGENT, args["user_agent"]) c.setopt(pycurl.USERAGENT, args["user_agent"])
if "header" in args: if "header" in args:
c.setopt(pycurl.HTTPHEADER, args["header"]) c.setopt(pycurl.HTTPHEADER, args["header"])
if "proxy" in args and args["proxy"]:
proxy_protocol = args["proxy_protocol"]
if proxy_protocol == "HTTP":
c.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_HTTP)
elif proxy_protocol == "SOCKS4":
c.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS4)
elif proxy_protocol == "SOCKS4A":
c.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS4A)
elif proxy_protocol == "SOCKS5":
c.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS5)
notify ("Note, socks5 leaks DNS queries. Use socks5h to proxy DNS as well.", verbosity, platform)
elif proxy_protocol == "SOCKS5H":
c.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS5_HOSTNAME)
c.setopt(pycurl.PROXY, args["proxy_hostname"])
c.setopt(pycurl.PROXYPORT, args["proxy_port"])
notify ("Downloading " + url + "...", verbosity, platform) notify ("Downloading " + url + "...", verbosity, platform)
try: try:
c.perform() c.perform()

View File

@ -73,7 +73,7 @@ def parse_options(params):
"non-network", "non-network",
"output=", "output=",
"output-format=", "output-format=",
"proxy", "proxy=",
"quiet", "quiet",
"sites", "sites",
"suppress-errors", "suppress-errors",
@ -118,7 +118,33 @@ def parse_options(params):
options["output_format"] = str.lower(arg) options["output_format"] = str.lower(arg)
arg_count += 2 arg_count += 2
elif opt in ("-p", "--proxy"): elif opt in ("-p", "--proxy"):
options["curl_args"]["proxy"] = arg if str.lower(arg) == "none":
options["curl_args"]["proxy"] = False
else:
options["curl_args"]["proxy"] = True
index1 = arg.find("://")
index2 = arg.rfind(":")
proxy_protocol = str.upper(arg[:index1])
# TODO: Probably support authentication, right?
# see https://github.com/pycurl/pycurl/blob/master/src/module.c for pycurl PROXYTYPE options
if proxy_protocol in ("HTTP", "SOCKS4", "SOCKS4A", "SOCKS5", "SOCKS5H"):
options["curl_args"]["proxy_protocol"] = proxy_protocol
else:
print ("Invalid proxy type: " + proxy_protocol + ". Supported types: http, socks4, socks4a, socks5, socks5h")
sys.exit()
# TODO: Limit valid hostnames? Right now, just trust the user.
proxy_hostname = arg[index1+3:index2]
options["curl_args"]["proxy_hostname"] = proxy_hostname
proxy_port = int(arg[index2+1:])
if proxy_port in range(1,65535):
options["curl_args"]["proxy_port"] = proxy_port
else:
print ("Invalid proxy port: " + proxy_port)
sys.exit()
arg_count += 2 arg_count += 2
elif opt in ("-q", "--quiet"): elif opt in ("-q", "--quiet"):
options["verbosity"] = 1 options["verbosity"] = 1