86 lines
3.8 KiB
Python
86 lines
3.8 KiB
Python
|
import urlquick # @UnresolvedImport
|
||
|
from ..settings import option
|
||
|
import socks
|
||
|
|
||
|
class SocksiPyConnection(urlquick.HTTPConnection):
|
||
|
def __init__(self, proxytype, proxyaddr, proxyport=None, host='127.0.0.1', port=9050, rdns=True, username=None, password=None, **kwargs):
|
||
|
self.proxyargs = (proxytype, proxyaddr, proxyport, rdns, username, password)
|
||
|
urlquick.HTTPConnection.__init__(self, host, port=port, **kwargs)
|
||
|
|
||
|
def connect(self):
|
||
|
self.sock = socks.socksocket()
|
||
|
self.sock.setproxy(*self.proxyargs)
|
||
|
if type(self.timeout) in (int, float):
|
||
|
self.sock.settimeout(self.timeout)
|
||
|
self.sock.connect((self.host, self.port))
|
||
|
|
||
|
class SocksiPyConnectionS(urlquick.HTTPSConnection):
|
||
|
def __init__(self, proxytype, proxyaddr, proxyport=None, host='127.0.0.1', port=9050, rdns=True, username=None, password=None, **kwargs):
|
||
|
self.proxyargs = (proxytype, proxyaddr, proxyport, rdns, username, password)
|
||
|
urlquick.HTTPSConnection.__init__(self, host, port=port, **kwargs)
|
||
|
|
||
|
def connect(self):
|
||
|
sock = socks.socksocket()
|
||
|
sock.setproxy(*self.proxyargs)
|
||
|
if type(self.timeout) in (int, float):
|
||
|
sock.settimeout(self.timeout)
|
||
|
sock.connect((self.host, self.port))
|
||
|
self.sock = urlquick.ssl.wrap_socket(sock, self.key_file, self.cert_file)
|
||
|
|
||
|
class Session(urlquick.Session):
|
||
|
def connect(self, req, timeout, verify):
|
||
|
# Fetch connection from pool and attempt to reuse if available
|
||
|
pool = self.request_handler[req.type]
|
||
|
if req.host in pool:
|
||
|
try:
|
||
|
# noinspection PyTypeChecker
|
||
|
return self.send_request(pool[req.host], req)
|
||
|
except Exception as e:
|
||
|
# Remove the connection from the pool as it's unusable
|
||
|
pool[req.host].close()
|
||
|
del pool[req.host]
|
||
|
|
||
|
# Raise the exception if it's not a subclass of UrlError
|
||
|
if not isinstance(e, urlquick.UrlError):
|
||
|
raise
|
||
|
|
||
|
|
||
|
host_port = req.host.split(':')
|
||
|
host = host_port[0]
|
||
|
port = int(host_port[2]) if len(host_port) > 1 else 443 if req.type == 'https' else 80
|
||
|
# Create a new connection
|
||
|
if not option.get_boolean('use_socks'): # @UndefinedVariable
|
||
|
if req.type == "https":
|
||
|
context = urlquick.ssl._create_unverified_context() if verify is False else None
|
||
|
conn = urlquick.HTTPSConnection(host, port, timeout=timeout, context=context)
|
||
|
else:
|
||
|
conn = urlquick.HTTPConnection(host, port, timeout=timeout)
|
||
|
else:
|
||
|
if req.type == "https":
|
||
|
context = urlquick.ssl._create_unverified_context() if verify is False else None
|
||
|
conn = SocksiPyConnectionS(socks.PROXY_TYPE_SOCKS5, option['socks_ip'], proxyport=int(option['socks_port']),
|
||
|
host=host, port=port, timeout=timeout, context=context)
|
||
|
else:
|
||
|
conn = SocksiPyConnection(socks.PROXY_TYPE_SOCKS5, option['socks_ip'], proxyport=int(option['socks_port']),
|
||
|
host=host, port=port, timeout=timeout)
|
||
|
|
||
|
# Make first connection to server
|
||
|
response = self.send_request(conn, req)
|
||
|
|
||
|
# Add connection to the pool if the response is not set to close
|
||
|
if not response.will_close:
|
||
|
pool[req.host] = conn
|
||
|
return response
|
||
|
def torrent_file_fetch(url, referer, cookies):
|
||
|
sess = Session()
|
||
|
headers = {
|
||
|
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 YaBrowser/14.10.2062.12061 Safari/537.36',
|
||
|
'Referer': referer
|
||
|
}
|
||
|
resp = sess.get(url, cookies=cookies, headers=headers)
|
||
|
if resp.ok:
|
||
|
return resp.content
|
||
|
else:
|
||
|
return None
|
||
|
|