Skip to content

Commit

Permalink
Merge branch 'master' of github.com:cjimti/iotwifi
Browse files Browse the repository at this point in the history
  • Loading branch information
cjimti committed Mar 15, 2018
2 parents 575fbc8 + b384499 commit 2a572c4
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 91 deletions.
28 changes: 28 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM arm32v7/golang:1.9 AS builder

ENV GOPATH /go
WORKDIR /go/src

RUN go get github.com/bhoriuchi/go-bunyan/bunyan
RUN go get github.com/gorilla/mux
RUN go get github.com/gorilla/handlers

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

RUN CGO_ENABLED=0 go build -a -installsuffix cgo -o /go/bin/wifi /go/src/github.com/cjimti/iotwifi/main.go

FROM arm32v6/alpine

RUN apk update
RUN apk add bridge hostapd wireless-tools wpa_supplicant dnsmasq iw

RUN mkdir -p /etc/wpa_supplicant/
COPY ./dev/configs/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf

WORKDIR /

COPY --from=builder /go/bin/wifi /wifi
ENTRYPOINT ["/wifi"]


10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
IMAGE ?= iotwifi
IMAGE ?= cjimti/iotwifi
NAME ?= iotwifi

all: build push

dev: dev_build dev_run

build:
docker build -t $(IMAGE) .

push:
docker push $(IMAGE)

dev_build:
docker build -t $(IMAGE) ./dev/

Expand Down
2 changes: 1 addition & 1 deletion cfg/wificfg.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"host_apd_cfg": {
"ip": "192.168.27.1",
"ssid": "iot-wifi",
"ssid": "iot-wifi-cfg-3",
"wpa_passphrase":"iotwifipass",
"channel": "6"
},
Expand Down
2 changes: 1 addition & 1 deletion dev/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ ENV GOPATH /go
WORKDIR /go/src

RUN go get github.com/bhoriuchi/go-bunyan/bunyan
RUN go get github.com/ThomasRooney/gexpect
RUN go get github.com/gorilla/mux
RUN go get github.com/gorilla/handlers
53 changes: 2 additions & 51 deletions iotwifi/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,6 @@ import (
"github.com/bhoriuchi/go-bunyan/bunyan"
)

type SetupCfg struct {
DnsmasqCfg DnsmasqCfg `json:"dnsmasq_cfg"`
HostApdCfg HostApdCfg `json:"host_apd_cfg"`
WpaSupplicantCfg WpaSupplicantCfg `json:"wpa_supplicant_cfg"`
}

type DnsmasqCfg struct {
Address string `json:"address"` // --address=/#/192.168.27.1",
DhcpRange string `json:"dhcp_range"` // "--dhcp-range=192.168.27.100,192.168.27.150,1h",
VendorClass string `json:"vendor_class"` // "--dhcp-vendorclass=set:device,IoT",
}

type HostApdCfg struct {
Ssid string `json:"ssid"` // ssid=iotwifi2
WpaPassphrase string `json:"wpa_passphrase"` // wpa_passphrase=iotwifipass
Channel string `json:"channel"` // channel=6
Ip string `json:"ip"` // 192.168.27.1
}

type WpaSupplicantCfg struct {
CfgFile string `json:"cfg_file"` // /etc/wpa_supplicant/wpa_supplicant.conf
}

