feat(shell-proxy): support SOCKS protocol based on URI scheme (#10069)

This commit is contained in:
Bryan Lee 2021-12-29 16:42:35 +08:00 committed by GitHub
parent 234ad7398d
commit eb94df6428
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,15 +2,30 @@
import os
import subprocess
import sys
import urllib.parse
from urllib.parse import urlparse
proxy = next(os.environ[_] for _ in ("HTTP_PROXY", "HTTPS_PROXY") if _ in os.environ)
parsed = urlparse(proxy)
proxy_protocols = {
"http": "connect",
"https": "connect",
"socks": "5",
"socks5": "5",
"socks4": "4",
"socks4a": "4",
}
if parsed.scheme not in proxy_protocols:
raise TypeError('unsupported proxy protocol: "{}"'.format(parsed.scheme))
argv = [
"nc",
"-X",
"connect",
proxy_protocols[parsed.scheme], # Supported protocols are 4 (SOCKS v4), 5 (SOCKS v5) and connect (HTTP proxy). Default SOCKS v5 is used.
"-x",
urllib.parse.urlparse(proxy).netloc, # proxy-host:proxy-port
parsed.netloc, # proxy-host:proxy-port
sys.argv[1], # host
sys.argv[2], # port
]