@@ -685,7 +685,8 @@ def dnslib_record2iplist(record):
685
685
686
686
def get_dnsserver_list ():
687
687
if os .name == 'nt' :
688
- import ctypes , ctypes .wintypes , struct , socket
688
+ import ctypes
689
+ import ctypes .wintypes
689
690
DNS_CONFIG_DNS_SERVER_LIST = 6
690
691
buf = ctypes .create_string_buffer (2048 )
691
692
ctypes .windll .dnsapi .DnsQueryConfig (DNS_CONFIG_DNS_SERVER_LIST , 0 , None , None , ctypes .byref (buf ), ctypes .byref (ctypes .wintypes .DWORD (len (buf ))))
@@ -2288,24 +2289,33 @@ def handle_error(self, *args):
2288
2289
2289
2290
class UserAgentFilter (BaseProxyHandlerFilter ):
2290
2291
"""user agent filter"""
2292
+ def __init__ (self , user_agent ):
2293
+ self .user_agent = user_agent
2294
+
2291
2295
def filter (self , handler ):
2292
- if common .USERAGENT_ENABLE :
2293
- handler .headers ['User-Agent' ] = common .USERAGENT_STRING
2296
+ handler .headers ['User-Agent' ] = self .user_agent
2294
2297
2295
2298
2296
2299
class WithGAEFilter (BaseProxyHandlerFilter ):
2297
2300
"""with gae filter"""
2301
+ def __init__ (self , withgae_sites ):
2302
+ self .withgae_sites = set (withgae_sites )
2303
+
2298
2304
def filter (self , handler ):
2299
- if handler .host in common . HTTP_WITHGAE :
2305
+ if handler .host in self . withgae_sites :
2300
2306
logging .debug ('WithGAEFilter metched %r %r' , handler .path , handler .headers )
2301
2307
# assume the last one handler is GAEFetchFilter
2302
2308
return handler .handler_filters [- 1 ].filter (handler )
2303
2309
2304
2310
2305
2311
class ForceHttpsFilter (BaseProxyHandlerFilter ):
2306
2312
"""force https filter"""
2313
+ def __init__ (self , forcehttps_sites , noforcehttps_sites ):
2314
+ self .forcehttps_sites = tuple (forcehttps_sites )
2315
+ self .noforcehttps_sites = set (noforcehttps_sites )
2316
+
2307
2317
def filter (self , handler ):
2308
- if handler .command != 'CONNECT' and handler .host .endswith (common . HTTP_FORCEHTTPS ) and handler .host not in common . HTTP_NOFORCEHTTPS :
2318
+ if handler .command != 'CONNECT' and handler .host .endswith (self . forcehttps_sites ) and handler .host not in self . noforcehttps_sites :
2309
2319
if not handler .headers .get ('Referer' , '' ).startswith ('https://' ) and not handler .path .startswith ('https://' ):
2310
2320
logging .debug ('ForceHttpsFilter metched %r %r' , handler .path , handler .headers )
2311
2321
headers = {'Location' : handler .path .replace ('http://' , 'https://' , 1 ), 'Connection' : 'close' }
@@ -2314,8 +2324,12 @@ def filter(self, handler):
2314
2324
2315
2325
class FakeHttpsFilter (BaseProxyHandlerFilter ):
2316
2326
"""fake https filter"""
2327
+ def __init__ (self , fakehttps_sites , nofakehttps_sites ):
2328
+ self .fakehttps_sites = tuple (fakehttps_sites )
2329
+ self .nofakehttps_sites = set (nofakehttps_sites )
2330
+
2317
2331
def filter (self , handler ):
2318
- if handler .command == 'CONNECT' and handler .host .endswith (common . HTTP_FAKEHTTPS ) and handler .host not in common . HTTP_NOFAKEHTTPS :
2332
+ if handler .command == 'CONNECT' and handler .host .endswith (self . fakehttps_sites ) and handler .host not in self . nofakehttps_sites :
2319
2333
logging .debug ('FakeHttpsFilter metched %r %r' , handler .path , handler .headers )
2320
2334
return [handler .STRIP , True , None ]
2321
2335
@@ -2409,6 +2423,9 @@ class DirectRegionFilter(BaseProxyHandlerFilter):
2409
2423
geoip = pygeoip .GeoIP (os .path .join (os .path .dirname (os .path .abspath (__file__ )), 'GeoIP.dat' )) if pygeoip and common .GAE_REGIONS else None
2410
2424
region_cache = LRUCache (16 * 1024 )
2411
2425
2426
+ def __init__ (self , regions ):
2427
+ self .regions = set (regions )
2428
+
2412
2429
def get_country_code (self , hostname , dnsservers ):
2413
2430
"""http://dev.maxmind.com/geoip/legacy/codes/iso3166/"""
2414
2431
try :
@@ -2432,7 +2449,7 @@ def get_country_code(self, hostname, dnsservers):
2432
2449
def filter (self , handler ):
2433
2450
if self .geoip :
2434
2451
country_code = self .get_country_code (handler .host , handler .dns_servers )
2435
- if country_code in common . GAE_REGIONS :
2452
+ if country_code in self . regions :
2436
2453
if handler .command == 'CONNECT' :
2437
2454
return [handler .FORWARD , handler .host , handler .port , handler .connect_timeout ]
2438
2455
else :
@@ -2483,7 +2500,7 @@ def filter(self, handler):
2483
2500
2484
2501
class GAEProxyHandler (AdvancedProxyHandler ):
2485
2502
"""GAE Proxy Handler"""
2486
- handler_filters = [UserAgentFilter (), WithGAEFilter (), FakeHttpsFilter (), ForceHttpsFilter (), URLRewriteFilter (), HostsFilter (), DirectRegionFilter (), AutoRangeFilter (), GAEFetchFilter ()]
2503
+ handler_filters = [GAEFetchFilter ()]
2487
2504
2488
2505
def first_run (self ):
2489
2506
"""GAEProxyHandler setup, init domain/iplist map"""
@@ -2525,7 +2542,7 @@ def filter(self, handler):
2525
2542
class PHPProxyHandler (AdvancedProxyHandler ):
2526
2543
"""PHP Proxy Handler"""
2527
2544
first_run_lock = threading .Lock ()
2528
- handler_filters = [UserAgentFilter (), FakeHttpsFilter (), ForceHttpsFilter (), PHPFetchFilter ()]
2545
+ handler_filters = [PHPFetchFilter ()]
2529
2546
2530
2547
def first_run (self ):
2531
2548
if common .PHP_USEHOSTS :
@@ -3113,8 +3130,6 @@ class PACProxyHandler(SimpleProxyHandler):
3113
3130
3114
3131
3115
3132
def get_process_list ():
3116
- import os
3117
- import glob
3118
3133
import ctypes
3119
3134
import collections
3120
3135
Process = collections .namedtuple ('Process' , 'pid name exe' )
@@ -3239,7 +3254,26 @@ def pre_start():
3239
3254
RangeFetch .maxsize = common .AUTORANGE_MAXSIZE
3240
3255
RangeFetch .bufsize = common .AUTORANGE_BUFSIZE
3241
3256
RangeFetch .waitsize = common .AUTORANGE_WAITSIZE
3242
- if common .LISTEN_USERNAME and common .LISTEN_PASSWORD :
3257
+ if True :
3258
+ GAEProxyHandler .handler_filters .insert (0 , AutoRangeFilter ())
3259
+ if common .GAE_REGIONS :
3260
+ GAEProxyHandler .handler_filters .insert (0 , DirectRegionFilter (common .GAE_REGIONS ))
3261
+ if True :
3262
+ GAEProxyHandler .handler_filters .insert (0 , HostsFilter ())
3263
+ if True :
3264
+ GAEProxyHandler .handler_filters .insert (0 , URLRewriteFilter ())
3265
+ if common .HTTP_FAKEHTTPS :
3266
+ GAEProxyHandler .handler_filters .insert (0 , FakeHttpsFilter (common .HTTP_FAKEHTTPS , common .HTTP_NOFAKEHTTPS ))
3267
+ PHPProxyHandler .handler_filters .insert (0 , FakeHttpsFilter (common .HTTP_FAKEHTTPS , common .HTTP_NOFAKEHTTPS ))
3268
+ if common .HTTP_FORCEHTTPS :
3269
+ GAEProxyHandler .handler_filters .insert (0 , ForceHttpsFilter (common .HTTP_FORCEHTTPS , common .HTTP_NOFORCEHTTPS ))
3270
+ PHPProxyHandler .handler_filters .insert (0 , ForceHttpsFilter (common .HTTP_FORCEHTTPS , common .HTTP_NOFORCEHTTPS ))
3271
+ if common .HTTP_WITHGAE :
3272
+ GAEProxyHandler .handler_filters .insert (0 , WithGAEFilter (common .HTTP_WITHGAE ))
3273
+ if common .USERAGENT_ENABLE :
3274
+ GAEProxyHandler .handler_filters .insert (0 , UserAgentFilter (common .USERAGENT_STRING ))
3275
+ PHPProxyHandler .handler_filters .insert (0 , UserAgentFilter (common .USERAGENT_STRING ))
3276
+ if common .LISTEN_USERNAME :
3243
3277
GAEProxyHandler .handler_filters .insert (0 , AuthFilter (common .LISTEN_USERNAME , common .LISTEN_PASSWORD ))
3244
3278
3245
3279
0 commit comments