mirror of
https://github.com/speatzle/nfsense.git
synced 2025-05-10 18:38:22 +00:00
86 lines
2.8 KiB
Go
86 lines
2.8 KiB
Go
package firewall
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"nfsense.net/nfsense/internal/definitions/firewall"
|
|
)
|
|
|
|
type GetForwardRulesResult struct {
|
|
ForwardRules []firewall.ForwardRule `json:"forward_rules"`
|
|
}
|
|
|
|
func (f *Firewall) GetForwardRules(ctx context.Context, params struct{}) (GetForwardRulesResult, error) {
|
|
return GetForwardRulesResult{
|
|
ForwardRules: f.ConfigManager.GetPendingConfig().Firewall.ForwardRules,
|
|
}, nil
|
|
}
|
|
|
|
type CreateForwardRuleParameters struct {
|
|
ForwardRule firewall.ForwardRule `json:"forward_rule"`
|
|
}
|
|
|
|
func (f *Firewall) CreateForwardRule(ctx context.Context, params CreateForwardRuleParameters) (struct{}, error) {
|
|
t, conf := f.ConfigManager.StartTransaction()
|
|
defer t.Discard()
|
|
|
|
conf.Firewall.ForwardRules = append(conf.Firewall.ForwardRules, params.ForwardRule)
|
|
return struct{}{}, t.Commit()
|
|
}
|
|
|
|
type UpdateForwardRuleParameters struct {
|
|
Index uint64 `json:"index"`
|
|
ForwardRule firewall.ForwardRule `json:"forward_rule"`
|
|
}
|
|
|
|
func (f *Firewall) UpdateForwardRule(ctx context.Context, params UpdateForwardRuleParameters) (struct{}, error) {
|
|
if int(params.Index) >= len(f.ConfigManager.GetPendingConfig().Firewall.ForwardRules) {
|
|
return struct{}{}, fmt.Errorf("ForwardRule does not Exist")
|
|
}
|
|
|
|
t, conf := f.ConfigManager.StartTransaction()
|
|
defer t.Discard()
|
|
|
|
conf.Firewall.ForwardRules[params.Index] = params.ForwardRule
|
|
return struct{}{}, t.Commit()
|
|
}
|
|
|
|
type MoveForwardRuleParameters struct {
|
|
Index uint64 `json:"index"`
|
|
ToIndex uint64 `json:"to_index"`
|
|
}
|
|
|
|
func (f *Firewall) MoveForwardRule(ctx context.Context, params MoveForwardRuleParameters) (struct{}, error) {
|
|
if int(params.Index) >= len(f.ConfigManager.GetPendingConfig().Firewall.ForwardRules) {
|
|
return struct{}{}, fmt.Errorf("ForwardRule does not Exist")
|
|
}
|
|
|
|
t, conf := f.ConfigManager.StartTransaction()
|
|
defer t.Discard()
|
|
|
|
rule := conf.Firewall.ForwardRules[params.Index]
|
|
sliceWithoutRule := append(conf.Firewall.ForwardRules[:params.Index], conf.Firewall.ForwardRules[params.Index+1:]...)
|
|
newSlice := make([]firewall.ForwardRule, params.ToIndex+1)
|
|
copy(newSlice, sliceWithoutRule[:params.ToIndex])
|
|
newSlice[params.ToIndex] = rule
|
|
conf.Firewall.ForwardRules = append(newSlice, sliceWithoutRule[params.ToIndex:]...)
|
|
|
|
return struct{}{}, t.Commit()
|
|
}
|
|
|
|
type DeleteForwardRuleParameters struct {
|
|
Index uint64 `json:"index"`
|
|
}
|
|
|
|
func (f *Firewall) DeleteForwardRule(ctx context.Context, params DeleteForwardRuleParameters) (struct{}, error) {
|
|
if int(params.Index) >= len(f.ConfigManager.GetPendingConfig().Firewall.ForwardRules) {
|
|
return struct{}{}, fmt.Errorf("ForwardRule does not Exist")
|
|
}
|
|
|
|
t, conf := f.ConfigManager.StartTransaction()
|
|
defer t.Discard()
|
|
|
|
conf.Firewall.ForwardRules = append(conf.Firewall.ForwardRules[:params.Index], conf.Firewall.ForwardRules[params.Index+1:]...)
|
|
return struct{}{}, t.Commit()
|
|
}
|