Browse Source

send fw updates

Abhishek Kondur 2 năm trước cách đây
mục cha
commit
1de551cc49
4 tập tin đã thay đổi với 59 bổ sung21 xóa
  1. 27 5
      controllers/node.go
  2. 13 3
      logic/peers.go
  3. 8 10
      models/mqtt.go
  4. 11 3
      mq/publishers.go

+ 27 - 5
controllers/node.go

@@ -461,7 +461,17 @@ func createEgressGateway(w http.ResponseWriter, r *http.Request) {
 	w.WriteHeader(http.StatusOK)
 	json.NewEncoder(w).Encode(apiNode)
 	go func() {
-		mq.PublishPeerUpdate()
+		host, err := logic.GetHost(node.HostID.String())
+		if err != nil {
+			logger.Log(0, "failed to get egress host: ", err.Error())
+		}
+		f, err := logic.GetFwUpdate(host)
+		if err != nil {
+			logger.Log(0, "failed to get egreess host: ", err.Error())
+			return
+		}
+		mq.PublishFwUpdate(host, &f)
+
 	}()
 	runUpdates(&node, true)
 }
@@ -497,7 +507,18 @@ func deleteEgressGateway(w http.ResponseWriter, r *http.Request) {
 	w.WriteHeader(http.StatusOK)
 	json.NewEncoder(w).Encode(apiNode)
 	go func() {
-		mq.PublishPeerUpdate()
+
+		host, err := logic.GetHost(node.HostID.String())
+		if err != nil {
+			logger.Log(0, "failed to get egress host: ", err.Error())
+		}
+		f, err := logic.GetFwUpdate(host)
+		if err != nil {
+			logger.Log(0, "failed to get egreess host: ", err.Error())
+			return
+		}
+		mq.PublishFwUpdate(host, &f)
+
 	}()
 	runUpdates(&node, true)
 }
@@ -592,9 +613,10 @@ func deleteIngressGateway(w http.ResponseWriter, r *http.Request) {
 				removedClients[:],
 			)
 		}
-		mq.PublishFwUpdate(host, &models.FwAction{
-			Action: models.FwIngressDel,
-		})
+		// TODO: FW
+		// mq.PublishFwUpdate(host, &models.FwAction{
+		// 	Action: models.FwIngressDel,
+		// })
 	}
 
 	runUpdates(&node, true)

+ 13 - 3
logic/peers.go

@@ -257,6 +257,7 @@ func GetPeerUpdateForHost(ctx context.Context, network string, host *models.Host
 										IP:   net.ParseIP(extPeerIdAndAddr.Address),
 										Mask: GetCIDRMaskFromAddr(extPeerIdAndAddr.Address),
 									},
+									Network: node.Network,
 									PeerKey: extPeerIdAndAddr.ID,
 									Allow:   true,
 									ID:      extPeerIdAndAddr.ID,
@@ -273,6 +274,7 @@ func GetPeerUpdateForHost(ctx context.Context, network string, host *models.Host
 							IP:   net.ParseIP(peer.PrimaryAddress()),
 							Mask: GetCIDRMaskFromAddr(peer.PrimaryAddress()),
 						},
+						Network: peer.Network,
 						PeerKey: peerHost.PublicKey.String(),
 						Allow:   true,
 						ID:      peer.ID.String(),
@@ -339,6 +341,7 @@ func GetPeerUpdateForHost(ctx context.Context, network string, host *models.Host
 							IP:   net.ParseIP(extPeerIdAndAddr.Address),
 							Mask: GetCIDRMaskFromAddr(extPeerIdAndAddr.Address),
 						},
+						Network: node.Network,
 						PeerKey: extPeerIdAndAddr.ID,
 						Allow:   true,
 						ID:      extPeerIdAndAddr.ID,
@@ -361,7 +364,8 @@ func GetPeerUpdateForHost(ctx context.Context, network string, host *models.Host
 							IP:   net.ParseIP(node.PrimaryAddress()),
 							Mask: GetCIDRMaskFromAddr(node.PrimaryAddress()),
 						},
