fix SOCKS

This commit is contained in:
Kashiko Koibumi 2024-06-01 06:01:32 +09:00
parent a504384c94
commit de0cd04772
No known key found for this signature in database
GPG Key ID: 8F06E069E37C40C4
2 changed files with 16 additions and 16 deletions

View File

@ -40,11 +40,11 @@ class Socks4a(Proxy):
def state_pre_connect(self): def state_pre_connect(self):
"""Handle feedback from SOCKS4a while it is connecting on our behalf""" """Handle feedback from SOCKS4a while it is connecting on our behalf"""
# Get the response # Get the response
if self.read_buf[0:1] != six.int2byte(0x00).encode(): if self.read_buf[0:1] != six.int2byte(0x00):
# bad data # bad data
self.close() self.close()
raise GeneralProxyError(1) raise GeneralProxyError(1)
elif self.read_buf[1:2] != six.int2byte(0x5A).encode(): elif self.read_buf[1:2] != six.int2byte(0x5A):
# Connection failed # Connection failed
self.close() self.close()
if six.byte2int(self.read_buf[1:2]) in (91, 92, 93): if six.byte2int(self.read_buf[1:2]) in (91, 92, 93):
@ -103,9 +103,9 @@ class Socks4aConnection(Socks4a):
self.append_write_buf(self.ipaddr) self.append_write_buf(self.ipaddr)
if self._auth: if self._auth:
self.append_write_buf(self._auth[0]) self.append_write_buf(self._auth[0])
self.append_write_buf(six.int2byte(0x00).encode()) self.append_write_buf(six.int2byte(0x00))
if rmtrslv: if rmtrslv:
self.append_write_buf(self.destination[0] + six.int2byte(0x00).encode()) self.append_write_buf(self.destination[0].encode("utf-8", "replace") + six.int2byte(0x00))
self.set_state("pre_connect", length=0, expectBytes=8) self.set_state("pre_connect", length=0, expectBytes=8)
return True return True
@ -133,8 +133,8 @@ class Socks4aResolver(Socks4a):
self.append_write_buf(struct.pack("BBBB", 0x00, 0x00, 0x00, 0x01)) self.append_write_buf(struct.pack("BBBB", 0x00, 0x00, 0x00, 0x01))
if self._auth: if self._auth:
self.append_write_buf(self._auth[0]) self.append_write_buf(self._auth[0])
self.append_write_buf(six.int2byte(0x00).encode()) self.append_write_buf(six.int2byte(0x00))
self.append_write_buf(self.host + six.int2byte(0x00).encode()) self.append_write_buf(self.host + six.int2byte(0x00))
self.set_state("pre_connect", length=0, expectBytes=8) self.set_state("pre_connect", length=0, expectBytes=8)
return True return True

View File

@ -98,10 +98,10 @@ class Socks5(Proxy):
def state_pre_connect(self): def state_pre_connect(self):
"""Handle feedback from socks5 while it is connecting on our behalf.""" """Handle feedback from socks5 while it is connecting on our behalf."""
# Get the response # Get the response
if self.read_buf[0:1] != six.int2byte(0x05).encode(): if self.read_buf[0:1] != six.int2byte(0x05):
self.close() self.close()
raise GeneralProxyError(1) raise GeneralProxyError(1)
elif self.read_buf[1:2] != six.int2byte(0x00).encode(): elif self.read_buf[1:2] != six.int2byte(0x00):
# Connection failed # Connection failed
self.close() self.close()
if six.byte2int(self.read_buf[1:2]) <= 8: if six.byte2int(self.read_buf[1:2]) <= 8:
@ -109,9 +109,9 @@ class Socks5(Proxy):
else: else:
raise Socks5Error(9) raise Socks5Error(9)
# Get the bound address/port # Get the bound address/port
elif self.read_buf[3:4] == six.int2byte(0x01).encode(): elif self.read_buf[3:4] == six.int2byte(0x01):
self.set_state("proxy_addr_1", length=4, expectBytes=4) self.set_state("proxy_addr_1", length=4, expectBytes=4)
elif self.read_buf[3:4] == six.int2byte(0x03).encode(): elif self.read_buf[3:4] == six.int2byte(0x03):
self.set_state("proxy_addr_2_1", length=4, expectBytes=1) self.set_state("proxy_addr_2_1", length=4, expectBytes=1)
else: else:
self.close() self.close()
@ -172,19 +172,19 @@ class Socks5Connection(Socks5):
# use the IPv4 address request even if remote resolving was specified. # use the IPv4 address request even if remote resolving was specified.
try: try:
self.ipaddr = socket.inet_aton(self.destination[0]) self.ipaddr = socket.inet_aton(self.destination[0])
self.append_write_buf(six.int2byte(0x01).encode() + self.ipaddr) self.append_write_buf(six.int2byte(0x01) + self.ipaddr)
except socket.error: # may be IPv6! except socket.error: # may be IPv6!
# Well it's not an IP number, so it's probably a DNS name. # Well it's not an IP number, so it's probably a DNS name.
if self._remote_dns: if self._remote_dns:
# Resolve remotely # Resolve remotely
self.ipaddr = None self.ipaddr = None
self.append_write_buf(six.int2byte(0x03).encode() + six.int2byte( self.append_write_buf(six.int2byte(0x03) + six.int2byte(
len(self.destination[0])).encode() + self.destination[0]) len(self.destination[0])) + self.destination[0].encode("utf-8", "replace"))
else: else:
# Resolve locally # Resolve locally
self.ipaddr = socket.inet_aton( self.ipaddr = socket.inet_aton(
socket.gethostbyname(self.destination[0])) socket.gethostbyname(self.destination[0]))
self.append_write_buf(six.int2byte(0x01).encode() + self.ipaddr) self.append_write_buf(six.int2byte(0x01) + self.ipaddr)
self.append_write_buf(struct.pack(">H", self.destination[1])) self.append_write_buf(struct.pack(">H", self.destination[1]))
self.set_state("pre_connect", length=0, expectBytes=4) self.set_state("pre_connect", length=0, expectBytes=4)
return True return True
@ -209,8 +209,8 @@ class Socks5Resolver(Socks5):
"""Perform resolving""" """Perform resolving"""
# Now we can request the actual connection # Now we can request the actual connection
self.append_write_buf(struct.pack('BBB', 0x05, 0xF0, 0x00)) self.append_write_buf(struct.pack('BBB', 0x05, 0xF0, 0x00))
self.append_write_buf(six.int2byte(0x03).encode() + six.int2byte( self.append_write_buf(six.int2byte(0x03) + six.int2byte(
len(self.host)).encode() + str(self.host)) len(self.host)) + bytes(self.host))
self.append_write_buf(struct.pack(">H", self.port)) self.append_write_buf(struct.pack(">H", self.port))
self.set_state("pre_connect", length=0, expectBytes=4) self.set_state("pre_connect", length=0, expectBytes=4)
return True return True