tailscale/portlist/portlist_windows.go
Brad Fitzpatrick 46ce80758d portlist: update some internals to use append-style APIs
In prep for reducing garbage, being able to reuse memory.  So far this
doesn't actually reuse much. This is just changing signatures around.

But some improvement in any case:

    bradfitz@tsdev:~/src/tailscale.com$ ~/go/bin/benchstat before after
    name       old time/op    new time/op    delta
    GetList-8    11.8ms ± 9%     9.9ms ± 3%  -15.98%  (p=0.000 n=10+10)

    name       old alloc/op   new alloc/op   delta
    GetList-8    99.5kB ± 2%    91.9kB ± 0%   -7.62%  (p=0.000 n=9+9)

    name       old allocs/op  new allocs/op  delta
    GetList-8     3.05k ± 1%     2.93k ± 0%   -3.83%  (p=0.000 n=8+9)

More later, once parsers can reuse strings from previous parses.

Updates #5958

Change-Id: I76cd5048246dd24d11c4e263d8bb8041747fb2b0
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-10-21 22:26:37 -07:00

44 lines
1.1 KiB
Go

// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package portlist
import (
"os/exec"
"syscall"
"time"
"golang.org/x/sys/windows"
)
// Forking on Windows is insanely expensive, so don't do it too often.
const pollInterval = 5 * time.Second
func appendListeningPorts(base []Port) ([]Port, error) {
// TODO(bradfitz): stop shelling out to netstat and use the
// net/netstat package instead. When doing so, be sure to filter
// out all of 127.0.0.0/8 and not just 127.0.0.1.
return appendListeningPortsNetstat(base, "-na")
}
func addProcesses(pl []Port) ([]Port, error) {
// OpenCurrentProcessToken instead of GetCurrentProcessToken,
// as GetCurrentProcessToken only works on Windows 8+.
tok, err := windows.OpenCurrentProcessToken()
if err != nil {
return nil, err
}
defer tok.Close()
if !tok.IsElevated() {
return appendListeningPortsNetstat(nil, "-na")
}
return appendListeningPortsNetstat(nil, "-nab")
}
func init() {
osHideWindow = func(c *exec.Cmd) {
c.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
}
}