|
|
@ -14,8 +14,8 @@ import socket
|
|
|
|
|
|
|
|
|
|
|
|
from .compat import (
|
|
|
|
from .compat import (
|
|
|
|
compat_ord,
|
|
|
|
compat_ord,
|
|
|
|
struct_pack,
|
|
|
|
compat_struct_pack,
|
|
|
|
struct_unpack,
|
|
|
|
compat_struct_unpack,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
__author__ = 'Timo Schmid <coding@timoschmid.de>'
|
|
|
|
__author__ = 'Timo Schmid <coding@timoschmid.de>'
|
|
|
@ -26,7 +26,7 @@ SOCKS4_REPLY_VERSION = 0x00
|
|
|
|
# if the client cannot resolve the destination host's domain name to find its
|
|
|
|
# if the client cannot resolve the destination host's domain name to find its
|
|
|
|
# IP address, it should set the first three bytes of DSTIP to NULL and the last
|
|
|
|
# IP address, it should set the first three bytes of DSTIP to NULL and the last
|
|
|
|
# byte to a non-zero value.
|
|
|
|
# byte to a non-zero value.
|
|
|
|
SOCKS4_DEFAULT_DSTIP = struct_pack('!BBBB', 0, 0, 0, 0xFF)
|
|
|
|
SOCKS4_DEFAULT_DSTIP = compat_struct_pack('!BBBB', 0, 0, 0, 0xFF)
|
|
|
|
|
|
|
|
|
|
|
|
SOCKS5_VERSION = 5
|
|
|
|
SOCKS5_VERSION = 5
|
|
|
|
SOCKS5_USER_AUTH_VERSION = 0x01
|
|
|
|
SOCKS5_USER_AUTH_VERSION = 0x01
|
|
|
@ -128,11 +128,11 @@ class sockssocket(socket.socket):
|
|
|
|
|
|
|
|
|
|
|
|
def _recv_bytes(self, cnt):
|
|
|
|
def _recv_bytes(self, cnt):
|
|
|
|
data = self.recvall(cnt)
|
|
|
|
data = self.recvall(cnt)
|
|
|
|
return struct_unpack('!{0}B'.format(cnt), data)
|
|
|
|
return compat_struct_unpack('!{0}B'.format(cnt), data)
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@staticmethod
|
|
|
|
def _len_and_data(data):
|
|
|
|
def _len_and_data(data):
|
|
|
|
return struct_pack('!B', len(data)) + data
|
|
|
|
return compat_struct_pack('!B', len(data)) + data
|
|
|
|
|
|
|
|
|
|
|
|
def _check_response_version(self, expected_version, got_version):
|
|
|
|
def _check_response_version(self, expected_version, got_version):
|
|
|
|
if got_version != expected_version:
|
|
|
|
if got_version != expected_version:
|
|
|
@ -153,7 +153,7 @@ class sockssocket(socket.socket):
|
|
|
|
|
|
|
|
|
|
|
|
ipaddr = self._resolve_address(destaddr, SOCKS4_DEFAULT_DSTIP, use_remote_dns=is_4a)
|
|
|
|
ipaddr = self._resolve_address(destaddr, SOCKS4_DEFAULT_DSTIP, use_remote_dns=is_4a)
|
|
|
|
|
|
|
|
|
|
|
|
packet = struct_pack('!BBH', SOCKS4_VERSION, Socks4Command.CMD_CONNECT, port) + ipaddr
|
|
|
|
packet = compat_struct_pack('!BBH', SOCKS4_VERSION, Socks4Command.CMD_CONNECT, port) + ipaddr
|
|
|
|
|
|
|
|
|
|
|
|
username = (self._proxy.username or '').encode('utf-8')
|
|
|
|
username = (self._proxy.username or '').encode('utf-8')
|
|
|
|
packet += username + b'\x00'
|
|
|
|
packet += username + b'\x00'
|
|
|
@ -163,7 +163,7 @@ class sockssocket(socket.socket):
|
|
|
|
|
|
|
|
|
|
|
|
self.sendall(packet)
|
|
|
|
self.sendall(packet)
|
|
|
|
|
|
|
|
|
|
|
|
version, resp_code, dstport, dsthost = struct_unpack('!BBHI', self.recvall(8))
|
|
|
|
version, resp_code, dstport, dsthost = compat_struct_unpack('!BBHI', self.recvall(8))
|
|
|
|
|
|
|
|
|
|
|
|
self._check_response_version(SOCKS4_REPLY_VERSION, version)
|
|
|
|
self._check_response_version(SOCKS4_REPLY_VERSION, version)
|
|
|
|
|
|
|
|
|
|
|
@ -177,14 +177,14 @@ class sockssocket(socket.socket):
|
|
|
|
self._setup_socks4(address, is_4a=True)
|
|
|
|
self._setup_socks4(address, is_4a=True)
|
|
|
|
|
|
|
|
|
|
|
|
def _socks5_auth(self):
|
|
|
|
def _socks5_auth(self):
|
|
|
|
packet = struct_pack('!B', SOCKS5_VERSION)
|
|
|
|
packet = compat_struct_pack('!B', SOCKS5_VERSION)
|
|
|
|
|
|
|
|
|
|
|
|
auth_methods = [Socks5Auth.AUTH_NONE]
|
|
|
|
auth_methods = [Socks5Auth.AUTH_NONE]
|
|
|
|
if self._proxy.username and self._proxy.password:
|
|
|
|
if self._proxy.username and self._proxy.password:
|
|
|
|
auth_methods.append(Socks5Auth.AUTH_USER_PASS)
|
|
|
|
auth_methods.append(Socks5Auth.AUTH_USER_PASS)
|
|
|
|
|
|
|
|
|
|
|
|
packet += struct_pack('!B', len(auth_methods))
|
|
|
|
packet += compat_struct_pack('!B', len(auth_methods))
|
|
|
|
packet += struct_pack('!{0}B'.format(len(auth_methods)), *auth_methods)
|
|
|
|
packet += compat_struct_pack('!{0}B'.format(len(auth_methods)), *auth_methods)
|
|
|
|
|
|
|
|
|
|
|
|
self.sendall(packet)
|
|
|
|
self.sendall(packet)
|
|
|
|
|
|
|
|
|
|
|
@ -199,7 +199,7 @@ class sockssocket(socket.socket):
|
|
|
|
if method == Socks5Auth.AUTH_USER_PASS:
|
|
|
|
if method == Socks5Auth.AUTH_USER_PASS:
|
|
|
|
username = self._proxy.username.encode('utf-8')
|
|
|
|
username = self._proxy.username.encode('utf-8')
|
|
|
|
password = self._proxy.password.encode('utf-8')
|
|
|
|
password = self._proxy.password.encode('utf-8')
|
|
|
|
packet = struct_pack('!B', SOCKS5_USER_AUTH_VERSION)
|
|
|
|
packet = compat_struct_pack('!B', SOCKS5_USER_AUTH_VERSION)
|
|
|
|
packet += self._len_and_data(username) + self._len_and_data(password)
|
|
|
|
packet += self._len_and_data(username) + self._len_and_data(password)
|
|
|
|
self.sendall(packet)
|
|
|
|
self.sendall(packet)
|
|
|
|
|
|
|
|
|
|
|
@ -221,14 +221,14 @@ class sockssocket(socket.socket):
|
|
|
|
self._socks5_auth()
|
|
|
|
self._socks5_auth()
|
|
|
|
|
|
|
|
|
|
|
|
reserved = 0
|
|
|
|
reserved = 0
|
|
|
|
packet = struct_pack('!BBB', SOCKS5_VERSION, Socks5Command.CMD_CONNECT, reserved)
|
|
|
|
packet = compat_struct_pack('!BBB', SOCKS5_VERSION, Socks5Command.CMD_CONNECT, reserved)
|
|
|
|
if ipaddr is None:
|
|
|
|
if ipaddr is None:
|
|
|
|
destaddr = destaddr.encode('utf-8')
|
|
|
|
destaddr = destaddr.encode('utf-8')
|
|
|
|
packet += struct_pack('!B', Socks5AddressType.ATYP_DOMAINNAME)
|
|
|
|
packet += compat_struct_pack('!B', Socks5AddressType.ATYP_DOMAINNAME)
|
|
|
|
packet += self._len_and_data(destaddr)
|
|
|
|
packet += self._len_and_data(destaddr)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
packet += struct_pack('!B', Socks5AddressType.ATYP_IPV4) + ipaddr
|
|
|
|
packet += compat_struct_pack('!B', Socks5AddressType.ATYP_IPV4) + ipaddr
|
|
|
|
packet += struct_pack('!H', port)
|
|
|
|
packet += compat_struct_pack('!H', port)
|
|
|
|
|
|
|
|
|
|
|
|
self.sendall(packet)
|
|
|
|
self.sendall(packet)
|
|
|
|
|
|
|
|
|
|
|
@ -247,7 +247,7 @@ class sockssocket(socket.socket):
|
|
|
|
destaddr = self.recvall(alen)
|
|
|
|
destaddr = self.recvall(alen)
|
|
|
|
elif atype == Socks5AddressType.ATYP_IPV6:
|
|
|
|
elif atype == Socks5AddressType.ATYP_IPV6:
|
|
|
|
destaddr = self.recvall(16)
|
|
|
|
destaddr = self.recvall(16)
|
|
|
|
destport = struct_unpack('!H', self.recvall(2))[0]
|
|
|
|
destport = compat_struct_unpack('!H', self.recvall(2))[0]
|
|
|
|
|
|
|
|
|
|
|
|
return (destaddr, destport)
|
|
|
|
return (destaddr, destport)
|
|
|
|
|
|
|
|
|
|
|
|