From b8f0a554c10407a3326801ad12cf0ac98b219493 Mon Sep 17 00:00:00 2001 From: Philippe Caseiro Date: Wed, 19 Sep 2018 14:39:00 +0200 Subject: [PATCH] Adding begin of openwrt manipulation module --- openwrt/uci.go | 78 ++++++++++++++++++++++++++++++++++++++++++++ openwrt/uci_test.go | 48 +++++++++++++++++++++++++++ openwrt/utils.go | 38 +++++++++++++++++++++ openwrt/wifi_cell.go | 31 ++++++++++++++++++ 4 files changed, 195 insertions(+) create mode 100644 openwrt/uci.go create mode 100644 openwrt/uci_test.go create mode 100644 openwrt/utils.go create mode 100644 openwrt/wifi_cell.go diff --git a/openwrt/uci.go b/openwrt/uci.go new file mode 100644 index 0000000..65f3c0c --- /dev/null +++ b/openwrt/uci.go @@ -0,0 +1,78 @@ +package openwrt + +import ( + "bytes" + "fmt" + "log" + "os/exec" + "time" +) + +// Action is the result of an UCI action output and return code +type Action struct { + output string + errors string + returnCode int +} + +// uciRun, private method to run the UCI command +func uciRun(uciAction string, param string) *Action { + cmd := "uci" + + res := Run(cmd, uciAction, param) + return &Action{ + output: res.stdout, + errors: res.stderr, + returnCode: res.returnCode, + } +} + +// UciAdd add an entry to UCI configuration, specify the Module and the value +func UciAdd(module string, name string) *Action { + actionRes := uciRun("add", fmt.Sprintf("%s %s", module, name)) + return actionRes +} + +// UciDelete delete an entry from UCI configuration specify the entry name +func UciDelete(entry string) *Action { + return uciRun("delete", entry) +} + +// UciSet set a value ton an UCI configuration entry +func UciSet(entry string, value string) *Action { + return uciRun("set", fmt.Sprintf("%s=%s", entry, value)) +} + +// UciCommit the recent actions to UCI +func UciCommit() *Action { + return uciRun("commit", "") +} + +// UciReload reload uci configuration +func UciReload() *Action { + var out bytes.Buffer + var stderr bytes.Buffer + + exe := exec.Command("reload_config") + exe.Stdout = &out + exe.Stderr = &stderr + + err := exe.Run() + if err != nil { + fmt.Println(fmt.Sprint(err) + ": " + stderr.String()) + log.Fatal(err) + } + + time.Sleep(5) + + return &Action{ + output: out.String(), + returnCode: 0, + } +} + +// UciAddWireless Create a new Wireless entry in UCI configuration +func UciAddWireless(name string) int { + res := UciAdd("wireless", name) + return res.returnCode +} diff --git a/openwrt/uci_test.go b/openwrt/uci_test.go new file mode 100644 index 0000000..14ba08f --- /dev/null +++ b/openwrt/uci_test.go @@ -0,0 +1,48 @@ +package openwrt + +import ( + "log" + "strings" + "testing" +) + +var retCode int +var retStdout string +var retStderr string + +type UCI struct { + exec Executor +} + +func (u *UCI) Run(command string, args []string) (stdout string, stderr string, exit int, err error) { + return u.exec.Run(command, args) +} + +func NewUCI(exec Executor) *UCI { + return &UCI{exec} +} + +type Executor interface { + Run(command string, args []string) (stdout string, stderr string, exit int, err error) +} + +type fakeExecutor struct{} + +func (e *fakeExecutor) Run(command string, args []string) (stdout string, stderr string, exit int, err error) { + log.Printf("executing '%s %s'", command, strings.Join(args, " ")) + return "", "", 0, nil +} + +func TestUciAdd(t *testing.T) { + + retCode = 0 + retStdout = "OK" + retStderr = "" + + // Return 0 run ! + res := UciAdd("wireless", "test") + if res.returnCode != 0 { + t.Error(res.errors) + } + +} diff --git a/openwrt/utils.go b/openwrt/utils.go new file mode 100644 index 0000000..e2e5a84 --- /dev/null +++ b/openwrt/utils.go @@ -0,0 +1,38 @@ +package openwrt + +import ( + "bytes" + "fmt" + "log" + "os/exec" +) + +// CommandResult contain all information about a command execution, stdout, stderr +type CommandResult struct { + stdout string + stderr string + returnCode int +} + +// Run executes a system command and returns +func Run(command string, params ...string) *CommandResult { + var out bytes.Buffer + var stderr bytes.Buffer + + exe := exec.Command(command, params...) + exe.Stdout = &out + exe.Stderr = &stderr + + err := exe.Run() + if err != nil { + fmt.Println(fmt.Sprint(err) + ": " + stderr.String()) + log.Fatal(err) + } + + return &CommandResult{ + stdout: out.String(), + stderr: stderr.String(), + // FIXME + returnCode: 0, + } +} diff --git a/openwrt/wifi_cell.go b/openwrt/wifi_cell.go new file mode 100644 index 0000000..15e0ffe --- /dev/null +++ b/openwrt/wifi_cell.go @@ -0,0 +1,31 @@ +package openwrt + +import ( + "fmt" + "strings" +) + +// WifiCell reprensents wifi network Cell +type WifiCell struct { + // The SSID + Ssid string + // The cell mac adress + MacAdress string +} + +// NewWifiCell returns a new WifiCell object +func NewWifiCell(ssid string, mac string) *WifiCell { + return &WifiCell{ + Ssid: ssid, + MacAdress: mac, + } +} + +// GetWifiCells retrieves all availabe wifi cells for a card ! +func GetWifiCells(iface string) { + res := Run("iwinfo", iface, "scan") + for _, line := range strings.Split(strings.TrimSuffix(res.stdout, "\n"), "\n") { + fmt.Println(line) + } + +}