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 04:30:47 +01:00
|
|
|
import (
|
2020-11-18 16:38:31 +00:00
|
|
|
"os/exec"
|
2020-06-08 17:04:31 +01:00
|
|
|
"syscall"
|
2020-05-01 04:30:47 +01:00
|
|
|
"time"
|
2020-06-08 17:04:31 +01:00
|
|
|
|
2020-10-28 16:18:18 +00:00
|
|
|
"golang.org/x/sys/windows"
|
2020-05-01 04:30:47 +01: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
|
|
|
|
|
|
|
func listPorts() (List, error) {
|
2021-04-12 17:23:49 +01: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.
|
2020-02-05 22:16:58 +00:00
|
|
|
return listPortsNetstat("-na")
|
|
|
|
}
|
|
|
|
|
|
|
|
func addProcesses(pl []Port) ([]Port, error) {
|
2021-04-12 17:23:49 +01:00
|
|
|
// 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() {
|
2020-10-28 16:18:18 +00:00
|
|
|
return listPortsNetstat("-na")
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
return listPortsNetstat("-nab")
|
|
|
|
}
|
2020-06-08 17:04:31 +01:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
osHideWindow = func(c *exec.Cmd) {
|
|
|
|
c.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
|
|
|
|
}
|
|
|
|
}
|