From 42dc5055afa144eaa6faf59e3b9b9d6978bbbec6 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 16 Jul 2019 14:00:54 +0800 Subject: [PATCH] remove main dns cache --- cmd/tun2socks/main.go | 4 ++-- cmd/tun2socks/main_d.go | 2 +- cmd/tun2socks/main_socks.go | 2 +- proxy/socks/tcp.go | 12 +++++------ proxy/socks/udp.go | 41 ++++++++++++++----------------------- 5 files changed, 25 insertions(+), 36 deletions(-) diff --git a/cmd/tun2socks/main.go b/cmd/tun2socks/main.go index f3e6f6a..b5221e9 100644 --- a/cmd/tun2socks/main.go +++ b/cmd/tun2socks/main.go @@ -19,7 +19,7 @@ import ( "github.com/xjasonlyu/tun2socks/tun" ) -var version = "undefined" +var Version = "unknown version" var handlerCreator = make(map[string]func(), 0) @@ -121,7 +121,7 @@ func main() { flag.Parse() if *args.Version { - fmt.Println(version) + fmt.Println("TUN2SOCKS", Version) os.Exit(0) } diff --git a/cmd/tun2socks/main_d.go b/cmd/tun2socks/main_d.go index feed409..1e35efe 100644 --- a/cmd/tun2socks/main_d.go +++ b/cmd/tun2socks/main_d.go @@ -31,7 +31,7 @@ func init() { proxyPort := uint16(proxyAddr.Port) proxyTCPHandler := socks.NewTCPHandler(proxyHost, proxyPort, fakeDns, sessionStater) - proxyUDPHandler := socks.NewUDPHandler(proxyHost, proxyPort, *args.UdpTimeout, dnsCache, fakeDns, sessionStater) + proxyUDPHandler := socks.NewUDPHandler(proxyHost, proxyPort, *args.UdpTimeout, fakeDns, sessionStater) sendThrough, err := net.ResolveTCPAddr("tcp", *args.ExceptionSendThrough) if err != nil { diff --git a/cmd/tun2socks/main_socks.go b/cmd/tun2socks/main_socks.go index 9b46d7a..a55d706 100644 --- a/cmd/tun2socks/main_socks.go +++ b/cmd/tun2socks/main_socks.go @@ -25,6 +25,6 @@ func init() { proxyPort := uint16(proxyAddr.Port) core.RegisterTCPConnHandler(socks.NewTCPHandler(proxyHost, proxyPort, fakeDns, sessionStater)) - core.RegisterUDPConnHandler(socks.NewUDPHandler(proxyHost, proxyPort, *args.UdpTimeout, dnsCache, fakeDns, sessionStater)) + core.RegisterUDPConnHandler(socks.NewUDPHandler(proxyHost, proxyPort, *args.UdpTimeout, fakeDns, sessionStater)) }) } diff --git a/proxy/socks/tcp.go b/proxy/socks/tcp.go index 31f2900..797badf 100644 --- a/proxy/socks/tcp.go +++ b/proxy/socks/tcp.go @@ -148,14 +148,14 @@ func (h *tcpHandler) Handle(conn net.Conn, target *net.TCPAddr) error { } // Replace with a domain name if target address IP is a fake IP. - var targetHost string - if h.fakeDns != nil && h.fakeDns.IsFakeIP(target.IP) { - targetHost = h.fakeDns.IPToHost(target.IP) - } else { - targetHost = target.IP.String() + var targetHost = target.IP.String() + if h.fakeDns != nil { + if t := h.fakeDns.IPToHost(target.IP); t != "" { + targetHost = t + } } - dest := net.JoinHostPort(targetHost, strconv.Itoa(target.Port)) + dest := net.JoinHostPort(targetHost, strconv.Itoa(target.Port)) c, err := dialer.Dial(target.Network(), dest) if err != nil { return err diff --git a/proxy/socks/udp.go b/proxy/socks/udp.go index ef596be..c4d611e 100644 --- a/proxy/socks/udp.go +++ b/proxy/socks/udp.go @@ -26,19 +26,17 @@ type udpHandler struct { remoteAddrs map[core.UDPConn]*net.UDPAddr // UDP relay server addresses timeout time.Duration - dnsCache dns.DnsCache fakeDns dns.FakeDns sessionStater stats.SessionStater } -func NewUDPHandler(proxyHost string, proxyPort uint16, timeout time.Duration, dnsCache dns.DnsCache, fakeDns dns.FakeDns, sessionStater stats.SessionStater) core.UDPConnHandler { +func NewUDPHandler(proxyHost string, proxyPort uint16, timeout time.Duration, fakeDns dns.FakeDns, sessionStater stats.SessionStater) core.UDPConnHandler { return &udpHandler{ proxyHost: proxyHost, proxyPort: proxyPort, udpConns: make(map[core.UDPConn]net.PacketConn, 8), tcpConns: make(map[core.UDPConn]net.Conn, 8), remoteAddrs: make(map[core.UDPConn]*net.UDPAddr, 8), - dnsCache: dnsCache, fakeDns: fakeDns, timeout: timeout, sessionStater: sessionStater, @@ -94,17 +92,6 @@ func (h *udpHandler) fetchUDPInput(conn core.UDPConn, input net.PacketConn) { log.Warnf("write local failed: %v", err) return } - - if h.dnsCache != nil { - _, port, err := net.SplitHostPort(addr.String()) - if err != nil { - panic("impossible error") - } - if port == strconv.Itoa(dns.CommonDnsPort) { - h.dnsCache.Store(buf[int(3+len(addr)):n]) - return // DNS response - } - } } } @@ -114,13 +101,15 @@ func (h *udpHandler) Connect(conn core.UDPConn, target *net.UDPAddr) error { } // Replace with a domain name if target address IP is a fake IP. - targetHost := target.IP.String() + var targetHost = target.IP.String() if h.fakeDns != nil { - if target.Port == dns.CommonDnsPort { - return nil // skip dns - } - if h.fakeDns.IsFakeIP(target.IP) { - targetHost = h.fakeDns.IPToHost(target.IP) + /* + if target.Port == dns.CommonDnsPort { + return nil // skip dns + } + */ + if t := h.fakeDns.IPToHost(target.IP); t != "" { + targetHost = t } } dest := net.JoinHostPort(targetHost, strconv.Itoa(target.Port)) @@ -222,14 +211,14 @@ func (h *udpHandler) ReceiveTo(conn core.UDPConn, data []byte, addr *net.UDPAddr // use system DNS instead of force override if ok1 && ok2 { - var targetHost string - if h.fakeDns != nil && h.fakeDns.IsFakeIP(addr.IP) { - targetHost = h.fakeDns.IPToHost(addr.IP) - } else { - targetHost = addr.IP.String() + var targetHost = addr.IP.String() + if h.fakeDns != nil { + if t := h.fakeDns.IPToHost(addr.IP); t != "" { + targetHost = t + } } - dest := net.JoinHostPort(targetHost, strconv.Itoa(addr.Port)) + dest := net.JoinHostPort(targetHost, strconv.Itoa(addr.Port)) buf := append([]byte{0, 0, 0}, ParseAddr(dest)...) buf = append(buf, data[:]...) n, err := pc.WriteTo(buf, remoteAddr)