use proxy for hls
This commit is contained in:
		@@ -2,7 +2,6 @@
 | 
				
			|||||||
import json
 | 
					import json
 | 
				
			||||||
import sys
 | 
					import sys
 | 
				
			||||||
import urllib.parse
 | 
					import urllib.parse
 | 
				
			||||||
import re
 | 
					 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
import time
 | 
					import time
 | 
				
			||||||
import base64
 | 
					import base64
 | 
				
			||||||
@@ -224,61 +223,6 @@ try:
 | 
				
			|||||||
except Exception as e:
 | 
					except Exception as e:
 | 
				
			||||||
    logger.info(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):
 | 
					class MainHandler(tornado.web.RequestHandler):
 | 
				
			||||||
    async def handle_any(self, redir):
 | 
					    async def handle_any(self, redir):
 | 
				
			||||||
        handler = UpstreamHandler()
 | 
					        handler = UpstreamHandler()
 | 
				
			||||||
@@ -324,16 +268,9 @@ class MainHandler(tornado.web.RequestHandler):
 | 
				
			|||||||
        else:
 | 
					        else:
 | 
				
			||||||
            upstream_proxy = await handler.proxy.proxy_url(upstream, None)
 | 
					            upstream_proxy = await handler.proxy.proxy_url(upstream, None)
 | 
				
			||||||
            ctype = await handler.proxy.content_type(upstream_proxy)
 | 
					            ctype = await handler.proxy.content_type(upstream_proxy)
 | 
				
			||||||
            data = None
 | 
					            self.set_header("Content-Type", ctype)
 | 
				
			||||||
            if "mpegurl" in ctype.lower():
 | 
					            if redir:
 | 
				
			||||||
                data = await rewrite(upstream_proxy, upstream, handler.proxy)
 | 
					                self.redirect(upstream_proxy, status=303)
 | 
				
			||||||
                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)
 | 
					 | 
				
			||||||
    async def get(self):
 | 
					    async def get(self):
 | 
				
			||||||
        await self.handle_any(True)
 | 
					        await self.handle_any(True)
 | 
				
			||||||
    async def head(self):
 | 
					    async def head(self):
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user