Skip to content

Commit 44210f6

Browse files
committed
refine filters
1 parent bddad1d commit 44210f6

File tree

1 file changed

+46
-12
lines changed

1 file changed

+46
-12
lines changed

local/proxy.py

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,8 @@ def dnslib_record2iplist(record):
685685

686686
def get_dnsserver_list():
687687
if os.name == 'nt':
688-
import ctypes, ctypes.wintypes, struct, socket
688+
import ctypes
689+
import ctypes.wintypes
689690
DNS_CONFIG_DNS_SERVER_LIST = 6
690691
buf = ctypes.create_string_buffer(2048)
691692
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):
22882289

22892290
class UserAgentFilter(BaseProxyHandlerFilter):
22902291
"""user agent filter"""
2292+
def __init__(self, user_agent):
2293+
self.user_agent = user_agent
2294+
22912295
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
22942297

22952298

22962299
class WithGAEFilter(BaseProxyHandlerFilter):
22972300
"""with gae filter"""
2301+
def __init__(self, withgae_sites):
2302+
self.withgae_sites = set(withgae_sites)
2303+
22982304
def filter(self, handler):
2299-
if handler.host in common.HTTP_WITHGAE:
2305+
if handler.host in self.withgae_sites:
23002306
logging.debug('WithGAEFilter metched %r %r', handler.path, handler.headers)
23012307
# assume the last one handler is GAEFetchFilter
23022308
return handler.handler_filters[-1].filter(handler)
23032309

23042310

23052311
class ForceHttpsFilter(BaseProxyHandlerFilter):
23062312
"""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+
23072317
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:
23092319
if not handler.headers.get('Referer', '').startswith('https://') and not handler.path.startswith('https://'):
23102320
logging.debug('ForceHttpsFilter metched %r %r', handler.path, handler.headers)
23112321
headers = {'Location': handler.path.replace('http://', 'https://', 1), 'Connection': 'close'}
@@ -2314,8 +2324,12 @@ def filter(self, handler):
23142324

23152325
class FakeHttpsFilter(BaseProxyHandlerFilter):
23162326
"""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+
23172331
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:
23192333
logging.debug('FakeHttpsFilter metched %r %r', handler.path, handler.headers)
23202334
return [handler.STRIP, True, None]
23212335

@@ -2409,6 +2423,9 @@ class DirectRegionFilter(BaseProxyHandlerFilter):
24092423
geoip = pygeoip.GeoIP(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'GeoIP.dat')) if pygeoip and common.GAE_REGIONS else None
24102424
region_cache = LRUCache(16*1024)
24112425

2426+
def __init__(self, regions):
2427+
self.regions = set(regions)
2428+
24122429
def get_country_code(self, hostname, dnsservers):
24132430
"""http://dev.maxmind.com/geoip/legacy/codes/iso3166/"""
24142431
try:
@@ -2432,7 +2449,7 @@ def get_country_code(self, hostname, dnsservers):
24322449
def filter(self, handler):
24332450
if self.geoip:
24342451
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:
24362453
if handler.command == 'CONNECT':
24372454
return [handler.FORWARD, handler.host, handler.port, handler.connect_timeout]
24382455
else:
@@ -2483,7 +2500,7 @@ def filter(self, handler):
24832500

24842501
class GAEProxyHandler(AdvancedProxyHandler):
24852502
"""GAE Proxy Handler"""
2486-
handler_filters = [UserAgentFilter(), WithGAEFilter(), FakeHttpsFilter(), ForceHttpsFilter(), URLRewriteFilter(), HostsFilter(), DirectRegionFilter(), AutoRangeFilter(), GAEFetchFilter()]
2503+
handler_filters = [GAEFetchFilter()]
24872504

24882505
def first_run(self):
24892506
"""GAEProxyHandler setup, init domain/iplist map"""
@@ -2525,7 +2542,7 @@ def filter(self, handler):
25252542
class PHPProxyHandler(AdvancedProxyHandler):
25262543
"""PHP Proxy Handler"""
25272544
first_run_lock = threading.Lock()
2528-
handler_filters = [UserAgentFilter(), FakeHttpsFilter(), ForceHttpsFilter(), PHPFetchFilter()]
2545+
handler_filters = [PHPFetchFilter()]
25292546

25302547
def first_run(self):
25312548
if common.PHP_USEHOSTS:
@@ -3113,8 +3130,6 @@ class PACProxyHandler(SimpleProxyHandler):
31133130

31143131

31153132
def get_process_list():
3116-
import os
3117-
import glob
31183133
import ctypes
31193134
import collections
31203135
Process = collections.namedtuple('Process', 'pid name exe')
@@ -3239,7 +3254,26 @@ def pre_start():
32393254
RangeFetch.maxsize = common.AUTORANGE_MAXSIZE
32403255
RangeFetch.bufsize = common.AUTORANGE_BUFSIZE
32413256
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:
32433277
GAEProxyHandler.handler_filters.insert(0, AuthFilter(common.LISTEN_USERNAME, common.LISTEN_PASSWORD))
32443278

32453279

0 commit comments

Comments
 (0)