peerhelper.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package ncutils
  2. import (
  3. "net"
  4. "strconv"
  5. "strings"
  6. "time"
  7. "github.com/gravitl/netmaker/logger"
  8. "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
  9. )
  10. // GetPeers - gets the peers from a given WireGuard interface
  11. func GetPeers(iface string) ([]wgtypes.Peer, error) {
  12. var peers []wgtypes.Peer
  13. output, err := RunCmd("wg show "+iface+" dump", true)
  14. if err != nil {
  15. return peers, err
  16. }
  17. for i, line := range strings.Split(strings.TrimSuffix(output, "\n"), "\n") {
  18. if i == 0 {
  19. continue
  20. }
  21. var allowedIPs []net.IPNet
  22. fields := strings.Fields(line)
  23. if len(fields) < 4 {
  24. logger.Log(0, "error parsing peer: "+line)
  25. continue
  26. }
  27. pubkeystring := fields[0]
  28. endpointstring := fields[2]
  29. allowedipstring := fields[3]
  30. var pkeepalivestring string
  31. if len(fields) > 7 {
  32. pkeepalivestring = fields[7]
  33. }
  34. // AllowedIPs = private IP + defined networks
  35. pubkey, err := wgtypes.ParseKey(pubkeystring)
  36. if err != nil {
  37. logger.Log(0, "error parsing peer key "+pubkeystring)
  38. continue
  39. }
  40. ipstrings := strings.Split(allowedipstring, ",")
  41. for _, ipstring := range ipstrings {
  42. var netip net.IP
  43. if netip = net.ParseIP(strings.Split(ipstring, "/")[0]); netip != nil {
  44. allowedIPs = append(
  45. allowedIPs,
  46. net.IPNet{
  47. IP: netip,
  48. Mask: netip.DefaultMask(),
  49. },
  50. )
  51. }
  52. }
  53. if len(allowedIPs) == 0 {
  54. logger.Log(0, "error parsing peer "+pubkeystring+", no allowedips found")
  55. continue
  56. }
  57. var endpointarr []string
  58. var endpointip net.IP
  59. if endpointarr = strings.Split(endpointstring, ":"); len(endpointarr) != 2 {
  60. logger.Log(0, "error parsing peer "+pubkeystring+", could not parse endpoint: "+endpointstring)
  61. continue
  62. }
  63. if endpointip = net.ParseIP(endpointarr[0]); endpointip == nil {
  64. logger.Log(0, "error parsing peer "+pubkeystring+", could not parse endpoint: "+endpointarr[0])
  65. continue
  66. }
  67. var port int
  68. if port, err = strconv.Atoi(endpointarr[1]); err != nil {
  69. logger.Log(0, "error parsing peer "+pubkeystring+", could not parse port: "+err.Error())
  70. continue
  71. }
  72. var endpoint = net.UDPAddr{
  73. IP: endpointip,
  74. Port: port,
  75. }
  76. var dur time.Duration
  77. if pkeepalivestring != "" {
  78. if dur, err = time.ParseDuration(pkeepalivestring + "s"); err != nil {
  79. logger.Log(0, "error parsing peer "+pubkeystring+", could not parse keepalive: "+err.Error())
  80. }
  81. }
  82. peers = append(peers, wgtypes.Peer{
  83. PublicKey: pubkey,
  84. Endpoint: &endpoint,
  85. AllowedIPs: allowedIPs,
  86. PersistentKeepaliveInterval: dur,
  87. })
  88. }
  89. return peers, err
  90. }