forked from fpfeng/g2w.online
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
137 lines (112 loc) · 3.51 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
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
import subprocess
import shlex
import re
import os
from flask import Flask, make_response, abort, current_app, render_template
from IPy import IP
from utils import ListConverter
from config import configs
app = Flask(__name__)
app.config.from_object(configs[os.environ.get('g2pconf', 'test')])
app.url_map.converters['list'] = ListConverter
@app.route('/')
def index():
return render_template('index.html')
@app.route('/ipset/<path:args>')
def ipset(args):
try:
name_addr = args.split(',')
name, addr = name_addr
if len(name_addr) == 2 and check_valid_ipset(name, addr):
ip, port = addr.split(':')
command = create_cmd_str(ip, port, name)
return maek_resp_from_stdout(command, name + '_ipset.conf')
abort(404)
except:
abort(404)
@app.route('/dnsq/<path:args>')
def dnsq(args):
try:
ip, port = args.split(':')
if check_addr(ip, port):
command = create_cmd_str(ip, port)
return maek_resp_from_stdout(command, 'gfwlist_dnsmasq.conf')
abort(404)
except:
abort(404)
def maek_resp_from_stdout(cmd, filename):
stdout = subprocess.check_output(cmd)
resp = make_response(stdout)
resp.headers["Content-Disposition"] = \
'attachment; filename=' + filename
return resp
def create_cmd_str(ip, port, name=None):
command = shlex.split(
current_app.config['G2D_PTAH'] +
' -l ' +
current_app.config['TXTLIST_PTAH'] +
' -d ' +
ip +
' -p ' +
port +
' -o -')
if not name: # without ipset
command += ['-i', '-']
else: # with ipset
command += ['-i', name]
return command
def check_valid_ipset(name, addr):
pass_check = False
if len(name) <= 20 and re.match("^[a-zA-Z0-9_]*$", name)\
and check_addr(*addr.split(':')):
pass_check = True
return pass_check
@app.route('/pac/<list:proxies>')
def pac(proxies):
try:
if len(proxies) <= 10:
all_args = []
for p in proxies:
pass_check, arg = check_vaild_pac(p.split(','))
if pass_check:
all_args.append(arg)
to_str = '"' + '; '.join(all_args) + '"'
command = shlex.split(
'genpac --gfwlist-url="-" ' +
'--gfwlist-local=' +
current_app.config['TXTLIST_PTAH'] +
' -p ' +
to_str +
' --user-rule="||naver.jp"')
return maek_resp_from_stdout(command, 'gfwlist.pac')
abort(404)
except:
abort(404)
def check_vaild_pac(type_addr):
pass_check, arg = False, None
if len(type_addr) == 2:
p_type = type_addr[0]
addr = type_addr[1]
if p_type in ['h', 's'] and check_addr(*addr.split(':')):
pass_check = True
arg = create_proxy_arg(p_type, addr)
return pass_check, arg
def check_addr(ip, port):
pass_check = True
if ip and port and 0 < int(port) < 65536:
try:
_ = IP(ip)
except ValueError:
pass_check = False
else:
pass_check = False
return pass_check
def create_proxy_arg(p_type, addr):
arg = ''
if p_type == 's':
arg = 'SOCKS5 ' + addr + '; ' + 'SOCKS ' + addr
else:
arg = 'PROXY ' + addr
return arg
if __name__ == '__main__':
app.run()