Skip to content

Commit

Permalink
Add wifi config formatter. Add hooks to API
Browse files Browse the repository at this point in the history
  • Loading branch information
danesparza committed Sep 12, 2017
1 parent e762d93 commit 7a22678
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 9 deletions.
35 changes: 35 additions & 0 deletions api/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/danesparza/appliance-monitor/data"
"github.com/danesparza/appliance-monitor/network"
"github.com/spf13/viper"
)

Expand All @@ -17,6 +18,17 @@ type CurrentState struct {
DeviceID string `json:"deviceId"`
}

// WifiUpdateRequest describes the request to update the Wifi credentials
type WifiUpdateRequest struct {
SSID string `json:"ssid"`
Passphrase string `json:"passphrase"`
}

type WifiUpdateResponse struct {
Status int `json:"status"`
Description string `json:"description"`
}

// GetCurrentState gets the current running state of the application
func GetCurrentState(rw http.ResponseWriter, req *http.Request) {

Expand All @@ -42,3 +54,26 @@ func GetCurrentState(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("Content-Type", "application/json; charset=utf-8")
json.NewEncoder(rw).Encode(currentState)
}

// UpdateWifi updates the wifi credentials for the machine
func UpdateWifi(rw http.ResponseWriter, req *http.Request) {

// Decode the request if it was a POST:
request := WifiUpdateRequest{}
err := json.NewDecoder(req.Body).Decode(&request)
if err != nil {
sendErrorResponse(rw, err, http.StatusBadRequest)
return
}

// Send the request to the wifi helper:
response := WifiUpdateResponse{Status: 200, Description: "Successful"}
err = network.UpdateWifiCredentials(request.SSID, request.Passphrase)
if err != nil {
sendErrorResponse(rw, err, http.StatusInternalServerError)
}

// Serialize to JSON & return the response:
rw.Header().Set("Content-Type", "application/json; charset=utf-8")
json.NewEncoder(rw).Encode(response)
}
5 changes: 1 addition & 4 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ func start(cmd *cobra.Command, args []string) {
log.Printf("[ERROR] There was a problem resetting the host name: %v", err)
return
}
log.Println("Rebooting...")
syscall.Sync()
syscall.Reboot(syscall.LINUX_REBOOT_CMD_RESTART)
}

// Get a reference to the config database
Expand Down Expand Up @@ -108,7 +105,7 @@ func start(cmd *cobra.Command, args []string) {

// System information
Router.HandleFunc("/system/state", api.GetCurrentState).Methods("GET")
Router.HandleFunc("/system/wifi", nil)
Router.HandleFunc("/system/wifi", api.UpdateWifi).Methods("POST")

// System resets
Router.HandleFunc("/reset/network", nil)
Expand Down
40 changes: 40 additions & 0 deletions network/configformatter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package network

import (
"bytes"
"html/template"
)

var wifiSupplicant = []byte(`country=US
network={
ssid="{{.SSID}}"
psk="{{.Passphrase}}"
key_mgmt=WPA-PSK
}
`)

// WifiInfo describes the information needed to configure the wifi client
type WifiInfo struct {
SSID string
Passphrase string
}

// FormatWifiCredentials formats the given credentials as a new wifi supplicant config file
func FormatWifiCredentials(ssid, password string) (string, error) {

credentials := WifiInfo{ssid, password}
tmpl, err := template.New("wifisupp").Parse(string(wifiSupplicant))
if err != nil {
return "", err
}

var tpl bytes.Buffer
err = tmpl.Execute(&tpl, credentials)
if err != nil {
return "", err
}

return tpl.String(), nil
}
36 changes: 36 additions & 0 deletions network/configformatter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package network_test

import (
"testing"

"github.com/danesparza/appliance-monitor/network"
)

// The formatting utility should format the wifi supplicant file
func TestFormatWifiCredentials_WithValidParams_ShouldReturnProperFormat(t *testing.T) {

// Arrange
ssid := "testssid"
passphrase := "testpassphrase"
expectedconfig := `country=US
network={
ssid="testssid"
psk="testpassphrase"
key_mgmt=WPA-PSK
}
`

// Act
retval, err := network.FormatWifiCredentials(ssid, passphrase)

// Assert
if err != nil {
t.Errorf("An error occured while formatting the wifi config: %v", err)
}

if retval != expectedconfig {
t.Errorf("Configuration doesn't match what we expect. Here's what we got:\n%v", retval)
}
}
5 changes: 5 additions & 0 deletions network/hostname_linux_arm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"os"
"strings"
"syscall"
)

// ResetHostname changes the hostname for the local machine
Expand Down Expand Up @@ -44,5 +45,9 @@ func ResetHostname(newname string) error {
return err
}

log.Println("Rebooting...")
syscall.Sync()
syscall.Reboot(syscall.LINUX_REBOOT_CMD_RESTART)

return nil
}
8 changes: 3 additions & 5 deletions network/wifi.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
package network

import (
"log"
"fmt"
)

// ResetHostname changes the hostname for the local machine
// UpdateWifiCredentials changes the hostname for the local machine
func UpdateWifiCredentials(ssid, password string) error {
log.Println("[INFO] Not running on Linux/ARM, so wifi can't be changed...")

return nil
return fmt.Errorf("Not running on Linux/ARM, so wifi can't be changed")
}

0 comments on commit 7a22678

Please sign in to comment.