def test_inet_pton(): print(hex_2_bin(inet_pton(socket.AF_INET, '192.168.1.1'))) print(hex_2_bin(inet_pton(socket.AF_INET, "255.255.255.255"))) print(hex_2_bin(inet_pton(socket.AF_INET, "0.0.0.0"))) print( hex_2_bin( inet_pton(socket.AF_INET6, '1234:5678:1234:5678:1234:5678:1234:5678')))
def _get_redirect_host(self, client_address, ogn_data): host_list = self._redir_list or ["0.0.0.0:0"] hash_code = binascii.crc32(ogn_data) addrs = socket.getaddrinfo(client_address[0], client_address[1], 0, socket.SOCK_STREAM, socket.SOL_TCP) af, socktype, proto, canonname, sa = addrs[0] address_bytes = common.inet_pton(af, sa[0]) if len(address_bytes) == 16: addr = struct.unpack('>Q', address_bytes[8:])[0] if len(address_bytes) == 4: addr = struct.unpack('>I', address_bytes)[0] else: addr = 0 if type(host_list) == list: host_post = common.to_str( host_list[((hash_code & 0xffffffff) + addr) % len(host_list)]) else: host_post = host_list items = host_post.rsplit(':', 1) if len(items) > 1: try: return (items[0], int(items[1])) except: pass return (host_post, 80)
def _get_redirect_host(self, client_address, ogn_data): # test host_list = [(b"www.bing.com", 80), (b"www.microsoft.com", 80), (b"www.baidu.com", 443), (b"www.qq.com", 80), (b"www.csdn.net", 80), (b"1.2.3.4", 1000)] hash_code = binascii.crc32(ogn_data) addrs = socket.getaddrinfo(client_address[0], client_address[1], 0, socket.SOCK_STREAM, socket.SOL_TCP) af, socktype, proto, canonname, sa = addrs[0] address_bytes = common.inet_pton(af, sa[0]) if len(address_bytes) == 16: addr = struct.unpack('>Q', address_bytes[8:])[0] if len(address_bytes) == 4: addr = struct.unpack('>I', address_bytes)[0] else: addr = 0 return host_list[((hash_code & 0xffffffff) + addr + 3) % len(host_list)]
def _get_redirect_host(self, client_address, ogn_data): # test host_list = [(b"www.bing.com", 80), (b"www.microsoft.com", 80), (b"cloudfront.com", 80), (b"cloudflare.com", 80), (b"1.2.3.4", 1000), (b"0.0.0.0", 0)] hash_code = binascii.crc32(ogn_data) addrs = socket.getaddrinfo(client_address[0], client_address[1], 0, socket.SOCK_STREAM, socket.SOL_TCP) af, socktype, proto, canonname, sa = addrs[0] address_bytes = common.inet_pton(af, sa[0]) if len(address_bytes) == 16: addr = struct.unpack('>Q', address_bytes[8:])[0] if len(address_bytes) == 4: addr = struct.unpack('>I', address_bytes)[0] else: addr = 0 return host_list[((hash_code & 0xffffffff) + addr + 3) % len(host_list)]
def _get_redirect_host(self, client_address, ogn_data): host_list = self._redir_list or ["0.0.0.0:0"] hash_code = binascii.crc32(ogn_data) addrs = socket.getaddrinfo(client_address[0], client_address[1], 0, socket.SOCK_STREAM, socket.SOL_TCP) af, socktype, proto, canonname, sa = addrs[0] address_bytes = common.inet_pton(af, sa[0]) if af == socket.AF_INET6: addr = struct.unpack('>Q', address_bytes[8:])[0] elif af == socket.AF_INET: addr = struct.unpack('>I', address_bytes)[0] else: addr = 0 host_port = [] match_port = False if type(host_list) != list: host_list = [host_list] for host in host_list: items = common.to_str(host).rsplit(':', 1) if len(items) > 1: try: port = int(items[1]) if port == self._server._listen_port: match_port = True host_port.append((items[0], port)) except: pass else: host_port.append((host, 80)) if match_port: last_host_port = host_port host_port = [] for host in last_host_port: if host[1] == self._server._listen_port: host_port.append(host) return host_port[((hash_code & 0xffffffff) + addr) % len(host_port)]
def _get_redirect_host(self, client_address, ogn_data): host_list = self._redir_list or ["0.0.0.0:0"] hash_code = binascii.crc32(ogn_data) addrs = socket.getaddrinfo(client_address[0], client_address[1], 0, socket.SOCK_STREAM, socket.SOL_TCP) af, socktype, proto, canonname, sa = addrs[0] address_bytes = common.inet_pton(af, sa[0]) if len(address_bytes) == 16: addr = struct.unpack('>Q', address_bytes[8:])[0] if len(address_bytes) == 4: addr = struct.unpack('>I', address_bytes)[0] else: addr = 0 if type(host_list) == list: host_post = common.to_str(host_list[((hash_code & 0xffffffff) + addr) % len(host_list)]) else: host_post = host_list items = host_post.rsplit(':', 1) if len(items) > 1: try: return (items[0], int(items[1])) except: pass return (host_post, 80)