2022-07-29 20:37:09 +02:00
|
|
|
package http
|
2022-05-21 15:21:39 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-06-14 10:32:54 +02:00
|
|
|
"github.com/gorilla/mux"
|
2022-05-21 15:21:39 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server"
|
2022-06-14 10:32:54 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/http/api"
|
2022-11-11 20:36:45 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/http/util"
|
2022-05-21 15:21:39 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/jwtclaims"
|
2022-11-11 20:36:45 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server/status"
|
2022-05-21 15:21:39 +02:00
|
|
|
"github.com/rs/xid"
|
2022-06-14 10:32:54 +02:00
|
|
|
"net/http"
|
2022-05-21 15:21:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Rules is a handler that returns rules of the account
|
|
|
|
type Rules struct {
|
|
|
|
jwtExtractor jwtclaims.ClaimsExtractor
|
|
|
|
accountManager server.AccountManager
|
|
|
|
authAudience string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRules(accountManager server.AccountManager, authAudience string) *Rules {
|
|
|
|
return &Rules{
|
|
|
|
accountManager: accountManager,
|
|
|
|
authAudience: authAudience,
|
|
|
|
jwtExtractor: *jwtclaims.NewClaimsExtractor(nil),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAllRulesHandler list for the account
|
|
|
|
func (h *Rules) GetAllRulesHandler(w http.ResponseWriter, r *http.Request) {
|
2022-11-11 20:36:45 +01:00
|
|
|
claims := h.jwtExtractor.ExtractClaimsFromRequestContext(r, h.authAudience)
|
|
|
|
account, user, err := h.accountManager.GetAccountFromToken(claims)
|
2022-05-21 15:21:39 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2022-05-21 15:21:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-05 10:24:50 +01:00
|
|
|
accountRules, err := h.accountManager.ListRules(account.Id, user.Id)
|
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2022-11-05 10:24:50 +01:00
|
|
|
return
|
|
|
|
}
|
2022-06-14 10:32:54 +02:00
|
|
|
rules := []*api.Rule{}
|
2022-11-05 10:24:50 +01:00
|
|
|
for _, r := range accountRules {
|
2022-05-21 15:21:39 +02:00
|
|
|
rules = append(rules, toRuleResponse(account, r))
|
|
|
|
}
|
|
|
|
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteJSONObject(w, rules)
|
2022-05-21 15:21:39 +02:00
|
|
|
}
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
// UpdateRuleHandler handles update to a rule identified by a given ID
|
|
|
|
func (h *Rules) UpdateRuleHandler(w http.ResponseWriter, r *http.Request) {
|
2022-11-11 20:36:45 +01:00
|
|
|
claims := h.jwtExtractor.ExtractClaimsFromRequestContext(r, h.authAudience)
|
2023-01-02 15:11:32 +01:00
|
|
|
account, user, err := h.accountManager.GetAccountFromToken(claims)
|
2022-06-14 10:32:54 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
ruleID := vars["id"]
|
|
|
|
if len(ruleID) == 0 {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "invalid rule ID"), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := account.Rules[ruleID]
|
|
|
|
if !ok {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.NotFound, "couldn't find rule id %s", ruleID), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var req api.PutApiRulesIdJSONRequestBody
|
2022-11-11 20:36:45 +01:00
|
|
|
err = json.NewDecoder(r.Body).Decode(&req)
|
|
|
|
if err != nil {
|
|
|
|
util.WriteErrorResponse("couldn't parse JSON request", http.StatusBadRequest, w)
|
2022-06-14 10:32:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.Name == "" {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "rule name shouldn't be empty"), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var reqSources []string
|
|
|
|
if req.Sources != nil {
|
|
|
|
reqSources = *req.Sources
|
|
|
|
}
|
|
|
|
|
|
|
|
var reqDestinations []string
|
|
|
|
if req.Destinations != nil {
|
|
|
|
reqDestinations = *req.Destinations
|
|
|
|
}
|
|
|
|
|
|
|
|
rule := server.Rule{
|
|
|
|
ID: ruleID,
|
|
|
|
Name: req.Name,
|
|
|
|
Source: reqSources,
|
|
|
|
Destination: reqDestinations,
|
|
|
|
Disabled: req.Disabled,
|
|
|
|
Description: req.Description,
|
|
|
|
}
|
|
|
|
|
|
|
|
switch req.Flow {
|
|
|
|
case server.TrafficFlowBidirectString:
|
|
|
|
rule.Flow = server.TrafficFlowBidirect
|
|
|
|
default:
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "unknown flow type"), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-02 15:11:32 +01:00
|
|
|
err = h.accountManager.SaveRule(account.Id, user.Id, &rule)
|
2022-11-11 20:36:45 +01:00
|
|
|
if err != nil {
|
|
|
|
util.WriteError(err, w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := toRuleResponse(account, &rule)
|
|
|
|
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteJSONObject(w, &resp)
|
2022-06-14 10:32:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// PatchRuleHandler handles patch updates to a rule identified by a given ID
|
|
|
|
func (h *Rules) PatchRuleHandler(w http.ResponseWriter, r *http.Request) {
|
2022-11-11 20:36:45 +01:00
|
|
|
claims := h.jwtExtractor.ExtractClaimsFromRequestContext(r, h.authAudience)
|
|
|
|
account, _, err := h.accountManager.GetAccountFromToken(claims)
|
2022-06-14 10:32:54 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
ruleID := vars["id"]
|
|
|
|
if len(ruleID) == 0 {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "invalid rule ID"), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := account.Rules[ruleID]
|
|
|
|
if !ok {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.NotFound, "couldn't find rule ID %s", ruleID), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var req api.PatchApiRulesIdJSONRequestBody
|
2022-11-11 20:36:45 +01:00
|
|
|
err = json.NewDecoder(r.Body).Decode(&req)
|
|
|
|
if err != nil {
|
|
|
|
util.WriteErrorResponse("couldn't parse JSON request", http.StatusBadRequest, w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(req) == 0 {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "no patch instruction received"), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var operations []server.RuleUpdateOperation
|
|
|
|
|
|
|
|
for _, patch := range req {
|
|
|
|
switch patch.Path {
|
|
|
|
case api.RulePatchOperationPathName:
|
|
|
|
if patch.Op != api.RulePatchOperationOpReplace {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument,
|
|
|
|
"name field only accepts replace operation, got %s", patch.Op), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(patch.Value) == 0 || patch.Value[0] == "" {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "rule name shouldn't be empty"), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
operations = append(operations, server.RuleUpdateOperation{
|
|
|
|
Type: server.UpdateRuleName,
|
|
|
|
Values: patch.Value,
|
|
|
|
})
|
|
|
|
case api.RulePatchOperationPathDescription:
|
|
|
|
if patch.Op != api.RulePatchOperationOpReplace {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument,
|
|
|
|
"description field only accepts replace operation, got %s", patch.Op), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
operations = append(operations, server.RuleUpdateOperation{
|
|
|
|
Type: server.UpdateRuleDescription,
|
|
|
|
Values: patch.Value,
|
|
|
|
})
|
|
|
|
case api.RulePatchOperationPathFlow:
|
|
|
|
if patch.Op != api.RulePatchOperationOpReplace {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument,
|
|
|
|
"flow field only accepts replace operation, got %s", patch.Op), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
operations = append(operations, server.RuleUpdateOperation{
|
|
|
|
Type: server.UpdateRuleFlow,
|
|
|
|
Values: patch.Value,
|
|
|
|
})
|
|
|
|
case api.RulePatchOperationPathDisabled:
|
|
|
|
if patch.Op != api.RulePatchOperationOpReplace {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument,
|
|
|
|
"disabled field only accepts replace operation, got %s", patch.Op), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
operations = append(operations, server.RuleUpdateOperation{
|
|
|
|
Type: server.UpdateRuleStatus,
|
|
|
|
Values: patch.Value,
|
|
|
|
})
|
|
|
|
case api.RulePatchOperationPathSources:
|
|
|
|
switch patch.Op {
|
|
|
|
case api.RulePatchOperationOpReplace:
|
|
|
|
operations = append(operations, server.RuleUpdateOperation{
|
|
|
|
Type: server.UpdateSourceGroups,
|
|
|
|
Values: patch.Value,
|
|
|
|
})
|
|
|
|
case api.RulePatchOperationOpRemove:
|
|
|
|
operations = append(operations, server.RuleUpdateOperation{
|
|
|
|
Type: server.RemoveGroupsFromSource,
|
|
|
|
Values: patch.Value,
|
|
|
|
})
|
|
|
|
case api.RulePatchOperationOpAdd:
|
|
|
|
operations = append(operations, server.RuleUpdateOperation{
|
|
|
|
Type: server.InsertGroupsToSource,
|
|
|
|
Values: patch.Value,
|
|
|
|
})
|
|
|
|
default:
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument,
|
|
|
|
"invalid operation \"%s\" on Source field", patch.Op), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
case api.RulePatchOperationPathDestinations:
|
|
|
|
switch patch.Op {
|
|
|
|
case api.RulePatchOperationOpReplace:
|
|
|
|
operations = append(operations, server.RuleUpdateOperation{
|
|
|
|
Type: server.UpdateDestinationGroups,
|
|
|
|
Values: patch.Value,
|
|
|
|
})
|
|
|
|
case api.RulePatchOperationOpRemove:
|
|
|
|
operations = append(operations, server.RuleUpdateOperation{
|
|
|
|
Type: server.RemoveGroupsFromDestination,
|
|
|
|
Values: patch.Value,
|
|
|
|
})
|
|
|
|
case api.RulePatchOperationOpAdd:
|
|
|
|
operations = append(operations, server.RuleUpdateOperation{
|
|
|
|
Type: server.InsertGroupsToDestination,
|
|
|
|
Values: patch.Value,
|
|
|
|
})
|
|
|
|
default:
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument,
|
|
|
|
"invalid operation \"%s\" on Destination field", patch.Op), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
default:
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "invalid patch path"), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rule, err := h.accountManager.UpdateRule(account.Id, ruleID, operations)
|
|
|
|
|
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := toRuleResponse(account, rule)
|
|
|
|
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteJSONObject(w, &resp)
|
2022-06-14 10:32:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateRuleHandler handles rule creation request
|
|
|
|
func (h *Rules) CreateRuleHandler(w http.ResponseWriter, r *http.Request) {
|
2022-11-11 20:36:45 +01:00
|
|
|
claims := h.jwtExtractor.ExtractClaimsFromRequestContext(r, h.authAudience)
|
2023-01-02 15:11:32 +01:00
|
|
|
account, user, err := h.accountManager.GetAccountFromToken(claims)
|
2022-05-21 15:21:39 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2022-05-21 15:21:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
var req api.PostApiRulesJSONRequestBody
|
2022-11-11 20:36:45 +01:00
|
|
|
err = json.NewDecoder(r.Body).Decode(&req)
|
|
|
|
if err != nil {
|
|
|
|
util.WriteErrorResponse("couldn't parse JSON request", http.StatusBadRequest, w)
|
2022-05-21 15:21:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
if req.Name == "" {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "rule name shouldn't be empty"), w)
|
2022-06-14 10:32:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var reqSources []string
|
|
|
|
if req.Sources != nil {
|
|
|
|
reqSources = *req.Sources
|
|
|
|
}
|
|
|
|
|
|
|
|
var reqDestinations []string
|
|
|
|
if req.Destinations != nil {
|
|
|
|
reqDestinations = *req.Destinations
|
2022-05-21 15:21:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rule := server.Rule{
|
2022-06-14 10:32:54 +02:00
|
|
|
ID: xid.New().String(),
|
2022-05-21 15:21:39 +02:00
|
|
|
Name: req.Name,
|
2022-06-14 10:32:54 +02:00
|
|
|
Source: reqSources,
|
|
|
|
Destination: reqDestinations,
|
|
|
|
Disabled: req.Disabled,
|
|
|
|
Description: req.Description,
|
2022-05-21 15:21:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch req.Flow {
|
2022-06-14 10:32:54 +02:00
|
|
|
case server.TrafficFlowBidirectString:
|
2022-05-21 15:21:39 +02:00
|
|
|
rule.Flow = server.TrafficFlowBidirect
|
|
|
|
default:
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "unknown flow type"), w)
|
2022-05-21 15:21:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-02 15:11:32 +01:00
|
|
|
err = h.accountManager.SaveRule(account.Id, user.Id, &rule)
|
2022-11-11 20:36:45 +01:00
|
|
|
if err != nil {
|
|
|
|
util.WriteError(err, w)
|
2022-05-21 15:21:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
resp := toRuleResponse(account, &rule)
|
|
|
|
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteJSONObject(w, &resp)
|
2022-05-21 15:21:39 +02:00
|
|
|
}
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
// DeleteRuleHandler handles rule deletion request
|
2022-05-21 15:21:39 +02:00
|
|
|
func (h *Rules) DeleteRuleHandler(w http.ResponseWriter, r *http.Request) {
|
2022-11-11 20:36:45 +01:00
|
|
|
claims := h.jwtExtractor.ExtractClaimsFromRequestContext(r, h.authAudience)
|
2023-01-02 15:11:32 +01:00
|
|
|
account, user, err := h.accountManager.GetAccountFromToken(claims)
|
2022-05-21 15:21:39 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2022-05-21 15:21:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
aID := account.Id
|
|
|
|
|
|
|
|
rID := mux.Vars(r)["id"]
|
|
|
|
if len(rID) == 0 {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "invalid rule ID"), w)
|
2022-05-21 15:21:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-02 15:11:32 +01:00
|
|
|
err = h.accountManager.DeleteRule(aID, rID, user.Id)
|
2022-11-11 20:36:45 +01:00
|
|
|
if err != nil {
|
|
|
|
util.WriteError(err, w)
|
2022-05-21 15:21:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteJSONObject(w, "")
|
2022-05-21 15:21:39 +02:00
|
|
|
}
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
// GetRuleHandler handles a group Get request identified by ID
|
2022-05-21 15:21:39 +02:00
|
|
|
func (h *Rules) GetRuleHandler(w http.ResponseWriter, r *http.Request) {
|
2022-11-11 20:36:45 +01:00
|
|
|
claims := h.jwtExtractor.ExtractClaimsFromRequestContext(r, h.authAudience)
|
|
|
|
account, user, err := h.accountManager.GetAccountFromToken(claims)
|
2022-05-21 15:21:39 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(err, w)
|
2022-05-21 15:21:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
|
|
|
ruleID := mux.Vars(r)["id"]
|
|
|
|
if len(ruleID) == 0 {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.InvalidArgument, "invalid rule ID"), w)
|
2022-05-21 15:21:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-05 10:24:50 +01:00
|
|
|
rule, err := h.accountManager.GetRule(account.Id, ruleID, user.Id)
|
2022-05-21 15:21:39 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.NotFound, "rule not found"), w)
|
2022-05-21 15:21:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteJSONObject(w, toRuleResponse(account, rule))
|
2022-05-21 15:21:39 +02:00
|
|
|
default:
|
2022-11-11 20:36:45 +01:00
|
|
|
util.WriteError(status.Errorf(status.NotFound, "method not found"), w)
|
2022-05-21 15:21:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 10:32:54 +02:00
|
|
|
func toRuleResponse(account *server.Account, rule *server.Rule) *api.Rule {
|
|
|
|
cache := make(map[string]api.GroupMinimum)
|
|
|
|
gr := api.Rule{
|
|
|
|
Id: rule.ID,
|
|
|
|
Name: rule.Name,
|
|
|
|
Description: rule.Description,
|
|
|
|
Disabled: rule.Disabled,
|
2022-05-21 15:21:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch rule.Flow {
|
|
|
|
case server.TrafficFlowBidirect:
|
2022-06-14 10:32:54 +02:00
|
|
|
gr.Flow = server.TrafficFlowBidirectString
|
2022-05-21 15:21:39 +02:00
|
|
|
default:
|
|
|
|
gr.Flow = "unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, gid := range rule.Source {
|
2022-06-14 10:32:54 +02:00
|
|
|
_, ok := cache[gid]
|
|
|
|
if ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-21 15:21:39 +02:00
|
|
|
if group, ok := account.Groups[gid]; ok {
|
2022-06-14 10:32:54 +02:00
|
|
|
minimum := api.GroupMinimum{
|
|
|
|
Id: group.ID,
|
2022-05-21 15:21:39 +02:00
|
|
|
Name: group.Name,
|
|
|
|
PeersCount: len(group.Peers),
|
2022-06-14 10:32:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
gr.Sources = append(gr.Sources, minimum)
|
|
|
|
cache[gid] = minimum
|
2022-05-21 15:21:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, gid := range rule.Destination {
|
2022-06-14 10:32:54 +02:00
|
|
|
cachedMinimum, ok := cache[gid]
|
|
|
|
if ok {
|
|
|
|
gr.Destinations = append(gr.Destinations, cachedMinimum)
|
|
|
|
continue
|
|
|
|
}
|
2022-05-21 15:21:39 +02:00
|
|
|
if group, ok := account.Groups[gid]; ok {
|
2022-06-14 10:32:54 +02:00
|
|
|
minimum := api.GroupMinimum{
|
|
|
|
Id: group.ID,
|
2022-05-21 15:21:39 +02:00
|
|
|
Name: group.Name,
|
|
|
|
PeersCount: len(group.Peers),
|
2022-06-14 10:32:54 +02:00
|
|
|
}
|
|
|
|
gr.Destinations = append(gr.Destinations, minimum)
|
|
|
|
cache[gid] = minimum
|
2022-05-21 15:21:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &gr
|
|
|
|
}
|