Browse Source

refine test

auth
clowwindy 11 years ago
parent
commit
de5461a17e
  1. 51
      shadowsocks/asyncdns.py

51
shadowsocks/asyncdns.py

@ -21,6 +21,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE. # SOFTWARE.
import time
import socket import socket
import struct import struct
import logging import logging
@ -29,9 +30,9 @@ import lru_cache
import eventloop import eventloop
common.patch_socket() CACHE_SWEEP_INTERVAL = 30
_request_count = 1 common.patch_socket()
# rfc1035 # rfc1035
# format # format
@ -85,14 +86,10 @@ def build_address(address):
return ''.join(results) return ''.join(results)
def build_request(address, qtype): def build_request(address, qtype, request_id):
global _request_count header = struct.pack('!HBBHHHH', request_id, 1, 0, 1, 0, 0, 0)
header = struct.pack('!HBBHHHH', _request_count, 1, 0, 1, 0, 0, 0)
addr = build_address(address) addr = build_address(address)
qtype_qclass = struct.pack('!HH', qtype, QCLASS_IN) qtype_qclass = struct.pack('!HH', qtype, QCLASS_IN)
_request_count += 1
if _request_count > 65535:
_request_count = 1
return header + addr + qtype_qclass return header + addr + qtype_qclass
@ -240,10 +237,12 @@ class DNSResolver(object):
def __init__(self): def __init__(self):
self._loop = None self._loop = None
self._request_id = 1
self._hostname_status = {} self._hostname_status = {}
self._hostname_to_cb = {} self._hostname_to_cb = {}
self._cb_to_hostname = {} self._cb_to_hostname = {}
self._cache = lru_cache.LRUCache(timeout=300) self._cache = lru_cache.LRUCache(timeout=300)
self._last_time = time.time()
self._sock = None self._sock = None
self._parse_config() self._parse_config()
@ -327,6 +326,10 @@ class DNSResolver(object):
break break
self._handle_data(data) self._handle_data(data)
break break
now = time.time()
if now - self._last_time > CACHE_SWEEP_INTERVAL:
self._cache.sweep()
self._last_time = now
def remove_callback(self, callback): def remove_callback(self, callback):
hostname = self._cb_to_hostname.get(callback) hostname = self._cb_to_hostname.get(callback)
@ -343,7 +346,10 @@ class DNSResolver(object):
def _send_req(self, hostname, qtype): def _send_req(self, hostname, qtype):
logging.debug('resolving %s with type %d using server %s', hostname, logging.debug('resolving %s with type %d using server %s', hostname,
qtype, self._dns_server) qtype, self._dns_server)
req = build_request(hostname, qtype) 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) self._sock.sendto(req, self._dns_server)
def resolve(self, hostname, callback): def resolve(self, hostname, callback):
@ -382,19 +388,20 @@ def test():
resolver = DNSResolver() resolver = DNSResolver()
resolver.add_to_loop(loop) resolver.add_to_loop(loop)
resolver.resolve('www.google.com', _callback) for hostname in ['www.google.com',
resolver.resolve('8.8.8.8', _callback) '8.8.8.8',
resolver.resolve('www.twitter.com', _callback) 'www.twitter.com',
resolver.resolve('ipv6.google.com', _callback) 'ipv6.google.com',
resolver.resolve('ipv6.l.google.com', _callback) 'ipv6.l.google.com',
resolver.resolve('www.gmail.com', _callback) 'www.gmail.com',
resolver.resolve('r4---sn-3qqp-ioql.googlevideo.com', _callback) 'r4---sn-3qqp-ioql.googlevideo.com',
resolver.resolve('www.baidu.com', _callback) 'www.baidu.com',
resolver.resolve('www.a.shifen.com', _callback) 'www.a.shifen.com',
resolver.resolve('m.baidu.jp', _callback) 'm.baidu.jp',
resolver.resolve('www.youku.com', _callback) 'www.youku.com',
resolver.resolve('www.twitter.com', _callback) 'www.twitter.com',
resolver.resolve('ipv6.google.com', _callback) 'ipv6.google.com']:
resolver.resolve(hostname, _callback)
loop.run() loop.run()

Loading…
Cancel
Save