@@ -394,8 +394,8 @@ def recvfrom(self, bufsize, flags=0):
394
394
395
395
buf = BytesIO (super (socksocket , self ).recv (bufsize + 1024 , flags ))
396
396
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 :
399
399
raise NotImplementedError ("Received UDP packet fragment" )
400
400
fromhost , fromport = self ._read_SOCKS5_address (buf )
401
401
@@ -488,9 +488,10 @@ def _SOCKS5_request(self, conn, cmd, dst):
488
488
"Server requested username/password"
489
489
" authentication" )
490
490
491
- writer .write (b"\x01 " + chr (len (username )).encode ()
491
+ writer .write (b"\x01 "
492
+ + struct .pack (">B" , len (username ))
492
493
+ username
493
- + chr ( len (password )). encode ( )
494
+ + struct . pack ( ">B" , len (password ))
494
495
+ password )
495
496
writer .flush ()
496
497
auth_status = self ._readall (reader , 2 )
@@ -526,7 +527,7 @@ def _SOCKS5_request(self, conn, cmd, dst):
526
527
raise GeneralProxyError (
527
528
"SOCKS5 proxy server sent invalid data" )
528
529
529
- status = ord ( resp [1 :2 ])
530
+ status = struct . unpack ( ">B" , resp [1 :2 ])[ 0 ]
530
531
if status != 0x00 :
531
532
# Connection failed: server returned an error
532
533
error = SOCKS5_ERRORS .get (status , "Unknown error" )
@@ -567,7 +568,7 @@ def _write_SOCKS5_address(self, addr, file):
567
568
if rdns :
568
569
# Resolve remotely
569
570
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 )
571
572
else :
572
573
# Resolve locally
573
574
addresses = socket .getaddrinfo (host , port , socket .AF_UNSPEC ,
@@ -592,7 +593,7 @@ def _read_SOCKS5_address(self, file):
592
593
addr = socket .inet_ntoa (self ._readall (file , 4 ))
593
594
elif atyp == b"\x03 " :
594
595
length = self ._readall (file , 1 )
595
- addr = self ._readall (file , ord ( length ))
596
+ addr = self ._readall (file , struct . unpack ( ">B" , length )[ 0 ] )
596
597
elif atyp == b"\x04 " :
597
598
addr = socket .inet_ntop (socket .AF_INET6 , self ._readall (file , 16 ))
598
599
else :
@@ -644,7 +645,7 @@ def _negotiate_SOCKS4(self, dest_addr, dest_port):
644
645
raise GeneralProxyError (
645
646
"SOCKS4 proxy server sent invalid data" )
646
647
647
- status = ord ( resp [1 :2 ])
648
+ status = struct . unpack ( ">B" , resp [1 :2 ])[ 0 ]
648
649
if status != 0x5A :
649
650
# Connection failed: server returned an error
650
651
error = SOCKS4_ERRORS .get (status , "Unknown error" )
0 commit comments