// Command for device network commands
type Command struct {
Log bunyan.Logger
Expand Down Expand Up @@ -72,11 +49,12 @@ func (c *Command) CheckApInterface() {

// StartWpaSupplicant
func (c *Command) StartWpaSupplicant() {

args := []string{
"-d",
"-Dnl80211",
"-iwlan0",
"-c" + c.SetupCfg.WpaSupplicantCfg.CfgFile,
"-c/etc/wpa_supplicant/wpa_supplicant.conf",
}

cmd := exec.Command("wpa_supplicant", args...)
Expand All @@ -101,30 +79,3 @@ func (c *Command) StartDnsmasq() {
cmd := exec.Command("dnsmasq", args...)
go c.Runner.ProcessCmd("dnsmasq", cmd)
}

// StartHostapd
func (c *Command) StartHostapd() {

c.Runner.Log.Info("Starting hostapd.")

cmd := exec.Command("hostapd", "-d", "/dev/stdin")
hostapdPipe, _ := cmd.StdinPipe()
c.Runner.ProcessCmd("hostapd", cmd)

cfg := `interface=uap0
ssid=` + c.SetupCfg.HostApdCfg.Ssid + `
hw_mode=g
channel=` + c.SetupCfg.HostApdCfg.Channel + `
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=` + c.SetupCfg.HostApdCfg.WpaPassphrase + `
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP`

hostapdPipe.Write([]byte(cfg))
hostapdPipe.Close()

}
60 changes: 50 additions & 10 deletions iotwifi/iotwifi.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"os"
"os/exec"
"time"
"net/http"
"regexp"

"github.com/bhoriuchi/go-bunyan/bunyan"
)
Expand All @@ -34,21 +36,46 @@ type CmdMessage struct {
Stdin *io.WriteCloser
}

func loadCfg() (*SetupCfg, error) {
fileData, err := ioutil.ReadFile("./cfg/wificfg.json")
if err != nil {
panic(err)
}
func loadCfg(cfgLocation string) (*SetupCfg, error) {

v := &SetupCfg{}
var jsonData []byte

urlDelimR, _ := regexp.Compile("://")
isUrl := urlDelimR.Match([]byte(cfgLocation))

// if not a url
if !isUrl {
fileData, err := ioutil.ReadFile(cfgLocation)
if err != nil {
panic(err)
}
jsonData = fileData
}

if isUrl {
res, err := http.Get(cfgLocation)
if err != nil {
panic(err)
}

defer res.Body.Close()

urlData, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err)
}

err = json.Unmarshal(fileData, v)
jsonData = urlData
}

err := json.Unmarshal(jsonData, v)

return v, err
}

// RunWifi starts AP and Station
func RunWifi(log bunyan.Logger, messages chan CmdMessage) {
func RunWifi(log bunyan.Logger, messages chan CmdMessage, cfgLocation string) {

log.Info("Loading IoT Wifi...")

Expand All @@ -59,7 +86,7 @@ func RunWifi(log bunyan.Logger, messages chan CmdMessage) {
Commands: make(map[string]*exec.Cmd, 0),
}

setupCfg, err := loadCfg()
setupCfg, err := loadCfg(cfgLocation)
if err != nil {
log.Error("Could not load config: %s", err.Error())
return
Expand All @@ -77,14 +104,27 @@ func RunWifi(log bunyan.Logger, messages chan CmdMessage) {
os.Exit(1)
})

wpacfg := NewWpaCfg(log)
wpacfg.StartAP()

wpacfg := NewWpaCfg(log, cfgLocation)
wpacfg.StartAP()

time.Sleep(10 * time.Second)

command.StartWpaSupplicant()

// Scan
time.Sleep(5 * time.Second)
wpacfg.ScanNetworks()

command.StartDnsmasq()

go func() {
for {
wpacfg.ScanNetworks()
time.Sleep(30 * time.Second)
}
}()

// staticFields for logger
staticFields := make(map[string]interface{})

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

type SetupCfg struct {
DnsmasqCfg DnsmasqCfg `json:"dnsmasq_cfg"`
HostApdCfg HostApdCfg `json:"host_apd_cfg"`
WpaSupplicantCfg WpaSupplicantCfg `json:"wpa_supplicant_cfg"`
}

type DnsmasqCfg struct {
Address string `json:"address"` // --address=/#/192.168.27.1",
DhcpRange string `json:"dhcp_range"` // "--dhcp-range=192.168.27.100,192.168.27.150,1h",
VendorClass string `json:"vendor_class"` // "--dhcp-vendorclass=set:device,IoT",
}

type HostApdCfg struct {
Ssid string `json:"ssid"` // ssid=iotwifi2
WpaPassphrase string `json:"wpa_passphrase"` // wpa_passphrase=iotwifipass
Channel string `json:"channel"` // channel=6
Ip string `json:"ip"` // 192.168.27.1
}

type WpaSupplicantCfg struct {
CfgFile string `json:"cfg_file"` // /etc/wpa_supplicant/wpa_supplicant.conf
}

23 changes: 16 additions & 7 deletions iotwifi/wpacfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
type WpaCfg struct {
Log bunyan.Logger
WpaCmd []string
WpaCfg *SetupCfg
}

type WpaNetwork struct {
Expand All @@ -37,10 +38,17 @@ type WpaConnection struct {
Message string `json:"message"`
}

func NewWpaCfg(log bunyan.Logger) *WpaCfg {
func NewWpaCfg(log bunyan.Logger, cfgLocation string) *WpaCfg {

setupCfg, err := loadCfg(cfgLocation)
if err != nil {
log.Error("Could not load config: %s", err.Error())
panic(err)
}

return &WpaCfg{
Log: log,
WpaCfg: setupCfg,
}
}

Expand All @@ -49,7 +57,8 @@ func (wpa *WpaCfg) StartAP() {
wpa.Log.Info("Starting Hostapd.")

command := &Command{
Log: wpa.Log,
Log: wpa.Log,
SetupCfg: wpa.WpaCfg,
}

command.RemoveApInterface()
Expand All @@ -71,25 +80,25 @@ func (wpa *WpaCfg) StartAP() {
stdOutScanner := bufio.NewScanner(cmdStdoutReader)
go func() {
for stdOutScanner.Scan() {
wpa.Log.Info("GOT: %s", stdOutScanner.Text())
wpa.Log.Info("HOSTAPD GOT: %s", stdOutScanner.Text())
messages <- stdOutScanner.Text()
}
}()

cfg := `interface=uap0
ssid=iotwifi2
ssid=` + wpa.WpaCfg.HostApdCfg.Ssid + `
hw_mode=g
channel=6
channel=` + wpa.WpaCfg.HostApdCfg.Channel + `
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=iotwifipass
wpa_passphrase=` + wpa.WpaCfg.HostApdCfg.WpaPassphrase + `
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP`


wpa.Log.Info("Hostapd CFG: %s", cfg)
hostapdPipe.Write([]byte(cfg))

cmd.Start()
Expand Down
Loading

0 comments on commit 2a572c4

Please sign in to comment.