recycle dns udp packet

This commit is contained in:
Kr328 committed 2020-03-31 23:21:02 +08:00
1 parent 4b6b5b5d95
commit 27cd92b4b1
8 files changed
+58 -33

No files matched your search

+5 -12
View File
@@ -1,13 +1,12 @@
package bridge
import (
"sync"
"github.com/Dreamacro/clash/component/mmdb"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/log"
"github.com/Dreamacro/clash/tunnel"
"github.com/kr328/cfa/config"
"sync"
)
var (
@@ -19,15 +18,13 @@ type LogCallback interface {
OnLogEvent(level, payload string)
}
func LoadMMDB(data []byte) {
dataClone := make([]byte, len(data))
copy(dataClone, data)
func InitCore(geoipDatabase[] byte, homeDir string, version string) {
dataClone := make([]byte, len(geoipDatabase))
copy(dataClone, geoipDatabase)
mmdb.LoadFromBytes(dataClone)
}
func SetHome(homeDir string) {
C.SetHomeDir(homeDir)
config.ApplicationVersion = version
}
func Reset() {
@@ -35,10 +32,6 @@ func Reset() {
tunnel.DefaultManager.ResetStatistic()
}
func SetApplicationVersion(version string) {
config.ApplicationVersion = version
}
func SetLogCallback(callback LogCallback) {
logSubscribe.Do(func() {
go func() {
+2 -2
View File
@@ -30,13 +30,13 @@ func LoadProfileFile(path, baseDir string, callback DoneCallback) {
func DownloadProfileAndCheck(url, output, baseDir string, callback DoneCallback) {
go func() {
call(config.DownloadRemote(url, output, baseDir), callback)
call(config.PullRemote(url, output, baseDir), callback)
}()
}
func ReadProfileAndCheck(fd int, output, baseDir string, callback DoneCallback) {
go func() {
call(config.DownloadLocal(fd, output, baseDir), callback)
call(config.PullLocal(fd, output, baseDir), callback)
}()
}
+2 -2
View File
@@ -79,7 +79,7 @@ func fetchLocal(fd int) ([]byte, error) {
return ioutil.ReadAll(file)
}
func DownloadRemote(url, output, baseDir string) error {
func PullRemote(url, output, baseDir string) error {
data, err := fetchRemote(url)
if err != nil {
return err
@@ -88,7 +88,7 @@ func DownloadRemote(url, output, baseDir string) error {
return save(data, output, baseDir)
}
func DownloadLocal(fd int, output, baseDir string) error {
func PullLocal(fd int, output, baseDir string) error {
data, err := fetchLocal(fd)
if err != nil {
return err
-7
View File
@@ -1,7 +1,6 @@
package config
import (
"fmt"
"github.com/Dreamacro/clash/component/fakeip"
"github.com/Dreamacro/clash/config"
"net/url"
@@ -21,12 +20,6 @@ func patchRawConfig(rawConfig *config.RawConfig) {
rawConfig.ExternalUI = ""
rawConfig.ExternalController = ""
if len(rawConfig.Rule) != 0 {
rawConfig.Rule = append([]string{fmt.Sprintf("IP-CIDR,%s,REJECT,no-resolve", tunAddress)}, rawConfig.Rule...)
} else {
rawConfig.RuleOld = append([]string{fmt.Sprintf("IP-CIDR,%s,REJECT,no-resolve", tunAddress)}, rawConfig.RuleOld...)
}
if d := DnsPatch; d != nil {
rawConfig.DNS = *d
} else if d := OptionalDnsPatch; d != nil {
+3 -1
View File
@@ -98,7 +98,7 @@ func hijackTCPDNS(conn net.Conn, endpoint *binding.Endpoint) bool {
return true
}
func hijackDNS(payload []byte, endpoint *binding.Endpoint, sender redirect.UDPSender) bool {
func hijackDNS(payload []byte, endpoint *binding.Endpoint, sender redirect.UDPSender, recycle func([]byte)) bool {
if endpoint.Target.Port != 53 {
return false
}
@@ -118,6 +118,8 @@ func hijackDNS(payload []byte, endpoint *binding.Endpoint, sender redirect.UDPSe
endpoint: endpoint,
sender: sender,
}, msg)
recycle(payload)
}()
return true
+43 -5
View File
@@ -1,6 +1,7 @@
package tun
import (
"errors"
adapters "github.com/Dreamacro/clash/adapters/inbound"
"github.com/Dreamacro/clash/component/socks5"
C "github.com/Dreamacro/clash/constant"
@@ -16,6 +17,10 @@ import (
"github.com/Dreamacro/clash/log"
)
const (
maxUdpPacketSize = 65535
)
var adapter *tun2socket.Tun2Socket
var mutex sync.Mutex
@@ -26,14 +31,42 @@ func StartTunDevice(fd, mtu int, gateway, mirror, dnsAddress string) error {
if adapter != nil {
adapter.Close()
adapter = nil
log.Infoln("Android tun stopped")
}
gatewayIP, gatewayNet, err := net.ParseCIDR(gateway)
mirrorIP := net.ParseIP(mirror)
if err != nil || mirrorIP == nil || !gatewayNet.Contains(mirrorIP) {
return errors.New("invalid gateway or mirror")
}
udpPool := sync.Pool{New: func() interface{} {
return make([]byte, maxUdpPacketSize)
}}
udpRecycle := func(bytes []byte) {
if cap(bytes) == maxUdpPacketSize {
udpPool.Put(bytes[:maxUdpPacketSize])
}
}
file := os.NewFile(uintptr(fd), "/dev/tun")
_ = syscall.SetNonblock(fd, true)
adapter = tun2socket.NewTun2Socket(file, mtu, net.ParseIP(gateway).To4(), net.ParseIP(mirror).To4())
adapter = tun2socket.NewTun2Socket(file, mtu, gatewayIP, mirrorIP.To4())
adapter.SetAllocator(func(length int) []byte {
if length <= maxUdpPacketSize {
return udpPool.Get().([]byte)[:length]
}
return make([]byte, length)
})
adapter.SetTCPHandler(func(conn net.Conn, endpoint *binding.Endpoint) {
if gatewayNet.Contains(endpoint.Target.IP) {
return
}
if hijackTCPDNS(conn, endpoint) {
return
}
@@ -47,7 +80,12 @@ func StartTunDevice(fd, mtu int, gateway, mirror, dnsAddress string) error {
tunnel.Add(adapters.NewSocket(addr, conn, C.SOCKS, C.TCP))
})
adapter.SetUDPHandler(func(payload []byte, endpoint *binding.Endpoint, sender redirect.UDPSender) {
if hijackDNS(payload, endpoint, sender) {
if gatewayNet.Contains(endpoint.Target.IP) {
udpRecycle(payload)
return
}
if hijackDNS(payload, endpoint, sender, udpRecycle) {
return
}
@@ -82,7 +120,7 @@ func StopTunDevice() {
if adapter != nil {
adapter.Close()
adapter = nil
}
log.Infoln("Android tun stopped")
}
log.Infoln("Android tun stopped")
}
}
@@ -29,9 +29,7 @@ object Clash {
val bytes = context.assets.open("Country.mmdb")
.use(InputStream::readBytes)
Bridge.loadMMDB(bytes)
Bridge.setHome(context.cacheDir.absolutePath)
Bridge.setApplicationVersion(BuildConfig.VERSION_NAME)
Bridge.initCore(bytes, context.cacheDir.absolutePath, BuildConfig.VERSION_NAME)
Bridge.reset()
}
@@ -9,6 +9,7 @@ import com.github.kr328.clash.service.settings.ServiceSettings
import com.github.kr328.clash.service.util.asSocketAddressText
import com.github.kr328.clash.service.util.broadcastNetworkChanged
import kotlinx.coroutines.*
import kotlin.time.Duration
class TunService : VpnService(), CoroutineScope by MainScope() {
companion object {
@@ -48,7 +49,7 @@ class TunService : VpnService(), CoroutineScope by MainScope() {
Clash.setDnsOverrideEnabled(settings.get(ServiceSettings.OVERRIDE_DNS))
Clash.startTunDevice(fd.detachFd(), VPN_MTU,
PRIVATE_VLAN4_CLIENT, PRIVATE_VLAN4_MIRROR,
"$PRIVATE_VLAN4_CLIENT/$PRIVATE_VLAN4_SUBNET", PRIVATE_VLAN4_MIRROR,
dnsAddress,
this::protect, this::stopSelf)