From 2d0d9a920e4563585290bf16aa46719f6aa9004b Mon Sep 17 00:00:00 2001 From: Roy Olav Purser Date: Wed, 12 May 2021 00:15:01 +0200 Subject: [PATCH] proxy file --- stream.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/stream.py b/stream.py index 18a9077..19f6d76 100755 --- a/stream.py +++ b/stream.py @@ -86,14 +86,12 @@ def get_proxy_url(proxy, current, path): presp = requests.post(proxy_server, json=data) return presp.text -def rewrite(current, provider, proxy): +def upstream_type(current, proxy): resp = requests.head(current, proxies=proxy.req) - ctype = resp.headers.get("Content-Type") - if ctype is None: - return None - else: - if "mpegurl" not in ctype.lower(): - return None + return resp.headers.get("Content-Type", "binary/octet-stream") + +def rewrite(current, provider, proxy, ctype): + resp = requests.get(current, proxies=proxy.req) ndata = None if resp.text is not None: @@ -203,7 +201,20 @@ class MainHandler(tornado.web.RequestHandler): if write: self.write("Stream not found.") else: - data = rewrite(upstream, provider, proxy) + ctype = upstream_type(upstream, proxy) + if "mpgurl" in ctype: + data = rewrite(upstream, provider, proxy) + else: + ldata = {} + ldata["upstream"] = upstream + ldata["proxy"] = proxy.proxy + ldata["proxied"] = isinstance(proxy.proxy, str) + links = [ldata] + resp = requests.post(proxy_server, json=links) + if isinstance(resp.text, list): + new_links = json.loads(resp.text) + if len(new_links) == 1: + upstream = new_links.pop() if data is None: self.redirect(upstream, status=303) else: