Procházet zdrojové kódy

Merge remote-tracking branch 'origin/NET-10' into NET-137

Matthew R Kasun před 2 roky
rodič
revize
804581a6db
4 změnil soubory, kde provedl 81 přidání a 3 odebrání
  1. 2 0
      auth/host_session.go
  2. 4 3
      logic/peers.go
  3. 13 0
      models/mqtt.go
  4. 62 0
      mq/publishers.go

+ 2 - 0
auth/host_session.go

@@ -237,6 +237,7 @@ func CheckNetRegAndHostUpdate(networks []string, h *models.Host) {
 				Host:   *h,
 				Node:   *newNode,
 			})
+			mq.PublishPeerAction(h)
 		}
 	}
 	if servercfg.IsMessageQueueBackend() {
@@ -247,6 +248,7 @@ func CheckNetRegAndHostUpdate(networks []string, h *models.Host) {
 		if err := mq.PublishPeerUpdate(); err != nil {
 			logger.Log(0, "failed to publish peer update during registration -", err.Error())
 		}
+
 	}
 }
 

+ 4 - 3
logic/peers.go

@@ -385,10 +385,11 @@ func GetPeerUpdateForHost(ctx context.Context, network string, host *models.Host
 // GetPeerListenPort - given a host, retrieve it's appropriate listening port
 func GetPeerListenPort(host *models.Host) int {
 	peerPort := host.ListenPort
+	if host.PublicListenPort != 0 {
+		peerPort = host.PublicListenPort
+	}
 	if host.ProxyEnabled {
-		if host.PublicListenPort != 0 {
-			peerPort = host.PublicListenPort
-		} else if host.ProxyListenPort != 0 {
+		if host.ProxyListenPort != 0 {
 			peerPort = host.ProxyListenPort
 		}
 	}

+ 13 - 0
models/mqtt.go

@@ -60,3 +60,16 @@ type KeyUpdate struct {
 	Network   string `json:"network" bson:"network"`
 	Interface string `json:"interface" bson:"interface"`
 }
+
+type PeerActionType string
+
+const (
+	AddPeer    PeerActionType = "ADD_PEER"
+	UpdatePeer PeerActionType = "UPDATE_PEER"
+	RemovePeer PeerActionType = "REMOVE_PEER"
+)
+
+type PeerAction struct {
+	Action PeerActionType     `json:"action"`
+	Peer   wgtypes.PeerConfig `json:"peer"`
+}

+ 62 - 0
mq/publishers.go

@@ -110,6 +110,68 @@ func PublishSingleHostPeerUpdate(ctx context.Context, host *models.Host, deleted
 	return publish(host, fmt.Sprintf("peers/host/%s/%s", host.ID.String(), servercfg.GetServer()), data)
 }
 
+func BroadCastDelPeer(host *models.Host, network string) error {
+	//relatedHosts := logic.GetRelatedHosts(host.ID.String())
+	nodes, err := logic.GetNetworkNodes(network)
+	if err != nil {
+		return err
+	}
+	p := models.PeerAction{
+		Action: models.RemovePeer,
+		Peer: wgtypes.PeerConfig{
+			PublicKey: host.PublicKey,
+			Remove:    true,
+		},
+	}
+	data, err := json.Marshal(p)
+	if err != nil {
+		return err
+	}
+	for _, nodeI := range nodes {
+		peerHost, err := logic.GetHost(nodeI.HostID.String())
+		if err == nil {
+			publish(peerHost, fmt.Sprintf("peer/host/%s/%s", host.ID.String(), servercfg.GetServer()), data)
+		}
+	}
+	return nil
+}
+
+func BroadCastAddPeer(host *models.Host, node *models.Node, network string, update bool) error {
+	nodes, err := logic.GetNetworkNodes(network)
+	if err != nil {
+		return err
+	}
+
+	p := models.PeerAction{
+		Action: models.AddPeer,
+		Peer: wgtypes.PeerConfig{
+			PublicKey: host.PublicKey,
+			Endpoint: &net.UDPAddr{
+				IP:   host.EndpointIP,
+				Port: logic.GetPeerListenPort(host),
+			},
+			PersistentKeepaliveInterval: &node.PersistentKeepalive,
+			ReplaceAllowedIPs:           true,
+		},
+	}
+	if update {
+		p.Action = models.UpdatePeer
+	}
+	for _, nodeI := range nodes {
+		// update allowed ips, according to the peer node
+		p.Peer.AllowedIPs = logic.GetAllowedIPs(&nodeI, node, nil)
+		data, err := json.Marshal(p)
+		if err != nil {
+			continue
+		}
+		peerHost, err := logic.GetHost(nodeI.HostID.String())
+		if err == nil {
+			publish(peerHost, fmt.Sprintf("peer/host/%s/%s", host.ID.String(), servercfg.GetServer()), data)
+		}
+	}
+	return nil
+}
+
 // NodeUpdate -- publishes a node update
 func NodeUpdate(node *models.Node) error {
 	host, err := logic.GetHost(node.HostID.String())