forked from shashstormer/m3u8_proxy-cors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
145 lines (132 loc) · 6.11 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import json
import aiohttp
import multidict
import uvicorn
from fastapi import FastAPI, Request, Response
from fastapi.responses import RedirectResponse
import secrets
app = FastAPI()
TOKENS = {}
CharLIST = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
async def home(request: Request) -> Response:
return RedirectResponse("/docs")
def NEW_ID(item):
id_len = 32
_id = ''
if item in list(TOKENS.values()):
for ite in TOKENS:
if TOKENS[ite] == item:
return ite
while True:
while len(_id) < id_len:
_id += secrets.choice(CharLIST)
if _id in TOKENS:
_id = ''
else:
TOKENS[_id] = item
return _id
@app.get('/cors')
async def handle(request: Request):
headers_ = request.query_params.get('headers',
"{\"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36}\"}")
headers_ = json.loads(headers_)
headers_[
"User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36}"
async with aiohttp.ClientSession(headers=multidict.CIMultiDict(headers_)) as session:
url = request.query_params.get('url')
url = url.replace("+", "%2B")
typr = request.query_params.get('origin', "normal")
if url.endswith('.m3u8'):
if typr == "aninin":
url += "#.mp4"
async with session.get(url) as resp:
headers = resp.headers.copy()
ret_head = resp.headers.copy()
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Expose-Headers'] = 'Content-Disposition'
headers['Content-Disposition'] = 'attachment; filename="master.m3u8"'
del_keys = ['Vary', 'Server', 'Report-To', 'NEL', 'Content-Encoding', 'Transfer-Encoding',
'Content-Length']
del_ret = ['Content-Type', 'Content-Encoding', 'Transfer-Encoding', 'Content-Length',
'Server', 'Report-To', 'NEL']
for head in del_keys:
try:
del headers[head]
except KeyError:
pass
for head in del_ret:
try:
del ret_head[head]
except KeyError:
pass
text = await resp.text()
base_url = '/'.join(url.split('/')[:-1]) + '/'
modified_text = ''
if ("404 not" not in text) and ("404 Not" not in text):
for line in text.split('\n'):
line = line.strip(" ").strip('\n')
if not line.strip(" ").strip('\n'):
continue
if line == "":
continue
if line.startswith('#'):
modified_text += line + '\n'
continue
line = line.replace("+", "%2B")
if ".m3u8" in line:
if "http" not in line:
line = base_url + line
modified_text += '/cors?url=' + line + '&headers=' + json.dumps(dict(ret_head)) + '\n'
else:
if ('http://' not in url) or ('https://' not in url):
line = base_url + line
line = NEW_ID(line)
modified_text += '/token?url=' + line + '&headers=' + json.dumps(dict(ret_head)) + '\n'
else:
modified_text = text
if len(list(TOKENS.keys())) > 10000:
del TOKENS[list(TOKENS.keys())[1]]
print(modified_text)
return Response(content=modified_text, headers=headers)
else:
async with session.get(url) as resp:
headers = resp.headers.copy()
headers['Access-Control-Allow-Origin'] = '*'
del_head = ['Vary', 'Server', 'Report-To', 'NEL', 'Transfer-Encoding', 'Content-Encoding',
'Content-Length']
for key in del_head:
try:
del headers[key]
except KeyError:
pass
data = await resp.read()
return Response(content=data, headers=headers)
@app.get('/token')
async def token_response(request: Request):
headers_ = request.query_params.get('headers',
"{\"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36}\"}")
headers_ = json.loads(headers_)
headers_[
"User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36}"
headers = {'Access-Control-Allow-Origin': '*'}
async with aiohttp.ClientSession(headers=multidict.CIMultiDict(headers_)) as session:
url = request.query_params.get('url')
url = TOKENS[url]
if url:
async with session.get(url) as resp:
headers = resp.headers.copy()
headers['Access-Control-Allow-Origin'] = '*'
del_head = ['Vary', 'Server', 'Report-To', 'NEL', 'Transfer-Encoding', 'Content-Encoding',
'Content-Length']
for key in del_head:
try:
del headers[key]
except KeyError:
pass
data = await resp.read()
return Response(content=data, headers=headers)
app.add_api_route('/cors', handle, methods=['GET'])
app.add_api_route('/', home, methods=['GET'])
app.add_api_route('/token', token_response, methods=['GET'])
if __name__ == '__main__':
uvicorn.run(app, host="0.0.0.0", port=5010)