async def read_addr(self, atyp): if atyp == 1: # IPV4 data = await self._stream.read_exactly(4) host = socket.inet_ntoa(data) elif atyp == 4: # IPV6 data = await self._stream.read_exactly(16) host = socket.inet_ntop(socket.AF_INET6, data) elif atyp == 3: # hostname data = (await self._stream.read_exactly(1)) data += await self._stream.read_exactly(data[0]) host = data[1:].decode('ascii') else: raise Exception(f'unknow atyp: {atyp}') from None data_port = await self._stream.read_exactly(2) port = int.from_bytes(data_port, 'big') return host, port, atyp.to_bytes(1, 'big') + data + data_port
async def read_addr(self): atyp = await self.read_exactly(1) if atyp == b'\x01': # IPV4 ipv4 = await self.read_exactly(4) host = socket.inet_ntoa(ipv4) elif atyp == b'\x04': # IPV6 ipv6 = await self.read_exactly(16) host = socket.inet_ntop(socket.AF_INET6, ipv6) elif atyp == b'\x03': # hostname length = (await self.read_exactly(1))[0] hostname = await self.read_exactly(length) host = hostname.decode('ascii') else: raise Exception(f'unknow atyp: {atyp}') from None port = int.from_bytes((await self.read_exactly(2)), 'big') return (host, port)
async def read_addr(self): atyp = await self._stream.read_exactly(1) if atyp == b"\x01": # IPV4 data = await self._stream.read_exactly(4) host = socket.inet_ntoa(data) elif atyp == b"\x04": # IPV6 data = await self._stream.read_exactly(16) host = socket.inet_ntop(socket.AF_INET6, data) elif atyp == b"\x03": # hostname data = await self._stream.read_exactly(1) data += await self._stream.read_exactly(data[0]) host = data[1:].decode("ascii") else: raise Exception(f"unknow atyp: {atyp}") data_port = await self._stream.read_exactly(2) port = int.from_bytes(data_port, "big") return (host, port), atyp + data + data_port
async def read_addr(self): atyp = await self._stream.read_exactly(1) if atyp == b'\x01': # IPV4 data = await self._stream.read_exactly(4) host = socket.inet_ntoa(data) elif atyp == b'\x04': # IPV6 data = await self._stream.read_exactly(16) host = socket.inet_ntop(socket.AF_INET6, data) elif atyp == b'\x03': # hostname data = await self._stream.read_exactly(1) data += await self._stream.read_exactly(data[0]) host = data[1:].decode('ascii') else: raise Exception(f'unknow atyp: {atyp}') from None data_port = await self._stream.read_exactly(2) port = int.from_bytes(data_port, 'big') return (host, port), atyp + data + data_port
def unpack_addr(data, start=0): atyp = data[start] if atyp == 1: # IPV4 end = start + 5 ipv4 = data[start+1:end] host = socket.inet_ntoa(ipv4) elif atyp == 4: # IPV6 end = start + 17 ipv6 = data[start:end] host = socket.inet_ntop(socket.AF_INET6, ipv6) elif atyp == 3: # hostname length = data[start+1] end = start + 2 + length host = data[start+2:end].decode('ascii') else: raise Exception(f'unknow atyp: {atyp}') from None port = int.from_bytes(data[end:end+2], 'big') return (host, port), data[end+2:]
def unpack_addr(data, start=0): atyp = data[start] if atyp == 1: # IPV4 end = start + 5 ipv4 = data[start + 1:end] host = socket.inet_ntoa(ipv4) elif atyp == 4: # IPV6 end = start + 17 ipv6 = data[start:end] host = socket.inet_ntop(socket.AF_INET6, ipv6) elif atyp == 3: # hostname length = data[start + 1] end = start + 2 + length host = data[start + 2:end].decode("ascii") else: raise Exception(f"unknow atyp: {atyp}") port = int.from_bytes(data[end:end + 2], "big") return (host, port), data[end + 2:]