2021-05-26 00:48:04 +08:00
|
|
|
package serverctl
|
|
|
|
|
|
|
|
import (
|
2021-07-21 05:18:45 +08:00
|
|
|
"github.com/gravitl/netmaker/functions"
|
2021-05-26 00:48:04 +08:00
|
|
|
"golang.zx2c4.com/wireguard/wgctrl"
|
|
|
|
)
|
|
|
|
|
2021-07-26 02:22:20 +08:00
|
|
|
func GetPeers(networkName string) (map[string]string, error) {
|
|
|
|
peers := make(map[string]string)
|
|
|
|
network, err := functions.GetParentNetwork(networkName)
|
|
|
|
if err != nil {
|
|
|
|
return peers, err
|
|
|
|
}
|
|
|
|
iface := network.DefaultInterface
|
|
|
|
|
|
|
|
client, err := wgctrl.New()
|
|
|
|
if err != nil {
|
|
|
|
return peers, err
|
|
|
|
}
|
|
|
|
device, err := client.Device(iface)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, peer := range device.Peers {
|
2021-08-06 10:36:27 +08:00
|
|
|
if functions.IsBase64(peer.PublicKey.String()) && peer.Endpoint != nil && functions.CheckEndpoint(peer.Endpoint.String()) {
|
2021-08-03 02:41:43 +08:00
|
|
|
peers[peer.PublicKey.String()] = peer.Endpoint.String()
|
|
|
|
}
|
2021-07-26 02:22:20 +08:00
|
|
|
}
|
|
|
|
return peers, nil
|
|
|
|
}
|