netmaker/netclient/local/local.go

124 lines
3.1 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-09-20 04:18:22 +08:00
"net"
2021-10-09 03:07:12 +08:00
"os"
"os/exec"
2021-08-03 06:06:26 +08:00
"runtime"
2021-05-06 08:51:59 +08:00
"strings"
2021-10-09 03:07:12 +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-10-09 03:07:12 +08:00
// SetIPForwarding - Sets IP forwarding if it's mac or linux
2021-08-03 06:06:26 +08:00
func SetIPForwarding() error {
os := runtime.GOOS
var err error
switch os {
case "linux":
err = SetIPForwardingUnix()
case "freebsd":
err = SetIPForwardingFreeBSD()
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
}
2021-10-09 03:07:12 +08:00
// SetIPForwardingLinux - sets the ipforwarding for linux
func SetIPForwardingUnix() 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
}
// SetIPForwardingLinux - sets the ipforwarding for linux
func SetIPForwardingFreeBSD() error {
out, err := ncutils.RunCmd("sysctl net.inet.ip.forwarding", true)
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[1] != "1" {
_, err = ncutils.RunCmd("sysctl -w net.inet.ip.forwarding=1", true)
if err != nil {
log.Println("WARNING: Error encountered setting ip forwarding. You may want to investigate this.")
return err
}
}
}
return nil
}
2021-10-09 03:07:12 +08:00
// SetIPForwardingMac - sets ip forwarding for mac
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-10-09 03:07:12 +08:00
// IsWGInstalled - checks if WireGuard is installed
2021-09-20 02:03:47 +08:00
func IsWGInstalled() bool {
out, err := ncutils.RunCmd("wg help", true)
if err != nil {
2021-09-22 09:35:52 +08:00
_, err = exec.LookPath(os.Getenv("WG_QUICK_USERSPACE_IMPLEMENTATION"))
return err == nil
2021-08-03 06:06:26 +08:00
}
2021-09-20 02:03:47 +08:00
return strings.Contains(out, "Available subcommand")
}
2021-10-09 03:07:12 +08:00
// GetMacIface - gets mac interface
2021-09-20 04:18:22 +08:00
func GetMacIface(ipstring string) (string, error) {
var wgiface string
_, checknet, err := net.ParseCIDR(ipstring + "/24")
if err != nil {
2021-09-20 04:18:22 +08:00
return wgiface, errors.New("could not parse ip " + ipstring)
2021-08-03 06:06:26 +08:00
}
2021-09-20 04:18:22 +08:00
ifaces, err := net.Interfaces()
if err != nil {
return wgiface, err
}
for _, iface := range ifaces {
addrs, err := iface.Addrs()
if err != nil {
continue
}
for _, addr := range addrs {
ip := addr.(*net.IPNet).IP
if checknet.Contains(ip) {
wgiface = iface.Name
break
}
2021-08-03 06:06:26 +08:00
}
}
2021-09-20 04:18:22 +08:00
if wgiface == "" {
2021-09-22 02:01:52 +08:00
err = errors.New("could not find iface for address " + ipstring)
2021-06-02 23:00:10 +08:00
}
2021-09-20 04:18:22 +08:00
return wgiface, err
2021-03-26 00:17:52 +08:00
}
2021-10-09 03:07:12 +08:00
// HasNetwork - checks if a network exists locally
2021-08-03 06:06:26 +08:00
func HasNetwork(network string) bool {
2021-10-09 03:07:12 +08:00
return ncutils.FileExists(ncutils.GetNetclientPathSpecific() + "netconfig-" + network)
2021-03-26 00:17:52 +08:00
}