local.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. "strconv"
  11. "strings"
  12. "github.com/gravitl/netmaker/netclient/ncutils"
  13. )
  14. // SetIPForwarding - Sets IP forwarding if it's mac or linux
  15. func SetIPForwarding() error {
  16. os := runtime.GOOS
  17. var err error
  18. switch os {
  19. case "linux":
  20. err = SetIPForwardingUnix()
  21. case "freebsd":
  22. err = SetIPForwardingFreeBSD()
  23. case "darwin":
  24. err = SetIPForwardingMac()
  25. default:
  26. err = errors.New("this OS is not currently supported")
  27. }
  28. return err
  29. }
  30. // SetIPForwardingLinux - sets the ipforwarding for linux
  31. func SetIPForwardingUnix() error {
  32. out, err := ncutils.RunCmd("sysctl net.ipv4.ip_forward", true)
  33. if err != nil {
  34. log.Println("WARNING: Error encountered setting ip forwarding. This can break functionality.")
  35. return err
  36. } else {
  37. s := strings.Fields(string(out))
  38. if s[2] != "1" {
  39. _, err = ncutils.RunCmd("sysctl -w net.ipv4.ip_forward=1", true)
  40. if err != nil {
  41. log.Println("WARNING: Error encountered setting ip forwarding. You may want to investigate this.")
  42. return err
  43. }
  44. }
  45. }
  46. return nil
  47. }
  48. // SetIPForwardingLinux - sets the ipforwarding for linux
  49. func SetIPForwardingFreeBSD() error {
  50. out, err := ncutils.RunCmd("sysctl net.inet.ip.forwarding", true)
  51. if err != nil {
  52. log.Println("WARNING: Error encountered setting ip forwarding. This can break functionality.")
  53. return err
  54. } else {
  55. s := strings.Fields(string(out))
  56. if s[1] != "1" {
  57. _, err = ncutils.RunCmd("sysctl -w net.inet.ip.forwarding=1", true)
  58. if err != nil {
  59. log.Println("WARNING: Error encountered setting ip forwarding. You may want to investigate this.")
  60. return err
  61. }
  62. }
  63. }
  64. return nil
  65. }
  66. // SetIPForwardingMac - sets ip forwarding for mac
  67. func SetIPForwardingMac() error {
  68. _, err := ncutils.RunCmd("sysctl -w net.inet.ip.forwarding=1", true)
  69. if err != nil {
  70. log.Println("WARNING: Error encountered setting ip forwarding. This can break functionality.")
  71. }
  72. return err
  73. }
  74. // IsWGInstalled - checks if WireGuard is installed
  75. func IsWGInstalled() bool {
  76. out, err := ncutils.RunCmd("wg help", true)
  77. if err != nil {
  78. _, err = exec.LookPath(os.Getenv("WG_QUICK_USERSPACE_IMPLEMENTATION"))
  79. return err == nil
  80. }
  81. return strings.Contains(out, "Available subcommand")
  82. }
  83. // GetMacIface - gets mac interface
  84. func GetMacIface(ipstring string) (string, error) {
  85. var wgiface string
  86. _, checknet, err := net.ParseCIDR(ipstring + "/24")
  87. if err != nil {
  88. return wgiface, errors.New("could not parse ip " + ipstring)
  89. }
  90. ifaces, err := net.Interfaces()
  91. if err != nil {
  92. return wgiface, err
  93. }
  94. for _, iface := range ifaces {
  95. addrs, err := iface.Addrs()
  96. if err != nil {
  97. continue
  98. }
  99. for _, addr := range addrs {
  100. ip := addr.(*net.IPNet).IP
  101. if checknet.Contains(ip) {
  102. wgiface = iface.Name
  103. break
  104. }
  105. }
  106. }
  107. if wgiface == "" {
  108. err = errors.New("could not find iface for address " + ipstring)
  109. }
  110. return wgiface, err
  111. }
  112. // HasNetwork - checks if a network exists locally
  113. func HasNetwork(network string) bool {
  114. return ncutils.FileExists(ncutils.GetNetclientPathSpecific() + "netconfig-" + network)
  115. }
  116. // Get LocalListenPort - Gets the port running on the local interface
  117. func GetLocalListenPort(ifacename string) (int32, error) {
  118. portstring, err := ncutils.RunCmd("wg show "+ifacename+" listen-port", false)
  119. if err != nil {
  120. return 0, err
  121. }
  122. portstring = strings.TrimSuffix(portstring, "\n")
  123. i, err := strconv.ParseInt(portstring, 10, 32)
  124. if err != nil {
  125. return 0, err
  126. } else if i == 0 {
  127. return 0, errors.New("parsed port is unset or invalid")
  128. }
  129. return int32(i), nil
  130. }