mirror of
https://github.com/gravitl/netmaker.git
synced 2024-11-10 17:48:25 +08:00
Merge pull request #1045 from gravitl/bugfix_v0.13.1_nc_list
netclient/functions/list.go: Added peer addresses back to list command
This commit is contained in:
commit
9b35b74a7b
1 changed files with 70 additions and 5 deletions
|
@ -3,19 +3,25 @@ package functions
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/gravitl/netmaker/logger"
|
||||
"github.com/gravitl/netmaker/models"
|
||||
"github.com/gravitl/netmaker/netclient/config"
|
||||
"github.com/gravitl/netmaker/netclient/ncutils"
|
||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||
)
|
||||
|
||||
// Peer - the peer struct for list
|
||||
type Peer struct {
|
||||
Name string `json:"name"`
|
||||
Interface string `json:"interface,omitempty"`
|
||||
PrivateIPv4 string `json:"private_ipv4,omitempty"`
|
||||
PrivateIPv6 string `json:"private_ipv6,omitempty"`
|
||||
PublicEndpoint string `json:"public_endpoint,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Interface string `json:"interface,omitempty"`
|
||||
PrivateIPv4 string `json:"private_ipv4,omitempty"`
|
||||
PrivateIPv6 string `json:"private_ipv6,omitempty"`
|
||||
PublicKey string `json:"public_key,omitempty"`
|
||||
PublicEndpoint string `json:"public_endpoint,omitempty"`
|
||||
Addresses []address `json:"addresses,omitempty"`
|
||||
}
|
||||
|
||||
// Network - the local node network representation for list command
|
||||
|
@ -26,6 +32,11 @@ type Network struct {
|
|||
Peers []Peer `json:"peers"`
|
||||
}
|
||||
|
||||
type address struct {
|
||||
CIDR string `json:"cidr,omitempty"`
|
||||
IP string `json:"ip,omitempty"`
|
||||
}
|
||||
|
||||
// List - lists the current peers for the local node with name and node ID
|
||||
func List(network string) error {
|
||||
nets := []Network{}
|
||||
|
@ -46,6 +57,10 @@ func List(network string) error {
|
|||
logger.Log(1, network+": Could not retrieve network configuration.")
|
||||
return err
|
||||
}
|
||||
peers, err := getPeers(network)
|
||||
if err == nil && len(peers) > 0 {
|
||||
net.Peers = peers
|
||||
}
|
||||
nets = append(nets, net)
|
||||
}
|
||||
|
||||
|
@ -80,3 +95,53 @@ func getNetwork(network string) (Network, error) {
|
|||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func getPeers(network string) ([]Peer, error) {
|
||||
cfg, err := config.ReadConfig(network)
|
||||
if err != nil {
|
||||
return []Peer{}, err
|
||||
}
|
||||
token, err := Authenticate(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
url := "https://" + cfg.Server.API + "/api/nodes/" + cfg.Network + "/" + cfg.Node.ID
|
||||
response, err := API("", http.MethodGet, url, token)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if response.StatusCode != http.StatusOK {
|
||||
bytes, err := io.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
return nil, (fmt.Errorf("%s %w", string(bytes), err))
|
||||
}
|
||||
defer response.Body.Close()
|
||||
var nodeGET models.NodeGet
|
||||
if err := json.NewDecoder(response.Body).Decode(&nodeGET); err != nil {
|
||||
return nil, fmt.Errorf("error decoding node %w", err)
|
||||
}
|
||||
if nodeGET.Peers == nil {
|
||||
nodeGET.Peers = []wgtypes.PeerConfig{}
|
||||
}
|
||||
|
||||
peers := []Peer{}
|
||||
for _, peer := range nodeGET.Peers {
|
||||
var addresses = []address{}
|
||||
for j := range peer.AllowedIPs {
|
||||
newAddress := address{
|
||||
CIDR: peer.AllowedIPs[j].String(),
|
||||
IP: peer.AllowedIPs[j].IP.String(),
|
||||
}
|
||||
addresses = append(addresses, newAddress)
|
||||
}
|
||||
peers = append(peers, Peer{
|
||||
PublicKey: peer.PublicKey.String(),
|
||||
PublicEndpoint: peer.Endpoint.String(),
|
||||
Addresses: addresses,
|
||||
})
|
||||
}
|
||||
|
||||
return peers, nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue