Browse Source

move peers to host level

Abhishek Kondur 2 years ago
parent
commit
72e4b914b9
6 changed files with 54 additions and 82 deletions
  1. 3 5
      controllers/node.go
  2. 1 1
      models/api_node.go
  3. 30 29
      models/host.go
  4. 20 21
      models/node.go
  5. 0 2
      models/structs.go
  6. 0 24
      mq/publishers.go

+ 3 - 5
controllers/node.go

@@ -455,7 +455,6 @@ func getNode(w http.ResponseWriter, r *http.Request) {
 		Node:         *legacy,
 		Peers:        peerUpdate.Peers,
 		ServerConfig: server,
-		PeerIDs:      peerUpdate.PeerIDs,
 	}
 
 	if servercfg.Is_EE && nodeRequest {
@@ -637,19 +636,18 @@ func createNode(w http.ResponseWriter, r *http.Request) {
 			return
 		}
 	}
-	peerUpdate, err := logic.GetPeerUpdate(&data.Node, &data.Host)
+	peerUpdate, err := logic.GetPeerUpdateForHost(&data.Host)
 	if err != nil && !database.IsEmptyRecord(err) {
 		logger.Log(0, r.Header.Get("user"),
 			fmt.Sprintf("error fetching wg peers config for node [ %s ]: %v", data.Node.ID.String(), err))
 		logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
 		return
 	}
-	data.Node.Peers = peerUpdate.Peers
-
+	data.Host.Peers = peerUpdate.Peers
 	response := models.NodeJoinResponse{
 		Node:         data.Node,
 		ServerConfig: server,
-		PeerIDs:      peerUpdate.PeerIDs,
+		Host:         data.Host,
 	}
 	logger.Log(1, r.Header.Get("user"), "created new node", data.Host.Name, "on network", networkName)
 	w.WriteHeader(http.StatusOK)

+ 1 - 1
models/api_node.go

@@ -59,7 +59,7 @@ func (a *ApiNode) ConvertToServerNode(currentNode *Node) *Node {
 	convertedNode.IsRelay = a.IsRelay
 	convertedNode.IsRelayed = a.IsRelayed
 	convertedNode.PendingDelete = a.PendingDelete
-	convertedNode.Peers = currentNode.Peers
+	//convertedNode.Peers = currentNode.Peers
 	convertedNode.Failover = a.Failover
 	convertedNode.IsEgressGateway = a.IsEgressGateway
 	convertedNode.IsIngressGateway = a.IsIngressGateway

+ 30 - 29
models/host.go

@@ -12,35 +12,36 @@ 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"`
+	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"`
 }
 
 // FormatBool converts a boolean to a [yes|no] string

+ 20 - 21
models/node.go

@@ -56,27 +56,26 @@ type Iface struct {
 
 // CommonNode - represents a commonn node data elements shared by netmaker and netclient
 type CommonNode struct {
-	ID                  uuid.UUID            `json:"id" yaml:"id"`
-	HostID              uuid.UUID            `json:"hostid" yaml:"hostid"`
-	Name                string               `json:"name" yaml:"name"`
-	Network             string               `json:"network" yaml:"network"`
-	NetworkRange        net.IPNet            `json:"networkrange" yaml:"networkrange"`
-	NetworkRange6       net.IPNet            `json:"networkrange6" yaml:"networkrange6"`
-	InternetGateway     *net.UDPAddr         `json:"internetgateway" yaml:"internetgateway"`
-	Server              string               `json:"server" yaml:"server"`
-	Connected           bool                 `json:"connected" yaml:"connected"`
-	Address             net.IPNet            `json:"address" yaml:"address"`
-	Address6            net.IPNet            `json:"address6" yaml:"address6"`
-	PostUp              string               `json:"postup" yaml:"postup"`
-	PostDown            string               `json:"postdown" yaml:"postdown"`
-	Action              string               `json:"action" yaml:"action"`
-	LocalAddress        net.IPNet            `json:"localaddress" yaml:"localaddress"`
-	IsLocal             bool                 `json:"islocal" yaml:"islocal"`
-	IsEgressGateway     bool                 `json:"isegressgateway" yaml:"isegressgateway"`
-	IsIngressGateway    bool                 `json:"isingressgateway" yaml:"isingressgateway"`
-	DNSOn               bool                 `json:"dnson" yaml:"dnson"`
-	PersistentKeepalive time.Duration        `json:"persistentkeepalive" yaml:"persistentkeepalive"`
-	Peers               []wgtypes.PeerConfig `json:"peers" yaml:"peers"`
+	ID                  uuid.UUID     `json:"id" yaml:"id"`
+	HostID              uuid.UUID     `json:"hostid" yaml:"hostid"`
+	Name                string        `json:"name" yaml:"name"`
+	Network             string        `json:"network" yaml:"network"`
+	NetworkRange        net.IPNet     `json:"networkrange" yaml:"networkrange"`
+	NetworkRange6       net.IPNet     `json:"networkrange6" yaml:"networkrange6"`
+	InternetGateway     *net.UDPAddr  `json:"internetgateway" yaml:"internetgateway"`
+	Server              string        `json:"server" yaml:"server"`
+	Connected           bool          `json:"connected" yaml:"connected"`
+	Address             net.IPNet     `json:"address" yaml:"address"`
+	Address6            net.IPNet     `json:"address6" yaml:"address6"`
+	PostUp              string        `json:"postup" yaml:"postup"`
+	PostDown            string        `json:"postdown" yaml:"postdown"`
+	Action              string        `json:"action" yaml:"action"`
+	LocalAddress        net.IPNet     `json:"localaddress" yaml:"localaddress"`
+	IsLocal             bool          `json:"islocal" yaml:"islocal"`
+	IsEgressGateway     bool          `json:"isegressgateway" yaml:"isegressgateway"`
+	IsIngressGateway    bool          `json:"isingressgateway" yaml:"isingressgateway"`
+	DNSOn               bool          `json:"dnson" yaml:"dnson"`
+	PersistentKeepalive time.Duration `json:"persistentkeepalive" yaml:"persistentkeepalive"`
 }
 
 // Node - a model of a network node

+ 0 - 2
models/structs.go

@@ -206,7 +206,6 @@ type NodeGet struct {
 	Host         Host                 `json:"host" yaml:"host"`
 	Peers        []wgtypes.PeerConfig `json:"peers" bson:"peers" yaml:"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
@@ -214,7 +213,6 @@ 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"`
-	PeerIDs      PeerMap      `json:"peerids,omitempty" bson:"peerids,omitempty" yaml:"peerids,omitempty"`
 }
 
 // ServerConfig - struct for dealing with the server information for a netclient

+ 0 - 24
mq/publishers.go

@@ -74,31 +74,7 @@ func PublishSingleHostUpdate(host *models.Host) error {
 
 // PublishPeerUpdate --- publishes a peer update to all the peers of a node
 func PublishExtPeerUpdate(node *models.Node) error {
-	host, err := logic.GetHost(node.HostID.String())
-	if err != nil {
-		return nil
-	}
-	if !servercfg.IsMessageQueueBackend() {
-		return nil
-	}
-	peerUpdate, err := logic.GetPeerUpdate(node, host)
-	if err != nil {
-		return err
-	}
-	data, err := json.Marshal(&peerUpdate)
-	if err != nil {
-		return err
-	}
-	if host.ProxyEnabled {
-		proxyUpdate, err := logic.GetPeersForProxy(node, false)
-		if err == nil {
-			peerUpdate.ProxyUpdate = proxyUpdate
-		}
-	}
 
-	if err = publish(host, fmt.Sprintf("peers/%s/%s", node.Network, node.ID), data); err != nil {
-		return err
-	}
 	go PublishPeerUpdate(node.Network, false)
 	return nil
 }