This commit is contained in:
Jesús
2020-12-15 21:14:27 -05:00
parent cf8e8ea5b1
commit f4b36a220d
4 changed files with 67 additions and 30 deletions

View File

@@ -21,8 +21,6 @@ import re
import sys
def youtu_be(env, start_response):
id = env['PATH_INFO'][1:]
env['PATH_INFO'] = '/watch'
@@ -32,6 +30,7 @@ def youtu_be(env, start_response):
env['QUERY_STRING'] += '&v=' + id
yield from yt_app(env, start_response)
def proxy_site(env, start_response, video=False):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)',
@@ -79,12 +78,14 @@ def proxy_site(env, start_response, video=False):
cleanup_func(response)
def proxy_video(env, start_response):
yield from proxy_site(env, start_response, video=True)
site_handlers = {
'youtube.com':yt_app,
'youtu.be':youtu_be,
'youtube.com': yt_app,
'youtu.be': youtu_be,
'ytimg.com': proxy_site,
'yt3.ggpht.com': proxy_site,
'lh3.googleusercontent.com': proxy_site,
@@ -92,6 +93,7 @@ site_handlers = {
'googlevideo.com': proxy_video,
}
def split_url(url):
''' Split https://sub.example.com/foo/bar.html into ('sub.example.com', '/foo/bar.html')'''
# XXX: Is this regex safe from REDOS?
@@ -103,11 +105,11 @@ def split_url(url):
return match.group(1), match.group(2)
def error_code(code, start_response):
start_response(code, ())
return code.encode()
def site_dispatch(env, start_response):
client_address = env['REMOTE_ADDR']
try:
@@ -117,7 +119,7 @@ def site_dispatch(env, start_response):
method = env['REQUEST_METHOD']
path = env['PATH_INFO']
if method=="POST" and client_address not in ('127.0.0.1', '::1'):
if method == "POST" and client_address not in ('127.0.0.1', '::1'):
yield error_code('403 Forbidden', start_response)
return
@@ -159,12 +161,15 @@ def site_dispatch(env, start_response):
class FilteredRequestLog:
'''Don't log noisy thumbnail and avatar requests'''
filter_re = re.compile(r'"GET /https://(i\.ytimg\.com/|www\.youtube\.com/data/subscription_thumbnails/|yt3\.ggpht\.com/|www\.youtube\.com/api/timedtext).*" 200')
def __init__(self):
pass
def write(self, s):
if not self.filter_re.search(s):
sys.stderr.write(s)
if __name__ == '__main__':
if settings.allow_foreign_addresses:
server = WSGIServer(('0.0.0.0', settings.port_number), site_dispatch,