diff --git a/shadowsocks/asyncdns.py b/shadowsocks/asyncdns.py index ce0076d..a3c276c 100644 --- a/shadowsocks/asyncdns.py +++ b/shadowsocks/asyncdns.py @@ -32,7 +32,7 @@ import lru_cache import eventloop -CACHE_SWEEP_INTERVAL = 10 +CACHE_SWEEP_INTERVAL = 30 VALID_HOSTNAME = re.compile("(?!-)[A-Z\d-]{1,63}(?= 2: server = parts[1] if is_ip(server): - servers.append(server) - # TODO support more servers - + self._servers.append(server) except IOError: pass - if not servers: - servers.append('8.8.8.8') - self._dns_server = (servers[0], 53) - - def _timedout(self, hostname): - error = Exception('timed out when resolving %s with DNS %s' % - (hostname, self._dns_server)) - self._server_index += 1 - self._server_index %= len(self._servers) - self._dns_server = (self._servers[self._server_index], 53) - self._call_callback(hostname, None, error=error) + if not self._servers: + self._servers = ['8.8.4.4', '8.8.8.8'] def _parse_hosts(self): etc_path = '/etc/hosts' @@ -389,7 +375,7 @@ class DNSResolver(object): self._loop.add(self._sock, eventloop.POLL_IN) else: data, addr = sock.recvfrom(1024) - if addr != self._dns_server: + if addr[0] not in self._servers: logging.warn('received a packet other than our dns') break self._handle_data(data) @@ -397,7 +383,6 @@ class DNSResolver(object): now = time.time() if now - self._last_time > CACHE_SWEEP_INTERVAL: self._cache.sweep() - self._cb_to_hostname.sweep() self._last_time = now def remove_callback(self, callback): @@ -413,13 +398,14 @@ class DNSResolver(object): del self._hostname_status[hostname] def _send_req(self, hostname, qtype): - logging.debug('resolving %s with type %d using server %s', hostname, - qtype, self._dns_server) self._request_id += 1 if self._request_id > 32768: self._request_id = 1 req = build_request(hostname, qtype, self._request_id) - self._sock.sendto(req, self._dns_server) + for server in self._servers: + logging.debug('resolving %s with type %d using server %s', hostname, + qtype, server) + self._sock.sendto(req, (server, 53)) def resolve(self, hostname, callback): if not hostname: