From c8423e360dfc99c81fcaf0f7882385c17eb28b48 Mon Sep 17 00:00:00 2001 From: BreakWa11 Date: Mon, 26 Oct 2015 16:37:27 +0800 Subject: [PATCH] DNS request ipv6 first --- server.py | 3 ++- shadowsocks/asyncdns.py | 44 ++++++++++++++++++++++++++++++++--------- shadowsocks/local.py | 9 ++++++++- shadowsocks/server.py | 9 ++++++++- 4 files changed, 53 insertions(+), 12 deletions(-) diff --git a/server.py b/server.py index 2030397..c93cee5 100644 --- a/server.py +++ b/server.py @@ -3,7 +3,8 @@ import time import sys import threading import os -os.chdir(os.path.split(os.path.realpath(__file__))[0]) + +os.chdir(os.path.dirname(os.path.realpath(__file__))) import server_pool import db_transfer diff --git a/shadowsocks/asyncdns.py b/shadowsocks/asyncdns.py index c5fc99d..08ec5f0 100644 --- a/shadowsocks/asyncdns.py +++ b/shadowsocks/asyncdns.py @@ -24,6 +24,13 @@ import struct import re import logging +if __name__ == '__main__': + import sys + try: + sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../')) + except: + sys.path.insert(0, os.path.join(sys.path[0], '../')) + from shadowsocks import common, lru_cache, eventloop, shell @@ -71,6 +78,17 @@ QTYPE_CNAME = 5 QTYPE_NS = 2 QCLASS_IN = 1 +def detect_ipv6_supprot(): + if 'has_ipv6' in dir(socket): + s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) + try: + s.connect(('ipv6.google.com', 0)) + return True + except: + pass + return False + +IPV6_CONNECTION_SUPPORT = detect_ipv6_supprot() def build_address(address): address = address.strip(b'.') @@ -338,17 +356,17 @@ class DNSResolver(object): answer[2] == QCLASS_IN: ip = answer[0] break - if not ip and self._hostname_status.get(hostname, STATUS_IPV6) \ - == STATUS_IPV4: - self._hostname_status[hostname] = STATUS_IPV6 - self._send_req(hostname, QTYPE_AAAA) + if not ip and self._hostname_status.get(hostname, STATUS_IPV4) \ + == STATUS_IPV6: + self._hostname_status[hostname] = STATUS_IPV4 + self._send_req(hostname, QTYPE_A) else: if ip: self._cache[hostname] = ip self._call_callback(hostname, ip) - elif self._hostname_status.get(hostname, None) == STATUS_IPV6: + elif self._hostname_status.get(hostname, None) == STATUS_IPV4: for question in response.questions: - if question[1] == QTYPE_AAAA: + if question[1] == QTYPE_A: self._call_callback(hostname, None) break @@ -414,14 +432,21 @@ class DNSResolver(object): return arr = self._hostname_to_cb.get(hostname, None) if not arr: - self._hostname_status[hostname] = STATUS_IPV4 - self._send_req(hostname, QTYPE_A) + if IPV6_CONNECTION_SUPPORT: + self._hostname_status[hostname] = STATUS_IPV6 + self._send_req(hostname, QTYPE_AAAA) + else: + self._hostname_status[hostname] = STATUS_IPV4 + self._send_req(hostname, QTYPE_A) self._hostname_to_cb[hostname] = [callback] self._cb_to_hostname[callback] = hostname else: arr.append(callback) # TODO send again only if waited too long - self._send_req(hostname, QTYPE_A) + if IPV6_CONNECTION_SUPPORT: + self._send_req(hostname, QTYPE_AAAA) + else: + self._send_req(hostname, QTYPE_A) def close(self): if self._sock: @@ -479,3 +504,4 @@ def test(): if __name__ == '__main__': test() + diff --git a/shadowsocks/local.py b/shadowsocks/local.py index 4255a2e..79fd04e 100755 --- a/shadowsocks/local.py +++ b/shadowsocks/local.py @@ -23,7 +23,14 @@ import os import logging import signal -sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../')) +if __name__ == '__main__': + try: + os.chdir(os.path.dirname(os.path.realpath(__file__))) + sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../')) + except: + os.chdir(os.path.dirname(sys.path[0])) + sys.path.insert(0, os.path.join(sys.path[0], '../')) + from shadowsocks import shell, daemon, eventloop, tcprelay, udprelay, asyncdns diff --git a/shadowsocks/server.py b/shadowsocks/server.py index 9734382..b6ef3c7 100755 --- a/shadowsocks/server.py +++ b/shadowsocks/server.py @@ -23,7 +23,14 @@ import os import logging import signal -sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../')) +if __name__ == '__main__': + try: + os.chdir(os.path.dirname(os.path.realpath(__file__))) + sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../')) + except: + os.chdir(os.path.dirname(sys.path[0])) + sys.path.insert(0, os.path.join(sys.path[0], '../')) + from shadowsocks import shell, daemon, eventloop, tcprelay, udprelay, \ asyncdns, manager