add proxy to upstream

This commit is contained in:
Roy Olav Purser 2021-05-09 11:37:19 +02:00
parent 0366e534d0
commit fcdc40497f
Signed by: roypur
GPG Key ID: E14D26A036F21656

View File

@ -15,16 +15,18 @@ providers["twitch"] = "https://twitch.tv"
proxies = {} proxies = {}
proxies_raw = {} proxies_raw = {}
proxies_req = {}
for key in providers: for key in providers:
proxies[key] = streamlink.Streamlink() proxies[key] = streamlink.Streamlink()
proxy = os.environ.get(key.upper() + "_PROXY") proxy = os.environ.get(key.upper() + "_PROXY")
proxies[key].set_option("http-timeout", 2.0) proxies[key].set_option("http-timeout", 2.0)
if proxy is not None: if proxy is not None:
proxies[key].set_option("https-proxy", proxy) proxies[key].set_option("https-proxy", "socks5://" + proxy)
splitted = proxy.split(":", 1) proxies_raw[key] = proxy
if len(splitted) == 2: proxies_req[key] = {}
proxies_raw[key] = splitted[1].lstrip("/") proxies_req[key]["http"] = "socks5://" + proxy
proxies_req[key]["https"] = "socks5://" + proxy
playlist = None playlist = None
icecast_server = os.environ.get("ICECAST_SERVER") icecast_server = os.environ.get("ICECAST_SERVER")
@ -46,8 +48,10 @@ if icecast_server is not None and stream_server is not None:
playlist += f'#EXTINF:0 radio="false", {name}\n' playlist += f'#EXTINF:0 radio="false", {name}\n'
playlist += stream_server + key + "\n" playlist += stream_server + key + "\n"
def rewrite(current, proxy): def rewrite(current, provider):
resp = requests.head(current) proxy_req = proxies_req.get(provider)
proxy = proxies.get(provider)
resp = requests.head(current, proxies=proxy_req)
ctype = resp.headers.get("Content-Type") ctype = resp.headers.get("Content-Type")
print(ctype) print(ctype)
if ctype is None: if ctype is None:
@ -79,10 +83,8 @@ class MainHandler(tornado.web.RequestHandler):
provider = self.get_query_argument("provider", None) provider = self.get_query_argument("provider", None)
upstream = None upstream = None
proxy = None proxy = None
proxy_raw = None
if provider is not None and provider in providers.keys(): if provider is not None and provider in providers.keys():
proxy = proxies.get(provider) proxy = proxies.get(provider)
proxy_raw = proxies_raw.get(provider)
src = providers[provider] + self.request.uri src = providers[provider] + self.request.uri
try: try:
resp = requests.get(src) resp = requests.get(src)
@ -106,7 +108,7 @@ class MainHandler(tornado.web.RequestHandler):
if write: if write:
self.write("stream not found") self.write("stream not found")
else: else:
data = rewrite(upstream, proxy_raw) data = rewrite(upstream, provider)
if data is None: if data is None:
self.redirect(upstream, status=303) self.redirect(upstream, status=303)
else: else: