-
Notifications
You must be signed in to change notification settings - Fork 127
/
syscalls_darwin.go
66 lines (54 loc) · 1.32 KB
/
syscalls_darwin.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
//
// date : 2017-07-14
// author: xjdrew
//
package kone
import (
"fmt"
"net"
"os/exec"
"strings"
"github.com/songgao/water"
)
func execCommand(name, sargs string) error {
args := strings.Split(sargs, " ")
cmd := exec.Command(name, args...)
logger.Infof("exec command: %s %s", name, sargs)
return cmd.Run()
}
func initTun(tun string, ipNet *net.IPNet, mtu int) error {
ip := ipNet.IP
maskIP := net.IP(ipNet.Mask)
sargs := fmt.Sprintf("%s %s %s mtu %d netmask %s up", tun, ip.String(), ip.String(), mtu, maskIP.String())
if err := execCommand("ifconfig", sargs); err != nil {
return err
}
return addRoute(tun, ipNet)
}
func addRoute(tun string, subnet *net.IPNet) error {
ip := subnet.IP
maskIP := net.IP(subnet.Mask)
sargs := fmt.Sprintf("-n add -net %s -netmask %s -interface %s", ip.String(), maskIP.String(), tun)
return execCommand("route", sargs)
}
func createTun(ip net.IP, mask net.IPMask) (*water.Interface, error) {
ifce, err := water.New(water.Config{
DeviceType: water.TUN,
})
if err != nil {
return nil, err
}
logger.Infof("create %s", ifce.Name())
ipNet := &net.IPNet{
IP: ip,
Mask: mask,
}
if err := initTun(ifce.Name(), ipNet, MTU); err != nil {
return nil, err
}
return ifce, nil
}
// can't listen on tun's ip in macosx
func fixTunIP(ip net.IP) net.IP {
return net.IPv4zero
}