|
1 | 1 | package tun |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "encoding/binary" |
5 | 4 | "io" |
6 | | - "net" |
7 | | - "os" |
8 | | - "time" |
| 5 | + "net/netip" |
9 | 6 |
|
10 | | - "github.com/Kr328/tun2socket" |
11 | | - |
12 | | - "github.com/metacubex/mihomo/adapter/inbound" |
13 | | - "github.com/metacubex/mihomo/common/pool" |
14 | 7 | C "github.com/metacubex/mihomo/constant" |
| 8 | + LC "github.com/metacubex/mihomo/listener/config" |
| 9 | + "github.com/metacubex/mihomo/listener/sing_tun" |
15 | 10 | "github.com/metacubex/mihomo/log" |
16 | | - "github.com/metacubex/mihomo/transport/socks5" |
17 | 11 | "github.com/metacubex/mihomo/tunnel" |
18 | 12 | ) |
19 | 13 |
|
20 | | -var _, ipv4LoopBack, _ = net.ParseCIDR("127.0.0.0/8") |
21 | | - |
22 | 14 | func Start(fd int, gateway, portal, dns string) (io.Closer, error) { |
23 | 15 | log.Debugln("TUN: fd = %d, gateway = %s, portal = %s, dns = %s", fd, gateway, portal, dns) |
24 | 16 |
|
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, |
32 | 25 | } |
33 | 26 |
|
34 | | - stack, err := tun2socket.StartTun2Socket(device, network, net.ParseIP(portal)) |
| 27 | + listener, err := sing_tun.New(options, tunnel.Tunnel) |
35 | 28 | if err != nil { |
36 | | - _ = device.Close() |
37 | | - |
| 29 | + log.Errorln("TUN:", err) |
38 | 30 | return nil, err |
39 | 31 | } |
40 | 32 |
|
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 |
161 | 34 | } |
0 commit comments