local.go 2.5 KB

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