Browse Source

update IPToHost func

pull/15/head
Jason 5 years ago
parent
commit
b7d3d0f3ff
  1. 2
      common/dns/dns.go
  2. 6
      common/dns/fakedns/server.go
  3. 4
      proxy/dnsfallback/udp.go
  4. 4
      proxy/socks/tcp.go
  5. 8
      proxy/socks/udp.go

2
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

6
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) {

4
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

4
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
}
}

8
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
}
}

Loading…
Cancel
Save