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

Disable PowerShell profile and simplify fingerprinting link speed on Windows #11183

Merged
merged 4 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions .changelog/11183.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
client: Fixed a bug where network speed fingerprint could fail on Windows
```
34 changes: 6 additions & 28 deletions client/fingerprint/network_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +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"
outBytes, err := exec.Command(path, command).Output()
powershellParams := "-NoProfile"

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)
value, err := strconv.Atoi(args[0])
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
}

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
}
34 changes: 0 additions & 34 deletions client/fingerprint/network_windows_test.go

This file was deleted.