Skip to content

Commit

Permalink
fingerprint: simplify windows network speed fingerprinting
Browse files Browse the repository at this point in the history
  • Loading branch information
lgfa29 committed Sep 16, 2021
1 parent 5d5fa41 commit 8f3843c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 64 deletions.
34 changes: 5 additions & 29 deletions client/fingerprint/network_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,22 @@ import (

// 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)
command := fmt.Sprintf("Get-NetAdapter -Name '%s' -ErrorAction Ignore | Select-Object -ExpandProperty 'Speed'", device)
path := "powershell.exe"
powershellParams := "-NoProfile"
outBytes, err := exec.Command(path, powershellParams, command).Output()

outBytes, err := exec.Command(path, powershellParams, command).Output()
if err != nil {
f.logger.Warn("failed to detect link speed", "device", device, "path", path, "command", command, "error", err)
return 0
}

output := strings.TrimSpace(string(outBytes))

return f.parseLinkSpeed(device, output)
}

func (f *NetworkFingerprint) parseLinkSpeed(device, commandOutput string) int {
args := strings.Split(commandOutput, " ")
if len(args) != 2 {
f.logger.Warn("couldn't split LinkSpeed output", "device", device, "output", commandOutput)
return 0
}

unit := strings.Replace(args[1], "\r\n", "", -1)
valueFloat, err := strconv.ParseFloat(args[0], 64)
value, err := strconv.Atoi(output)
if err != nil {
f.logger.Warn("unable to parse LinkSpeed value", "device", device, "value", commandOutput, "error", err)
f.logger.Warn("unable to parse Speed value", "device", device, "value", output, "error", err)
return 0
}
value := int(valueFloat)

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

return 0
return value / 1000000
}
35 changes: 0 additions & 35 deletions client/fingerprint/network_windows_test.go

This file was deleted.

0 comments on commit 8f3843c

Please sign in to comment.