From f9d8724aaa4d3cfbf070f01031e07e3d4934fc5b Mon Sep 17 00:00:00 2001 From: tanyewei Date: Thu, 31 Dec 2015 13:27:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81shadowsocks2.8.2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chinadns/dnsrelay.py | 64 +++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 33 deletions(-) diff --git a/chinadns/dnsrelay.py b/chinadns/dnsrelay.py index ffeb719..44cf6ef 100644 --- a/chinadns/dnsrelay.py +++ b/chinadns/dnsrelay.py @@ -28,16 +28,15 @@ import errno import struct import logging +import argparse +from shadowsocks import eventloop, asyncdns, lru_cache, common info = sys.version_info + if not (info[0] == 2 and info[1] >= 7): print 'Python 2.7 required' sys.exit(1) -import argparse -from shadowsocks import eventloop, asyncdns, lru_cache - - BUF_SIZE = 16384 CACHE_TIMEOUT = 10 @@ -79,7 +78,7 @@ def add_to_loop(self, loop): if self._loop: raise Exception('already add to loop') self._loop = loop - loop.add_handler(self.handle_events) + # loop.add_handler(self.handle_events) def _parse_hosts(self): etc_path = '/etc/hosts' @@ -92,7 +91,7 @@ def _parse_hosts(self): parts = line.split() if len(parts) >= 2: ip = parts[0] - if asyncdns.is_ip(ip): + if common.is_ip(ip): for i in xrange(1, len(parts)): hostname = parts[i] if hostname: @@ -118,11 +117,11 @@ def build_response(request, ip): # for hostname compression answer = struct.pack('!H', ((128 + 64) << 8 | 12)) + \ - struct.pack('!HHiH', qtype, asyncdns.QCLASS_IN, 300, - len(addr)) + addr + struct.pack('!HHiH', qtype, asyncdns.QCLASS_IN, 300, + len(addr)) + addr return header + question + answer - def handle_events(self, events): + def handle_event(self, sock, fd, event): pass @@ -158,14 +157,14 @@ def _rebuild_sockets(self): self._local_sock.close() self._remote_sock.close() self._create_sockets() - self._loop.add(self._local_sock, eventloop.POLL_IN) - self._loop.add(self._remote_sock, eventloop.POLL_IN) + self._loop.add(self._local_sock, eventloop.POLL_IN, self) + self._loop.add(self._remote_sock, eventloop.POLL_IN, self) def add_to_loop(self, loop): DNSRelay.add_to_loop(self, loop) - loop.add(self._local_sock, eventloop.POLL_IN) - loop.add(self._remote_sock, eventloop.POLL_IN) + loop.add(self._local_sock, eventloop.POLL_IN, self) + loop.add(self._remote_sock, eventloop.POLL_IN, self) def _handle_local(self, sock): try: @@ -224,6 +223,7 @@ def _handle_remote(self, sock): # delay empty results def _send_later(): self._local_sock.sendto(data, addr) + self._pending_responses.append((time.time(), _send_later)) return @@ -237,12 +237,11 @@ def _send_later(): # when we have changed our ip self._rebuild_sockets() - def handle_events(self, events): - for sock, fd, event in events: - if sock == self._local_sock: - self._handle_local(sock) - elif sock == self._remote_sock: - self._handle_remote(sock) + def handle_event(self, sock, fd, event): + if sock == self._local_sock: + self._handle_local(sock) + elif sock == self._remote_sock: + self._handle_remote(sock) now = time.time() if now - self._last_time > CACHE_TIMEOUT / 2: self._id_to_addr.sweep() @@ -282,8 +281,7 @@ def _handle_conn(self, sock): self._remote_addr[1], 0, socket.SOCK_STREAM, socket.SOL_TCP) if len(addrs) == 0: - raise Exception("can't get addrinfo for %s:%d" % - self._remote_addr) + raise Exception("can't get addrinfo for %s:null" % self._remote_addr) af, socktype, proto, canonname, sa = addrs[0] remote = socket.socket(af, socktype, proto) local.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1) @@ -291,8 +289,8 @@ def _handle_conn(self, sock): self._local_to_remote[local] = remote self._remote_to_local[remote] = local - self._loop.add(local, 0) - self._loop.add(remote, eventloop.POLL_OUT) + self._loop.add(local, 0, self) + self._loop.add(remote, eventloop.POLL_OUT, self) try: remote.connect(self._remote_addr) except (OSError, IOError) as e: @@ -356,17 +354,16 @@ def _handle_remote(self, remote, event): def add_to_loop(self, loop): DNSRelay.add_to_loop(self, loop) - loop.add(self._listen_sock, eventloop.POLL_IN) + loop.add(self._listen_sock, eventloop.POLL_IN, self) - def handle_events(self, events): - for sock, fd, event in events: - if sock == self._listen_sock: - self._handle_conn(sock) - elif sock in self._local_to_remote: - self._handle_local(sock, event) - elif sock in self._remote_to_local: - self._handle_remote(sock, event) - # TODO implement timeout + def handle_event(self, sock, fd, event): + if sock == self._listen_sock: + self._handle_conn(sock) + elif sock in self._local_to_remote: + self._handle_local(sock, event) + elif sock in self._remote_to_local: + self._handle_remote(sock, event) + # TODO implement timeout def main(): @@ -415,3 +412,4 @@ def main(): if __name__ == '__main__': main() +