From 55cb8797e247b52e2be38066dc2756daf71f669f Mon Sep 17 00:00:00 2001 From: DiMartinoXBMC Date: Sat, 12 Mar 2016 20:45:29 +0300 Subject: [PATCH] antizapret fix by inpos --- resources/proxy/antizapret.py | 56 ++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/resources/proxy/antizapret.py b/resources/proxy/antizapret.py index 127949f..e0f1ec2 100644 --- a/resources/proxy/antizapret.py +++ b/resources/proxy/antizapret.py @@ -14,52 +14,61 @@ if not os.path.exists(CACHE_DIR): CACHE = 24 * 3600 # 24 hour caching -@contextmanager +#@contextmanager def shelf(filename, ttl=0): import shelve filename = os.path.join(CACHE_DIR, filename) with LOCKS.get(filename, threading.RLock()): - with closing(shelve.open(filename, writeback=True)) as d: +# with closing(shelve.open(filename, writeback=True)) as d: + d = shelve.open(filename, writeback=True) + try: import time - if not d: + if not dict(d): d.update({ "created_at": time.time(), "data": {}, }) elif ttl > 0 and (time.time() - d["created_at"]) > ttl: + d["created_at"] = time.time() d["data"] = {} - yield d["data"] + return d + except: + d.close() + raise _config = {} def config(): global _config if not _config: - with shelf("antizapret.pac_config", ttl=CACHE) as pac_config: - if not pac_config: - log("[antizapret]: Fetching Antizapret PAC file on %s" %PAC_URL) - try: - pac_data = urllib2.urlopen(PAC_URL).read() - except: - pac_data = "" +# with shelf("antizapret.pac_config", ttl=CACHE) as pac_config: + d = shelf("antizapret.pac_config2", ttl=CACHE) + pac_config = d['data'] + if not pac_config: + log("[antizapret]: Fetching Antizapret PAC file on %s" %PAC_URL) + try: + pac_data = urllib2.urlopen(PAC_URL).read() + except: + pac_data = "" - r = re.search(r"\"PROXY (.*); DIRECT", pac_data) - if r: - pac_config["server"] = r.group(1) - pac_config["domains"] = map(lambda x: x.replace(r"\Z(?ms)", "").replace("\\", ""), map(fnmatch.translate, re.findall(r"\"(.*?)\",", pac_data))) - else: - pac_config["server"] = None - pac_config["domains"] = [] - _config = pac_config + r = re.search(r"\"PROXY (.*); DIRECT", pac_data) + if r: + pac_config["server"] = r.group(1) + pac_config["domains"] = map(lambda x: x.replace(r"\Z(?ms)", "").replace("\\", ""), map(fnmatch.translate, re.findall(r"\"(.*?)\",", pac_data))) + else: + pac_config["server"] = None + pac_config["domains"] = [] + d.close() + _config = pac_config return _config class AntizapretProxyHandler(urllib2.ProxyHandler, object): def __init__(self): self.config = config() urllib2.ProxyHandler.__init__(self, { - "http" : "", - "https": "", - "ftp" : "", + "http" : "", + "https": "", + "ftp" : "", }) def proxy_open(self, req, proxy, type): import socket @@ -95,5 +104,4 @@ def url_get(url, params={}, headers={}, post = None): return data except urllib2.HTTPError as e: log("[antizapret]: HTTP Error(%s): %s" % (e.errno, e.strerror)) - return None - + return None \ No newline at end of file