update IPToHost func

This commit is contained in:
Jason
2019-07-16 18:51:40 +08:00
parent 5115e74d66
commit b7d3d0f3ff
5 changed files with 12 additions and 12 deletions
+1 -1
View File
@@ -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
+3 -3
View File
@@ -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) {
+2 -2
View File
@@ -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
+2 -2
View File
@@ -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
}
}
+4 -4
View File
@@ -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
}
}