-
Notifications
You must be signed in to change notification settings - Fork 3
/
tun.go
223 lines (212 loc) · 6.85 KB
/
tun.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package netrelay
import (
"context"
"errors"
"fmt"
"log"
"net"
"os"
"runtime"
"strconv"
"strings"
"time"
"github.com/FavorLabs/favorX/pkg/netrelay/pb"
"github.com/FavorLabs/favorX/pkg/p2p"
"github.com/FavorLabs/favorX/pkg/p2p/protobuf"
"github.com/net-byte/vtun/common/cache"
"github.com/net-byte/vtun/common/counter"
"github.com/net-byte/vtun/common/netutil"
"github.com/net-byte/vtun/register"
"github.com/net-byte/water"
)
// CreateTun creates a tun interface
func (s *Service) CreateTun(config VpnConfig) (iface *water.Interface) {
c := water.Config{DeviceType: water.TUN}
c.PlatformSpecificParams = water.PlatformSpecificParams{}
os := runtime.GOOS
if os == "windows" {
c.PlatformSpecificParams.Name = "vtun"
c.PlatformSpecificParams.Network = []string{config.CIDR}
}
iface, err := water.New(c)
if err != nil {
s.logger.Errorf("failed to create tun interface:", err)
panic(err)
}
s.logger.Infof("tun interface created %v", iface.Name())
s.setRoute(config, iface)
go s.forwardVpn(iface)
s.iface = iface
s.vpnConfig = config
return iface
}
// setRoute sets the system routes
func (s *Service) setRoute(config VpnConfig, iface *water.Interface) {
ip, _, err := net.ParseCIDR(config.CIDR)
if err != nil {
log.Panicf("tun error cidr %v", config.CIDR)
}
_, _, err = net.ParseCIDR(config.CIDRv6)
if err != nil {
log.Panicf("tun error ipv6 cidr %v", config.CIDRv6)
}
physicalIface := netutil.GetInterface()
if physicalIface == "" {
s.logger.Errorf("get physical interface failed, please set nat for vpn by yourself")
}
o := runtime.GOOS
if o == "linux" {
netutil.ExecCmd("sysctl", "net.ipv4.ip_forward=1")
netutil.ExecCmd("sysctl", "net.ipv6.conf.all.forwarding=1")
netutil.ExecCmd("/sbin/ip", "link", "set", "dev", iface.Name(), "mtu", strconv.Itoa(config.MTU))
netutil.ExecCmd("/sbin/ip", "addr", "add", config.CIDR, "dev", iface.Name())
netutil.ExecCmd("/sbin/ip", "-6", "addr", "add", config.CIDRv6, "dev", iface.Name())
netutil.ExecCmd("/sbin/ip", "link", "set", "dev", iface.Name(), "up")
if physicalIface != "" {
netutil.ExecCmd("iptables", "-t", "nat", "-A", "POSTROUTING", "-o", physicalIface, "-j", "MASQUERADE")
netutil.ExecCmd("iptables", "-t", "nat", "-A", "POSTROUTING", "-o", iface.Name(), "-j", "MASQUERADE")
netutil.ExecCmd("iptables", "-A", "INPUT", "-i", physicalIface, "-m", "state", "--state", "RELATED,ESTABLISHED", "-j", "ACCEPT")
netutil.ExecCmd("iptables", "-A", "INPUT", "-i", iface.Name(), "-m", "state", "--state", "RELATED,ESTABLISHED", "-j", "ACCEPT")
netutil.ExecCmd("iptables", "-A", "FORWARD", "-j", "ACCEPT")
}
} else if o == "darwin" {
netutil.ExecCmd("sysctl", "net.inet.ip.forwarding=1")
netutil.ExecCmd("sysctl", "net.inet6.ip6.forwarding=1")
netutil.ExecCmd("ifconfig", iface.Name(), "mtu", strconv.Itoa(config.MTU))
netutil.ExecCmd("ifconfig", iface.Name(), "inet", ip.To4().String(), config.ServerIP)
netutil.ExecCmd("ifconfig", iface.Name(), "inet6", "add", config.CIDRv6)
netutil.ExecCmd("ifconfig", iface.Name(), "up")
netutil.ExecCmd("route", "add", config.CIDR, "-interface", iface.Name())
if physicalIface != "" {
nat4 := fmt.Sprintf("nat on %s inet from %s:network to any -> (%s)\n", physicalIface, iface.Name(), physicalIface)
nat6 := fmt.Sprintf("nat on %s inet6 from %s:network to any -> (%s)\n", physicalIface, iface.Name(), physicalIface)
filename := "/tmp/pf-nat.conf"
file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
panic(err)
}
_, err = file.WriteString(nat4)
if err != nil {
panic(err)
}
_, err = file.WriteString(nat6)
if err != nil {
panic(err)
}
netutil.ExecCmd("pfctl", "-d")
netutil.ExecCmd("pfctl", "-ef", filename)
}
}
}
func (s *Service) onVpnTun(_ context.Context, p p2p.Peer, stream p2p.Stream) (err error) {
defer func() {
if err != nil {
s.logger.Tracef("onVpnTun from %s err %s", p.Address, err)
_ = stream.Reset()
} else {
_ = stream.Close() // must use .Close instead of .FullClose, otherwise it will lead to goroutine leakage
s.logger.Tracef("onVpnTun from %s stream close", p.Address)
}
}()
if s.vpnGroup != "" && s.vpnConfig.CIDR == "" {
s.forwardStream(stream, s.vpnGroup, streamVpnTun)
return nil
}
packet := make([]byte, 64*1024)
for {
n, err := stream.Read(packet)
if err != nil {
return err
}
b := packet[:n]
if key := netutil.GetSrcKey(b); key != "" {
cache.GetCache().Set(key, stream, 24*time.Hour)
counter.IncrReadBytes(n)
s.iface.Write(b)
}
}
}
func (s *Service) forwardVpn(iface *water.Interface) {
packet := make([]byte, 64*1024)
for {
n, err := iface.Read(packet)
if err != nil {
s.logger.Errorf("vpn tun read %s", err)
break
}
b := packet[:n]
if key := netutil.GetDstKey(b); key != "" {
if v, ok := cache.GetCache().Get(key); ok {
_, err = v.(p2p.Stream).Write(b)
if err != nil {
cache.GetCache().Delete(key)
continue
}
counter.IncrWrittenBytes(n)
}
}
}
}
func (s *Service) onVpnRequest(ctx context.Context, p p2p.Peer, stream p2p.Stream) (err error) {
defer func() {
if err != nil {
s.logger.Tracef("onVpnRequest from %s err %s", p.Address, err)
_ = stream.Reset()
} else {
_ = stream.Close() // must use .Close instead of .FullClose, otherwise it will lead to goroutine leakage
s.logger.Tracef("onVpnRequest from %s stream close", p.Address)
}
}()
if s.vpnGroup != "" && s.vpnConfig.CIDR == "" {
s.forwardStream(stream, s.vpnGroup, streamVpnRequest)
return nil
}
w, r := protobuf.NewWriterAndReader(stream)
var req pb.VpnRequest
err = r.ReadMsgWithContext(ctx, &req)
if err != nil {
return err
}
switch req.Pattern {
case "/test":
err = w.WriteMsgWithContext(ctx, &pb.VpnResponse{Body: "OK"})
case "/register/pick/ip":
ip, pl := register.PickClientIP(s.vpnConfig.CIDR)
resp := fmt.Sprintf("%v/%v", ip, pl)
err = w.WriteMsgWithContext(ctx, &pb.VpnResponse{Body: resp})
case "/register/delete/ip":
if req.Ip != "" {
register.DeleteClientIP(req.Ip)
}
err = w.WriteMsgWithContext(ctx, &pb.VpnResponse{Body: "OK"})
case "/register/keepalive/ip":
if req.Ip != "" {
register.KeepAliveClientIP(req.Ip)
}
err = w.WriteMsgWithContext(ctx, &pb.VpnResponse{Body: "OK"})
case "/register/list/ip":
resp := strings.Join(register.ListClientIPs(), "\r\n")
err = w.WriteMsgWithContext(ctx, &pb.VpnResponse{Body: resp})
case "/register/prefix/ipv4":
_, ipv4Net, e := net.ParseCIDR(s.vpnConfig.CIDR)
var resp string
if e != nil {
resp = "error"
} else {
resp = ipv4Net.String()
}
err = w.WriteMsgWithContext(ctx, &pb.VpnResponse{Body: resp})
case "/register/prefix/ipv6":
_, ipv6Net, e := net.ParseCIDR(s.vpnConfig.CIDRv6)
var resp string
if e != nil {
resp = "error"
} else {
resp = ipv6Net.String()
}
err = w.WriteMsgWithContext(ctx, &pb.VpnResponse{Body: resp})
default:
return errors.New("mismatch vpn pattern")
}
return
}