Skip to content

Commit 158e47b

Browse files
committed
using sing-tun instead of tun2socket
1 parent 4df3934 commit 158e47b

File tree

7 files changed

+23
-227
lines changed

7 files changed

+23
-227
lines changed

core/src/main/golang/go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ module cfa
33
go 1.20
44

55
require (
6-
github.com/Kr328/tun2socket v0.0.0-20220414050025-d07c78d06d34
76
github.com/dlclark/regexp2 v1.11.4
87
github.com/metacubex/mihomo v1.7.0
9-
github.com/miekg/dns v1.1.62
108
github.com/oschwald/maxminddb-golang v1.12.0
119
golang.org/x/sync v0.8.0
1210
gopkg.in/yaml.v3 v3.0.1
@@ -68,6 +66,7 @@ require (
6866
github.com/metacubex/sing-wireguard v0.0.0-20240826061955-1e4e67afe5cd // indirect
6967
github.com/metacubex/tfo-go v0.0.0-20240830120620-c5e019b67785 // indirect
7068
github.com/metacubex/utls v1.6.6 // indirect
69+
github.com/miekg/dns v1.1.62 // indirect
7170
github.com/mroth/weightedrand/v2 v2.1.0 // indirect
7271
github.com/oasisprotocol/deoxysii v0.0.0-20220228165953-2091330c22b7 // indirect
7372
github.com/onsi/ginkgo/v2 v2.9.5 // indirect

core/src/main/golang/go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
github.com/3andne/restls-client-go v0.1.6 h1:tRx/YilqW7iHpgmEL4E1D8dAsuB0tFF3uvncS+B6I08=
22
github.com/3andne/restls-client-go v0.1.6/go.mod h1:iEdTZNt9kzPIxjIGSMScUFSBrUH6bFRNg0BWlP4orEY=
3-
github.com/Kr328/tun2socket v0.0.0-20220414050025-d07c78d06d34 h1:USCTqih5d1bUXUxWNS9ZD5Tx/lb0jXHEtRIIx/F9dMc=
4-
github.com/Kr328/tun2socket v0.0.0-20220414050025-d07c78d06d34/go.mod h1:YR9wK13TgI5ww8iKWm91MHiSoHC7Oz0U4beCCmtXqLw=
53
github.com/RyuaNerin/elliptic2 v1.0.0/go.mod h1:wWB8fWrJI/6EPJkyV/r1Rj0hxUgrusmqSj8JN6yNf/A=
64
github.com/RyuaNerin/go-krypto v1.2.4 h1:mXuNdK6M317aPV0llW6Xpjbo4moOlPF7Yxz4tb4b4Go=
75
github.com/RyuaNerin/go-krypto v1.2.4/go.mod h1:QqCYkoutU3yInyD9INt2PGolVRsc3W4oraQadVGXJ/8=

core/src/main/golang/native/config/process.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/dlclark/regexp2"
1010

1111
"cfa/native/common"
12+
1213
C "github.com/metacubex/mihomo/constant"
1314
"github.com/metacubex/mihomo/log"
1415

@@ -21,6 +22,7 @@ var processors = []processor{
2122
patchGeneral,
2223
patchProfile,
2324
patchDns,
25+
patchTun,
2426
patchProviders,
2527
validConfig,
2628
}
@@ -74,6 +76,12 @@ func patchDns(cfg *config.RawConfig, _ string) error {
7476
return nil
7577
}
7678

79+
func patchTun(cfg *config.RawConfig, _ string) error {
80+
cfg.Tun.Enable = false
81+
82+
return nil
83+
}
84+
7785
func patchProviders(cfg *config.RawConfig, profileDir string) error {
7886
forEachProviders(cfg, func(index int, total int, key string, provider map[string]any) {
7987
if path, ok := provider["path"].(string); ok {

core/src/main/golang/native/tun/dns.go

Lines changed: 0 additions & 33 deletions
This file was deleted.

core/src/main/golang/native/tun/metadata.go

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 14 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -1,161 +1,34 @@
11
package tun
22

33
import (
4-
"encoding/binary"
54
"io"
6-
"net"
7-
"os"
8-
"time"
5+
"net/netip"
96

10-
"github.com/Kr328/tun2socket"
11-
12-
"github.com/metacubex/mihomo/adapter/inbound"
13-
"github.com/metacubex/mihomo/common/pool"
147
C "github.com/metacubex/mihomo/constant"
8+
LC "github.com/metacubex/mihomo/listener/config"
9+
"github.com/metacubex/mihomo/listener/sing_tun"
1510
"github.com/metacubex/mihomo/log"
16-
"github.com/metacubex/mihomo/transport/socks5"
1711
"github.com/metacubex/mihomo/tunnel"
1812
)
1913

20-
var _, ipv4LoopBack, _ = net.ParseCIDR("127.0.0.0/8")
21-
2214
func Start(fd int, gateway, portal, dns string) (io.Closer, error) {
2315
log.Debugln("TUN: fd = %d, gateway = %s, portal = %s, dns = %s", fd, gateway, portal, dns)
2416

25-
device := os.NewFile(uintptr(fd), "/dev/tun")
26-
27-
ip, network, err := net.ParseCIDR(gateway)
28-
if err != nil {
29-
panic(err.Error())
30-
} else {
31-
network.IP = ip
17+
options := LC.Tun{
18+
Enable: true,
19+
Device: sing_tun.InterfaceName,
20+
Stack: C.TunSystem,
21+
DNSHijack: []string{dns + ":53"}, // "172.19.0.2" or "0.0.0.0"
22+
Inet4Address: []netip.Prefix{netip.MustParsePrefix(gateway)}, // "172.19.0.1/30"
23+
MTU: 9000, // private const val TUN_MTU = 9000 in TunService.kt
24+
FileDescriptor: fd,
3225
}
3326

34-
stack, err := tun2socket.StartTun2Socket(device, network, net.ParseIP(portal))
27+
listener, err := sing_tun.New(options, tunnel.Tunnel)
3528
if err != nil {
36-
_ = device.Close()
37-
29+
log.Errorln("TUN:", err)
3830
return nil, err
3931
}
4032

41-
dnsAddr := net.ParseIP(dns)
42-
43-
tcp := func() {
44-
defer stack.TCP().Close()
45-
defer log.Debugln("TCP: closed")
46-
47-
for stack.TCP().SetDeadline(time.Time{}) == nil {
48-
conn, err := stack.TCP().Accept()
49-
if err != nil {
50-
log.Debugln("Accept connection: %v", err)
51-
52-
continue
53-
}
54-
55-
lAddr := conn.LocalAddr().(*net.TCPAddr)
56-
rAddr := conn.RemoteAddr().(*net.TCPAddr)
57-
58-
if ipv4LoopBack.Contains(rAddr.IP) {
59-
conn.Close()
60-
61-
continue
62-
}
63-
64-
if shouldHijackDns(dnsAddr, rAddr.IP, rAddr.Port) {
65-
go func() {
66-
defer conn.Close()
67-
68-
buf := pool.Get(pool.UDPBufferSize)
69-
defer pool.Put(buf)
70-
71-
for {
72-
conn.SetReadDeadline(time.Now().Add(C.DefaultTCPTimeout))
73-
74-
length := uint16(0)
75-
if err := binary.Read(conn, binary.BigEndian, &length); err != nil {
76-
return
77-
}
78-
79-
if int(length) > len(buf) {
80-
return
81-
}
82-
83-
n, err := conn.Read(buf[:length])
84-
if err != nil {
85-
return
86-
}
87-
88-
msg, err := relayDns(buf[:n])
89-
if err != nil {
90-
return
91-
}
92-
93-
_, _ = conn.Write(msg)
94-
}
95-
}()
96-
97-
continue
98-
}
99-
100-
go tunnel.Tunnel.HandleTCPConn(conn, createMetadata(lAddr, rAddr))
101-
}
102-
}
103-
104-
udp := func() {
105-
defer stack.UDP().Close()
106-
defer log.Debugln("UDP: closed")
107-
108-
for {
109-
buf := pool.Get(pool.UDPBufferSize)
110-
111-
n, lRAddr, rRAddr, err := stack.UDP().ReadFrom(buf)
112-
if err != nil {
113-
return
114-
}
115-
116-
raw := buf[:n]
117-
lAddr := lRAddr.(*net.UDPAddr)
118-
rAddr := rRAddr.(*net.UDPAddr)
119-
120-
if ipv4LoopBack.Contains(rAddr.IP) {
121-
pool.Put(buf)
122-
123-
continue
124-
}
125-
126-
if shouldHijackDns(dnsAddr, rAddr.IP, rAddr.Port) {
127-
go func() {
128-
defer pool.Put(buf)
129-
130-
msg, err := relayDns(raw)
131-
if err != nil {
132-
return
133-
}
134-
135-
_, _ = stack.UDP().WriteTo(msg, rAddr, lAddr)
136-
}()
137-
138-
continue
139-
}
140-
141-
pkt := &packet{
142-
local: lAddr,
143-
data: raw,
144-
writeBack: func(b []byte, addr net.Addr) (int, error) {
145-
return stack.UDP().WriteTo(b, addr, lAddr)
146-
},
147-
drop: func() {
148-
pool.Put(buf)
149-
},
150-
}
151-
152-
tunnel.Tunnel.HandleUDPPacket(inbound.NewPacket(socks5.ParseAddrToSocksAddr(rAddr), pkt, C.SOCKS5))
153-
}
154-
}
155-
156-
go tcp()
157-
go udp()
158-
go udp()
159-
160-
return stack, nil
33+
return listener, nil
16134
}

core/src/main/golang/native/tun/udp.go

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)