Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link speed for windows network fingerprinting #1443

Merged
merged 2 commits into from
Jul 22, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/fingerprint/network_default.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !linux
// +build !linux,!windows

package fingerprint

Expand Down
47 changes: 47 additions & 0 deletions client/fingerprint/network_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package fingerprint

import (
"fmt"
"os/exec"
"strconv"
"strings"
)

// linkSpeed returns link speed in Mb/s, or 0 when unable to determine it.
func (f *NetworkFingerprint) linkSpeed(device string) int {
command := fmt.Sprintf("Get-NetAdapter -IncludeHidden | Where name -eq '%s' | Select -ExpandProperty LinkSpeed", device)
path := "powershell.exe"
outBytes, err := exec.Command(path, command).Output()

if err != nil {
f.logger.Printf("[WARN] fingerprint.network: Error calling %s (%s): %v", path, command, err)
return 0
}

output := strings.TrimSpace(string(outBytes))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would pull everything from here down into a parse method and then unit test this method.

args := strings.Split(output, " ")
if len(args) != 2 {
f.logger.Printf("[WARN] fingerprint.network: Couldn't split LinkSpeed (%s)", output)
return 0
}

unit := strings.Replace(args[1], "\r\n", "", -1)
value, err := strconv.Atoi(args[0])
if err != nil {
f.logger.Printf("[WARN] fingerprint.network: Unable to parse LinkSpeed value (%s)", output)
return 0
}

switch unit {
case "Mbps":
return value
case "Kbps":
return value / 1000
case "Gbps":
return value * 1000
case "bps":
return value / 1000000
}

return 0
}