def encode(cls, data): sub_blocks = [] stream = BytesIO(data) while True: next_bytes = stream.read(255) byte_len = struct.pack("B", len(next_bytes)) sub_blocks.append(cls(byte_len + next_bytes)) if not len(next_bytes): break return sub_blocks
def recvfrom(self, bufsize, flags=0): if self.type != socket.SOCK_DGRAM: return _BaseSocket.recvfrom(self, bufsize, flags) if not self._proxyconn: self.bind(("", 0)) buf = BytesIO(_BaseSocket.recv(self, bufsize + 1024, flags)) buf.seek(2, 1) frag = buf.read(1) if ord(frag): raise NotImplementedError("Received UDP packet fragment") fromhost, fromport = self._read_SOCKS5_address(buf) if self.proxy_peername: peerhost, peerport = self.proxy_peername if fromhost != peerhost or peerport not in (0, fromport): raise socket.error(EAGAIN, "Packet filtered") return (buf.read(bufsize), (fromhost, fromport))