-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.py
65 lines (54 loc) · 1.89 KB
/
index.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
# coding=utf-8
from modules import pack
from modules import indexhtml
import os
import re
from flask import Flask, request, abort
import requests
from urllib.parse import urlencode, unquote
from gevent import pywsgi
app = Flask(__name__)
# mainpage as well as simple description
@app.route("/")
def index():
return indexhtml.content.format(*((request.url_root,)*4)), {'Content-Type': 'text/html;charset=utf-8'}
# api
@app.route("/sub")
def sub():
url = request.args
# get interval
if "interval" in url:
interval = url["interval"]
else:
interval = "600"
# get port of zju connect
zju = {
"zjuPort": url.get("zjuport"),
"zjuSocksUser": url.get("zjusocksuser"),
"zjuSocksPasswd": url.get("zjusockspasswd")
}
# get the url of original subscription
url = url.get("url")
# get original headers
headers = {'Content-Type': 'text/yaml;charset=utf-8'}
originalHeaders = requests.head(url, headers={'User-Agent':'clash'}).headers
if 'subscription-userinfo' in originalHeaders: # containing info about ramaining flow
headers['subscription-userinfo'] = originalHeaders['subscription-userinfo']
if 'Content-Disposition' in originalHeaders: # containing filename
headers['Content-Disposition'] = originalHeaders['Content-Disposition'].replace("attachment", "inline")
urltem = {
"target": "clash",
"url": url,
}
providerConvUrl = os.environ.get("provider_converter")
domain = re.match(r"https?://(.+)", providerConvUrl).group(1)
urlAfterConv = providerConvUrl\
+ "/api/convert?" + urlencode(urltem)
result = pack.pack(urlAfterConv, interval, domain, zju)
return result, headers
if __name__ == "__main__":
# Debug
app.run(host="0.0.0.0", port=443, debug=True)
# Production
# server = pywsgi.WSGIServer(('0.0.0.0', 443), app)
# server.serve_forever()