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 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} } // 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) 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 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() if create.ReturnCode != 0 { return create } return uci.Commit() }