Skip to content

Commit 2c02761

Browse files
committed
Fix encoding of intl DNS
IDNA encoding will generate xn-- prefixed byte strings when needed.
1 parent 2e369b8 commit 2c02761

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

socks.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,8 @@ def _write_SOCKS5_address(self, addr, file):
476476
# Well it's not an IP number, so it's probably a DNS name.
477477
if rdns:
478478
# Resolve remotely
479-
file.write(b"\x03" + chr(len(host)).encode() + host.encode())
479+
host_bytes = host.encode('idna')
480+
file.write(b"\x03" + chr(len(host_bytes)).encode() + host_bytes)
480481
else:
481482
# Resolve locally
482483
addr_bytes = socket.inet_aton(socket.gethostbyname(host))
@@ -533,7 +534,7 @@ def _negotiate_SOCKS4(self, dest_addr, dest_port):
533534
# NOTE: This is actually an extension to the SOCKS4 protocol
534535
# called SOCKS4A and may not be supported in all cases.
535536
if remote_resolve:
536-
writer.write(dest_addr.encode() + b"\x00")
537+
writer.write(dest_addr.encode('idna') + b"\x00")
537538
writer.flush()
538539

539540
# Get the response from the server
@@ -568,8 +569,8 @@ def _negotiate_HTTP(self, dest_addr, dest_port):
568569
# If we need to resolve locally, we do this now
569570
addr = dest_addr if rdns else socket.gethostbyname(dest_addr)
570571

571-
self.sendall(b"CONNECT " + addr.encode() + b":" + str(dest_port).encode() +
572-
b" HTTP/1.1\r\n" + b"Host: " + dest_addr.encode() + b"\r\n\r\n")
572+
self.sendall(b"CONNECT " + addr.encode('idna') + b":" + str(dest_port).encode() +
573+
b" HTTP/1.1\r\n" + b"Host: " + dest_addr.encode('idna') + b"\r\n\r\n")
573574

574575
# We just need the first line to check if the connection was successful
575576
fobj = self.makefile()

0 commit comments

Comments
 (0)