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_raw = {}
proxies_req = {}
for key in providers:
proxies[key] = streamlink.Streamlink()
proxy = os.environ.get(key.upper() + "_PROXY")
proxies[key].set_option("http-timeout", 2.0)
if proxy is not None:
proxies[key].set_option("https-proxy", proxy)
splitted = proxy.split(":", 1)
if len(splitted) == 2:
proxies_raw[key] = splitted[1].lstrip("/")
proxies[key].set_option("https-proxy", "socks5://" + proxy)
proxies_raw[key] = proxy
proxies_req[key] = {}
proxies_req[key]["http"] = "socks5://" + proxy
proxies_req[key]["https"] = "socks5://" + proxy
playlist = None
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 += stream_server + key + "\n"
def rewrite(current, proxy):
resp = requests.head(current)
def rewrite(current, provider):
proxy_req = proxies_req.get(provider)
proxy = proxies.get(provider)
resp = requests.head(current, proxies=proxy_req)
ctype = resp.headers.get("Content-Type")
print(ctype)
if ctype is None:
@ -79,10 +83,8 @@ class MainHandler(tornado.web.RequestHandler):
provider = self.get_query_argument("provider", None)
upstream = None
proxy = None
proxy_raw = None
if provider is not None and provider in providers.keys():
proxy = proxies.get(provider)
proxy_raw = proxies_raw.get(provider)
src = providers[provider] + self.request.uri
try:
resp = requests.get(src)
@ -106,7 +108,7 @@ class MainHandler(tornado.web.RequestHandler):
if write:
self.write("stream not found")
else:
data = rewrite(upstream, proxy_raw)
data = rewrite(upstream, provider)
if data is None:
self.redirect(upstream, status=303)
else: