sandbox1
inpos 2016-03-12 15:22:35 +03:00
parent 971817c888
commit 515b573a74
1 changed files with 29 additions and 27 deletions

View File

@ -14,44 +14,46 @@ 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:
import time
if not dict(d):
d.update({
"created_at": time.time(),
"data": {},
})
elif ttl > 0 and (time.time() - d["created_at"]) > ttl:
d["data"] = {}
yield d["data"]
# with closing(shelve.open(filename, writeback=True)) as d:
import time
if not dict(d):
d.update({
"created_at": time.time(),
"data": {},
})
elif ttl > 0 and (time.time() - d["created_at"]) > ttl:
d["data"] = {}
return d
_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_config", 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"] = []
log("[antizapret]: +++++++++++ pac_config %s" % (repr(pac_config),))
_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):