-
Notifications
You must be signed in to change notification settings - Fork 6
/
web.py
149 lines (103 loc) · 4.98 KB
/
web.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
146
147
148
149
# -*- coding: utf-8 -*-
from flask import Response
import flask,requests
from flask import Flask,redirect
import os
import json
data={}
if os.path.isfile("config.json") == True:
print("配置文件存在,直接读取")
try:
with open("config.json", "r", encoding='utf-8') as jsonFile:
data = json.load(jsonFile)
jsonFile.close()
try:
main_site = data['main_site']
main_port= data['main_port']
new_port = data['new_port']
api_key = data['api_key']
redirects= data['redirects']
password_key= data['password_key']
if password_key=="True":
password_value = data['password_value']
replace_list = data['replace_list']
except Exception as e:
print(e)
except Exception as e:
print(e)
else:
input("请先文件目录下配置config.json")
with open("config.json", "a") as file: # 只需要将之前的”w"改为“a"即可,代表追加内容
file.close()
exit()
app = Flask(__name__)
@app.route('/', methods=['GET',"POST"])
def index():
#return redirect("/web/index.html")
url=flask.request.url
return redirect(url+"web/index.html")
#return redirect(url_for('/web/index.html'),code=302)
#http://176.113.81.126:8098/emby/videos/64292/stream.mp4?DeviceId=a8c06645-2729-4edc-ae73-09593a775031&MediaSourceId=2858a4cd04bfc490f714a625ecf7c537&Static=true&PlaySessionId=987cca26085644cd86fd2fbcd54ebc71&api_key=b82f726ac4be4817ac51294614674ebb
#http://127.0.0.1:8096/emby/videos/64292/stream.mp4?DeviceId=a8c06645-2729-4edc-ae73-09593a775031&MediaSourceId=2858a4cd04bfc490f714a625ecf7c537&Static=true&PlaySessionId=987cca26085644cd86fd2fbcd54ebc71&api_key=b82f726ac4be4817ac51294614674ebb
@app.route('/<path:path>',methods=['GET',"POST"])
def proxy(path):
headers=dict(flask.request.headers)
'''for key, value in headers.items():
# recurse into nested dicts
headers[key] = str(value).replace(domainsite, true_site)'''
par=flask.request.query_string.decode()
if par !="":
url=f'{main_site}{path}?{par}'
else:
url=f'{main_site}{path}'
if "stream" in url:
MediaSourceId = flask.request.args.get('MediaSourceId')
info_url = f"{main_site}emby/Items?Fields=Path&Ids={MediaSourceId}&api_key={api_key}"
info_json = requests.get(url=info_url).json()
index_url = str(info_json['Items'][0]['Path'])
for a in replace_list:
print(info_json['Items'][0]['Path'],a['from'], a['to'])
index_url = index_url.replace(a['from'], a['to'])
print(f"处理后的直链:{index_url}")
if redirects=="True":
true_result=requests.get(index_url,allow_redirects=False)
#return redirect(index_url)
if true_result.status_code==200 and password_key=="True":
data = {"password1": password_value}
true_result = requests.post(index_url, allow_redirects=False, data=data)
true_url=dict(true_result.headers)['Location']
print(f"重定向后的直链{true_url}")
return redirect(true_url)
else:
true_url=index_url
return redirect(true_url)
elif "Download" in url and "Items" in url:
MediaSourceId = flask.request.args.get('mediaSourceId')
info_url = f"{main_site}emby/Items?Fields=Path&Ids={MediaSourceId}&api_key={api_key}"
info_json = requests.get(url=info_url).json()
for a in replace_list:
index_url = str(info_json['Items'][0]['Path']).replace(a['from'], a['to'])
print(f"处理后的直链:{index_url}")
true_result=requests.get(index_url,allow_redirects=False)
if true_result.status_code==200 and password_key=="True":
data = {"password1": "wcy98151"}
true_result = requests.post(index_url, allow_redirects=False, data=data)
#return redirect(index_url)
true_url=dict(true_result.headers)['Location']
#print(true_url)
return redirect(true_url)
if flask.request.method == 'GET':
resp = requests.get(url=url,headers=headers)
excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers]
response = Response(resp.content, resp.status_code, headers)
return response
elif flask.request.method == 'POST':
data=flask.request.data
resp = requests.post(url=url,headers=headers,data=data)
excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers]
response = Response(resp.content, resp.status_code, headers)
return response
if __name__ == '__main__':
app.run(host='0.0.0.0', port=new_port)