package owrt import ( "fmt" ) // UCIFirewallRedirect is the description of an Wireless interface (cf Openwrt doc) on top of an Wireless Device type UCIFirewallRedirect struct { UCI *UCI Name string Index int Src string Dest string Target string Proto string SrcDIP string SrcDPort string DestIP string DestPort string } // NewUCIFirewallRedirect builds a new UCIFirewallRedirect instance func NewUCIFirewallRedirect(uci *UCI) *UCIFirewallRedirect { return &UCIFirewallRedirect{UCI: uci} } // Create add a new firewall rule in UCI Configuration func (rd *UCIFirewallRedirect) Create() *Action { uci := rd.UCI confPrefix := fmt.Sprintf("firewall.@redirect[%d]", rd.Index) conf := make(map[string][]string) conf["name"] = append(conf["name"], fmt.Sprintf("%s.name", confPrefix), rd.Name) conf["src"] = append(conf["src"], fmt.Sprintf("%s.src", confPrefix), rd.Src) conf["target"] = append(conf["target"], fmt.Sprintf("%s.target", confPrefix), rd.Target) conf["proto"] = append(conf["proto"], fmt.Sprintf("%s.proto", confPrefix), rd.Proto) conf["src_dip"] = append(conf["src_dip"], fmt.Sprintf("%s.src_dip", confPrefix), rd.SrcDIP) conf["src_dport"] = append(conf["src_dport"], fmt.Sprintf("%s.src_dport", confPrefix), rd.SrcDPort) conf["dest_ip"] = append(conf["dest_ip"], fmt.Sprintf("%s.dest_ip", confPrefix), rd.DestIP) conf["dest_port"] = append(conf["dest_port"], fmt.Sprintf("%s.dest_port", confPrefix), rd.DestPort) uci.Add("firewall", "redirect") 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 (rd *UCIFirewallRedirect) Save() *Action { uci := rd.UCI commitRes := uci.Commit() if commitRes.ReturnCode != 0 { return commitRes } reload := uci.Reload() return reload } // Delete remove wifi interface from UCI Configuration func (rd *UCIFirewallRedirect) Delete() *Action { uci := rd.UCI toDelete := fmt.Sprintf("firewall.@redirect[%d]", rd.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 (rd *UCIFirewallRedirect) Update() *Action { uci := rd.UCI rd.Delete() create := rd.Create() if create.ReturnCode != 0 { return create } return uci.Commit() }