forked from yinghuocho/firefly-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
364 lines (308 loc) · 12.6 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import os
import sys
import json
import codecs
import shutil
import time
from datetime import datetime, date
import multiprocessing
import threading
if getattr(sys, 'frozen', False):
if sys.platform == "darwin":
rootdir = os.getcwd()
else:
rootdir = os.path.dirname(sys.executable)
else:
rootdir = os.path.dirname(os.path.realpath(__file__))
# make all filenames based on rootdir being unicode
rootdir = rootdir.decode(sys.getfilesystemencoding())
sys.path.append(rootdir)
from lib.utils import init_logging, local_update_datafile, set_ca_certs_env, singleton_check, singleton_clean,\
open_url
from lib.ipc import ActorObject
from component.ui import UI
from component.admin import Admin
from component.circumvention import CircumventionChannel, remote_update_meek_relays
from component.local import HTTPProxy, SocksProxy
from component.brz import Browser, able_to_setproxy
from component.matcher import create_matcher, blacklist_info, remote_update_blacklist
from component.hosts import hosts_info, remote_update_hosts
class Coordinator(ActorObject):
def __init__(self, rootdir, conf_file):
super(Coordinator, self).__init__()
self.rootdir = rootdir
self.conf_file = conf_file
self.confdata = None
self.admin = None
self.ui = None
self.cc_channel = None
self.matcher = None
self.http_proxy = None
self.socks_proxy = None
self.browser = None
def loadconf(self):
f = codecs.open(os.path.join(self.rootdir, self.conf_file), "r", "utf-8")
self.confdata = json.loads(f.read())
f.close()
def backup_conf(self):
conf = os.path.join(self.rootdir, self.conf_file)
shutil.copy(conf, conf + ".last")
default = conf + ".default"
if not os.path.isfile(default):
shutil.copy(conf, default)
def recover_conf(self):
try:
conf = os.path.join(self.rootdir, self.conf_file)
shutil.copy(conf + ".last", conf)
return True
except:
return False
def initialize(self):
self.singleton = singleton_check(self.rootdir)
if not self.singleton:
sys.exit(-1)
self.loadconf()
self.ref().share('rootdir', self.rootdir)
self.ref().share('confdata', self.confdata)
self.start_actor()
def start_admin(self):
self.admin = Admin(self.ref())
self.admin.start()
def start_cc_channel(self):
try:
self.cc_channel = CircumventionChannel(self.ref())
self.cc_channel.start()
except Exception, e:
print "failed to start circumvention channel: %s" % str(e)
def start_local_proxy(self):
global rootdir
circumvention_url = self.IPC_circumvention_url()
self.matcher = create_matcher(rootdir, self.confdata, circumvention_url)
if self.confdata['enable_http_proxy']:
try:
self.http_proxy = HTTPProxy(self.ref(), self.matcher)
self.http_proxy.start()
except Exception, e:
print "failed to start http proxy: %s" % str(e)
if self.confdata['enable_socks_proxy']:
try:
self.socks_proxy = SocksProxy(self.ref(), self.matcher)
self.socks_proxy.start()
except Exception, e:
print "failed to start socks proxy: %s" % str(e)
def proxy_info(self):
if self.socks_proxy:
#ip, port = self.socks_proxy.ref().IPC_addr()
#return ProxyInfo(socks.PROXY_TYPE_SOCKS5, ip, port, True, None, None)
url = self.socks_proxy.ref().IPC_url()
return {'http': url, 'https': url}
elif self.http_proxy:
#ip, port = self.http_proxy.ref().IPC_addr()
#return ProxyInfo(socks.PROXY_TYPE_HTTP, ip, port, True, None, None)
url = self.http_proxy.ref().IPC_url()
return {'http': url, 'https': url}
else:
#return None
return {}
def update_matcher(self):
circumvention_url = self.IPC_circumvention_url()
self.matcher = create_matcher(rootdir, self.confdata, circumvention_url)
if self.http_proxy:
self.http_proxy.ref().IPC_update_matcher(self.matcher)
if self.socks_proxy:
self.socks_proxy.ref().IPC_update_matcher(self.matcher)
def start_browser(self, url=None):
http_proxy_enabled = True if self.http_proxy else False
socks_proxy_enabled = True if self.socks_proxy else False
try:
if self.confdata['launch_browser']:
set_proxy = True
else:
set_proxy = False
# if we cannot setproxy but required to, then we open config page which
# gives a tip.
if set_proxy and not able_to_setproxy() and not url:
url = self.admin.ref().IPC_url()
self.browser = Browser(self.ref(), http_proxy_enabled, socks_proxy_enabled, initial_url=url, set_proxy=set_proxy)
self.browser.start()
except Exception, e:
print "failed to launch browser failed: %s" % str(e)
def check_and_update_blacklist(self):
try:
blacklist_date = datetime.strptime(self.matcher.blacklist_matcher.meta['date'], '%Y-%m-%d').date()
if date.today() > blacklist_date:
updated = remote_update_blacklist(self.proxy_info(), self.rootdir, self.confdata)
if updated:
self.update_matcher()
except Exception, e:
print "failed to update blacklist: %s" % str(e)
def check_and_update_hosts(self):
try:
hosts_date = datetime.strptime(self.matcher.hosts.meta['date'], '%Y-%m-%d').date()
if date.today() > hosts_date:
updated = remote_update_hosts(self.proxy_info(), self.rootdir, self.confdata)
if updated:
self.update_matcher()
except Exception, e:
print "failed to update hosts: %s" % str(e)
def update_meek_relays(self):
try:
updated = remote_update_meek_relays(self.proxy_info(), self.rootdir, self.confdata)
if updated:
self.cc_channel.ref().IPC_update_meek_relays()
except Exception, e:
print "failed to update meek relays: %s" % str(e)
def check_for_update(self):
time.sleep(20)
if self.cc_channel.type == "meek":
self.update_meek_relays()
self.check_and_update_blacklist()
self.check_and_update_hosts()
def run(self):
try:
self.initialize()
self.start_cc_channel()
self.start_admin()
self.start_local_proxy()
except Exception, e:
print "failed to start basic steps/processes: %s, try to recover ..." % str(e)
if not self.recover_conf():
raise e
self.end()
self.initialize()
self.start_cc_channel()
self.start_admin()
self.start_local_proxy()
self.backup_conf()
if self.confdata['launch_browser']:
self.start_browser()
t = threading.Thread(target=self.check_for_update)
t.daemon = True
t.start()
self.ui = UI(self.ref())
self.ui.run()
self.end()
def end(self):
if self.admin:
self.admin.terminate()
self.admin.join()
if self.cc_channel:
self.cc_channel.terminate()
self.cc_channel.join()
if self.http_proxy:
self.http_proxy.terminate()
self.http_proxy.join()
if self.socks_proxy:
self.socks_proxy.terminate()
self.socks_proxy.join()
if self.browser:
self.browser.terminate()
self.browser.join()
singleton_clean(self.rootdir, self.singleton)
# IPC interfaces
def IPC_quit(self):
self.end()
return True
def IPC_circumvention_url(self):
"""ask circumvention channel for forwarding url"""
return self.cc_channel.ref().IPC_url()
def IPC_socks_proxy_addr(self):
return self.socks_proxy.ref().IPC_addr()
def IPC_http_proxy_addr(self):
return self.http_proxy.ref().IPC_addr()
def IPC_launch_browser(self):
if self.browser and self.browser.is_alive():
return self.browser.ref().IPC_open_default_page()
else:
self.start_browser()
def IPC_open_admin_url(self):
url = self.admin.ref().IPC_url()
if sys.platform == "darwin":
# the browser-open routines do not work in some cases of OS X
# use built-in interface to ensure admin url opens.
open_url(url)
return
if self.browser and self.browser.is_alive():
return self.browser.ref().IPC_open_url(url)
else:
self.start_browser(url)
def IPC_shadowsocks_methods(self):
return self.cc_channel.ref().IPC_shadowsocks_methods()
def IPC_blacklist_info(self):
return blacklist_info(self.rootdir, self.confdata, self.matcher.blacklist_matcher)
def IPC_hosts_info(self):
return hosts_info(self.rootdir, self.confdata, self.matcher.hosts)
def IPC_get_custom_blacklist(self):
return self.matcher.blacklist_matcher.get_custom_blacklist()
def IPC_get_custom_whitelist(self):
return self.matcher.blacklist_matcher.get_custom_whitelist()
def IPC_update_config(self, data):
try:
self.confdata.update(data)
f = codecs.open(os.path.join(self.rootdir, self.conf_file), "w", "utf-8")
f.write(json.dumps(self.confdata,
sort_keys=True,
indent=4,
separators=(',', ': '),
ensure_ascii=False))
f.close()
return data
except Exception, e:
print "failed to update config: %s" % str(e)
return None
def IPC_resume_default_config(self):
conf = os.path.join(self.rootdir, self.conf_file)
shutil.copy(conf + ".default", conf)
self.loadconf()
return self.confdata
def IPC_update_blacklist(self):
try:
updated = remote_update_blacklist(self.proxy_info(), self.rootdir, self.confdata)
if updated:
self.update_matcher()
return True
except Exception, e:
print "failed to update blacklist: %s" % str(e)
return False
def IPC_update_custom_list(self, custom_bl=None, custom_wl=None):
if custom_bl:
local_update_datafile(u"\n".join(custom_bl),
os.path.join(self.rootdir, self.confdata['custom_blacklist']))
if custom_wl:
local_update_datafile(u"\n".join(custom_wl),
os.path.join(self.rootdir, self.confdata['custom_whitelist']))
self.update_matcher()
def IPC_update_hosts(self, remote=True):
try:
if remote:
remote_update_hosts(self.proxy_info(), self.rootdir, self.confdata)
self.update_matcher()
return True
except Exception, e:
print "failed to update hosts: %s" % str(e)
return False
def IPC_update_hosts_disabled(self, disabled):
local_update_datafile(u"\n".join(disabled), os.path.join(self.rootdir, self.confdata['hosts']['disabled']))
self.update_matcher()
def IPC_support_ssh(self):
return self.cc_channel.ref().IPC_support_ssh()
def IPC_setproxy_tip(self):
if not self.confdata['launch_browser']:
return False
return not able_to_setproxy()
def close_std():
sys.stdin.close()
sys.stdin = open(os.devnull)
sys.stderr.close
sys.stderr = open(os.devnull)
def main():
close_std()
multiprocessing.freeze_support()
init_logging()
global rootdir
conf_file = "config.json"
set_ca_certs_env(os.path.join(rootdir, "cacert.pem").encode(sys.getfilesystemencoding()))
coordinator = Coordinator(rootdir, conf_file)
coordinator.run()
if __name__ == '__main__':
main()