From 219431b31271aaf0e0079f6a1bdb465a85a743f5 Mon Sep 17 00:00:00 2001 From: DiMartinoXBMC Date: Thu, 20 Aug 2015 18:07:03 +0300 Subject: [PATCH] antizapret --- Core.py | 47 ++++++------ Libtorrent.py | 25 +++---- Player.py | 3 +- SearcherABC.py | 16 ++++- resources/language/English/strings.xml | 5 ++ resources/language/Russian/strings.xml | 4 ++ resources/proxy/__init__.py | 0 resources/proxy/antizapret.py | 99 ++++++++++++++++++++++++++ resources/proxy/immunicity.py | 88 +++++++++++++++++++++++ resources/settings.xml | 1 + 10 files changed, 247 insertions(+), 41 deletions(-) create mode 100644 resources/proxy/__init__.py create mode 100644 resources/proxy/antizapret.py create mode 100644 resources/proxy/immunicity.py diff --git a/Core.py b/Core.py index 40cf66d..4077509 100644 --- a/Core.py +++ b/Core.py @@ -254,28 +254,33 @@ class Core: #playlist.add(path, listitem) #xbmc.Player().play(playlist) - try: - import cherrytorrent - http_config = { - 'port': 8089, - } + from resources.proxy import antizapret + config = antizapret.config() + log('[antizapret]: '+str(config["domains"])) + log('[antizapret]: '+str(config["server"])) - torrent_config = { - 'port': 6900, - 'max_download_rate': 0, - 'max_upload_rate': 0, - 'keep_files': False - } - server = cherrytorrent.Server(http_config, torrent_config) - server.run() - url="http://localhost:8089/add?uri=magnet%3A%3Fxt%3Durn%3Abtih%3Ac39fe3eefbdb62da9c27eb6398ff4a7d2e26e7ab%26dn%3Dbig%2Bbuck%2Bbunny%2Bbdrip%2Bxvid%2Bmedic%26tr%3Dudp%253A%252F%252Ftracker.publicbt.com%253A80%252Fannounce%26tr%3Dudp%253A%252F%252Fopen.demonii.com%253A1337" - print str(get_url('',url)) - xbmc.sleep(3000) - path="http://localhost:8089/video"#?info_hash=c39fe3eefbdb62da9c27eb6398ff4a7d2e26e7ab - xbmc.Player().play(path) - xbmc.sleep(30000) - finally: - get_url('',"http://localhost:8089/shutdown") + #try: + # import cherrytorrent + # http_config = { + # 'port': 8089, + # } + + # torrent_config = { + # 'port': 6900, + # 'max_download_rate': 0, + # 'max_upload_rate': 0, + # 'keep_files': False + # } + # server = cherrytorrent.Server(http_config, torrent_config) + # server.run() + # url="http://localhost:8089/add?uri=magnet%3A%3Fxt%3Durn%3Abtih%3Ac39fe3eefbdb62da9c27eb6398ff4a7d2e26e7ab%26dn%3Dbig%2Bbuck%2Bbunny%2Bbdrip%2Bxvid%2Bmedic%26tr%3Dudp%253A%252F%252Ftracker.publicbt.com%253A80%252Fannounce%26tr%3Dudp%253A%252F%252Fopen.demonii.com%253A1337" + # print str(get_url('',url)) + # xbmc.sleep(3000) + # path="http://localhost:8089/video"#?info_hash=c39fe3eefbdb62da9c27eb6398ff4a7d2e26e7ab + # xbmc.Player().play(path) + # xbmc.sleep(30000) + #finally: + # get_url('',"http://localhost:8089/shutdown") def DownloadStatus(self, params={}): db = DownloadDB() diff --git a/Libtorrent.py b/Libtorrent.py index 0e2c6ad..682b7a3 100644 --- a/Libtorrent.py +++ b/Libtorrent.py @@ -416,7 +416,7 @@ class Libtorrent: 'save_path': self.storageDirectory, #'storage_mode': self.lt.storage_mode_t(1), 'paused': False, - #'auto_managed': False, + 'auto_managed': False, #'duplicate_is_error': True } self.torrentHandle = self.session.add_torrent(torrent_info) @@ -452,17 +452,6 @@ class Libtorrent: self.torrentHandle.piece_priority(self.endPart - i, 7) # print str(i) - def fetchParts(self): - priorities = self.torrentHandle.piece_priorities() - status = self.torrentHandle.status() - if len(status.pieces) == 0: - return - if priorities[self.startPart] == 0: - self.torrentHandle.piece_priority(self.startPart, 2) - for part in range(self.startPart, self.endPart + 1): - if priorities[part] == 0: - self.torrentHandle.piece_priority(part, 1) - def checkThread(self): if self.threadComplete == True: log('checkThread KIIIIIIIIIIILLLLLLLLLLLLLLL') @@ -476,19 +465,20 @@ class Libtorrent: self.session.stop_dht() def debug(self): - try: + #try: + if 1==1: # print str(self.getFilePath(0)) s = self.torrentHandle.status() #get_cache_status=self.session.get_cache_status() #log('get_cache_status - %s/%s' % (str(get_cache_status.blocks_written), str(get_cache_status.blocks_read))) # get_settings=self.torrentHandle.status # print s.num_pieces - # priorities = self.torrentHandle.piece_priorities() - # self.dump(priorities) + priorities = self.torrentHandle.piece_priorities() + str(priorities) # print str('anonymous_mode '+str(get_settings['anonymous_mode'])) state_str = ['queued', 'checking', 'downloading metadata', - 'downloading', 'finished', 'seeding', 'allocating'] + 'downloading', 'finished', 'seeding', 'allocating', 'checking fastresume'] log('[%s] %.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s' % \ (self.lt.version, s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, s.num_peers, state_str[s.state])) @@ -512,7 +502,8 @@ class Libtorrent: # print 'True pieces: %d' % i # print s.current_tracker # print str(s.pieces) - except: + #except: + else: print 'debug error' pass diff --git a/Player.py b/Player.py index 78ed3a3..8d1f32e 100644 --- a/Player.py +++ b/Player.py @@ -227,7 +227,7 @@ class TorrentPlayer(xbmc.Player): # mp4 fix label = os.path.basename(self.torrent.getFilePath(self.contentId)) isMP4 = False - if '.' in label and str(label.split('.')[-1]).lower() == 'mp4': + if '.' in label and str(label.split('.')[-1]).lower() == 'mp4': isMP4 = True debug('setup_torrent: '+str((self.contentId, Offset, isMP4, label))) self.torrent.continueSession(self.contentId, Offset=Offset, isMP4=isMP4) @@ -277,7 +277,6 @@ class TorrentPlayer(xbmc.Player): self.torrent.checkThread() return xbmc.sleep(1000) - #self.torrent.torrentHandle.flush_cache() self.torrent.session.remove_torrent(self.torrent.torrentHandle) progressBar.update(0) progressBar.close() diff --git a/SearcherABC.py b/SearcherABC.py index 2f3dee4..e6924fe 100644 --- a/SearcherABC.py +++ b/SearcherABC.py @@ -41,6 +41,7 @@ class SearcherABC: sourceWeight = 1 cookieJar = None timeout_multi=int(sys.modules["__main__"].__settings__.getSetting("timeout")) + proxy=int(sys.modules["__main__"].__settings__.getSetting("proxy")) __plugin__='Empty v 0 0 0' baseurl = 'site.com' @@ -107,7 +108,20 @@ class SearcherABC: def makeRequest(self, url, data={}, headers={}): self.load_cookie() - opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookieJar)) + if self.proxy == 1: + from resources.proxy import antizapret + opener = urllib2.build_opener(antizapret.AntizapretProxyHandler(), urllib2.HTTPCookieProcessor(self.cookieJar)) + config = antizapret.config() + self.debug('[antizapret]: '+str(config["domains"])) + self.debug('[antizapret]: '+str(config["server"])) + elif self.proxy == 2: + from resources.proxy import immunicity + opener = urllib2.build_opener(immunicity.ImmunicityProxyHandler(), urllib2.HTTPCookieProcessor(self.cookieJar)) + config = immunicity.config() + self.debug('[immunicity]: '+str(config["domains"])) + self.debug('[immunicity]: '+str(config["server"])) + else: + opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookieJar)) opener.addheaders = headers if 0 < len(data): encodedData = urllib.urlencode(data) diff --git a/resources/language/English/strings.xml b/resources/language/English/strings.xml index bc9c99d..bfd2d39 100644 --- a/resources/language/English/strings.xml +++ b/resources/language/English/strings.xml @@ -44,6 +44,10 @@ Save files Ask to save BTclient (python-libtorrent via http) + Auto-unblocking proxy + None + Anti-zapret + Immunicity Interface P2P Network Advanced @@ -69,4 +73,5 @@ Choose searcher You don't have external searcher. Please install it first. + diff --git a/resources/language/Russian/strings.xml b/resources/language/Russian/strings.xml index 6a2fce1..1b4d968 100644 --- a/resources/language/Russian/strings.xml +++ b/resources/language/Russian/strings.xml @@ -44,6 +44,10 @@ Сохранять файлы Спросить о сохранении BTclient (python-libtorrent по http) + Антизапрет (прокси) + Не использовать + Anti-zapret + Immunicity Интерфейс P2P Сеть Дополнительные diff --git a/resources/proxy/__init__.py b/resources/proxy/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/resources/proxy/antizapret.py b/resources/proxy/antizapret.py new file mode 100644 index 0000000..127949f --- /dev/null +++ b/resources/proxy/antizapret.py @@ -0,0 +1,99 @@ +# -*- coding: utf-8 -*- + +import os, re, fnmatch, threading, urllib2 +from contextlib import contextmanager, closing +from functions import log, debug, tempdir + +LOCKS = {} +PAC_URL = "http://antizapret.prostovpn.org/proxy.pac" +CACHE_DIR = tempdir() +USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36" + +if not os.path.exists(CACHE_DIR): + os.makedirs(CACHE_DIR) + +CACHE = 24 * 3600 # 24 hour caching + +@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 d: + d.update({ + "created_at": time.time(), + "data": {}, + }) + elif ttl > 0 and (time.time() - d["created_at"]) > ttl: + d["data"] = {} + yield d["data"] + +_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 = "" + + 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 + return _config + +class AntizapretProxyHandler(urllib2.ProxyHandler, object): + def __init__(self): + self.config = config() + urllib2.ProxyHandler.__init__(self, { + "http" : "", + "https": "", + "ftp" : "", + }) + def proxy_open(self, req, proxy, type): + import socket + + if socket.gethostbyname(req.get_host().split(":")[0]) in self.config["domains"]: + debug("[antizapret]: Pass request through proxy " + self.config["server"]) + return urllib2.ProxyHandler.proxy_open(self, req, self.config["server"], type) + + return None + +def url_get(url, params={}, headers={}, post = None): + + if params: + import urllib + url = "%s?%s" % (url, urllib.urlencode(params)) + + if post: + import urllib + post = urllib.urlencode(post) + + req = urllib2.Request(url, post) + req.add_header("User-Agent", USER_AGENT) + + for k, v in headers.items(): + req.add_header(k, v) + + try: + with closing(urllib2.urlopen(req)) as response: + data = response.read() + if response.headers.get("Content-Encoding", "") == "gzip": + import zlib + return zlib.decompressobj(16 + zlib.MAX_WBITS).decompress(data) + return data + except urllib2.HTTPError as e: + log("[antizapret]: HTTP Error(%s): %s" % (e.errno, e.strerror)) + return None + diff --git a/resources/proxy/immunicity.py b/resources/proxy/immunicity.py new file mode 100644 index 0000000..bc3125e --- /dev/null +++ b/resources/proxy/immunicity.py @@ -0,0 +1,88 @@ +# -*- coding: utf-8 -*- + +import os, re, fnmatch, threading, urllib2 +from contextlib import contextmanager, closing +from functions import log, debug, tempdir + +LOCKS = {} +PAC_URL = "http://clientconfig.immunicity.org/pacs/all.pac" +CACHE_DIR = tempdir() +USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36" + +if not os.path.exists(CACHE_DIR): + os.makedirs(CACHE_DIR) + +CACHE = 24 * 3600 # 24 hour caching + +@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 d: + d.update({ + "created_at": time.time(), + "data": {}, + }) + elif ttl > 0 and (time.time() - d["created_at"]) > ttl: + d["data"] = {} + yield d["data"] + +_config = {} + +def config(): + global _config + if not _config: + with shelf("xbmctorrent.immunicity.pac_config", ttl=CACHE) as pac_config: + log("Fetching Immunicity PAC file") + pac_data = urllib2.urlopen(PAC_URL).read() + pac_config["server"] = re.search(r"var proxyserver = '(.*)'", pac_data).group(1) + pac_config["domains"] = map(lambda x: x.replace(r"\Z(?ms)", ""), map(fnmatch.translate, re.findall(r"\"(.*?)\",", pac_data))) + _config = pac_config + return _config + +class ImmunicityProxyHandler(urllib2.ProxyHandler, object): + def __init__(self): + self.config = config() + urllib2.ProxyHandler.__init__(self, { + "http" : "", + "https": "", + "ftp" : "", + }) + def proxy_open(self, req, proxy, type): + import socket + + if socket.gethostbyname(req.get_host().split(":")[0]) in self.config["domains"]: + debug("[immunicity]: Pass request through proxy " + self.config["server"]) + return urllib2.ProxyHandler.proxy_open(self, req, self.config["server"], type) + + return None + +def url_get(url, params={}, headers={}, post = None): + + if params: + import urllib + url = "%s?%s" % (url, urllib.urlencode(params)) + + if post: + import urllib + post = urllib.urlencode(post) + + req = urllib2.Request(url, post) + req.add_header("User-Agent", USER_AGENT) + + for k, v in headers.items(): + req.add_header(k, v) + + try: + with closing(urllib2.urlopen(req)) as response: + data = response.read() + if response.headers.get("Content-Encoding", "") == "gzip": + import zlib + return zlib.decompressobj(16 + zlib.MAX_WBITS).decompress(data) + return data + except urllib2.HTTPError as e: + log("[immunicity]: HTTP Error(%s): %s" % (e.errno, e.strerror)) + return None diff --git a/resources/settings.xml b/resources/settings.xml index 6cdd8a6..5b81080 100644 --- a/resources/settings.xml +++ b/resources/settings.xml @@ -11,6 +11,7 @@ +