@@ -394,8 +394,8 @@ def recvfrom(self, bufsize, flags=0):
394394
395395 buf = BytesIO (super (socksocket , self ).recv (bufsize + 1024 , flags ))
396396 buf .seek (2 , SEEK_CUR )
397- frag = buf .read (1 )
398- if ord ( frag ) :
397+ frag = struct . unpack ( ">B" , buf .read (1 ))[ 0 ]
398+ if frag :
399399 raise NotImplementedError ("Received UDP packet fragment" )
400400 fromhost , fromport = self ._read_SOCKS5_address (buf )
401401
@@ -488,9 +488,10 @@ def _SOCKS5_request(self, conn, cmd, dst):
488488 "Server requested username/password"
489489 " authentication" )
490490
491- writer .write (b"\x01 " + chr (len (username )).encode ()
491+ writer .write (b"\x01 "
492+ + struct .pack (">B" , len (username ))
492493 + username
493- + chr ( len (password )). encode ( )
494+ + struct . pack ( ">B" , len (password ))
494495 + password )
495496 writer .flush ()
496497 auth_status = self ._readall (reader , 2 )
@@ -526,7 +527,7 @@ def _SOCKS5_request(self, conn, cmd, dst):
526527 raise GeneralProxyError (
527528 "SOCKS5 proxy server sent invalid data" )
528529
529- status = ord ( resp [1 :2 ])
530+ status = struct . unpack ( ">B" , resp [1 :2 ])[ 0 ]
530531 if status != 0x00 :
531532 # Connection failed: server returned an error
532533 error = SOCKS5_ERRORS .get (status , "Unknown error" )
@@ -567,7 +568,7 @@ def _write_SOCKS5_address(self, addr, file):
567568 if rdns :
568569 # Resolve remotely
569570 host_bytes = host .encode ("idna" )
570- file .write (b"\x03 " + chr ( len (host_bytes )). encode ( ) + host_bytes )
571+ file .write (b"\x03 " + struct . pack ( ">B" , len (host_bytes )) + host_bytes )
571572 else :
572573 # Resolve locally
573574 addresses = socket .getaddrinfo (host , port , socket .AF_UNSPEC ,
@@ -592,7 +593,7 @@ def _read_SOCKS5_address(self, file):
592593 addr = socket .inet_ntoa (self ._readall (file , 4 ))
593594 elif atyp == b"\x03 " :
594595 length = self ._readall (file , 1 )
595- addr = self ._readall (file , ord ( length ))
596+ addr = self ._readall (file , struct . unpack ( ">B" , length )[ 0 ] )
596597 elif atyp == b"\x04 " :
597598 addr = socket .inet_ntop (socket .AF_INET6 , self ._readall (file , 16 ))
598599 else :
@@ -644,7 +645,7 @@ def _negotiate_SOCKS4(self, dest_addr, dest_port):
644645 raise GeneralProxyError (
645646 "SOCKS4 proxy server sent invalid data" )
646647
647- status = ord ( resp [1 :2 ])
648+ status = struct . unpack ( ">B" , resp [1 :2 ])[ 0 ]
648649 if status != 0x5A :
649650 # Connection failed: server returned an error
650651 error = SOCKS4_ERRORS .get (status , "Unknown error" )
0 commit comments