Skip to content

Commit

Permalink
Windows alternative to netsh (#65)
Browse files Browse the repository at this point in the history
* Included an alternative to netsh

* Cleaned up leftover logging and indentations
  • Loading branch information
ScottSWu authored and schollz committed Apr 23, 2016
1 parent 9adf776 commit db7541b
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,7 @@ nohup.out

# Temp files
*~

# binaries and binary folders
bin/
*.exe
131 changes: 128 additions & 3 deletions os_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ package main
// and the RSSI signal plus a boolean flag indicating if such content
// was found in the given line.

import "regexp"
import (
"crypto/sha1"
"errors"
"encoding/hex"
"io"
"net/http"
"os"
"regexp"
)

// Regular expression matching standard (IEEE 802) MAC-48 addresses
const macExpr = "([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})"
Expand Down Expand Up @@ -39,6 +47,70 @@ func GetConfiguration(os string, wlanInterface string) (OSConfig, bool) {
return config, ok
}

func ComputeSHA1Sum(filename string) (string, error) {
fd, err := os.Open(filename)

if err != nil {
return "", err
}
defer fd.Close()

h := sha1.New()
_, err = io.Copy(h, fd)
if err != nil {
return "", err
}

result := h.Sum(nil)

return hex.EncodeToString(result), nil
}

func DownloadVerifyFile(url string, target string, sha1sum string) (error) {
needsDownload := true

_, err := os.Stat(target)
if err == nil {
// File already exists, check if the hash matches
fileSum, _ := ComputeSHA1Sum(target)
if fileSum == sha1sum {
needsDownload = false
}
}

if needsDownload {
fd, err := os.Create(target)
if err != nil {
fd.Close()
return err
}

response, err := http.Get(url)
if err != nil {
return err
}
defer response.Body.Close()

n, err := io.Copy(fd, response.Body)
if err != nil {
return err
}

log.Info(n, "bytes downloaded.")
fd.Close()

fileSum, err := ComputeSHA1Sum(target)

if fileSum != sha1sum {
log.Warning("3rd party utility hash (" + fileSum + ") does NOT match expected hash (" + sha1sum + ").")
os.Remove(target)
return errors.New("Download failed")
}
}

return nil
}

func populateConfigurations(wlanInterface string) {
osConfigurations["darwin"] = OSConfig{
WifiScanCommand: "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -s",
Expand All @@ -59,8 +131,61 @@ func populateConfigurations(wlanInterface string) {
}
}

osConfigurations["windows"] = OSConfig{

// Check for an alternative Windows wifi utility
_, err := os.Stat("bin")
if os.IsNotExist(err) {
// Create a bin folder
os.Mkdir("bin", os.ModePerm)
}
windowsConfig := OSConfig{
WifiScanCommand: "netsh wlan show network mode=bssid",
ScanConfig: ScanParsingConfig{windowsFindMac, windowsFindRssi},
ScanConfig: ScanParsingConfig{windowsFindMac, windowsFindRssi},
}
needsPrompt := true

// Check if the alternative is downloaded
altSum := "5209b821e93d9a407b725b229223e53bb52495c9"
_, err = os.Stat("bin/windows-wlan-util.exe")
if err == nil {
// Verify the file
fileSum, _ := ComputeSHA1Sum("bin/windows-wlan-util.exe")
if fileSum == altSum {
windowsConfig = OSConfig{
WifiScanCommand: "bin/windows-wlan-util.exe query",
ScanConfig: ScanParsingConfig{windows2FindMac, windows2FindRssi},
}
needsPrompt = false
} else {
log.Warning("3rd party utility hash (" + fileSum + ") does NOT match expected hash (" + altSum + ").")
}
} else {
_, err := os.Stat("bin/windows-use-netsh")
if err == nil {
needsPrompt = false
}
}

if needsPrompt {
// Ask if the user wants to download the alternative
altUtil := getInput("Do you want to download a 3rd party utility for better wifi capture? (y/n)")
if altUtil == "y" {
err = DownloadVerifyFile(
"https://github.com/ScottSWu/windows-wlan-util/releases/download/v1.0/windows-wlan-util.exe",
"bin/windows-wlan-util.exe", "5209b821e93d9a407b725b229223e53bb52495c9")
if err == nil {
windowsConfig = OSConfig{
WifiScanCommand: "bin/windows-wlan-util.exe query",
ScanConfig: ScanParsingConfig{windows2FindMac, windows2FindRssi},
}
} else {
log.Warning("Failed to download 3rd party utility. You will be asked again next time.")
}
} else {
fd, _ := os.Create("bin/windows-use-netsh")
fd.Close()
}
}

osConfigurations["windows"] = windowsConfig
}
32 changes: 32 additions & 0 deletions windows2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"strconv"
"strings"
"time"
)

// windows2 uses a third party alternative to netsh
// See https://github.com/ScottSWu/windows-wlan-util for details

func windows2FindMac(line string) (string, bool) {
return linuxFindMac(line)
}

func windows2FindRssi(line string) (int, bool) {
line = strings.Trim(line, " ")
space := strings.Index(line, " ")

if space == -1 {
return 0, false
}

rssi, err := strconv.ParseFloat(line[:space], 10)

// This is probably not the best place for this
// Rough timings suggest that finding ~30 access points takes around 10
// seconds. Thus for each MAC/Rssi value found, we should wait about 300 ms.
time.Sleep(300 * time.Millisecond)

return int(rssi), (err == nil)
}

0 comments on commit db7541b

Please sign in to comment.