Skip to content

Commit

Permalink
While IDNA is nice thing, in 99.9% cases it's not used anyway
Browse files Browse the repository at this point in the history
  • Loading branch information
alxchk committed Apr 25, 2017
1 parent fc72b3d commit 58e3bfe
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions pupy/network/lib/socks.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
import win_inet_pton
import socket
except ImportError:
raise ImportError('To run PySocks under windows you need to install win_inet_pton')
raise ImportError('To run PySocks under windows you need to install win_inet_pton')
else:
import socket

Expand Down Expand Up @@ -537,7 +537,11 @@ def _write_SOCKS5_address(self, addr, file):
# Well it's not an IP number, so it's probably a DNS name.
if rdns:
# Resolve remotely
host_bytes = host.encode('idna')
try:
host_bytes = host.encode('idna')
except:
host_bytes = host

file.write(b"\x03" + chr(len(host_bytes)).encode() + host_bytes)
else:
# Resolve locally
Expand Down Expand Up @@ -603,7 +607,12 @@ def _negotiate_SOCKS4(self, dest_addr, dest_port):
# NOTE: This is actually an extension to the SOCKS4 protocol
# called SOCKS4A and may not be supported in all cases.
if remote_resolve:
writer.write(dest_addr.encode('idna') + b"\x00")
try:
dest_addr = dest_addr.encode('idna')
except:
pass

writer.write(dest_addr + b"\x00")
writer.flush()

# Get the response from the server
Expand Down Expand Up @@ -637,10 +646,19 @@ def _negotiate_HTTP(self, dest_addr, dest_port):

# If we need to resolve locally, we do this now
addr = dest_addr if rdns else socket.gethostbyname(dest_addr)
try:
addr = addr.encode('idna')
except:
pass

try:
dest_addr = dest_addr.encode('idna')
except:
pass

http_headers = [
b"CONNECT " + addr.encode('idna') + b":" + str(dest_port).encode() + b" HTTP/1.1",
b"Host: " + dest_addr.encode('idna')
b"CONNECT " + addr + b":" + str(dest_port).encode() + b" HTTP/1.1",
b"Host: " + dest_addr
]

if username and password:
Expand Down Expand Up @@ -775,4 +793,3 @@ def _proxy_addr(self):
if not proxy_port:
raise GeneralProxyError("Invalid proxy type")
return proxy_addr, proxy_port

0 comments on commit 58e3bfe

Please sign in to comment.