From b7d3d0f3ff1ef70757b365182f5a1d23cb7a20db Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 16 Jul 2019 18:51:40 +0800 Subject: [PATCH] update IPToHost func --- common/dns/dns.go | 2 +- common/dns/fakedns/server.go | 6 +++--- proxy/dnsfallback/udp.go | 4 ++-- proxy/socks/tcp.go | 4 ++-- proxy/socks/udp.go | 8 ++++---- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/common/dns/dns.go b/common/dns/dns.go index ce8994b..bcdb79d 100644 --- a/common/dns/dns.go +++ b/common/dns/dns.go @@ -11,7 +11,7 @@ type FakeDns interface { // GenerateFakeResponse(request []byte) ([]byte, error) // IPToHost returns the corresponding domain for the given IP. - IPToHost(ip net.IP) string + IPToHost(ip net.IP) (string, bool) // IsFakeIP checks if the given ip is a fake IP. // IsFakeIP(ip net.IP) bool diff --git a/common/dns/fakedns/server.go b/common/dns/fakedns/server.go index c80256f..163b221 100644 --- a/common/dns/fakedns/server.go +++ b/common/dns/fakedns/server.go @@ -57,13 +57,13 @@ func (s *Server) StartServer(addr string) error { return nil } -func (s *Server) IPToHost(ip net.IP) string { +func (s *Server) IPToHost(ip net.IP) (string, bool) { c := s.c.Get(ip.String()) if c == nil { - return "" + return "", false } fqdn := c.(*D.Msg).Question[0].Name - return strings.TrimRight(fqdn, ".") + return strings.TrimRight(fqdn, "."), true } func NewServer(fakeIPRange, hostsLine string) (*Server, error) { diff --git a/proxy/dnsfallback/udp.go b/proxy/dnsfallback/udp.go index f2f0cdb..a0df811 100644 --- a/proxy/dnsfallback/udp.go +++ b/proxy/dnsfallback/udp.go @@ -28,14 +28,14 @@ func NewUDPHandler() core.UDPConnHandler { func (h *udpHandler) Connect(conn core.UDPConn, udpAddr *net.UDPAddr) error { if udpAddr.Port != dns.CommonDnsPort { - return errors.New("Cannot handle non-DNS packet") + return errors.New("cannot handle non-DNS packet") } return nil } func (h *udpHandler) ReceiveTo(conn core.UDPConn, data []byte, addr *net.UDPAddr) error { if len(data) < dnsHeaderLength { - return errors.New("Received malformed DNS query") + return errors.New("received malformed DNS query") } // DNS Header // 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 diff --git a/proxy/socks/tcp.go b/proxy/socks/tcp.go index 797badf..88501e4 100644 --- a/proxy/socks/tcp.go +++ b/proxy/socks/tcp.go @@ -150,8 +150,8 @@ 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 = target.IP.String() if h.fakeDns != nil { - if t := h.fakeDns.IPToHost(target.IP); t != "" { - targetHost = t + if host, exist := h.fakeDns.IPToHost(target.IP); exist { + targetHost = host } } diff --git a/proxy/socks/udp.go b/proxy/socks/udp.go index c4d611e..7b3ce91 100644 --- a/proxy/socks/udp.go +++ b/proxy/socks/udp.go @@ -108,8 +108,8 @@ func (h *udpHandler) Connect(conn core.UDPConn, target *net.UDPAddr) error { return nil // skip dns } */ - if t := h.fakeDns.IPToHost(target.IP); t != "" { - targetHost = t + if host, exist := h.fakeDns.IPToHost(target.IP); exist { + targetHost = host } } dest := net.JoinHostPort(targetHost, strconv.Itoa(target.Port)) @@ -213,8 +213,8 @@ func (h *udpHandler) ReceiveTo(conn core.UDPConn, data []byte, addr *net.UDPAddr if ok1 && ok2 { var targetHost = addr.IP.String() if h.fakeDns != nil { - if t := h.fakeDns.IPToHost(addr.IP); t != "" { - targetHost = t + if host, exist := h.fakeDns.IPToHost(addr.IP); exist { + targetHost = host } }