From d69d0ed2e281e427ad3d4e4849f252711cf36602 Mon Sep 17 00:00:00 2001 From: abhishek9686 Date: Sat, 2 Mar 2024 12:42:04 +0700 Subject: [PATCH 1/2] validate relay req for inet gws --- controllers/node.go | 11 +++++++++++ logic/relay.go | 4 ++++ pro/initialize.go | 1 + pro/logic/relays.go | 3 +++ 4 files changed, 19 insertions(+) diff --git a/controllers/node.go b/controllers/node.go index d0a77ce75..2ff0456c9 100644 --- a/controllers/node.go +++ b/controllers/node.go @@ -645,6 +645,17 @@ func updateNode(w http.ResponseWriter, r *http.Request) { } relayUpdate := logic.RelayUpdates(¤tNode, newNode) + if relayUpdate && newNode.IsRelay { + err = logic.ValidateRelay(models.RelayRequest{ + NodeID: newNode.ID.String(), + NetID: newNode.Network, + RelayedNodes: newNode.RelayedNodes, + }) + if err != nil { + logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest")) + return + } + } _, err = logic.GetHost(newNode.HostID.String()) if err != nil { logger.Log(0, r.Header.Get("user"), diff --git a/logic/relay.go b/logic/relay.go index 181cf5114..225c77dc8 100644 --- a/logic/relay.go +++ b/logic/relay.go @@ -28,3 +28,7 @@ var SetRelayedNodes = func(setRelayed bool, relay string, relayed []string) []mo var RelayUpdates = func(currentNode, newNode *models.Node) bool { return false } + +var ValidateRelay = func(relay models.RelayRequest) error { + return nil +} diff --git a/pro/initialize.go b/pro/initialize.go index 2c06d3bb6..13e2bfaa6 100644 --- a/pro/initialize.go +++ b/pro/initialize.go @@ -101,6 +101,7 @@ func InitPro() { logic.UpdateRelayed = proLogic.UpdateRelayed logic.SetRelayedNodes = proLogic.SetRelayedNodes logic.RelayUpdates = proLogic.RelayUpdates + logic.ValidateRelay = proLogic.ValidateRelay logic.GetTrialEndDate = getTrialEndDate logic.SetDefaultGw = proLogic.SetDefaultGw logic.SetDefaultGwForRelayedUpdate = proLogic.SetDefaultGwForRelayedUpdate diff --git a/pro/logic/relays.go b/pro/logic/relays.go index 09e56dc0b..6be0c8a78 100644 --- a/pro/logic/relays.go +++ b/pro/logic/relays.go @@ -119,6 +119,9 @@ func ValidateRelay(relay models.RelayRequest) error { if relayedNode.IsIngressGateway { return errors.New("cannot relay an ingress gateway (" + relayedNodeID + ")") } + if relayedNode.IsInternetGateway { + return errors.New("cannot relay an internet gateway (" + relayedNodeID + ")") + } } return err } From 8ac864fec88676313783b5dda5b4eb65217f3a0c Mon Sep 17 00:00:00 2001 From: abhishek9686 Date: Mon, 4 Mar 2024 13:39:33 +0700 Subject: [PATCH 2/2] add update check for validate relay --- controllers/node.go | 2 +- logic/relay.go | 2 +- pro/logic/relays.go | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/controllers/node.go b/controllers/node.go index 2ff0456c9..2c2ce0431 100644 --- a/controllers/node.go +++ b/controllers/node.go @@ -650,7 +650,7 @@ func updateNode(w http.ResponseWriter, r *http.Request) { NodeID: newNode.ID.String(), NetID: newNode.Network, RelayedNodes: newNode.RelayedNodes, - }) + }, true) if err != nil { logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest")) return diff --git a/logic/relay.go b/logic/relay.go index 225c77dc8..bd3c80bb9 100644 --- a/logic/relay.go +++ b/logic/relay.go @@ -29,6 +29,6 @@ var RelayUpdates = func(currentNode, newNode *models.Node) bool { return false } -var ValidateRelay = func(relay models.RelayRequest) error { +var ValidateRelay = func(relay models.RelayRequest, update bool) error { return nil } diff --git a/pro/logic/relays.go b/pro/logic/relays.go index 6be0c8a78..6c06f8c0f 100644 --- a/pro/logic/relays.go +++ b/pro/logic/relays.go @@ -44,7 +44,7 @@ func CreateRelay(relay models.RelayRequest) ([]models.Node, models.Node, error) if host.OS != "linux" { return returnnodes, models.Node{}, fmt.Errorf("only linux machines can be relay nodes") } - err = ValidateRelay(relay) + err = ValidateRelay(relay, false) if err != nil { return returnnodes, models.Node{}, err } @@ -101,14 +101,14 @@ func SetRelayedNodes(setRelayed bool, relay string, relayed []string) []models.N // } // ValidateRelay - checks if relay is valid -func ValidateRelay(relay models.RelayRequest) error { +func ValidateRelay(relay models.RelayRequest, update bool) error { var err error node, err := logic.GetNodeByID(relay.NodeID) if err != nil { return err } - if node.IsRelay { + if !update && node.IsRelay { return errors.New("node is already acting as a relay") } for _, relayedNodeID := range relay.RelayedNodes {