|
|
|
@ -31,6 +31,9 @@ for tunneling connections through SOCKS proxies.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import socket
|
|
|
|
|
import struct
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
Minor modifications made by Christopher Gilbert (http://motomastyle.com/)
|
|
|
|
@ -41,65 +44,82 @@ mainly to merge bug fixes found in Sourceforge
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import socket
|
|
|
|
|
import struct
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
PROXY_TYPE_SOCKS4 = 1
|
|
|
|
|
PROXY_TYPE_SOCKS5 = 2
|
|
|
|
|
PROXY_TYPE_HTTP = 3
|
|
|
|
|
proxy_type_socks4 = 1
|
|
|
|
|
proxy_type_socks5 = 2
|
|
|
|
|
proxy_type_http = 3
|
|
|
|
|
|
|
|
|
|
_default_proxy = None
|
|
|
|
|
_org_socket = socket.socket
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ProxyError(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GeneralProxyError(ProxyError):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
_defaultproxy = None
|
|
|
|
|
_orgsocket = socket.socket
|
|
|
|
|
|
|
|
|
|
class ProxyError(Exception): pass
|
|
|
|
|
class GeneralProxyError(ProxyError): pass
|
|
|
|
|
class Socks5AuthError(ProxyError): pass
|
|
|
|
|
class Socks5Error(ProxyError): pass
|
|
|
|
|
class Socks4Error(ProxyError): pass
|
|
|
|
|
class HTTPError(ProxyError): pass
|
|
|
|
|
class Socks5AuthError(ProxyError):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Socks5Error(ProxyError):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Socks4Error(ProxyError):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class HTTPError(ProxyError):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_generalerrors = ("success",
|
|
|
|
|
"invalid data",
|
|
|
|
|
"not connected",
|
|
|
|
|
"not available",
|
|
|
|
|
"bad proxy type",
|
|
|
|
|
"bad input",
|
|
|
|
|
"timed out",
|
|
|
|
|
"network unreachable",
|
|
|
|
|
"connection refused",
|
|
|
|
|
"host unreachable")
|
|
|
|
|
"invalid data",
|
|
|
|
|
"not connected",
|
|
|
|
|
"not available",
|
|
|
|
|
"bad proxy type",
|
|
|
|
|
"bad input",
|
|
|
|
|
"timed out",
|
|
|
|
|
"network unreachable",
|
|
|
|
|
"connection refused",
|
|
|
|
|
"host unreachable")
|
|
|
|
|
|
|
|
|
|
_socks5errors = ("succeeded",
|
|
|
|
|
"general SOCKS server failure",
|
|
|
|
|
"connection not allowed by ruleset",
|
|
|
|
|
"Network unreachable",
|
|
|
|
|
"Host unreachable",
|
|
|
|
|
"Connection refused",
|
|
|
|
|
"TTL expired",
|
|
|
|
|
"Command not supported",
|
|
|
|
|
"Address type not supported",
|
|
|
|
|
"Unknown error")
|
|
|
|
|
"general SOCKS server failure",
|
|
|
|
|
"connection not allowed by ruleset",
|
|
|
|
|
"Network unreachable",
|
|
|
|
|
"Host unreachable",
|
|
|
|
|
"Connection refused",
|
|
|
|
|
"TTL expired",
|
|
|
|
|
"Command not supported",
|
|
|
|
|
"Address type not supported",
|
|
|
|
|
"Unknown error")
|
|
|
|
|
|
|
|
|
|
_socks5autherrors = ("succeeded",
|
|
|
|
|
"authentication is required",
|
|
|
|
|
"all offered authentication methods were rejected",
|
|
|
|
|
"unknown username or invalid password",
|
|
|
|
|
"unknown error")
|
|
|
|
|
"authentication is required",
|
|
|
|
|
"all offered authentication methods were rejected",
|
|
|
|
|
"unknown username or invalid password",
|
|
|
|
|
"unknown error")
|
|
|
|
|
|
|
|
|
|
_socks4errors = ("request granted",
|
|
|
|
|
"request rejected or failed",
|
|
|
|
|
"request rejected because SOCKS server cannot connect to identd on the client",
|
|
|
|
|
"request rejected because the client program and identd report different user-ids",
|
|
|
|
|
"unknown error")
|
|
|
|
|
"request rejected or failed",
|
|
|
|
|
"request rejected because SOCKS server cannot connect to identd on the client",
|
|
|
|
|
"request rejected because the client program and identd report different user-ids",
|
|
|
|
|
"unknown error")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def setdefaultproxy(proxytype=None, addr=None, port=None, rdns=True, username=None, password=None):
|
|
|
|
|
"""setdefaultproxy(proxytype, addr[, port[, rdns[, username[, password]]]])
|
|
|
|
|
Sets a default proxy which all further socksocket objects will use,
|
|
|
|
|
unless explicitly changed.
|
|
|
|
|
"""
|
|
|
|
|
global _defaultproxy
|
|
|
|
|
_defaultproxy = (proxytype, addr, port, rdns, username, password)
|
|
|
|
|
global _default_proxy
|
|
|
|
|
_default_proxy = (proxytype, addr, port, rdns, username, password)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def wrapmodule(module):
|
|
|
|
|
"""wrapmodule(module)
|
|
|
|
@ -108,12 +128,13 @@ def wrapmodule(module):
|
|
|
|
|
This will only work on modules that import socket directly into the namespace;
|
|
|
|
|
most of the Python Standard Library falls into this category.
|
|
|
|
|
"""
|
|
|
|
|
if _defaultproxy != None:
|
|
|
|
|
module.socket.socket = socksocket
|
|
|
|
|
if _default_proxy is not None:
|
|
|
|
|
module.socket.socket = SockSocket
|
|
|
|
|
else:
|
|
|
|
|
raise GeneralProxyError((4, "no proxy specified"))
|
|
|
|
|
|
|
|
|
|
class socksocket(socket.socket):
|
|
|
|
|
|
|
|
|
|
class SockSocket(socket.socket):
|
|
|
|
|
"""socksocket([family[, type[, proto]]]) -> socket object
|
|
|
|
|
Open a SOCKS enabled socket. The parameters are the same as
|
|
|
|
|
those of the standard socket init. In order for SOCKS to work,
|
|
|
|
@ -121,13 +142,13 @@ class socksocket(socket.socket):
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0, _sock=None):
|
|
|
|
|
_orgsocket.__init__(self, family, type, proto, _sock)
|
|
|
|
|
if _defaultproxy != None:
|
|
|
|
|
self.__proxy = _defaultproxy
|
|
|
|
|
_org_socket.__init__(self, family, type, proto, _sock)
|
|
|
|
|
if _default_proxy is not None:
|
|
|
|
|
self.__proxy = _default_proxy
|
|
|
|
|
else:
|
|
|
|
|
self.__proxy = (None, None, None, None, None, None)
|
|
|
|
|
self.__proxysockname = None
|
|
|
|
|
self.__proxypeername = None
|
|
|
|
|
self.__proxy_sock_name = None
|
|
|
|
|
self.__proxy_peer_name = None
|
|
|
|
|
|
|
|
|
|
def __recvall(self, count):
|
|
|
|
|
"""__recvall(count) -> data
|
|
|
|
@ -139,8 +160,9 @@ class socksocket(socket.socket):
|
|
|
|
|
except socket.timeout:
|
|
|
|
|
raise GeneralProxyError((6, "timed out"))
|
|
|
|
|
while len(data) < count:
|
|
|
|
|
d = self.recv(count-len(data))
|
|
|
|
|
if not d: raise GeneralProxyError((0, "connection closed unexpectedly"))
|
|
|
|
|
d = self.recv(count - len(data))
|
|
|
|
|
if not d:
|
|
|
|
|
raise GeneralProxyError((0, "connection closed unexpectedly"))
|
|
|
|
|
data = data + d
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
@ -181,7 +203,7 @@ class socksocket(socket.socket):
|
|
|
|
|
Negotiates a connection through a SOCKS5 server.
|
|
|
|
|
"""
|
|
|
|
|
# First we'll send the authentication packages we support.
|
|
|
|
|
if (self.__proxy[4]!=None) and (self.__proxy[5]!=None):
|
|
|
|
|
if self.__proxy[4] is not None and self.__proxy[5] is not None:
|
|
|
|
|
# The username/password details were supplied to the
|
|
|
|
|
# setproxy method so we support the USERNAME/PASSWORD
|
|
|
|
|
# authentication (in addition to the standard none).
|
|
|
|
@ -203,7 +225,10 @@ class socksocket(socket.socket):
|
|
|
|
|
elif chosenauth[1:2] == chr(0x02).encode():
|
|
|
|
|
# Okay, we need to perform a basic username/password
|
|
|
|
|
# authentication.
|
|
|
|
|
self.sendall(chr(0x01).encode() + chr(len(self.__proxy[4])) + self.__proxy[4] + chr(len(self.__proxy[5])) + self.__proxy[5])
|
|
|
|
|
self.sendall(
|
|
|
|
|
chr(0x01).encode() + chr(len(self.__proxy[4])) + self.__proxy[4] + chr(
|
|
|
|
|
len(self.__proxy[5])) + self.__proxy[5]
|
|
|
|
|
)
|
|
|
|
|
authstat = self.__recvall(2)
|
|
|
|
|
if authstat[0:1] != chr(0x01).encode():
|
|
|
|
|
# Bad response
|
|
|
|
@ -250,7 +275,7 @@ class socksocket(socket.socket):
|
|
|
|
|
elif resp[1:2] != chr(0x00).encode():
|
|
|
|
|
# Connection failed
|
|
|
|
|
self.close()
|
|
|
|
|
if ord(resp[1:2])<=8:
|
|
|
|
|
if ord(resp[1:2]) <= 8:
|
|
|
|
|
raise Socks5Error((ord(resp[1:2]), _socks5errors[ord(resp[1:2])]))
|
|
|
|
|
else:
|
|
|
|
|
raise Socks5Error((9, _socks5errors[9]))
|
|
|
|
@ -262,13 +287,13 @@ class socksocket(socket.socket):
|
|
|
|
|
boundaddr = self.__recvall(ord(resp[4:5]))
|
|
|
|
|
else:
|
|
|
|
|
self.close()
|
|
|
|
|
raise GeneralProxyError((1,_generalerrors[1]))
|
|
|
|
|
raise GeneralProxyError((1, _generalerrors[1]))
|
|
|
|
|
boundport = struct.unpack(">H", self.__recvall(2))[0]
|
|
|
|
|
self.__proxysockname = (boundaddr, boundport)
|
|
|
|
|
if ipaddr != None:
|
|
|
|
|
self.__proxypeername = (socket.inet_ntoa(ipaddr), destport)
|
|
|
|
|
self.__proxy_sock_name = (boundaddr, boundport)
|
|
|
|
|
if ipaddr:
|
|
|
|
|
self.__proxy_peer_name = (socket.inet_ntoa(ipaddr), destport)
|
|
|
|
|
else:
|
|
|
|
|
self.__proxypeername = (destaddr, destport)
|
|
|
|
|
self.__proxy_peer_name = (destaddr, destport)
|
|
|
|
|
|
|
|
|
|
def __resolvesocks5(self, host):
|
|
|
|
|
# Now we can request the actual connection
|
|
|
|
@ -277,7 +302,7 @@ class socksocket(socket.socket):
|
|
|
|
|
req = req + struct.pack(">H", 8444)
|
|
|
|
|
self.sendall(req)
|
|
|
|
|
# Get the response
|
|
|
|
|
ip = ""
|
|
|
|
|
|
|
|
|
|
resp = self.__recvall(4)
|
|
|
|
|
if resp[0:1] != chr(0x05).encode():
|
|
|
|
|
self.close()
|
|
|
|
@ -285,7 +310,7 @@ class socksocket(socket.socket):
|
|
|
|
|
elif resp[1:2] != chr(0x00).encode():
|
|
|
|
|
# Connection failed
|
|
|
|
|
self.close()
|
|
|
|
|
if ord(resp[1:2])<=8:
|
|
|
|
|
if ord(resp[1:2]) <= 8:
|
|
|
|
|
raise Socks5Error((ord(resp[1:2]), _socks5errors[ord(resp[1:2])]))
|
|
|
|
|
else:
|
|
|
|
|
raise Socks5Error((9, _socks5errors[9]))
|
|
|
|
@ -297,65 +322,64 @@ class socksocket(socket.socket):
|
|
|
|
|
ip = self.__recvall(ord(resp[4:5]))
|
|
|
|
|
else:
|
|
|
|
|
self.close()
|
|
|
|
|
raise GeneralProxyError((1,_generalerrors[1]))
|
|
|
|
|
boundport = struct.unpack(">H", self.__recvall(2))[0]
|
|
|
|
|
raise GeneralProxyError((1, _generalerrors[1]))
|
|
|
|
|
return ip
|
|
|
|
|
|
|
|
|
|
def getproxysockname(self):
|
|
|
|
|
"""getsockname() -> address info
|
|
|
|
|
Returns the bound IP address and port number at the proxy.
|
|
|
|
|
"""
|
|
|
|
|
return self.__proxysockname
|
|
|
|
|
return self.__proxy_sock_name
|
|
|
|
|
|
|
|
|
|
def getproxypeername(self):
|
|
|
|
|
"""getproxypeername() -> address info
|
|
|
|
|
Returns the IP and port number of the proxy.
|
|
|
|
|
"""
|
|
|
|
|
return _orgsocket.getpeername(self)
|
|
|
|
|
return _org_socket.getpeername(self)
|
|
|
|
|
|
|
|
|
|
def getpeername(self):
|
|
|
|
|
"""getpeername() -> address info
|
|
|
|
|
Returns the IP address and port number of the destination
|
|
|
|
|
machine (note: getproxypeername returns the proxy)
|
|
|
|
|
"""
|
|
|
|
|
return self.__proxypeername
|
|
|
|
|
return self.__proxy_peer_name
|
|
|
|
|
|
|
|
|
|
def getproxytype(self):
|
|
|
|
|
return self.__proxy[0]
|
|
|
|
|
|
|
|
|
|
def __negotiatesocks4(self,destaddr,destport):
|
|
|
|
|
def __negotiatesocks4(self, destination_address, destination_port):
|
|
|
|
|
"""__negotiatesocks4(self,destaddr,destport)
|
|
|
|
|
Negotiates a connection through a SOCKS4 server.
|
|
|
|
|
"""
|
|
|
|
|
# Check if the destination address provided is an IP address
|
|
|
|
|
rmtrslv = False
|
|
|
|
|
try:
|
|
|
|
|
ipaddr = socket.inet_aton(destaddr)
|
|
|
|
|
ipaddr = socket.inet_aton(destination_address)
|
|
|
|
|
except socket.error:
|
|
|
|
|
# It's a DNS name. Check where it should be resolved.
|
|
|
|
|
if self.__proxy[3]:
|
|
|
|
|
ipaddr = struct.pack("BBBB", 0x00, 0x00, 0x00, 0x01)
|
|
|
|
|
rmtrslv = True
|
|
|
|
|
else:
|
|
|
|
|
ipaddr = socket.inet_aton(socket.gethostbyname(destaddr))
|
|
|
|
|
ipaddr = socket.inet_aton(socket.gethostbyname(destination_address))
|
|
|
|
|
# Construct the request packet
|
|
|
|
|
req = struct.pack(">BBH", 0x04, 0x01, destport) + ipaddr
|
|
|
|
|
req = struct.pack(">BBH", 0x04, 0x01, destination_port) + ipaddr
|
|
|
|
|
# The username parameter is considered userid for SOCKS4
|
|
|
|
|
if self.__proxy[4] != None:
|
|
|
|
|
if self.__proxy[4] is not None:
|
|
|
|
|
req = req + self.__proxy[4]
|
|
|
|
|
req = req + chr(0x00).encode()
|
|
|
|
|
# DNS name if remote resolving is required
|
|
|
|
|
# NOTE: This is actually an extension to the SOCKS4 protocol
|
|
|
|
|
# called SOCKS4A and may not be supported in all cases.
|
|
|
|
|
if rmtrslv:
|
|
|
|
|
req = req + destaddr + chr(0x00).encode()
|
|
|
|
|
req = req + destination_address + chr(0x00).encode()
|
|
|
|
|
self.sendall(req)
|
|
|
|
|
# Get the response from the server
|
|
|
|
|
resp = self.__recvall(8)
|
|
|
|
|
if resp[0:1] != chr(0x00).encode():
|
|
|
|
|
# Bad data
|
|
|
|
|
self.close()
|
|
|
|
|
raise GeneralProxyError((1,_generalerrors[1]))
|
|
|
|
|
raise GeneralProxyError((1, _generalerrors[1]))
|
|
|
|
|
if resp[1:2] != chr(0x5A).encode():
|
|
|
|
|
# Server returned an error
|
|
|
|
|
self.close()
|
|
|
|
@ -365,44 +389,48 @@ class socksocket(socket.socket):
|
|
|
|
|
else:
|
|
|
|
|
raise Socks4Error((94, _socks4errors[4]))
|
|
|
|
|
# Get the bound address/port
|
|
|
|
|
self.__proxysockname = (socket.inet_ntoa(resp[4:]), struct.unpack(">H", resp[2:4])[0])
|
|
|
|
|
if rmtrslv != None:
|
|
|
|
|
self.__proxypeername = (socket.inet_ntoa(ipaddr), destport)
|
|
|
|
|
self.__proxy_sock_name = (socket.inet_ntoa(resp[4:]), struct.unpack(">H", resp[2:4])[0])
|
|
|
|
|
if rmtrslv is not None:
|
|
|
|
|
self.__proxy_peer_name = (socket.inet_ntoa(ipaddr), destination_port)
|
|
|
|
|
else:
|
|
|
|
|
self.__proxypeername = (destaddr, destport)
|
|
|
|
|
self.__proxy_peer_name = (destination_address, destination_port)
|
|
|
|
|
|
|
|
|
|
def __negotiatehttp(self, destaddr, destport):
|
|
|
|
|
def __negotiatehttp(self, destination_address, destination_port):
|
|
|
|
|
"""__negotiatehttp(self,destaddr,destport)
|
|
|
|
|
Negotiates a connection through an HTTP server.
|
|
|
|
|
"""
|
|
|
|
|
# If we need to resolve locally, we do this now
|
|
|
|
|
if not self.__proxy[3]:
|
|
|
|
|
addr = socket.gethostbyname(destaddr)
|
|
|
|
|
addr = socket.gethostbyname(destination_address)
|
|
|
|
|
else:
|
|
|
|
|
addr = destaddr
|
|
|
|
|
self.sendall(("CONNECT " + addr + ":" + str(destport) + " HTTP/1.1\r\n" + "Host: " + destaddr + "\r\n\r\n").encode())
|
|
|
|
|
addr = destination_address
|
|
|
|
|
self.sendall(
|
|
|
|
|
("CONNECT {} : {} HTTP/1.1\r\n Host: {} \r\n\r\n".format(
|
|
|
|
|
addr, str(destination_port), destination_address)).encode())
|
|
|
|
|
# We read the response until we get the string "\r\n\r\n"
|
|
|
|
|
resp = self.recv(1)
|
|
|
|
|
while resp.find("\r\n\r\n".encode()) == -1:
|
|
|
|
|
resp = resp + self.recv(1)
|
|
|
|
|
# We just need the first line to check if the connection
|
|
|
|
|
# was successful
|
|
|
|
|
statusline = resp.splitlines()[0].split(" ".encode(), 2)
|
|
|
|
|
if statusline[0] not in ("HTTP/1.0".encode(), "HTTP/1.1".encode()):
|
|
|
|
|
status_line = resp.splitlines()[0].split(" ".encode(), 2)
|
|
|
|
|
|
|
|
|
|
if status_line[0] not in ("HTTP/1.0".encode(), "HTTP/1.1".encode()):
|
|
|
|
|
self.close()
|
|
|
|
|
raise GeneralProxyError((1, _generalerrors[1]))
|
|
|
|
|
try:
|
|
|
|
|
statuscode = int(statusline[1])
|
|
|
|
|
statuscode = int(status_line[1])
|
|
|
|
|
except ValueError:
|
|
|
|
|
self.close()
|
|
|
|
|
raise GeneralProxyError((1, _generalerrors[1]))
|
|
|
|
|
|
|
|
|
|
if statuscode != 200:
|
|
|
|
|
self.close()
|
|
|
|
|
raise HTTPError((statuscode, statusline[2]))
|
|
|
|
|
self.__proxysockname = ("0.0.0.0", 0)
|
|
|
|
|
self.__proxypeername = (addr, destport)
|
|
|
|
|
raise HTTPError((statuscode, status_line[2]))
|
|
|
|
|
self.__proxy_sock_name = ("0.0.0.0", 0)
|
|
|
|
|
self.__proxy_peer_name = (addr, destination_port)
|
|
|
|
|
|
|
|
|
|
def connect(self, destpair):
|
|
|
|
|
def connect(self, destination_pair):
|
|
|
|
|
"""connect(self, despair)
|
|
|
|
|
Connects to the specified destination through a proxy.
|
|
|
|
|
destpar - A tuple of the IP/DNS address and the port number.
|
|
|
|
@ -410,15 +438,14 @@ class socksocket(socket.socket):
|
|
|
|
|
To select the proxy server use setproxy().
|
|
|
|
|
"""
|
|
|
|
|
# Do a minimal input check first
|
|
|
|
|
if (not type(destpair) in (list,tuple)) or (len(destpair) < 2) or (type(destpair[0]) != type('')) or (type(destpair[1]) != int):
|
|
|
|
|
if (not isinstance(destination_pair, (list, tuple))) or (len(destination_pair) < 2) \
|
|
|
|
|
or not isinstance(destination_pair[0], str) or not isinstance(destination_pair[1], int):
|
|
|
|
|
raise GeneralProxyError((5, _generalerrors[5]))
|
|
|
|
|
if self.__proxy[0] == PROXY_TYPE_SOCKS5:
|
|
|
|
|
if self.__proxy[2] != None:
|
|
|
|
|
portnum = self.__proxy[2]
|
|
|
|
|
else:
|
|
|
|
|
portnum = 1080
|
|
|
|
|
|
|
|
|
|
if self.__proxy[0] == proxy_type_socks5:
|
|
|
|
|
port_number = self.__proxy[2] if self.__proxy[2] is not None else 1080
|
|
|
|
|
try:
|
|
|
|
|
_orgsocket.connect(self, (self.__proxy[1], portnum))
|
|
|
|
|
_org_socket.connect(self, (self.__proxy[1], port_number))
|
|
|
|
|
except socket.error as e:
|
|
|
|
|
# ENETUNREACH, WSAENETUNREACH
|
|
|
|
|
if e[0] in [101, 10051]:
|
|
|
|
@ -431,21 +458,17 @@ class socksocket(socket.socket):
|
|
|
|
|
raise GeneralProxyError((9, _generalerrors[9]))
|
|
|
|
|
raise
|
|
|
|
|
self.__negotiatesocks5()
|
|
|
|
|
self.__connectsocks5(destpair[0], destpair[1])
|
|
|
|
|
elif self.__proxy[0] == PROXY_TYPE_SOCKS4:
|
|
|
|
|
if self.__proxy[2] != None:
|
|
|
|
|
portnum = self.__proxy[2]
|
|
|
|
|
else:
|
|
|
|
|
portnum = 1080
|
|
|
|
|
_orgsocket.connect(self,(self.__proxy[1], portnum))
|
|
|
|
|
self.__negotiatesocks4(destpair[0], destpair[1])
|
|
|
|
|
elif self.__proxy[0] == PROXY_TYPE_HTTP:
|
|
|
|
|
if self.__proxy[2] != None:
|
|
|
|
|
portnum = self.__proxy[2]
|
|
|
|
|
else:
|
|
|
|
|
portnum = 8080
|
|
|
|
|
self.__connectsocks5(destination_pair[0], destination_pair[1])
|
|
|
|
|
|
|
|
|
|
elif self.__proxy[0] == proxy_type_socks4:
|
|
|
|
|
port_number = self.__proxy[2] if self.__proxy[2] is not None else 1080
|
|
|
|
|
_org_socket.connect(self, (self.__proxy[1], port_number))
|
|
|
|
|
self.__negotiatesocks4(destination_pair[0], destination_pair[1])
|
|
|
|
|
|
|
|
|
|
elif self.__proxy[0] == proxy_type_http:
|
|
|
|
|
port_number = self.__proxy[2] if self.__proxy[2] is not None else 8080
|
|
|
|
|
try:
|
|
|
|
|
_orgsocket.connect(self,(self.__proxy[1], portnum))
|
|
|
|
|
_org_socket.connect(self, (self.__proxy[1], port_number))
|
|
|
|
|
except socket.error as e:
|
|
|
|
|
# ENETUNREACH, WSAENETUNREACH
|
|
|
|
|
if e[0] in [101, 10051]:
|
|
|
|
@ -457,19 +480,17 @@ class socksocket(socket.socket):
|
|
|
|
|
if e[0] in [113, 10065]:
|
|
|
|
|
raise GeneralProxyError((9, _generalerrors[9]))
|
|
|
|
|
raise
|
|
|
|
|
self.__negotiatehttp(destpair[0], destpair[1])
|
|
|
|
|
elif self.__proxy[0] == None:
|
|
|
|
|
_orgsocket.connect(self, (destpair[0], destpair[1]))
|
|
|
|
|
self.__negotiatehttp(destination_pair[0], destination_pair[1])
|
|
|
|
|
|
|
|
|
|
elif not self.__proxy[0]:
|
|
|
|
|
_org_socket.connect(self, (destination_pair[0], destination_pair[1]))
|
|
|
|
|
else:
|
|
|
|
|
raise GeneralProxyError((4, _generalerrors[4]))
|
|
|
|
|
|
|
|
|
|
def resolve(self, host):
|
|
|
|
|
if self.__proxy[0] == PROXY_TYPE_SOCKS5:
|
|
|
|
|
if self.__proxy[2] != None:
|
|
|
|
|
portnum = self.__proxy[2]
|
|
|
|
|
else:
|
|
|
|
|
portnum = 1080
|
|
|
|
|
_orgsocket.connect(self, (self.__proxy[1], portnum))
|
|
|
|
|
if self.__proxy[0] == proxy_type_socks5:
|
|
|
|
|
port_number = self.__proxy[2] if self.__proxy[2] is not None else 1080
|
|
|
|
|
_org_socket.connect(self, (self.__proxy[1], port_number))
|
|
|
|
|
self.__negotiatesocks5()
|
|
|
|
|
return self.__resolvesocks5(host)
|
|
|
|
|
else:
|
|
|
|
|