add proxy

This commit is contained in:
Roy Olav Purser 2021-05-08 19:12:21 +02:00
parent c1ab35efa3
commit 16aede77b5
Signed by: roypur
GPG Key ID: E14D26A036F21656

View File

@ -21,7 +21,7 @@ for key in providers:
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", proxy)
splitted = proxy.split(":", 1) splitted = proxy.split(":", 1)
if len(splitted) == 2: if len(splitted) == 2:
proxies_raw[key] = splitted[1].lstrip("/") proxies_raw[key] = splitted[1].lstrip("/")
@ -46,7 +46,7 @@ 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, endpoint, proxy): def rewrite(current, proxy):
resp = requests.get(current) resp = requests.get(current)
ndata = resp.text ndata = resp.text
if resp.text is not None and proxy_server is not None: if resp.text is not None and proxy_server is not None:
@ -66,7 +66,7 @@ def rewrite(current, endpoint, proxy):
class MainHandler(tornado.web.RequestHandler): class MainHandler(tornado.web.RequestHandler):
def handle_any(self, write): def handle_any(self, write):
provider = self.get_query_argument("provider", None) provider = self.get_query_argument("provider", None)
endpoint = None upstream = None
proxy = None proxy = None
proxy_raw = None proxy_raw = None
if provider is not None and provider in providers.keys(): if provider is not None and provider in providers.keys():
@ -85,18 +85,18 @@ class MainHandler(tornado.web.RequestHandler):
for key in reversed(streams): for key in reversed(streams):
stream = streams.get(key) stream = streams.get(key)
if hasattr(stream, "url"): if hasattr(stream, "url"):
endpoint = stream.url upstream = stream.url
break break
except Exception as e: except Exception as e:
print(str(e)) print(str(e))
endpoint = None upstream = None
if endpoint is None: if upstream is None:
self.set_status(404) self.set_status(404)
if write: if write:
self.write("stream not found") self.write("stream not found")
else: else:
rewrite(endpoint, "", proxy_raw) rewrite(upstream, proxy_raw)
self.redirect(endpoint, status=303) self.redirect(upstream, status=303)
def get(self): def get(self):
self.handle_any(True) self.handle_any(True)
def head(self): def head(self):