Skip to content

Commit

Permalink
scan networks
Browse files Browse the repository at this point in the history
  • Loading branch information
cjimti committed Feb 13, 2018
1 parent 4359802 commit 709409f
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 13 deletions.
7 changes: 1 addition & 6 deletions dev/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@ FROM arm32v6/alpine
RUN apk update
RUN apk add alpine-sdk go bridge hostapd wireless-tools wireless-tools-dev wpa_supplicant dnsmasq iw

RUN mkdir -p /etc/network/
RUN mkdir -p /etc/wpa_supplicant/
RUN mkdir -p /etc/hostapd/

COPY ./configs/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf
COPY ./configs/hostapd.conf /etc/hostapd/hostapd.conf
COPY ./configs/interfaces /etc/network/interfaces

RUN mkdir -p /go/src/github.com/cjimti/iotwifi/

ENV GOPATH /go
WORKDIR /go/src

RUN go get github.com/bhoriuchi/go-bunyan/bunyan

RUN go get github.com/ThomasRooney/gexpect
2 changes: 1 addition & 1 deletion iotwifi/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (c *Command) CheckApInterface() {
// StartWpaSupplicant
func (c *Command) StartWpaSupplicant() {
args := []string{
"-d",
// "-d",
"-Dnl80211",
"-iwlan0",
"-c/etc/wpa_supplicant/wpa_supplicant.conf",
Expand Down
27 changes: 23 additions & 4 deletions iotwifi/iotwifi.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import (
"os/exec"
"strings"
"os"
"io"

"github.com/bhoriuchi/go-bunyan/bunyan"
)

// CmdRunner
type CmdRunner struct {
Log bunyan.Logger
Messages chan CmdMessage
Handlers map[string]func(CmdMessage)
Log bunyan.Logger
Messages chan CmdMessage
Handlers map[string]func(CmdMessage)
Commands map[string]*exec.Cmd
}

Expand All @@ -28,6 +29,7 @@ type CmdMessage struct {
Message string
Error bool
Cmd *exec.Cmd
Stdin *io.WriteCloser
}

// RunWifi starts AP and Station
Expand Down Expand Up @@ -94,7 +96,7 @@ func RunWifi(log bunyan.Logger, messages chan CmdMessage) {
return
}

if strings.Contains(cmsg.Message, "uap0 Link encap") {
if strings.Contains(cmsg.Message, "Link encap") {

log.Info("uap0 is available")
cmsg.Cmd.Wait()
Expand All @@ -110,6 +112,23 @@ func RunWifi(log bunyan.Logger, messages chan CmdMessage) {
}
})

wpaSupplicantRunning := false

// once wap_supplicant is likly running
//
cmdRunner.HandleFunc("wpa_supplicant", func(cmsg CmdMessage) {

// wap_supplican is reporting state, so it's up and running
if !wpaSupplicantRunning && strings.Contains(cmsg.Message, "Successfully initialized wpa_supplicant") {
// fire up wpa_cli so we can communicate with wap_supplicant
wpaSupplicantRunning = true
log.Info("WPA Supplicant is running.")

//command.StartWpaCli()
}

})

// remove AP interface (if there is one) and start fresh
command.RemoveApInterface()

Expand Down
79 changes: 79 additions & 0 deletions iotwifi/wpacfg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package iotwifi

import (
"os/exec"
"strings"
"time"

"github.com/bhoriuchi/go-bunyan/bunyan"
)

// WpaCfg for configuring wpa
type WpaCfg struct {
Log bunyan.Logger
WpaCmd []string
}

type WpaNetwork struct {
Bssid string `json:"bssid"`
Frequency string `json:"frequency"`
SignalLevel string `json:"signal_level"`
Flags string `json:"flags"`
Ssid string `json:"ssid"`
}

func NewWpaCfg(log bunyan.Logger) *WpaCfg {

return &WpaCfg{
Log: log,
}
}

func (wpa *WpaCfg) ScanNetworks() (map[string]WpaNetwork, error) {
wpaNetworks := make(map[string]WpaNetwork,0)

scanOut, err := exec.Command("wpa_cli","-i","wlan0", "scan").Output()
if err != nil {
wpa.Log.Fatal(err)
return wpaNetworks, err
}
scanOutClean := strings.TrimSpace(string(scanOut))

// wait one second for results
time.Sleep(1 * time.Second)


if scanOutClean == "OK" {
networkListOut, err := exec.Command("wpa_cli","-i","wlan0", "scan_results").Output()
if err != nil {
wpa.Log.Fatal(err)
return wpaNetworks, err
}

networkListOutArr := strings.Split(string(networkListOut),"\n")
for _, netRecord := range networkListOutArr[1:] {
if strings.Contains(netRecord, "[P2P]") {
continue
}

fields := strings.Fields(netRecord)

if len(fields) > 4 {
ssid := strings.Join(fields[4:],",")
wpaNetworks[ssid] = WpaNetwork{
Bssid: fields[0],
Frequency: fields[1],
SignalLevel: fields[2],
Flags: fields[3],
Ssid: ssid,
}
}
}

}

return wpaNetworks, nil
}



58 changes: 56 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
package main

import (
"io"
"net/http"
"os"
"encoding/json"

"github.com/bhoriuchi/go-bunyan/bunyan"
"github.com/cjimti/iotwifi/iotwifi"
)

type ApiReturn struct {
Status string `json:"status"`
Message string `json:"message"`
Payload interface{} `json:"payload"`
}

func main() {

logConfig := bunyan.Config{
Expand All @@ -31,9 +37,57 @@ func main() {

go iotwifi.RunWifi(blog, messages)

retError := func(w http.ResponseWriter, err error) {
apiReturn := &ApiReturn{
Status: "Faile",
Message: err.Error(),
}
ret, _ := json.Marshal(apiReturn)

w.Header().Set("Content-Type", "application/json")
w.Write(ret)
}

http.HandleFunc("/scan", func(w http.ResponseWriter, r *http.Request) {
blog.Info("Got Scan")
wpa := iotwifi.NewWpaCfg(blog)
wpaNetworks, err := wpa.ScanNetworks()
if err != nil {
retError(w, err)
return
}

apiReturn := &ApiReturn{
Status: "OK",
Message: "Networks",
Payload: wpaNetworks,
}

ret, err := json.Marshal(apiReturn)
if err != nil {
retError(w, err)
return
}

w.Header().Set("Content-Type", "application/json")
w.Write(ret)
})

http.HandleFunc("/kill", func(w http.ResponseWriter, r *http.Request) {
messages <- iotwifi.CmdMessage{Id: "kill"}
io.WriteString(w, "OK\n")

apiReturn := &ApiReturn{
Status: "OK",
Message: "Killing service.",
}
ret, err := json.Marshal(apiReturn)
if err != nil {
retError(w, err)
return
}

w.Header().Set("Content-Type", "application/json")
w.Write(ret)
})

blog.Info("HTTP Listening on 8080")
Expand Down

0 comments on commit 709409f

Please sign in to comment.