This repository has been archived by the owner on Jan 2, 2024. It is now read-only.
/
helpers_linux.go
105 lines (88 loc) · 2.75 KB
/
helpers_linux.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
package subnet
import (
"bufio"
"bytes"
"errors"
"fmt"
"net"
"os"
"strconv"
"strings"
)
//SetInterfaceStatus brings up or down a network interface.
func SetInterfaceStatus(iName string, up bool, debug bool) error {
statusString := "down"
if up {
statusString = "up"
}
sargs := fmt.Sprintf("link set dev %s %s mtu %d qlen %d", iName, statusString, devMtuSize, devTxQueLen)
args := strings.Split(sargs, " ")
return commandExec("ip", args, debug)
}
//SetDevIP sets the local IP address of a network interface.
func SetDevIP(iName string, localAddr net.IP, addr *net.IPNet, debug bool) error {
sargs := fmt.Sprintf("%s %s netmask %s", iName, localAddr.String(), net.IP(addr.Mask).String())
args := strings.Split(sargs, " ")
return commandExec("ifconfig", args, debug)
}
// SetDefaultGateway sets the systems gateway to the IP / device specified.
func SetDefaultGateway(gw, iName string, debug bool) error {
sargs := fmt.Sprintf("add default gw %s dev %s", gw, iName)
args := strings.Split(sargs, " ")
return commandExec("route", args, debug)
}
// AddRoute routes all traffic for addr via interface iName.
func AddRoute(addr, viaAddr net.IP, iName string, debug bool) error {
sargs := fmt.Sprintf("add %s gw %s dev %s", addr.String(), viaAddr.String(), iName)
args := strings.Split(sargs, " ")
return commandExec("route", args, debug)
}
// DelRoute deletes the route in the system routing table to a specific destination.
func DelRoute(addr, viaAddr net.IP, iName string, debug bool) error {
sargs := fmt.Sprintf("del %s gw %s dev %s", addr.String(), viaAddr.String(), iName)
args := strings.Split(sargs, " ")
return commandExec("route", args, debug)
}
// GetNetGateway return net gateway (default route) and nic.
// Credit: https://github.com/bigeagle/gohop/blob/master/hop/iface.go
func GetNetGateway() (gw, dev string, err error) {
file, err := os.Open("/proc/net/route")
if err != nil {
return "", "", err
}
defer file.Close()
rd := bufio.NewReader(file)
s2byte := func(s string) byte {
b, _ := strconv.ParseUint(s, 16, 8)
return byte(b)
}
for {
line, isPrefix, err := rd.ReadLine()
if err != nil {
return "", "", err
}
if isPrefix {
return "", "", errors.New("Parse error: Line too long")
}
buf := bytes.NewBuffer(line)
scanner := bufio.NewScanner(buf)
scanner.Split(bufio.ScanWords)
tokens := make([]string, 0, 8)
for scanner.Scan() {
tokens = append(tokens, scanner.Text())
}
iface := tokens[0]
dest := tokens[1]
gw := tokens[2]
mask := tokens[7]
if bytes.Equal([]byte(dest), []byte("00000000")) &&
bytes.Equal([]byte(mask), []byte("00000000")) {
a := s2byte(gw[6:8])
b := s2byte(gw[4:6])
c := s2byte(gw[2:4])
d := s2byte(gw[0:2])
ip := net.IPv4(a, b, c, d)
return ip.String(), iface, nil
}
}
}