From 9d230d35407e08f7cc400b2c3c9c585127c75199 Mon Sep 17 00:00:00 2001 From: 0xdcarns Date: Thu, 6 Apr 2023 12:27:33 -0400 Subject: [PATCH] initial commit --- controllers/node.go | 14 +++++++++++++- logic/gateway.go | 22 +++++++++++++++------- logic/peers.go | 19 +++++++++++-------- mq/publishers.go | 6 +++--- 4 files changed, 42 insertions(+), 19 deletions(-) diff --git a/controllers/node.go b/controllers/node.go index 6cfc5e3c1..daf8993e6 100644 --- a/controllers/node.go +++ b/controllers/node.go @@ -564,7 +564,7 @@ func deleteIngressGateway(w http.ResponseWriter, r *http.Request) { var params = mux.Vars(r) nodeid := params["nodeid"] netid := params["network"] - node, wasFailover, err := logic.DeleteIngressGateway(netid, nodeid) + node, wasFailover, removedClients, err := logic.DeleteIngressGateway(netid, nodeid) if err != nil { logger.Log(0, r.Header.Get("user"), fmt.Sprintf("failed to delete ingress gateway on node [%s] on network [%s]: %v", @@ -584,6 +584,18 @@ func deleteIngressGateway(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(apiNode) + if len(removedClients) > 0 { + host, err := logic.GetHost(node.HostID.String()) + if err == nil { + go mq.PublishSingleHostPeerUpdate( + context.Background(), + host, + nil, + removedClients[:], + ) + } + } + runUpdates(&node, true) } diff --git a/logic/gateway.go b/logic/gateway.go index 5e50311bc..2637f4f1b 100644 --- a/logic/gateway.go +++ b/logic/gateway.go @@ -134,10 +134,11 @@ func CreateIngressGateway(netid string, nodeid string, failover bool) (models.No } // DeleteIngressGateway - deletes an ingress gateway -func DeleteIngressGateway(networkName string, nodeid string) (models.Node, bool, error) { +func DeleteIngressGateway(networkName string, nodeid string) (models.Node, bool, []models.ExtClient, error) { + removedClients := []models.ExtClient{} node, err := GetNodeByID(nodeid) if err != nil { - return models.Node{}, false, err + return models.Node{}, false, removedClients, err } //host, err := GetHost(node.ID.String()) //if err != nil { @@ -145,11 +146,18 @@ func DeleteIngressGateway(networkName string, nodeid string) (models.Node, bool, //} //network, err := GetParentNetwork(networkName) if err != nil { - return models.Node{}, false, err + return models.Node{}, false, removedClients, err } + clients, err := GetExtClientsByID(nodeid, networkName) + if err != nil { + return models.Node{}, false, removedClients, err + } + + removedClients = clients + // delete ext clients belonging to ingress gateway if err = DeleteGatewayExtClients(node.ID.String(), networkName); err != nil { - return models.Node{}, false, err + return models.Node{}, false, removedClients, err } logger.Log(3, "deleting ingress gateway") wasFailover := node.Failover @@ -169,14 +177,14 @@ func DeleteIngressGateway(networkName string, nodeid string) (models.Node, bool, data, err := json.Marshal(&node) if err != nil { - return models.Node{}, false, err + return models.Node{}, false, removedClients, err } err = database.Insert(node.ID.String(), string(data), database.NODES_TABLE_NAME) if err != nil { - return models.Node{}, wasFailover, err + return models.Node{}, wasFailover, removedClients, err } err = SetNetworkNodesLastModified(networkName) - return node, wasFailover, err + return node, wasFailover, removedClients, err } // DeleteGatewayExtClients - deletes ext clients based on gateway (mac) of ingress node and network diff --git a/logic/peers.go b/logic/peers.go index f004a46e0..afaa6c3e1 100644 --- a/logic/peers.go +++ b/logic/peers.go @@ -140,7 +140,7 @@ func ResetPeerUpdateContext() { } // GetPeerUpdateForHost - gets the consolidated peer update for the host from all networks -func GetPeerUpdateForHost(ctx context.Context, network string, host *models.Host, deletedNode *models.Node, deletedClient *models.ExtClient) (models.HostPeerUpdate, error) { +func GetPeerUpdateForHost(ctx context.Context, network string, host *models.Host, deletedNode *models.Node, deletedClients []models.ExtClient) (models.HostPeerUpdate, error) { if host == nil { return models.HostPeerUpdate{}, errors.New("host is nil") } @@ -408,13 +408,16 @@ func GetPeerUpdateForHost(ctx context.Context, network string, host *models.Host hostPeerUpdate.NodePeers[i] = peer } - if deletedClient != nil { - key, err := wgtypes.ParseKey(deletedClient.PublicKey) - if err == nil { - hostPeerUpdate.Peers = append(hostPeerUpdate.Peers, wgtypes.PeerConfig{ - PublicKey: key, - Remove: true, - }) + if len(deletedClients) > 0 { + for i := range deletedClients { + deletedClient := deletedClients[i] + key, err := wgtypes.ParseKey(deletedClient.PublicKey) + if err == nil { + hostPeerUpdate.Peers = append(hostPeerUpdate.Peers, wgtypes.PeerConfig{ + PublicKey: key, + Remove: true, + }) + } } } diff --git a/mq/publishers.go b/mq/publishers.go index c530071df..ec5eb0593 100644 --- a/mq/publishers.go +++ b/mq/publishers.go @@ -71,7 +71,7 @@ func PublishDeletedClientPeerUpdate(delClient *models.ExtClient) error { logic.ResetPeerUpdateContext() for _, host := range hosts { host := host - if err = PublishSingleHostPeerUpdate(logic.PeerUpdateCtx, &host, nil, delClient); err != nil { + if err = PublishSingleHostPeerUpdate(logic.PeerUpdateCtx, &host, nil, []models.ExtClient{*delClient}); err != nil { logger.Log(1, "failed to publish peer update to host", host.ID.String(), ": ", err.Error()) } } @@ -79,9 +79,9 @@ func PublishDeletedClientPeerUpdate(delClient *models.ExtClient) error { } // PublishSingleHostPeerUpdate --- determines and publishes a peer update to one host -func PublishSingleHostPeerUpdate(ctx context.Context, host *models.Host, deletedNode *models.Node, deletedClient *models.ExtClient) error { +func PublishSingleHostPeerUpdate(ctx context.Context, host *models.Host, deletedNode *models.Node, deletedClients []models.ExtClient) error { - peerUpdate, err := logic.GetPeerUpdateForHost(ctx, "", host, deletedNode, deletedClient) + peerUpdate, err := logic.GetPeerUpdateForHost(ctx, "", host, deletedNode, deletedClients) if err != nil { return err }