Browse Source

rm peer from host model

Abhishek Kondur 2 years ago
parent
commit
950fd3c290
3 changed files with 44 additions and 34 deletions
  1. 10 1
      controllers/node.go
  2. 29 30
      models/host.go
  3. 5 3
      models/structs.go

+ 10 - 1
controllers/node.go

@@ -442,6 +442,13 @@ func getNode(w http.ResponseWriter, r *http.Request) {
 		logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
 		return
 	}
+	hostPeerUpdate, err := logic.GetPeerUpdateForHost(host)
+	if err != nil && !database.IsEmptyRecord(err) {
+		logger.Log(0, r.Header.Get("user"),
+			fmt.Sprintf("error fetching wg peers config for host [ %s ]: %v", host.ID.String(), err))
+		logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
+		return
+	}
 	server := servercfg.GetServerInfo()
 	network, err := logic.GetNetwork(node.Network)
 	if err != nil {
@@ -453,7 +460,9 @@ func getNode(w http.ResponseWriter, r *http.Request) {
 	legacy := node.Legacy(host, &server, &network)
 	response := models.NodeGet{
 		Node:         *legacy,
+		Host:         *host,
 		Peers:        peerUpdate.Peers,
+		HostPeers:    hostPeerUpdate.Peers,
 		ServerConfig: server,
 		PeerIDs:      peerUpdate.PeerIDs,
 	}
@@ -644,11 +653,11 @@ func createNode(w http.ResponseWriter, r *http.Request) {
 		logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
 		return
 	}
-	data.Host.Peers = hostPeerUpdate.Peers
 	response := models.NodeJoinResponse{
 		Node:         data.Node,
 		ServerConfig: server,
 		Host:         data.Host,
+		Peers:        hostPeerUpdate.Peers,
 	}
 	logger.Log(1, r.Header.Get("user"), "created new node", data.Host.Name, "on network", networkName)
 	w.WriteHeader(http.StatusOK)

+ 29 - 30
models/host.go

@@ -12,36 +12,35 @@ const WIREGUARD_INTERFACE = "netmaker"
 
 // Host - represents a host on the network
 type Host struct {
-	ID               uuid.UUID            `json:"id" yaml:"id"`
-	Verbosity        int                  `json:"verbosity" yaml:"verbosity"`
-	FirewallInUse    string               `json:"firewallinuse" yaml:"firewallinuse"`
-	Version          string               `json:"version" yaml:"version"`
-	IPForwarding     bool                 `json:"ipforwarding" yaml:"ipforwarding"`
-	DaemonInstalled  bool                 `json:"daemoninstalled" yaml:"daemoninstalled"`
-	HostPass         string               `json:"hostpass" yaml:"hostpass"`
-	Name             string               `json:"name" yaml:"name"`
-	OS               string               `json:"os" yaml:"os"`
-	Interface        string               `json:"interface" yaml:"interface"`
-	Debug            bool                 `json:"debug" yaml:"debug"`
-	ListenPort       int                  `json:"listenport" yaml:"listenport"`
-	LocalAddress     net.IPNet            `json:"localaddress" yaml:"localaddress"`
-	LocalRange       net.IPNet            `json:"localrange" yaml:"localrange"`
-	LocalListenPort  int                  `json:"locallistenport" yaml:"locallistenport"`
-	ProxyListenPort  int                  `json:"proxy_listen_port" yaml:"proxy_listen_port"`
-	MTU              int                  `json:"mtu" yaml:"mtu"`
-	PublicKey        wgtypes.Key          `json:"publickey" yaml:"publickey"`
-	MacAddress       net.HardwareAddr     `json:"macaddress" yaml:"macaddress"`
-	TrafficKeyPublic []byte               `json:"traffickeypublic" yaml:"trafficekeypublic"`
-	InternetGateway  net.UDPAddr          `json:"internetgateway" yaml:"internetgateway"`
-	Nodes            []string             `json:"nodes" yaml:"nodes"`
-	Interfaces       []Iface              `json:"interfaces" yaml:"interfaces"`
-	EndpointIP       net.IP               `json:"endpointip" yaml:"endpointip"`
-	ProxyEnabled     bool                 `json:"proxy_enabled" yaml:"proxy_enabled"`
-	IsDocker         bool                 `json:"isdocker" yaml:"isdocker"`
-	IsK8S            bool                 `json:"isk8s" yaml:"isk8s"`
-	IsStatic         bool                 `json:"isstatic" yaml:"isstatic"`
-	IsDefault        bool                 `json:"isdefault" yaml:"isdefault"`
-	Peers            []wgtypes.PeerConfig `json:"peers" bson:"peers" yaml:"peers"`
+	ID               uuid.UUID        `json:"id" yaml:"id"`
+	Verbosity        int              `json:"verbosity" yaml:"verbosity"`
+	FirewallInUse    string           `json:"firewallinuse" yaml:"firewallinuse"`
+	Version          string           `json:"version" yaml:"version"`
+	IPForwarding     bool             `json:"ipforwarding" yaml:"ipforwarding"`
+	DaemonInstalled  bool             `json:"daemoninstalled" yaml:"daemoninstalled"`
+	HostPass         string           `json:"hostpass" yaml:"hostpass"`
+	Name             string           `json:"name" yaml:"name"`
+	OS               string           `json:"os" yaml:"os"`
+	Interface        string           `json:"interface" yaml:"interface"`
+	Debug            bool             `json:"debug" yaml:"debug"`
+	ListenPort       int              `json:"listenport" yaml:"listenport"`
+	LocalAddress     net.IPNet        `json:"localaddress" yaml:"localaddress"`
+	LocalRange       net.IPNet        `json:"localrange" yaml:"localrange"`
+	LocalListenPort  int              `json:"locallistenport" yaml:"locallistenport"`
+	ProxyListenPort  int              `json:"proxy_listen_port" yaml:"proxy_listen_port"`
+	MTU              int              `json:"mtu" yaml:"mtu"`
+	PublicKey        wgtypes.Key      `json:"publickey" yaml:"publickey"`
+	MacAddress       net.HardwareAddr `json:"macaddress" yaml:"macaddress"`
+	TrafficKeyPublic []byte           `json:"traffickeypublic" yaml:"trafficekeypublic"`
+	InternetGateway  net.UDPAddr      `json:"internetgateway" yaml:"internetgateway"`
+	Nodes            []string         `json:"nodes" yaml:"nodes"`
+	Interfaces       []Iface          `json:"interfaces" yaml:"interfaces"`
+	EndpointIP       net.IP           `json:"endpointip" yaml:"endpointip"`
+	ProxyEnabled     bool             `json:"proxy_enabled" yaml:"proxy_enabled"`
+	IsDocker         bool             `json:"isdocker" yaml:"isdocker"`
+	IsK8S            bool             `json:"isk8s" yaml:"isk8s"`
+	IsStatic         bool             `json:"isstatic" yaml:"isstatic"`
+	IsDefault        bool             `json:"isdefault" yaml:"isdefault"`
 }
 
 // FormatBool converts a boolean to a [yes|no] string

+ 5 - 3
models/structs.go

@@ -205,15 +205,17 @@ type NodeGet struct {
 	Node         LegacyNode           `json:"node" bson:"node" yaml:"node"`
 	Host         Host                 `json:"host" yaml:"host"`
 	Peers        []wgtypes.PeerConfig `json:"peers" bson:"peers" yaml:"peers"`
+	HostPeers    []wgtypes.PeerConfig `json:"host_peers" bson:"host_peers" yaml:"host_peers"`
 	ServerConfig ServerConfig         `json:"serverconfig" bson:"serverconfig" yaml:"serverconfig"`
 	PeerIDs      PeerMap              `json:"peerids,omitempty" bson:"peerids,omitempty" yaml:"peerids,omitempty"`
 }
 
 // NodeJoinResponse data returned to node in response to join
 type NodeJoinResponse struct {
-	Node         Node         `json:"node" bson:"node" yaml:"node"`
-	Host         Host         `json:"host" yaml:"host"`
-	ServerConfig ServerConfig `json:"serverconfig" bson:"serverconfig" yaml:"serverconfig"`
+	Node         Node                 `json:"node" bson:"node" yaml:"node"`
+	Host         Host                 `json:"host" yaml:"host"`
+	ServerConfig ServerConfig         `json:"serverconfig" bson:"serverconfig" yaml:"serverconfig"`
+	Peers        []wgtypes.PeerConfig `json:"peers" bson:"peers" yaml:"peers"`
 }
 
 // ServerConfig - struct for dealing with the server information for a netclient