Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix length field conversion #164

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions socks.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,8 @@ def recvfrom(self, bufsize, flags=0):

buf = BytesIO(super(socksocket, self).recv(bufsize + 1024, flags))
buf.seek(2, SEEK_CUR)
frag = buf.read(1)
if ord(frag):
frag = struct.unpack(">B", buf.read(1))[0]
if frag:
raise NotImplementedError("Received UDP packet fragment")
fromhost, fromport = self._read_SOCKS5_address(buf)

Expand Down Expand Up @@ -488,9 +488,10 @@ def _SOCKS5_request(self, conn, cmd, dst):
"Server requested username/password"
" authentication")

writer.write(b"\x01" + chr(len(username)).encode()
writer.write(b"\x01"
+ struct.pack(">B", len(username))
+ username
+ chr(len(password)).encode()
+ struct.pack(">B", len(password))
+ password)
writer.flush()
auth_status = self._readall(reader, 2)
Expand Down Expand Up @@ -526,7 +527,7 @@ def _SOCKS5_request(self, conn, cmd, dst):
raise GeneralProxyError(
"SOCKS5 proxy server sent invalid data")

status = ord(resp[1:2])
status = struct.unpack(">B", resp[1:2])[0]
if status != 0x00:
# Connection failed: server returned an error
error = SOCKS5_ERRORS.get(status, "Unknown error")
Expand Down Expand Up @@ -567,7 +568,7 @@ def _write_SOCKS5_address(self, addr, file):
if rdns:
# Resolve remotely
host_bytes = host.encode("idna")
file.write(b"\x03" + chr(len(host_bytes)).encode() + host_bytes)
file.write(b"\x03" + struct.pack(">B", len(host_bytes)) + host_bytes)
else:
# Resolve locally
addresses = socket.getaddrinfo(host, port, socket.AF_UNSPEC,
Expand All @@ -592,7 +593,7 @@ def _read_SOCKS5_address(self, file):
addr = socket.inet_ntoa(self._readall(file, 4))
elif atyp == b"\x03":
length = self._readall(file, 1)
addr = self._readall(file, ord(length))
addr = self._readall(file, struct.unpack(">B", length)[0])
elif atyp == b"\x04":
addr = socket.inet_ntop(socket.AF_INET6, self._readall(file, 16))
else:
Expand Down Expand Up @@ -644,7 +645,7 @@ def _negotiate_SOCKS4(self, dest_addr, dest_port):
raise GeneralProxyError(
"SOCKS4 proxy server sent invalid data")

status = ord(resp[1:2])
status = struct.unpack(">B", resp[1:2])[0]
if status != 0x5A:
# Connection failed: server returned an error
error = SOCKS4_ERRORS.get(status, "Unknown error")
Expand Down