2020-02-05 22:16:58 +00:00
|
|
|
// 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
|
|
|
|
|
2020-05-01 03:30:47 +00:00
|
|
|
import (
|
2020-11-18 16:38:31 +00:00
|
|
|
"os/exec"
|
2020-06-08 16:04:31 +00:00
|
|
|
"syscall"
|
2020-05-01 03:30:47 +00:00
|
|
|
"time"
|
2020-06-08 16:04:31 +00:00
|
|
|
|
2020-10-28 16:18:18 +00:00
|
|
|
"golang.org/x/sys/windows"
|
2020-05-01 03:30:47 +00:00
|
|
|
)
|
2020-03-14 03:53:58 +00:00
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
// Forking on Windows is insanely expensive, so don't do it too often.
|
2020-03-14 03:53:58 +00:00
|
|
|
const pollInterval = 5 * time.Second
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2022-10-22 04:30:40 +00:00
|
|
|
func appendListeningPorts(base []Port) ([]Port, error) {
|
2021-04-12 16:23:49 +00:00
|
|
|
// 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.
|
2022-10-22 04:30:40 +00:00
|
|
|
return appendListeningPortsNetstat(base, "-na")
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func addProcesses(pl []Port) ([]Port, error) {
|
2021-06-18 16:22:55 +00:00
|
|
|
// OpenCurrentProcessToken instead of GetCurrentProcessToken,
|
2021-04-12 16:23:49 +00:00
|
|
|
// as GetCurrentProcessToken only works on Windows 8+.
|
|
|
|
tok, err := windows.OpenCurrentProcessToken()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer tok.Close()
|
|
|
|
if !tok.IsElevated() {
|
2022-10-22 04:30:40 +00:00
|
|
|
return appendListeningPortsNetstat(nil, "-na")
|
2020-10-28 16:18:18 +00:00
|
|
|
}
|
2022-10-22 04:30:40 +00:00
|
|
|
return appendListeningPortsNetstat(nil, "-nab")
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2020-06-08 16:04:31 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
osHideWindow = func(c *exec.Cmd) {
|
|
|
|
c.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
|
|
|
|
}
|
|
|
|
}
|