Browse Source

remove main dns cache

pull/15/head
Jason 5 years ago
parent
commit
42dc5055af
  1. 4
      cmd/tun2socks/main.go
  2. 2
      cmd/tun2socks/main_d.go
  3. 2
      cmd/tun2socks/main_socks.go
  4. 12
      proxy/socks/tcp.go
  5. 41
      proxy/socks/udp.go

4
cmd/tun2socks/main.go

@ -19,7 +19,7 @@ import (
"github.com/xjasonlyu/tun2socks/tun" "github.com/xjasonlyu/tun2socks/tun"
) )
var version = "undefined" var Version = "unknown version"
var handlerCreator = make(map[string]func(), 0) var handlerCreator = make(map[string]func(), 0)
@ -121,7 +121,7 @@ func main() {
flag.Parse() flag.Parse()
if *args.Version { if *args.Version {
fmt.Println(version) fmt.Println("TUN2SOCKS", Version)
os.Exit(0) os.Exit(0)
} }

2
cmd/tun2socks/main_d.go

@ -31,7 +31,7 @@ func init() {
proxyPort := uint16(proxyAddr.Port) proxyPort := uint16(proxyAddr.Port)
proxyTCPHandler := socks.NewTCPHandler(proxyHost, proxyPort, fakeDns, sessionStater) 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) sendThrough, err := net.ResolveTCPAddr("tcp", *args.ExceptionSendThrough)
if err != nil { if err != nil {

2
cmd/tun2socks/main_socks.go

@ -25,6 +25,6 @@ func init() {
proxyPort := uint16(proxyAddr.Port) proxyPort := uint16(proxyAddr.Port)
core.RegisterTCPConnHandler(socks.NewTCPHandler(proxyHost, proxyPort, fakeDns, sessionStater)) 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))
}) })
} }

12
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. // Replace with a domain name if target address IP is a fake IP.
var targetHost string var targetHost = target.IP.String()
if h.fakeDns != nil && h.fakeDns.IsFakeIP(target.IP) { if h.fakeDns != nil {
targetHost = h.fakeDns.IPToHost(target.IP) if t := h.fakeDns.IPToHost(target.IP); t != "" {
} else { targetHost = t
targetHost = target.IP.String() }
} }
dest := net.JoinHostPort(targetHost, strconv.Itoa(target.Port))
dest := net.JoinHostPort(targetHost, strconv.Itoa(target.Port))
c, err := dialer.Dial(target.Network(), dest) c, err := dialer.Dial(target.Network(), dest)
if err != nil { if err != nil {
return err return err

41
proxy/socks/udp.go

@ -26,19 +26,17 @@ type udpHandler struct {
remoteAddrs map[core.UDPConn]*net.UDPAddr // UDP relay server addresses remoteAddrs map[core.UDPConn]*net.UDPAddr // UDP relay server addresses
timeout time.Duration timeout time.Duration
dnsCache dns.DnsCache
fakeDns dns.FakeDns fakeDns dns.FakeDns
sessionStater stats.SessionStater 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{ return &udpHandler{
proxyHost: proxyHost, proxyHost: proxyHost,
proxyPort: proxyPort, proxyPort: proxyPort,
udpConns: make(map[core.UDPConn]net.PacketConn, 8), udpConns: make(map[core.UDPConn]net.PacketConn, 8),
tcpConns: make(map[core.UDPConn]net.Conn, 8), tcpConns: make(map[core.UDPConn]net.Conn, 8),
remoteAddrs: make(map[core.UDPConn]*net.UDPAddr, 8), remoteAddrs: make(map[core.UDPConn]*net.UDPAddr, 8),
dnsCache: dnsCache,
fakeDns: fakeDns, fakeDns: fakeDns,
timeout: timeout, timeout: timeout,
sessionStater: sessionStater, sessionStater: sessionStater,
@ -94,17 +92,6 @@ func (h *udpHandler) fetchUDPInput(conn core.UDPConn, input net.PacketConn) {
log.Warnf("write local failed: %v", err) log.Warnf("write local failed: %v", err)
return 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. // 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 h.fakeDns != nil {
if target.Port == dns.CommonDnsPort { /*
return nil // skip dns if target.Port == dns.CommonDnsPort {
} return nil // skip dns
if h.fakeDns.IsFakeIP(target.IP) { }
targetHost = h.fakeDns.IPToHost(target.IP) */
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))
@ -222,14 +211,14 @@ func (h *udpHandler) ReceiveTo(conn core.UDPConn, data []byte, addr *net.UDPAddr
// use system DNS instead of force override // use system DNS instead of force override
if ok1 && ok2 { if ok1 && ok2 {
var targetHost string var targetHost = addr.IP.String()
if h.fakeDns != nil && h.fakeDns.IsFakeIP(addr.IP) { if h.fakeDns != nil {
targetHost = h.fakeDns.IPToHost(addr.IP) if t := h.fakeDns.IPToHost(addr.IP); t != "" {
} else { targetHost = t
targetHost = addr.IP.String() }
} }
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([]byte{0, 0, 0}, ParseAddr(dest)...)
buf = append(buf, data[:]...) buf = append(buf, data[:]...)
n, err := pc.WriteTo(buf, remoteAddr) n, err := pc.WriteTo(buf, remoteAddr)

Loading…
Cancel
Save