mirror of
https://github.com/xjasonlyu/tun2socks.git
synced 2024-12-30 02:37:01 +00:00
update IPToHost func
This commit is contained in:
+1
-1
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
@@ -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
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user