local.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package local
  2. import (
  3. //"github.com/davecgh/go-spew/spew"
  4. "errors"
  5. "log"
  6. "net"
  7. "os"
  8. "os/exec"
  9. "runtime"
  10. "strings"
  11. "github.com/gravitl/netmaker/netclient/ncutils"
  12. )
  13. // SetIPForwarding - Sets IP forwarding if it's mac or linux
  14. func SetIPForwarding() error {
  15. os := runtime.GOOS
  16. var err error
  17. switch os {
  18. case "linux":
  19. err = SetIPForwardingLinux()
  20. case "darwin":
  21. err = SetIPForwardingMac()
  22. default:
  23. err = errors.New("This OS is not supported")
  24. }
  25. return err
  26. }
  27. // SetIPForwardingLinux - sets the ipforwarding for linux
  28. func SetIPForwardingLinux() error {
  29. out, err := ncutils.RunCmd("sysctl net.ipv4.ip_forward", true)
  30. if err != nil {
  31. log.Println("WARNING: Error encountered setting ip forwarding. This can break functionality.")
  32. return err
  33. } else {
  34. s := strings.Fields(string(out))
  35. if s[2] != "1" {
  36. _, err = ncutils.RunCmd("sysctl -w net.ipv4.ip_forward=1", true)
  37. if err != nil {
  38. log.Println("WARNING: Error encountered setting ip forwarding. You may want to investigate this.")
  39. return err
  40. }
  41. }
  42. }
  43. return nil
  44. }
  45. // SetIPForwardingMac - sets ip forwarding for mac
  46. func SetIPForwardingMac() error {
  47. _, err := ncutils.RunCmd("sysctl -w net.inet.ip.forwarding=1", true)
  48. if err != nil {
  49. log.Println("WARNING: Error encountered setting ip forwarding. This can break functionality.")
  50. }
  51. return err
  52. }
  53. // IsWGInstalled - checks if WireGuard is installed
  54. func IsWGInstalled() bool {
  55. out, err := ncutils.RunCmd("wg help", true)
  56. if err != nil {
  57. _, err = exec.LookPath(os.Getenv("WG_QUICK_USERSPACE_IMPLEMENTATION"))
  58. return err == nil
  59. }
  60. return strings.Contains(out, "Available subcommand")
  61. }
  62. // GetMacIface - gets mac interface
  63. func GetMacIface(ipstring string) (string, error) {
  64. var wgiface string
  65. _, checknet, err := net.ParseCIDR(ipstring + "/24")
  66. if err != nil {
  67. return wgiface, errors.New("could not parse ip " + ipstring)
  68. }
  69. ifaces, err := net.Interfaces()
  70. if err != nil {
  71. return wgiface, err
  72. }
  73. for _, iface := range ifaces {
  74. addrs, err := iface.Addrs()
  75. if err != nil {
  76. continue
  77. }
  78. for _, addr := range addrs {
  79. ip := addr.(*net.IPNet).IP
  80. if checknet.Contains(ip) {
  81. wgiface = iface.Name
  82. break
  83. }
  84. }
  85. }
  86. if wgiface == "" {
  87. err = errors.New("could not find iface for address " + ipstring)
  88. }
  89. return wgiface, err
  90. }
  91. // HasNetwork - checks if a network exists locally
  92. func HasNetwork(network string) bool {
  93. return ncutils.FileExists(ncutils.GetNetclientPathSpecific() + "netconfig-" + network)
  94. }