Browse Source

cache DNS results in UDPRelay

master
clowwindy 9 years ago
parent
commit
13a6bb007c
  1. 5
      shadowsocks/udprelay.py

5
shadowsocks/udprelay.py

@ -103,6 +103,7 @@ class UDPRelay(object):
close_callback=self._close_client) close_callback=self._close_client)
self._client_fd_to_server_addr = \ self._client_fd_to_server_addr = \
lru_cache.LRUCache(timeout=config['timeout']) lru_cache.LRUCache(timeout=config['timeout'])
self._dns_cache = lru_cache.LRUCache(timeout=300)
self._eventloop = None self._eventloop = None
self._closed = False self._closed = False
self._last_time = time.time() self._last_time = time.time()
@ -170,11 +171,15 @@ class UDPRelay(object):
else: else:
server_addr, server_port = dest_addr, dest_port server_addr, server_port = dest_addr, dest_port
addrs = self._dns_cache.get(server_addr, None)
if addrs is None:
addrs = socket.getaddrinfo(server_addr, server_port, 0, addrs = socket.getaddrinfo(server_addr, server_port, 0,
socket.SOCK_DGRAM, socket.SOL_UDP) socket.SOCK_DGRAM, socket.SOL_UDP)
if not addrs: if not addrs:
# drop # drop
return return
else:
self._dns_cache[server_addr] = addrs
af, socktype, proto, canonname, sa = addrs[0] af, socktype, proto, canonname, sa = addrs[0]
key = client_key(r_addr, af) key = client_key(r_addr, af)

Loading…
Cancel
Save