-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtun.go
44 lines (41 loc) · 1.24 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
package learn_tun
import (
"github.com/songgao/water"
"log"
"os/exec"
"reflect"
"runtime"
)
func CreateTun(name string) *water.Interface {
conf := water.Config{DeviceType: water.TUN}
if runtime.GOOS == "linux" && name != "" {
// Use reflect to avoid separate build file for linux-only.
reflect.ValueOf(&conf).Elem().FieldByName("Name").SetString(name)
}
iface, err := water.New(conf)
if err != nil {
log.Fatalf("create tun err %v", err)
}
log.Printf("created tun device: %v\n", iface.Name())
return iface
}
func TunCommand(local, remote, name string) {
switch runtime.GOOS {
case "linux":
if err := exec.Command("/sbin/ip", "link", "set", "dev", name, "mtu", "1300").Run(); err != nil {
log.Fatalf("ip link error: %v", err)
}
if err := exec.Command("/sbin/ip", "addr", "add", local+"/24", "dev", name).Run(); err != nil {
log.Fatalf("ip addr error: %v", err)
}
if err := exec.Command("/sbin/ip", "link", "set", "dev", name, "up").Run(); err != nil {
log.Fatalf("ip link error: %v", err)
}
case "darwin":
if err := exec.Command("/sbin/ifconfig", name, "mtu", "1300", local, remote, "up").Run(); err != nil {
log.Fatalf("ifconfig error: %v", err)
}
default:
log.Fatalf("no tun support for: %v", runtime.GOOS)
}
}