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 __init__(self, dns_config_object): self.server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.server.bind(('', 0)) (self.controller_address, self.controller_port) = ('localhost', self.server.getsockname()[1]) self.dns_config = dns_config_object.get_config() self.dns_map = {} self.listen_thread_list = [] self.cache = {} self.enable_cache = self.dns_config['enable_cache'] self.hosts = self.load_hosts() self.dns_bypass_china = self.dns_config['dns_bypass_china'] if self.dns_bypass_china: self.net_list = [ line.rstrip('\n') for line in open('chnroute.txt') ] upstream_timeout = self.dns_config['upstream_timeout'] self.bootstrap_dns_object = upstream.UDPUpstream( self, self.controller_port, self.dns_config['bootstrap_dns_address'], upstream_timeout) self.enable_log = self.dns_config['enable_log'] if self.enable_log: self.logger = log.Logger() self.logger.create_log() self.init_listen()