|
@ -123,29 +123,6 @@ RSP_STATE_ERROR = b"\x03" |
|
|
RSP_STATE_DISCONNECT = b"\x04" |
|
|
RSP_STATE_DISCONNECT = b"\x04" |
|
|
RSP_STATE_REDIRECT = b"\x05" |
|
|
RSP_STATE_REDIRECT = b"\x05" |
|
|
|
|
|
|
|
|
class UDPAsyncDNSHandler(object): |
|
|
|
|
|
def __init__(self, params): |
|
|
|
|
|
self.params = params |
|
|
|
|
|
self.remote_addr = None |
|
|
|
|
|
self.call_back = None |
|
|
|
|
|
|
|
|
|
|
|
def resolve(self, dns_resolver, remote_addr, call_back): |
|
|
|
|
|
self.call_back = call_back |
|
|
|
|
|
self.remote_addr = remote_addr |
|
|
|
|
|
dns_resolver.resolve(remote_addr[0], self._handle_dns_resolved) |
|
|
|
|
|
|
|
|
|
|
|
def _handle_dns_resolved(self, result, error): |
|
|
|
|
|
if error: |
|
|
|
|
|
logging.error("%s when resolve DNS" % (error,)) #drop |
|
|
|
|
|
return |
|
|
|
|
|
if result: |
|
|
|
|
|
ip = result[1] |
|
|
|
|
|
if ip: |
|
|
|
|
|
if self.call_back: |
|
|
|
|
|
self.call_back(self.remote_addr, None, ip, True, *self.params) |
|
|
|
|
|
return |
|
|
|
|
|
logging.warning("can't resolve %s" % (self.remote_addr,)) |
|
|
|
|
|
|
|
|
|
|
|
def client_key(source_addr, server_af): |
|
|
def client_key(source_addr, server_af): |
|
|
# notice this is server af, not dest af |
|
|
# notice this is server af, not dest af |
|
|
return '%s:%s:%d' % (source_addr[0], source_addr[1], server_af) |
|
|
return '%s:%s:%d' % (source_addr[0], source_addr[1], server_af) |
|
@ -406,10 +383,14 @@ class UDPRelay(object): |
|
|
server_addr, server_port = dest_addr, dest_port |
|
|
server_addr, server_port = dest_addr, dest_port |
|
|
|
|
|
|
|
|
if (addrtype & 7) == 3: |
|
|
if (addrtype & 7) == 3: |
|
|
handler = UDPAsyncDNSHandler((data, r_addr, uid, header_length)) |
|
|
af = common.is_ip(server_addr) |
|
|
|
|
|
if af == False: |
|
|
|
|
|
handler = common.UDPAsyncDNSHandler((data, r_addr, uid, header_length)) |
|
|
handler.resolve(self._dns_resolver, (server_addr, server_port), self._handle_server_dns_resolved) |
|
|
handler.resolve(self._dns_resolver, (server_addr, server_port), self._handle_server_dns_resolved) |
|
|
else: |
|
|
else: |
|
|
self._handle_server_dns_resolved((server_addr, server_port), None, server_addr, False, data, r_addr, uid, header_length) |
|
|
self._handle_server_dns_resolved((server_addr, server_port), None, server_addr, False, data, r_addr, uid, header_length) |
|
|
|
|
|
else: |
|
|
|
|
|
self._handle_server_dns_resolved((server_addr, server_port), None, server_addr, False, data, r_addr, uid, header_length) |
|
|
|
|
|
|
|
|
def _handle_server_dns_resolved(self, remote_addr, addrs, server_addr, dns_resolved, data, r_addr, uid, header_length): |
|
|
def _handle_server_dns_resolved(self, remote_addr, addrs, server_addr, dns_resolved, data, r_addr, uid, header_length): |
|
|
user_id = self._listen_port |
|
|
user_id = self._listen_port |
|
@ -645,7 +626,6 @@ class UDPRelay(object): |
|
|
if self._closed: |
|
|
if self._closed: |
|
|
self._cache.clear(0) |
|
|
self._cache.clear(0) |
|
|
self._cache_dns_client.clear(0) |
|
|
self._cache_dns_client.clear(0) |
|
|
#self._dns_cache.sweep() |
|
|
|
|
|
if self._eventloop: |
|
|
if self._eventloop: |
|
|
self._eventloop.remove_periodic(self.handle_periodic) |
|
|
self._eventloop.remove_periodic(self.handle_periodic) |
|
|
self._eventloop.remove(self._server_socket) |
|
|
self._eventloop.remove(self._server_socket) |
|
@ -657,7 +637,6 @@ class UDPRelay(object): |
|
|
before_sweep_size = len(self._sockets) |
|
|
before_sweep_size = len(self._sockets) |
|
|
self._cache.sweep() |
|
|
self._cache.sweep() |
|
|
self._cache_dns_client.sweep() |
|
|
self._cache_dns_client.sweep() |
|
|
#self._dns_cache.sweep() |
|
|
|
|
|
if before_sweep_size != len(self._sockets): |
|
|
if before_sweep_size != len(self._sockets): |
|
|
logging.debug('UDP port %5d sockets %d' % (self._listen_port, len(self._sockets))) |
|
|
logging.debug('UDP port %5d sockets %d' % (self._listen_port, len(self._sockets))) |
|
|
self._sweep_timeout() |
|
|
self._sweep_timeout() |
|
|