Browse Source

process DNS

dev
破娃酱 8 years ago
parent
commit
74083af472
  1. 20
      shadowsocks/udprelay.py

20
shadowsocks/udprelay.py

@ -902,7 +902,7 @@ class UDPRelay(object):
self._cache_dns_client = lru_cache.LRUCache(timeout=10, self._cache_dns_client = lru_cache.LRUCache(timeout=10,
close_callback=self._close_client_pair) close_callback=self._close_client_pair)
self._client_fd_to_server_addr = {} self._client_fd_to_server_addr = {}
self._dns_cache = lru_cache.LRUCache(timeout=300) self._dns_cache = lru_cache.LRUCache(timeout=1800)
self._eventloop = None self._eventloop = None
self._closed = False self._closed = False
self.server_transfer_ul = 0 self.server_transfer_ul = 0
@ -1158,20 +1158,28 @@ class UDPRelay(object):
connecttype, dest_addr, dest_port, header_length = header_result connecttype, dest_addr, dest_port, header_length = header_result
if self._is_local: if self._is_local:
connecttype = 3
server_addr, server_port = self._get_a_server() server_addr, server_port = self._get_a_server()
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 (connecttype & 7) == 3:
if addrs is None: addrs = self._dns_cache.get(server_addr, None)
# TODO async getaddrinfo if addrs is None:
# TODO async getaddrinfo
addrs = socket.getaddrinfo(server_addr, server_port, 0,
socket.SOCK_DGRAM, socket.SOL_UDP)
if not addrs:
# drop
return
else:
self._dns_cache[server_addr] = addrs
else:
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