owrt/uci_firewall_rules.go

88 lines
2.2 KiB
Go
Raw Permalink Normal View History

2018-11-07 11:50:59 +01:00
package owrt
import (
"fmt"
)
// UCIFirewallRule is the description of an Wireless interface (cf Openwrt doc) on top of an Wireless Device
type UCIFirewallRule struct {
UCI *UCI
2018-11-07 11:50:59 +01:00
Name string
Index int
Src string
Target string
Proto string
DestPort string
SourcePort string
}
// NewUCIFirewallRule builds a new UCIFirewallRule instance
func NewUCIFirewallRule(uci *UCI) *UCIFirewallRule {
return &UCIFirewallRule{UCI: uci}
2018-11-07 11:50:59 +01:00
}
// Create add a new firewall rule in UCI Configuration
func (fw *UCIFirewallRule) Create() *Action {
uci := fw.UCI
confPrefix := fmt.Sprintf("firewall.@rule[%d]", fw.Index)
2018-11-07 11:50:59 +01:00
conf := make(map[string][]string)
conf["name"] = append(conf["network"], fmt.Sprintf("%s.name", confPrefix), fw.Name)
conf["src"] = append(conf["src"], fmt.Sprintf("%s.src", confPrefix), fw.Src)
conf["target"] = append(conf["target"], fmt.Sprintf("%s.target", confPrefix), fw.Target)
conf["proto"] = append(conf["proto"], fmt.Sprintf("%s.proto", confPrefix), fw.Proto)
conf["dest_port"] = append(conf["dest_port"], fmt.Sprintf("%s.dest_port", confPrefix), fw.DestPort)
conf["src_port"] = append(conf["src_port"], fmt.Sprintf("%s.src_port", confPrefix), fw.SourcePort)
uci.Add("firewall", "rule")
for _, value := range conf {
if value[1] != "" {
result := uci.Set(value[0], value[1])
if result.ReturnCode != 0 {
return result
}
}
}
return &Action{
CommandResult: &CommandResult{
Stdout: "",
Stderr: "",
ReturnCode: 0,
},
}
}
// Save commit and relaod configuration (writes it to files !)
func (fw *UCIFirewallRule) Save(uci *UCI) *Action {
commitRes := uci.Commit()
if commitRes.ReturnCode != 0 {
return commitRes
}
reload := uci.Reload()
return reload
}
// Delete remove wifi interface from UCI Configuration
func (fw *UCIFirewallRule) Delete() *Action {
uci := fw.UCI
2018-11-07 11:50:59 +01:00
toDelete := fmt.Sprintf("firewall.@rule[%d]", fw.Index)
del := uci.Delete(toDelete)
if del.ReturnCode != 0 {
return del
}
return uci.Commit()
}
// Update add a new entry for wifi interface in UCI Configuration
func (fw *UCIFirewallRule) Update() *Action {
uci := fw.UCI
fw.Delete()
create := fw.Create()
2018-11-07 11:50:59 +01:00
if create.ReturnCode != 0 {
return create
}
return uci.Commit()
}