netmaker/netclient/local/local.go

101 lines
2.4 KiB
Go
Raw Normal View History

2021-05-26 00:48:04 +08:00
package local
2021-03-26 00:17:52 +08:00
import (
2021-08-03 06:06:26 +08:00
//"github.com/davecgh/go-spew/spew"
"errors"
"log"
2021-08-03 06:06:26 +08:00
"runtime"
2021-05-06 08:51:59 +08:00
"strings"
2021-08-03 06:06:26 +08:00
2021-09-20 02:03:47 +08:00
"github.com/gravitl/netmaker/netclient/ncutils"
2021-03-26 00:17:52 +08:00
)
2021-08-03 06:06:26 +08:00
func SetIPForwarding() error {
os := runtime.GOOS
var err error
switch os {
case "linux":
err = SetIPForwardingLinux()
2021-09-20 02:03:47 +08:00
case "darwin":
err = SetIPForwardingMac()
2021-08-03 06:06:26 +08:00
default:
err = errors.New("This OS is not supported")
}
return err
}
func SetIPForwardingLinux() error {
2021-09-20 02:03:47 +08:00
out, err := ncutils.RunCmd("sysctl net.ipv4.ip_forward", true)
2021-08-03 06:06:26 +08:00
if err != nil {
log.Println("WARNING: Error encountered setting ip forwarding. This can break functionality.")
return err
} else {
s := strings.Fields(string(out))
if s[2] != "1" {
2021-09-20 02:03:47 +08:00
_, err = ncutils.RunCmd("sysctl -w net.ipv4.ip_forward=1", true)
2021-08-03 06:06:26 +08:00
if err != nil {
log.Println("WARNING: Error encountered setting ip forwarding. You may want to investigate this.")
return err
}
}
}
return nil
}
2021-09-20 02:03:47 +08:00
func SetIPForwardingMac() error {
_, err := ncutils.RunCmd("sysctl -w net.inet.ip.forwarding=1", true)
if err != nil {
log.Println("WARNING: Error encountered setting ip forwarding. This can break functionality.")
2021-08-03 06:06:26 +08:00
}
return err
2021-05-26 00:48:04 +08:00
}
2021-09-20 02:03:47 +08:00
func IsWGInstalled() bool {
out, err := ncutils.RunCmd("wg help", true)
if err != nil {
2021-08-03 06:06:26 +08:00
return false
}
2021-09-20 02:03:47 +08:00
return strings.Contains(out, "Available subcommand")
}
2021-09-20 02:03:47 +08:00
func GetMacIface(addr string) (string, error) {
out, err := ncutils.RunCmd("route get "+addr, false)
var iface string
if err != nil {
2021-09-20 02:03:47 +08:00
return iface, errors.New(string(out))
2021-08-03 06:06:26 +08:00
}
2021-09-20 02:03:47 +08:00
for _, line := range strings.Split(strings.TrimSuffix(string(out), "\n"), "\n") {
if strings.Contains(line, "interface: ") {
iface = getLineAfter(string(out), "interface: ")
iface = strings.Split(iface, "\n")[0]
break
2021-08-03 06:06:26 +08:00
}
}
2021-09-20 02:03:47 +08:00
if iface == "" {
err = errors.New("could not find iface for ip addr " + addr)
2021-06-02 23:00:10 +08:00
}
2021-09-20 02:03:47 +08:00
return iface, err
2021-03-26 00:17:52 +08:00
}
2021-09-20 02:03:47 +08:00
func getLineAfter(value string, a string) string {
// Get substring after a string.
pos := strings.LastIndex(value, a)
if pos == -1 {
return ""
2021-06-02 23:00:10 +08:00
}
2021-09-20 02:03:47 +08:00
adjustedPos := pos + len(a)
if adjustedPos >= len(value) {
return ""
2021-08-03 06:06:26 +08:00
}
2021-09-20 02:03:47 +08:00
return value[adjustedPos:len(value)]
2021-05-26 00:48:04 +08:00
}
2021-08-03 06:06:26 +08:00
func HasNetwork(network string) bool {
2021-05-26 00:48:04 +08:00
2021-09-20 02:03:47 +08:00
if ncutils.IsWindows() {
return ncutils.FileExists(ncutils.GetNetclientPathSpecific() + "netconfig-" + network)
2021-03-26 00:17:52 +08:00
}
2021-09-20 02:03:47 +08:00
return ncutils.FileExists("/etc/systemd/system/netclient-"+network+".timer") ||
ncutils.FileExists(ncutils.GetNetclientPathSpecific()+"netconfig-"+network)
2021-03-26 00:17:52 +08:00
}