From de0cd04772daf6a6e9f1867ee2d3c0057ea23e99 Mon Sep 17 00:00:00 2001
From: Kashiko Koibumi <kashiko@tuta.io>
Date: Sat, 1 Jun 2024 06:01:32 +0900
Subject: [PATCH] fix SOCKS

---
 src/network/socks4a.py | 12 ++++++------
 src/network/socks5.py  | 20 ++++++++++----------
 2 files changed, 16 insertions(+), 16 deletions(-)

diff --git a/src/network/socks4a.py b/src/network/socks4a.py
index bf0adf29..122114b3 100644
--- a/src/network/socks4a.py
+++ b/src/network/socks4a.py
@@ -40,11 +40,11 @@ class Socks4a(Proxy):
     def state_pre_connect(self):
         """Handle feedback from SOCKS4a while it is connecting on our behalf"""
         # Get the response
-        if self.read_buf[0:1] != six.int2byte(0x00).encode():
+        if self.read_buf[0:1] != six.int2byte(0x00):
             # bad data
             self.close()
             raise GeneralProxyError(1)
-        elif self.read_buf[1:2] != six.int2byte(0x5A).encode():
+        elif self.read_buf[1:2] != six.int2byte(0x5A):
             # Connection failed
             self.close()
             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)
         if self._auth:
             self.append_write_buf(self._auth[0])
-        self.append_write_buf(six.int2byte(0x00).encode())
+        self.append_write_buf(six.int2byte(0x00))
         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)
         return True
 
@@ -133,8 +133,8 @@ class Socks4aResolver(Socks4a):
         self.append_write_buf(struct.pack("BBBB", 0x00, 0x00, 0x00, 0x01))
         if self._auth:
             self.append_write_buf(self._auth[0])
-        self.append_write_buf(six.int2byte(0x00).encode())
-        self.append_write_buf(self.host + six.int2byte(0x00).encode())
+        self.append_write_buf(six.int2byte(0x00))
+        self.append_write_buf(self.host + six.int2byte(0x00))
         self.set_state("pre_connect", length=0, expectBytes=8)
         return True
 
diff --git a/src/network/socks5.py b/src/network/socks5.py
index c295480c..ca5a52e8 100644
--- a/src/network/socks5.py
+++ b/src/network/socks5.py
@@ -98,10 +98,10 @@ class Socks5(Proxy):
     def state_pre_connect(self):
         """Handle feedback from socks5 while it is connecting on our behalf."""
         # Get the response
-        if self.read_buf[0:1] != six.int2byte(0x05).encode():
+        if self.read_buf[0:1] != six.int2byte(0x05):
             self.close()
             raise GeneralProxyError(1)
-        elif self.read_buf[1:2] != six.int2byte(0x00).encode():
+        elif self.read_buf[1:2] != six.int2byte(0x00):
             # Connection failed
             self.close()
             if six.byte2int(self.read_buf[1:2]) <= 8:
@@ -109,9 +109,9 @@ class Socks5(Proxy):
             else:
                 raise Socks5Error(9)
         # 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)
-        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)
         else:
             self.close()
@@ -172,19 +172,19 @@ class Socks5Connection(Socks5):
         # use the IPv4 address request even if remote resolving was specified.
         try:
             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!
             # Well it's not an IP number,  so it's probably a DNS name.
             if self._remote_dns:
                 # Resolve remotely
                 self.ipaddr = None
-                self.append_write_buf(six.int2byte(0x03).encode() + six.int2byte(
-                    len(self.destination[0])).encode() + self.destination[0])
+                self.append_write_buf(six.int2byte(0x03) + six.int2byte(
+                    len(self.destination[0])) + self.destination[0].encode("utf-8", "replace"))
             else:
                 # Resolve locally
                 self.ipaddr = socket.inet_aton(
                     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.set_state("pre_connect", length=0, expectBytes=4)
         return True
@@ -209,8 +209,8 @@ class Socks5Resolver(Socks5):
         """Perform resolving"""
         # Now we can request the actual connection
         self.append_write_buf(struct.pack('BBB', 0x05, 0xF0, 0x00))
-        self.append_write_buf(six.int2byte(0x03).encode() + six.int2byte(
-            len(self.host)).encode() + str(self.host))
+        self.append_write_buf(six.int2byte(0x03) + six.int2byte(
+            len(self.host)) + bytes(self.host))
         self.append_write_buf(struct.pack(">H", self.port))
         self.set_state("pre_connect", length=0, expectBytes=4)
         return True