-						Network: node.PrimaryNetworkRange(),
+						Network:     node.PrimaryNetworkRange(),
+						NetworkName: node.Network,
 						ExtPeerAddr: net.IPNet{
 							IP:   net.ParseIP(extPeerIdAndAddr.Address),
 							Mask: GetCIDRMaskFromAddr(extPeerIdAndAddr.Address),
@@ -427,7 +431,7 @@ func GetPeerUpdateForHost(ctx context.Context, network string, host *models.Host
 
 func GetFwUpdate(host *models.Host) (models.FwAction, error) {
 	fwUpdate := models.FwAction{
-		Action: models.FwAllUpdate,
+		Action: models.FwUpdate,
 		IngressInfo: models.IngressInfo{
 			ExtPeers: make(map[string]models.ExtClientInfo),
 		},
@@ -474,6 +478,7 @@ func GetFwUpdate(host *models.Host) (models.FwAction, error) {
 									IP:   net.ParseIP(extPeerIdAndAddr.Address),
 									Mask: GetCIDRMaskFromAddr(extPeerIdAndAddr.Address),
 								},
+								Network: peer.Network,
 								PeerKey: extPeerIdAndAddr.ID,
 								Allow:   true,
 								ID:      extPeerIdAndAddr.ID,
@@ -490,6 +495,7 @@ func GetFwUpdate(host *models.Host) (models.FwAction, error) {
 						IP:   net.ParseIP(peer.PrimaryAddress()),
 						Mask: GetCIDRMaskFromAddr(peer.PrimaryAddress()),
 					},
+					Network: peer.Network,
 					PeerKey: peerHost.PublicKey.String(),
 					Allow:   true,
 					ID:      peer.ID.String(),
@@ -498,6 +504,7 @@ func GetFwUpdate(host *models.Host) (models.FwAction, error) {
 		}
 		var extPeerIDAndAddrs []models.IDandAddr
 		if node.IsIngressGateway {
+			fwUpdate.IsIngressGw = true
 			_, extPeerIDAndAddrs, err = GetExtPeers(&node)
 			if err == nil {
 				for _, extPeerIdAndAddr := range extPeerIDAndAddrs {
@@ -507,6 +514,7 @@ func GetFwUpdate(host *models.Host) (models.FwAction, error) {
 							IP:   net.ParseIP(extPeerIdAndAddr.Address),
 							Mask: GetCIDRMaskFromAddr(extPeerIdAndAddr.Address),
 						},
+						Network: node.Network,
 						PeerKey: extPeerIdAndAddr.ID,
 						Allow:   true,
 						ID:      extPeerIdAndAddr.ID,
@@ -521,7 +529,8 @@ func GetFwUpdate(host *models.Host) (models.FwAction, error) {
 							IP:   net.ParseIP(node.PrimaryAddress()),
 							Mask: GetCIDRMaskFromAddr(node.PrimaryAddress()),
 						},
-						Network: node.PrimaryNetworkRange(),
+						Network:     node.PrimaryNetworkRange(),
+						NetworkName: node.Network,
 						ExtPeerAddr: net.IPNet{
 							IP:   net.ParseIP(extPeerIdAndAddr.Address),
 							Mask: GetCIDRMaskFromAddr(extPeerIdAndAddr.Address),
@@ -535,6 +544,7 @@ func GetFwUpdate(host *models.Host) (models.FwAction, error) {
 			}
 		}
 		if node.IsEgressGateway {
+			fwUpdate.IsEgressGw = true
 			fwUpdate.EgressInfo[node.ID.String()] = models.EgressInfo{
 				EgressID: node.ID.String(),
 				Network:  node.PrimaryNetworkRange(),

+ 8 - 10
models/mqtt.go

@@ -41,6 +41,7 @@ type EgressInfo struct {
 type PeerRouteInfo struct {
 	PeerAddr net.IPNet `json:"peer_addr" yaml:"peer_addr"`
 	PeerKey  string    `json:"peer_key" yaml:"peer_key"`
+	Network  string    `json:"network" yaml:"network"`
 	Allow    bool      `json:"allow" yaml:"allow"`
 	ID       string    `json:"id,omitempty" yaml:"id,omitempty"`
 }
@@ -49,6 +50,7 @@ type PeerRouteInfo struct {
 type ExtClientInfo struct {
 	IngGwAddr   net.IPNet                `json:"ingress_gw_addr" yaml:"ingress_gw_addr"`
 	Network     net.IPNet                `json:"network" yaml:"network"`
+	NetworkName string                   `json:"network_name"`
 	Masquerade  bool                     `json:"masquerade" yaml:"masquerade"`
 	ExtPeerAddr net.IPNet                `json:"ext_peer_addr" yaml:"ext_peer_addr"`
 	ExtPeerKey  string                   `json:"ext_peer_key" yaml:"ext_peer_key"`
@@ -82,17 +84,13 @@ type PeerAction struct {
 type FwActionType string
 
 type FwAction struct {
-	Action        FwActionType          `json:"action"`
-	IsIngressGw   bool                  `json:"is_ingress_gw"`
-	IsEgressGw    bool                  `json:"is_egress_gw"`
-	ExtClientInfo ExtClientInfo         `json:"ext_client_info"`
-	IngressInfo   IngressInfo           `json:"ingress_info"`
-	EgressInfo    map[string]EgressInfo `json:"egress_info"`
+	Action      FwActionType          `json:"action"`
+	IsIngressGw bool                  `json:"is_ingress_gw"`
+	IsEgressGw  bool                  `json:"is_egress_gw"`
+	IngressInfo IngressInfo           `json:"ingress_info"`
+	EgressInfo  map[string]EgressInfo `json:"egress_info"`
 }
 
 const (
-	FwIngressUpdate FwActionType = "FW_INGRESS_UPDATE"
-	FwAllUpdate     FwActionType = "FW_ALL_UPDATE"
-	FwIngressDel    FwActionType = "FW_INGRESS_DELETE"
-	FwEgressDel     FwActionType = "FW_EGRESS_DELETE"
+	FwUpdate FwActionType = "FW_UPDATE"
 )

+ 11 - 3
mq/publishers.go

@@ -213,6 +213,9 @@ func BroadcastDelPeer(host *models.Host, network string) error {
 		peerHost, err := logic.GetHost(nodeI.HostID.String())
 		if err == nil {
 			publish(peerHost, fmt.Sprintf("peer/host/%s/%s", peerHost.ID.String(), servercfg.GetServer()), data)
+			if nodeI.IsIngressGateway {
+				// TODO: FW
+			}
 		}
 	}
 	return nil
@@ -294,7 +297,6 @@ func BroadcastExtClient(ingressHost *models.Host, ingressNode *models.Node) erro
 	go FlushNetworkPeersToHost(ingressHost, ingressNode, nodes)
 	// broadcast to update ingress peer to other hosts
 	go BroadcastAddOrUpdatePeer(ingressHost, ingressNode, true)
-	// TODO - send fw update
 	return nil
 }
 
@@ -319,8 +321,14 @@ func BroadcastDelExtClient(ingressHost *models.Host, ingressNode *models.Node, e
 	if err != nil {
 		return err
 	}
-	publish(ingressHost, fmt.Sprintf("peer/host/%s/%s", ingressHost.ID.String(), servercfg.GetServer()), data)
-	return nil
+	err = publish(ingressHost, fmt.Sprintf("peer/host/%s/%s", ingressHost.ID.String(), servercfg.GetServer()), data)
+	if err != nil {
+		return err
+	}
+	return PublishFwUpdate(ingressHost, &models.FwAction{
+		Action:  models.FwIngressDelExtClient,
+		PeerKey: extclient.PublicKey,
+	})
 }
 
 // NodeUpdate -- publishes a node update