-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.py
74 lines (62 loc) · 2.29 KB
/
app.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
# -*- coding: utf-8 -*-
from flask import Flask
from flask import request
import requests,base64,json,urllib,urlparse
app = Flask(__name__)
ssr_subscribe = {
"CordCloud": "http://xxxxxxxxxxx",
"SSRCloud": "http://xxxxxxxxx"
}
def decode_base64(data, decode_utf8=True):
missing_padding = 4 - len(data) % 4
if missing_padding:
data += '='* missing_padding
if decode_utf8:
return base64.urlsafe_b64decode(data.encode('ascii')).decode("utf-8")
else:
return base64.urlsafe_b64decode(data.encode('ascii'))
def encode_base64(data):
return base64.urlsafe_b64encode(data.encode("utf-8")).decode("utf-8")
def get_remarks(ssrConfig):
param = urlparse.parse_qs(urlparse.urlsplit(ssrConfig).query)
remarks_base64 = param['remarks'][0].replace('-', '+').replace('_', '/')
remarks = decode_base64(remarks_base64, False)
return urllib.quote(remarks)
def convert_ssr2ss(ssrConfig):
ssConfig = "ss://"
networkConfig = ssrConfig.split('/?')[0].split(':')
serverIP = networkConfig[0]
serverPort = networkConfig[1]
encryption = networkConfig[3]
passwd = decode_base64(networkConfig[5])
remarks = get_remarks(ssrConfig)
ssConfig += encode_base64(encryption + ":" + passwd) + "@" + serverIP + ":" + serverPort + "/#" + remarks
return ssConfig
def handle_ssr2ss(ssr_subscribe_url):
ssConfigs = []
setheaders = { "User-Agent": "ShadowsocksX-NG-R 1.4.3 Version R8(3)" }
response = requests.get(ssr_subscribe_url, timeout = 3, headers = setheaders)
if response.ok:
resault_base64_str = decode_base64(response.text)
resault_list = resault_base64_str.split('\n')
for ssrConfig in resault_list:
if len(ssrConfig) > 0:
sconfig = decode_base64(ssrConfig.split('://')[1])
ssConfigs.append(convert_ssr2ss(sconfig))
dist = '\n'.join(ssConfigs)
return encode_base64(dist)
else:
return ""
@app.route('/')
def index():
response = app.response_class(
response = "",
status = 500,
)
return response
@app.route('/CordCloud', methods=['GET'])
def create_cordcloud_ss_subscription():
return handle_ssr2ss(ssr_subscribe['CordCloud'])
if __name__ == "__main__":
# test environment
app.run(host="0.0.0.0", port=9999)