def select_upstream(self): upstream_dns_list = self.dns_config['upstream_dns'] enable_weight = self.dns_config['upstream_weight'] upstream_timeout = self.dns_config['upstream_timeout'] weight_list = [] if enable_weight: for item in upstream_dns_list: weight_list.append(item['weight']) upstream_dns = random.choices(population=upstream_dns_list, weights=weight_list, k=1) upstream_dns = upstream_dns[0] else: upstream_dns = random.choice(upstream_dns_list) protocol = upstream_dns['protocol'] port = self.controller_port if protocol == 'udp': upstream_object = upstream.UDPUpstream(self, port, upstream_dns, upstream_timeout) elif protocol == 'tcp': upstream_object = upstream.TCPUpstream(self, port, upstream_dns, upstream_timeout) elif protocol == 'https': upstream_object = upstream.HTTPSUpstream(self, port, upstream_dns, upstream_timeout) elif protocol == 'tls': upstream_object = upstream.TLSUpstream(self, port, upstream_dns, upstream_timeout) else: return None return upstream_object
def shake_hand(self, item): if item['protocol'] == 'https': https_upstream = upstream.HTTPSUpstream( self.server, self.dns_config['listen_port'], item, self.dns_config['upstream_timeout']) return https_upstream if item['protocol'] == 'tls': tls_upstream = upstream.TLSUpstream( self.server, self.dns_config['listen_port'], item, self.dns_config['upstream_timeout']) tls_upstream.shake_hand() return tls_upstream