Browse Source

Python3: Various bytes/unicode fixes

Nick Mathewson 5 years ago
parent
commit
620efe453c
1 changed files with 9 additions and 7 deletions
  1. 9 7
      lib/chutney/Traffic.py

+ 9 - 7
lib/chutney/Traffic.py

@@ -41,15 +41,17 @@ def socks_cmd(addr_port):
     """
     ver = 4  # Only SOCKSv4 for now.
     cmd = 1  # Stream connection.
-    user = '\x00'
+    user = b'\x00'
     dnsname = ''
     host, port = addr_port
     try:
         addr = socket.inet_aton(host)
     except socket.error:
-        addr = '\x00\x00\x00\x01'
+        addr = b'\x00\x00\x00\x01'
         dnsname = '%s\x00' % host
     debug("Socks 4a request to %s:%d" % (host, port))
+    if type(dnsname) != type(b""):
+        dnsname = dnsname.encode("ascii")
     return struct.pack('!BBH', ver, cmd, port) + addr + user + dnsname
 
 
@@ -133,7 +135,7 @@ class Sink(Peer):
 
     def __init__(self, tt, s):
         super(Sink, self).__init__(Peer.SINK, tt, s)
-        self.inbuf = ''
+        self.inbuf = b''
         self.repetitions = self.tt.repetitions
 
     def on_readable(self):
@@ -193,8 +195,8 @@ class Source(Peer):
         super(Source, self).__init__(Peer.SOURCE, tt)
         self.state = self.NOT_CONNECTED
         self.data = buf
-        self.outbuf = ''
-        self.inbuf = ''
+        self.outbuf = b''
+        self.inbuf = b''
         self.proxy = proxy
         self.repetitions = repetitions
         self._sent_no_bytes = 0
@@ -229,10 +231,10 @@ class Source(Peer):
                 return -1
             self.inbuf += inp
             if len(self.inbuf) == 8:
-                if ord(self.inbuf[0]) == 0 and ord(self.inbuf[1]) == 0x5a:
+                if self.inbuf[:2] == b'\x00\x5a':
                     debug("proxy handshake successful (fd=%d)" % self.fd())
                     self.state = self.CONNECTED
-                    self.inbuf = ''
+                    self.inbuf = b''
                     debug("successfully connected (fd=%d)" % self.fd())
                     # if we have no reps or no data, skip sending actual data
                     if self.want_to_write():