use proxy for hls
This commit is contained in:
		@@ -2,7 +2,6 @@
 | 
			
		||||
import json
 | 
			
		||||
import sys
 | 
			
		||||
import urllib.parse
 | 
			
		||||
import re
 | 
			
		||||
import os
 | 
			
		||||
import time
 | 
			
		||||
import base64
 | 
			
		||||
@@ -224,61 +223,6 @@ try:
 | 
			
		||||
except Exception as e:
 | 
			
		||||
    logger.info(e)
 | 
			
		||||
 | 
			
		||||
async def rewrite(upstream, current, proxy):
 | 
			
		||||
    ndata = None
 | 
			
		||||
    text = None
 | 
			
		||||
    try:
 | 
			
		||||
        async with proxy.session() as session:
 | 
			
		||||
            resp = await session.get(upstream)
 | 
			
		||||
            text = await resp.text()
 | 
			
		||||
    except Exception as e:
 | 
			
		||||
        logger.info(e)
 | 
			
		||||
    if text is not None:
 | 
			
		||||
        lines = text.splitlines()
 | 
			
		||||
        links = []
 | 
			
		||||
        pattern_match = re.compile(r'(?<=URI=").+(?=")')
 | 
			
		||||
        pattern_sub = re.compile(r'URI=".+"')
 | 
			
		||||
        for line in lines:
 | 
			
		||||
            if line.startswith("#EXT-X-KEY:METHOD="):
 | 
			
		||||
                matches = pattern_match.findall(line)
 | 
			
		||||
                if len(matches) == 1:
 | 
			
		||||
                    ldata = {}
 | 
			
		||||
                    ldata["upstream"] = urllib.parse.urljoin(current, matches[0])
 | 
			
		||||
                    ldata["proxy"] = proxy.proxy
 | 
			
		||||
                    ldata["proxied"] = isinstance(proxy.proxy, str)
 | 
			
		||||
                    links.append(ldata)
 | 
			
		||||
            elif line.startswith("#"):
 | 
			
		||||
                pass
 | 
			
		||||
            else:
 | 
			
		||||
                ldata = {}
 | 
			
		||||
                ldata["upstream"] = urllib.parse.urljoin(current, line)
 | 
			
		||||
                ldata["proxy"] = proxy.proxy
 | 
			
		||||
                ldata["proxied"] = isinstance(proxy.proxy, str)
 | 
			
		||||
                links.append(ldata)
 | 
			
		||||
        if isinstance(proxy_server, str):
 | 
			
		||||
            ndata = ""
 | 
			
		||||
            try:
 | 
			
		||||
                async with proxy.local() as session:
 | 
			
		||||
                    resp = await session.post(proxy_server, json=links)
 | 
			
		||||
                    link_text = await resp.text()
 | 
			
		||||
            except Exception as e:
 | 
			
		||||
                logger.info(e)
 | 
			
		||||
            else:
 | 
			
		||||
                if isinstance(link_text, str):
 | 
			
		||||
                    links = json.loads(link_text)
 | 
			
		||||
                    for line in lines:
 | 
			
		||||
                        if line.startswith("#EXT-X-KEY:METHOD="):
 | 
			
		||||
                            matches = pattern_match.findall(line)
 | 
			
		||||
                            if len(matches) == 1:
 | 
			
		||||
                                new_url = links.pop(0)
 | 
			
		||||
                                ndata += pattern_sub.sub(f'URI="{new_url}"', line)
 | 
			
		||||
                        elif line.startswith("#"):
 | 
			
		||||
                            ndata += line
 | 
			
		||||
                        else:
 | 
			
		||||
                            ndata += links.pop(0)
 | 
			
		||||
                        ndata += "\n"
 | 
			
		||||
        return ndata
 | 
			
		||||
 | 
			
		||||
class MainHandler(tornado.web.RequestHandler):
 | 
			
		||||
    async def handle_any(self, redir):
 | 
			
		||||
        handler = UpstreamHandler()
 | 
			
		||||
@@ -324,16 +268,9 @@ class MainHandler(tornado.web.RequestHandler):
 | 
			
		||||
        else:
 | 
			
		||||
            upstream_proxy = await handler.proxy.proxy_url(upstream, None)
 | 
			
		||||
            ctype = await handler.proxy.content_type(upstream_proxy)
 | 
			
		||||
            data = None
 | 
			
		||||
            if "mpegurl" in ctype.lower():
 | 
			
		||||
                data = await rewrite(upstream_proxy, upstream, handler.proxy)
 | 
			
		||||
                if isinstance(data, str):
 | 
			
		||||
                    self.set_header("Content-Type", "application/vnd.apple.mpegurl")
 | 
			
		||||
                    self.write(data)
 | 
			
		||||
            else:
 | 
			
		||||
                self.set_header("Content-Type", ctype)
 | 
			
		||||
                if redir:
 | 
			
		||||
                    self.redirect(upstream_proxy, status=303)
 | 
			
		||||
            self.set_header("Content-Type", ctype)
 | 
			
		||||
            if redir:
 | 
			
		||||
                self.redirect(upstream_proxy, status=303)
 | 
			
		||||
    async def get(self):
 | 
			
		||||
        await self.handle_any(True)
 | 
			
		||||
    async def head(self):
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user