fix dns redirect
This commit is contained in:
7 files changed
+46
-193
No files matched your search
Submodule core/src/main/golang/clash updated: 435f60f50c...2de374c561.
@@ -5,7 +5,6 @@ go 1.13
|
||||
require (
|
||||
github.com/Dreamacro/clash v0.0.0 // local
|
||||
github.com/google/go-cmp v0.3.1 // indirect
|
||||
github.com/google/netstack v0.0.0-20191031000057-4787376a6744
|
||||
golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe
|
||||
)
|
||||
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
github.com/Dreamacro/clash v0.16.0 h1:ZvV9apOrDHC0s5ff4YCHTRANd/XyOuLWvpyLWOkwS6U=
|
||||
github.com/Dreamacro/clash v0.16.0/go.mod h1:4ZBtABBmIGqISniBtu9fpYORrE5mKqIP8SMCTrNNVNY=
|
||||
github.com/Dreamacro/go-shadowsocks2 v0.1.3 h1:1ffY/q4e3o+MnztYgIq1iZiX1BWoWQ6D3AIO1kkb8bc=
|
||||
github.com/Dreamacro/go-shadowsocks2 v0.1.3/go.mod h1:0x17IhQ+mlY6q/ffKRpzaE7u4aHMxxnitTRSrV5G6TU=
|
||||
github.com/Dreamacro/go-shadowsocks2 v0.1.5-0.20191012162057-46254afc8b68 h1:UBDLpj1IGVkUcUBuZWE6DmZApPTZcnmcV6AfyDN/yhg=
|
||||
github.com/Dreamacro/go-shadowsocks2 v0.1.5-0.20191012162057-46254afc8b68/go.mod h1:Y8obOtHDOqxMGHjPglfCiXZBKExOA9VL6I6sJagOwYM=
|
||||
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da h1:KjTM2ks9d14ZYCvmHS9iAKVt9AyzRSqNU1qabPih5BY=
|
||||
@@ -17,16 +21,30 @@ github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg=
|
||||
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
github.com/google/netstack v0.0.0-20191031000057-4787376a6744 h1:wKeh74w+ydKcE1Eo44WDzIOcPHWmxxmtAzkAL0Mlspc=
|
||||
github.com/google/netstack v0.0.0-20191031000057-4787376a6744/go.mod h1:r/rILWg3r1Qy9G1IFMhsqWLq2GjwuYoTuPgG7ckMAjk=
|
||||
github.com/google/netstack v0.0.0-20191116005144-95bf25ab4723 h1:z92yYpQg57ql9n96PBLFhT+N5X44uQ2f3+CNp9Osmu8=
|
||||
github.com/google/netstack v0.0.0-20191116005144-95bf25ab4723/go.mod h1:r/rILWg3r1Qy9G1IFMhsqWLq2GjwuYoTuPgG7ckMAjk=
|
||||
github.com/google/netstack v0.0.0-20191123085552-55fcc16cd0eb h1:/YcrD0GSdU5gtckXHVjSEd0Y6VgboNW7VYyImZS3y6g=
|
||||
github.com/google/netstack v0.0.0-20191123085552-55fcc16cd0eb/go.mod h1:r/rILWg3r1Qy9G1IFMhsqWLq2GjwuYoTuPgG7ckMAjk=
|
||||
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
|
||||
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
||||
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
|
||||
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||
github.com/miekg/dns v1.1.9 h1:OIdC9wT96RzuZMf2PfKRhFgsStHUUBZLM/lo1LqiM9E=
|
||||
github.com/miekg/dns v1.1.9/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
|
||||
github.com/miekg/dns v1.1.22 h1:Jm64b3bO9kP43ddLjL2EY3Io6bmy1qGb9Xxz6TqS6rc=
|
||||
github.com/miekg/dns v1.1.22/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso=
|
||||
github.com/oschwald/geoip2-golang v1.2.1 h1:3iz+jmeJc6fuCyWeKgtXSXu7+zvkxJbHFXkMT5FVebU=
|
||||
github.com/oschwald/geoip2-golang v1.2.1/go.mod h1:0LTTzix/Ao1uMvOhAV4iLU0Lz7eCrP94qZWBTDKf0iE=
|
||||
github.com/oschwald/geoip2-golang v1.3.0 h1:D+Hsdos1NARPbzZ2aInUHZL+dApIzo8E0ErJVsWcku8=
|
||||
github.com/oschwald/geoip2-golang v1.3.0/go.mod h1:0LTTzix/Ao1uMvOhAV4iLU0Lz7eCrP94qZWBTDKf0iE=
|
||||
github.com/oschwald/maxminddb-golang v1.3.0 h1:oTh8IBSj10S5JNlUDg5WjJ1QdBMdeaZIkPEVfESSWgE=
|
||||
github.com/oschwald/maxminddb-golang v1.3.0/go.mod h1:3jhIUymTJ5VREKyIhWm66LJiQt04F0UCDdodShpjWsY=
|
||||
github.com/oschwald/maxminddb-golang v1.5.0 h1:rmyoIV6z2/s9TCJedUuDiKht2RN12LWJ1L7iRGtWY64=
|
||||
github.com/oschwald/maxminddb-golang v1.5.0/go.mod h1:3jhIUymTJ5VREKyIhWm66LJiQt04F0UCDdodShpjWsY=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sirupsen/logrus v1.4.1 h1:GL2rEmy6nsikmW0r8opw9JIRScdMF5hA8cOYLH7In1k=
|
||||
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
|
||||
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
|
||||
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
@@ -34,17 +52,22 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734 h1:p/H982KKEjUnLJkM3tt/LemDnOc1GiZL5FCVlORJ5zo=
|
||||
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20190923035154-9ee001bba392/go.mod h1:/lpIB1dKB+9EgE3H3cr1v9wB50oz8l4C4h62xy7jSTY=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20191011234655-491137f69257 h1:ry8e2D+cwaV6hk7lb3aRTjjZo24shrbK0e11QEOkTIg=
|
||||
golang.org/x/net v0.0.0-20191011234655-491137f69257/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY=
|
||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
|
||||
@@ -2,7 +2,6 @@ package profile
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
adapters "github.com/Dreamacro/clash/adapters/outbound"
|
||||
"github.com/Dreamacro/clash/component/auth"
|
||||
@@ -63,7 +62,7 @@ func LoadDefault() {
|
||||
defaultC.Proxies["REJECT"] = reject
|
||||
defaultC.Proxies["GLOBAL"] = adapters.NewProxy(global)
|
||||
|
||||
tun.SetDNSRedirect(nil)
|
||||
tun.ResetDnsRedirect()
|
||||
|
||||
executor.ApplyConfig(defaultC, true)
|
||||
}
|
||||
@@ -77,20 +76,7 @@ func LoadFromFile(path string) error {
|
||||
|
||||
executor.ApplyConfig(cfg, true)
|
||||
|
||||
if cfg.DNS != nil && cfg.DNS.Enable && dns.DefaultResolver != nil && dns.ReCreateServer(cfg.DNS.Listen, dns.DefaultResolver) == nil {
|
||||
addr := cfg.DNS.Listen
|
||||
addr = strings.ReplaceAll(addr, "0.0.0.0", "127.0.0.1")
|
||||
addr = strings.ReplaceAll(addr, "::", "::1")
|
||||
|
||||
listen, err := net.ResolveUDPAddr("udp", addr)
|
||||
if err != nil {
|
||||
log.Errorln("Unable to parse dns address" + err.Error())
|
||||
}
|
||||
|
||||
tun.SetDNSRedirect(listen)
|
||||
} else {
|
||||
tun.SetDNSRedirect(nil)
|
||||
}
|
||||
tun.ResetDnsRedirect()
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,126 +0,0 @@
|
||||
package tun
|
||||
|
||||
import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/Dreamacro/clash/common/pool"
|
||||
"github.com/google/netstack/tcpip/adapters/gonet"
|
||||
"github.com/google/netstack/tcpip/buffer"
|
||||
"github.com/google/netstack/tcpip/stack"
|
||||
"github.com/google/netstack/tcpip/transport/udp"
|
||||
"github.com/google/netstack/waiter"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultTimeout = 5
|
||||
defaultDNS = "8.8.8.8"
|
||||
)
|
||||
|
||||
type DnsRedirectEndpoint struct {
|
||||
stack *stack.Stack
|
||||
udpForwarder *udp.Forwarder
|
||||
targetAddr *net.UDPAddr
|
||||
}
|
||||
|
||||
func NewDnsRedirect(s *stack.Stack) *DnsRedirectEndpoint {
|
||||
result := &DnsRedirectEndpoint{
|
||||
stack: s,
|
||||
udpForwarder: nil,
|
||||
targetAddr: nil,
|
||||
}
|
||||
|
||||
result.SetDefaultTargetAddress()
|
||||
|
||||
result.udpForwarder = udp.NewForwarder(s, func(request *udp.ForwarderRequest) {
|
||||
var wq waiter.Queue
|
||||
ep, err := request.CreateEndpoint(&wq)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
conn := gonet.NewConn(&wq, ep)
|
||||
|
||||
targetConn, udpErr := net.DialUDP("udp", &net.UDPAddr{IP: net.IPv4zero, Port: 0}, result.targetAddr)
|
||||
if udpErr != nil {
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
|
||||
// send
|
||||
go func() {
|
||||
buffer := pool.BufPool.Get().([]byte)
|
||||
defer pool.BufPool.Put(buffer[:cap(buffer)])
|
||||
|
||||
defer targetConn.Close()
|
||||
defer conn.Close()
|
||||
|
||||
for {
|
||||
conn.SetDeadline(time.Now().Add(defaultTimeout * time.Second))
|
||||
|
||||
n, err := conn.Read(buffer[:])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = targetConn.Write(buffer[:n])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// receive
|
||||
go func() {
|
||||
buffer := pool.BufPool.Get().([]byte)
|
||||
defer pool.BufPool.Put(buffer[:cap(buffer)])
|
||||
|
||||
defer targetConn.Close()
|
||||
defer conn.Close()
|
||||
|
||||
for {
|
||||
conn.SetDeadline(time.Now().Add(defaultTimeout * time.Second))
|
||||
|
||||
n, err := targetConn.Read(buffer[:])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = conn.Write(buffer[:n])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (d *DnsRedirectEndpoint) SetDefaultTargetAddress() {
|
||||
d.targetAddr = &net.UDPAddr{IP: net.ParseIP(defaultDNS), Port: 53}
|
||||
}
|
||||
|
||||
func (d *DnsRedirectEndpoint) SetTargetAddress(addr *net.UDPAddr) {
|
||||
d.targetAddr = addr
|
||||
}
|
||||
|
||||
func (d *DnsRedirectEndpoint) UniqueID() uint64 {
|
||||
return 999
|
||||
}
|
||||
|
||||
func (d *DnsRedirectEndpoint) HandlePacket(r *stack.Route, id stack.TransportEndpointID, pkt buffer.VectorisedView) {
|
||||
d.udpForwarder.HandlePacket(r, id, nil, pkt)
|
||||
}
|
||||
|
||||
func (d *DnsRedirectEndpoint) HandleControlPacket(id stack.TransportEndpointID, typ stack.ControlType, extra uint32, pkt buffer.VectorisedView) {
|
||||
// Unsupported
|
||||
}
|
||||
|
||||
func (d *DnsRedirectEndpoint) Close() {
|
||||
// Unsupported
|
||||
}
|
||||
|
||||
func (d *DnsRedirectEndpoint) Wait() {
|
||||
// Unsupported
|
||||
}
|
||||
@@ -1,67 +1,40 @@
|
||||
package tun
|
||||
|
||||
import (
|
||||
"net"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/Dreamacro/clash/log"
|
||||
"github.com/Dreamacro/clash/proxy/tun"
|
||||
|
||||
"github.com/google/netstack/tcpip"
|
||||
"github.com/google/netstack/tcpip/network/ipv4"
|
||||
"github.com/google/netstack/tcpip/network/ipv6"
|
||||
"github.com/google/netstack/tcpip/stack"
|
||||
"github.com/google/netstack/tcpip/transport/udp"
|
||||
"github.com/Dreamacro/clash/dns"
|
||||
T "github.com/Dreamacro/clash/proxy/tun"
|
||||
)
|
||||
|
||||
type handler struct {
|
||||
tunAdapter *tun.TunAdapter
|
||||
dnsRedirect *DnsRedirectEndpoint
|
||||
tunAdapter *T.TunAdapter
|
||||
}
|
||||
|
||||
const dnsRedirectAddr = "119.29.29.29"
|
||||
|
||||
var (
|
||||
instance *handler
|
||||
dnsRedirectAddr *net.UDPAddr
|
||||
instance *handler
|
||||
)
|
||||
|
||||
// StartTunProxy - start
|
||||
func StartTunProxy(fd, mtu int) error {
|
||||
StopTunProxy()
|
||||
|
||||
adapter, err := tun.NewTunProxy("fd://" + strconv.Itoa(fd) + "?mtu=" + strconv.Itoa(mtu))
|
||||
adapter, err := T.NewTunProxy("fd://" + strconv.Itoa(fd) + "?mtu=" + strconv.Itoa(mtu))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s := adapter.Stack()
|
||||
|
||||
endpoint := NewDnsRedirect(s)
|
||||
|
||||
id := &stack.TransportEndpointID{
|
||||
LocalAddress: "",
|
||||
LocalPort: 53,
|
||||
RemotePort: 0,
|
||||
RemoteAddress: "",
|
||||
}
|
||||
|
||||
if err := s.RegisterTransportEndpoint(1,
|
||||
[]tcpip.NetworkProtocolNumber{
|
||||
ipv4.ProtocolNumber,
|
||||
ipv6.ProtocolNumber,
|
||||
},
|
||||
udp.ProtocolNumber,
|
||||
*id,
|
||||
endpoint,
|
||||
true,
|
||||
1); err != nil {
|
||||
log.Errorln("Unable to set dns redirect" + err.String())
|
||||
}
|
||||
|
||||
instance = &handler{
|
||||
tunAdapter: &adapter,
|
||||
dnsRedirect: endpoint,
|
||||
tunAdapter: &adapter,
|
||||
}
|
||||
|
||||
ResetDnsRedirect()
|
||||
|
||||
fmt.Println("Android tun started")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -73,15 +46,15 @@ func StopTunProxy() {
|
||||
}
|
||||
}
|
||||
|
||||
// SetDNSRedirect - set dns
|
||||
func SetDNSRedirect(addr *net.UDPAddr) {
|
||||
func ResetDnsRedirect() {
|
||||
if instance == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if addr != nil {
|
||||
instance.dnsRedirect.SetTargetAddress(addr)
|
||||
resolver := dns.DefaultResolver
|
||||
if resolver != nil {
|
||||
(*instance.tunAdapter).CreateDNSServer(resolver, dnsRedirectAddr+":53")
|
||||
} else {
|
||||
instance.dnsRedirect.SetDefaultTargetAddress()
|
||||
(*instance.tunAdapter).DestroyDNSSerrvice()
|
||||
}
|
||||
}
|
||||
@@ -14,10 +14,9 @@ class TunService : VpnService(), IClashEventObserver {
|
||||
companion object {
|
||||
// from https://github.com/shadowsocks/shadowsocks-android/blob/master/core/src/main/java/com/github/shadowsocks/bg/VpnService.kt
|
||||
private const val VPN_MTU = 1500
|
||||
private const val PRIVATE_VLAN_REDIRECT_DNS = "119.29.29.29" // sync with tun/tun.go/dnsRedirectAddr
|
||||
private const val PRIVATE_VLAN4_CLIENT = "172.19.0.1"
|
||||
private const val PRIVATE_VLAN4_DNS = "172.19.0.2"
|
||||
private const val PRIVATE_VLAN6_CLIENT = "fdfe:dcba:9876::1"
|
||||
private const val PRIVATE_VLAN6_DNS = "fdfe:dcba:9876::2"
|
||||
}
|
||||
|
||||
private var start = true
|
||||
@@ -58,8 +57,7 @@ class TunService : VpnService(), IClashEventObserver {
|
||||
fileDescriptor = Builder()
|
||||
.addAddress(PRIVATE_VLAN4_CLIENT, 30)
|
||||
.addAddress(PRIVATE_VLAN6_CLIENT, 126)
|
||||
.addDnsServer(PRIVATE_VLAN4_DNS)
|
||||
.addDnsServer(PRIVATE_VLAN6_DNS)
|
||||
.addDnsServer(PRIVATE_VLAN_REDIRECT_DNS)
|
||||
.addBypassApplications()
|
||||
.addBypassPrivateRoute()
|
||||
.setMtu(VPN_MTU)
|
||||
|
||||
Reference in new issue
Block a user