From 01d28e648365b4bffdc06dc039dc954781fd95bf Mon Sep 17 00:00:00 2001 From: Abhishek Kondur Date: Tue, 10 Jan 2023 18:54:52 +0530 Subject: [PATCH] fetch related hosts on relay creation to enable proxy --- controllers/relay.go | 32 ++++++++------------------------ logic/hosts.go | 23 +++++++++++++++++++++++ logic/relay.go | 3 +++ 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/controllers/relay.go b/controllers/relay.go index 07ff4a83..548aa4fb 100644 --- a/controllers/relay.go +++ b/controllers/relay.go @@ -124,23 +124,17 @@ func createHostRelay(w http.ResponseWriter, r *http.Request) { } logger.Log(1, r.Header.Get("user"), "created relay on host", relay.HostID) - // for _, relayedHost := range relayedHosts { - - // err = mq.PublishSingleHostUpdate(&relayedHost) - // if err != nil { - // logger.Log(1, "error sending update to relayed host ", relayedHost.ID.String(), ": ", err.Error()) - // } - // } - // // publish host update for relayhost - // err = mq.PublishSingleHostUpdate(relayHost) - // if err != nil { - // logger.Log(1, "error sending update to relay host ", relayHost.ID.String(), ": ", err.Error()) - // } - go func() { + go func(relayHostID string) { + relatedhosts := logic.GetRelatedHosts(relayHostID) + for _, relatedHost := range relatedhosts { + relatedHost.ProxyEnabled = true + logic.UpsertHost(&relatedHost) + } if err := mq.PublishPeerUpdate(); err != nil { logger.Log(0, "fail to publish peer update: ", err.Error()) } - }() + + }(relay.HostID) apiHostData := relayHost.ConvertNMHostToAPI() w.WriteHeader(http.StatusOK) @@ -169,16 +163,6 @@ func deleteHostRelay(w http.ResponseWriter, r *http.Request) { return } logger.Log(1, r.Header.Get("user"), "deleted relay host", hostid) - // for _, relayedHost := range relayedHosts { - // err = mq.PublishSingleHostUpdate(&relayedHost) - // if err != nil { - // logger.Log(1, "error sending update to relayed host ", relayedHost.ID.String(), ": ", err.Error()) - // } - // } - // err = mq.PublishSingleHostUpdate(relayHost) - // if err != nil { - // logger.Log(1, "error sending update to relayed host ", relayHost.ID.String(), ": ", err.Error()) - // } go func() { if err := mq.PublishPeerUpdate(); err != nil { logger.Log(0, "fail to publish peer update: ", err.Error()) diff --git a/logic/hosts.go b/logic/hosts.go index 0dfeedec..965a2548 100644 --- a/logic/hosts.go +++ b/logic/hosts.go @@ -295,3 +295,26 @@ func GetHostNetworks(hostID string) []string { } return nets } + +// GetRelatedHosts - fetches related hosts of a given host +func GetRelatedHosts(hostID string) []models.Host { + relatedHosts := []models.Host{} + networks := GetHostNetworks(hostID) + networkMap := make(map[string]struct{}) + for _, network := range networks { + networkMap[network] = struct{}{} + } + hosts, err := GetAllHosts() + if err == nil { + for _, host := range hosts { + networks := GetHostNetworks(host.ID.String()) + for _, network := range networks { + if _, ok := networkMap[network]; ok { + relatedHosts = append(relatedHosts, host) + break + } + } + } + } + return relatedHosts +} diff --git a/logic/relay.go b/logic/relay.go index 4cdbd7ab..a3cd5028 100644 --- a/logic/relay.go +++ b/logic/relay.go @@ -48,6 +48,7 @@ func CreateRelay(relay models.RelayRequest) ([]models.Node, models.Node, error) return returnnodes, node, nil } +// CreateHostRelay - creates a host relay func CreateHostRelay(relay models.HostRelayRequest) (relayHost *models.Host, relayedHosts []models.Host, err error) { relayHost, err = GetHost(relay.HostID) @@ -69,6 +70,7 @@ func CreateHostRelay(relay models.HostRelayRequest) (relayHost *models.Host, rel return } +// SetRelayedHosts - updates the relayed hosts status func SetRelayedHosts(setRelayed bool, relayHostID string, relayedHostIDs []string) []models.Host { var relayedHosts []models.Host for _, relayedHostID := range relayedHostIDs { @@ -133,6 +135,7 @@ func GetRelayedNodes(relayNode *models.Node) ([]models.Node, error) { return returnnodes, nil } +// GetRelayedHosts - gets the relayed hosts of a relay host func GetRelayedHosts(relayHost *models.Host) []models.Host { relayedHosts := []models.Host{}