-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage_unix.go
126 lines (101 loc) · 2.66 KB
/
manage_unix.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
//go:build !windows
package swiftunnel
import (
"errors"
"fmt"
"github.com/XenonCommunity/swiftunnel/swiftypes"
"github.com/vishvananda/netlink"
"os"
"syscall"
)
func ioctl(fd uintptr, request uintptr, argv uintptr) error {
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, fd, request, argv); errno != 0 {
return os.NewSyscallError("ioctl", errno)
}
return nil
}
func (a *SwiftInterface) GetAdapterName() (string, error) {
if a.name == "" {
return "", errors.New("adapter name is not set")
}
return a.name, nil
}
func (a *SwiftInterface) GetAdapterIndex() (int, error) {
if a.name == "" {
return 0, errors.New("adapter name is not set")
}
ifi, err := netlink.LinkByName(a.name)
if err != nil {
return 0, err
}
return ifi.Attrs().Index, nil
}
func (a *SwiftInterface) SetMTU(mtu int) error {
index, err := a.GetAdapterIndex()
if err != nil {
return err
}
link, err := netlink.LinkByIndex(index)
if err != nil {
return fmt.Errorf("failed to find interface: %w", err)
}
if err = netlink.LinkSetMTU(link, mtu); err != nil {
return fmt.Errorf("failed to set MTU: %w", err)
}
return nil
}
func (a *SwiftInterface) SetUnicastIpAddressEntry(config *swiftypes.UnicastConfig) error {
index, err := a.GetAdapterIndex()
if err != nil {
return err
}
link, err := netlink.LinkByIndex(index)
if err != nil {
return fmt.Errorf("failed to find interface: %w", err)
}
if err := netlink.AddrAdd(link, &netlink.Addr{
IPNet: config.IPNet,
}); err != nil {
return fmt.Errorf("failed to add address %v to interface %d: %v", config.IPNet, index, err)
}
if config.Gateway != nil {
if err := a.AddRoute(&netlink.Route{
LinkIndex: link.Attrs().Index,
Gw: config.Gateway,
}); err != nil {
return fmt.Errorf("failed to add route to gateway %v: %v", config.Gateway, err)
}
}
return nil
}
func (a *SwiftInterface) SetStatus(status swiftypes.InterfaceStatus) error {
index, err := a.GetAdapterIndex()
if err != nil {
return err
}
link, err := netlink.LinkByIndex(index)
if err != nil {
return fmt.Errorf("failed to find interface: %w", err)
}
switch status {
case swiftypes.InterfaceUp:
return netlink.LinkSetUp(link)
case swiftypes.InterfaceDown:
return netlink.LinkSetDown(link)
}
return nil
}
func (a *SwiftInterface) AddRoute(route *netlink.Route) error {
index, err := a.GetAdapterIndex()
if err != nil {
return err
}
route.LinkIndex = index
if err := netlink.RouteAdd(route); err != nil {
return fmt.Errorf("failed to add route %v: %v", route, err)
}
return nil
}
func (a *SwiftInterface) SetDNS(config *swiftypes.DNSConfig) error {
return errors.New("DNS configuration not supported on this platform")
}