2018-10-24 10:26:11 +02:00
|
|
|
package owrt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Action is the result of an UCI action output and return code
|
|
|
|
type Action struct {
|
|
|
|
*CommandResult
|
|
|
|
Command string
|
|
|
|
}
|
|
|
|
|
|
|
|
// UCI "Object"
|
|
|
|
type UCI struct {
|
2018-11-07 16:36:45 +01:00
|
|
|
exec Executor
|
|
|
|
CustomFirewallFile string
|
|
|
|
Wireless *UCIWirelessConf
|
2018-11-12 09:16:41 +01:00
|
|
|
Firewall *UCIFirewall
|
2018-10-24 10:26:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewUCI return an UCI instance to interact with UCI
|
|
|
|
func NewUCI() *UCI {
|
|
|
|
exec := &localExecutor{}
|
2018-11-07 16:36:45 +01:00
|
|
|
customFWFile := "/etc/"
|
2018-10-24 10:26:11 +02:00
|
|
|
wireless := &UCIWirelessConf{}
|
2018-11-12 09:16:41 +01:00
|
|
|
firewall := &UCIFirewall{}
|
|
|
|
return &UCI{exec, customFWFile, wireless, firewall}
|
2018-10-24 10:26:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewUCIWithExecutor returns a UCI Instance an gives you the ability to provide
|
|
|
|
// a different command executor than the default one.
|
2018-11-07 16:36:45 +01:00
|
|
|
func NewUCIWithExecutor(exec Executor, customFWFile string) *UCI {
|
|
|
|
|
2018-10-24 10:26:11 +02:00
|
|
|
wireless := &UCIWirelessConf{}
|
2018-11-12 09:16:41 +01:00
|
|
|
firewall := &UCIFirewall{}
|
|
|
|
return &UCI{exec, customFWFile, wireless, firewall}
|
2018-10-24 10:26:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// uciRun, private method to run the UCI command
|
|
|
|
func (u *UCI) uciRun(param ...string) *Action {
|
|
|
|
cmd := "/sbin/uci"
|
|
|
|
res := u.exec.Run(cmd, param...)
|
|
|
|
return &Action{res, fmt.Sprintf("%s %s", cmd, param)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add add an entry to UCI configuration, specify the Module and the value
|
|
|
|
func (u *UCI) Add(module string, name string) *Action {
|
2018-11-09 14:38:58 +01:00
|
|
|
cmd := "/sbin/uci"
|
|
|
|
opt := "add"
|
|
|
|
commandRes := u.exec.Run(cmd, opt, module, name)
|
2018-10-24 10:26:11 +02:00
|
|
|
return &Action{commandRes, fmt.Sprintf("%s %s %s", cmd, module, name)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete delete an entry from UCI configuration specify the entry name
|
|
|
|
func (u *UCI) Delete(entry string) *Action {
|
|
|
|
return u.uciRun("delete", entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set set a value ton an UCI configuration entry
|
|
|
|
func (u *UCI) Set(entry string, value string) *Action {
|
|
|
|
return u.uciRun("set", fmt.Sprintf("%s=%s", entry, value))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit the recent actions to UCI
|
|
|
|
func (u *UCI) Commit() *Action {
|
|
|
|
res := u.uciRun("commit")
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reload reload uci configuration
|
|
|
|
func (u *UCI) Reload() *Action {
|
|
|
|
cmdResult := u.exec.Run("reload_config")
|
|
|
|
|
2018-10-29 15:10:32 +01:00
|
|
|
time.Sleep(1 * time.Second)
|
2018-10-24 10:26:11 +02:00
|
|
|
|
|
|
|
return &Action{cmdResult, "reload_config"}
|
|
|
|
}
|
|
|
|
|
2018-10-25 10:24:45 +02:00
|
|
|
// Save commit and relaod configuration (writes it to files !)
|
|
|
|
func (u *UCI) Save() *Action {
|
|
|
|
commitRes := u.Commit()
|
|
|
|
if commitRes.ReturnCode != 0 {
|
|
|
|
return commitRes
|
|
|
|
}
|
|
|
|
|
|
|
|
reload := u.Reload()
|
|
|
|
return reload
|
|
|
|
}
|
|
|
|
|
2018-10-24 10:26:11 +02:00
|
|
|
// Show returns the output of uci show command
|
|
|
|
func (u *UCI) Show(target string) *Action {
|
|
|
|
cmdRes := u.uciRun("show", target)
|
|
|
|
return cmdRes
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddWireless Create a new Wireless entry in UCI configuration
|
|
|
|
func (u *UCI) AddWireless() *Action {
|
|
|
|
res := u.uciRun("add", "wireless", "wifi-iface")
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadWirelessConf scan UCI configuration and loads saved Wireless configuration
|
|
|
|
func (u *UCI) LoadWirelessConf() {
|
|
|
|
u.Wireless = NewUCIWirelessConf(u)
|
|
|
|
u.Wireless.Load()
|
|
|
|
}
|
|
|
|
|
2018-11-12 09:16:41 +01:00
|
|
|
// LoadFirewallConf scan UCI configration and load saved firewall rules
|
|
|
|
func (u *UCI) LoadFirewallConf() error {
|
|
|
|
fmt.Println("DEBUG ICI ICI ICI ICI !!!")
|
|
|
|
u.Firewall = NewUCIFirewall(u)
|
|
|
|
return u.Firewall.Load()
|
|
|
|
}
|
|
|
|
|
2018-10-24 10:26:11 +02:00
|
|
|
// GetWifiIface returns the wifi Interface by Index
|
|
|
|
func (u *UCI) GetWifiIface(idx int) *UCIWirelessInterface {
|
|
|
|
ifaces := u.Wireless.Interfaces
|
|
|
|
if len(ifaces) < idx {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ifaces[idx]
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetWifiIfaceByName returns the wifi Interface by Index
|
|
|
|
func (u *UCI) GetWifiIfaceByName(name string) *UCIWirelessInterface {
|
|
|
|
ifaces := u.Wireless.Interfaces
|
|
|
|
if len(ifaces) <= 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, ifa := range ifaces {
|
|
|
|
if ifa.Name == name {
|
|
|
|
return ifa
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-29 11:19:40 +01:00
|
|
|
// GetWifiIfaceBySSID returns the wifi Interface by Index
|
|
|
|
func (u *UCI) GetWifiIfaceBySSID(ssid string) *UCIWirelessInterface {
|
|
|
|
ifaces := u.Wireless.Interfaces
|
|
|
|
if len(ifaces) <= 0 {
|
|
|
|
fmt.Println("ifaces are empty !!! fuck")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, ifa := range ifaces {
|
|
|
|
if ifa.Ssid == ssid {
|
|
|
|
return ifa
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println("iface not found !")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-24 10:26:11 +02:00
|
|
|
// GetWifiIfaces wifi interfaces in configuration
|
2018-10-29 15:10:32 +01:00
|
|
|
func (u *UCI) GetWifiIfaces() map[int]*UCIWirelessInterface {
|
2018-10-24 10:26:11 +02:00
|
|
|
return u.Wireless.Interfaces
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetWifiDevices returns the wifi devices in configuration
|
|
|
|
func (u *UCI) GetWifiDevices() []map[string]string {
|
|
|
|
return u.Wireless.Devices
|
|
|
|
}
|
2018-10-30 09:55:54 +01:00
|
|
|
|
|
|
|
// GetWifiDeviceByName returns a wifi device by his name "radio0" "radio1"
|
|
|
|
func (u *UCI) GetWifiDeviceByName(name string) map[string]string {
|
|
|
|
for _, device := range u.Wireless.Devices {
|
|
|
|
if device["Device"] == name {
|
|
|
|
return device
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-11-07 16:36:45 +01:00
|
|
|
|
|
|
|
// Service make restart services via the UCI client possible
|
|
|
|
func (u *UCI) Service(name string, action string) error {
|
|
|
|
sys := NewSystemWithExecutor(u.exec)
|
|
|
|
res := sys.Service(name, action)
|
|
|
|
if res.ReturnCode != 0 {
|
|
|
|
return fmt.Errorf("%d - %s - %s - %s",
|
|
|
|
res.ReturnCode,
|
|
|
|
res.Command,
|
|
|
|
res.Stdout,
|
|
|
|
res.Stderr)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|