2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
package portlist
|
|
|
|
|
2020-05-01 04:30:47 +01:00
|
|
|
import (
|
|
|
|
"time"
|
2020-06-08 17:04:31 +01:00
|
|
|
|
2022-10-24 06:07:25 +01:00
|
|
|
"tailscale.com/net/netstat"
|
2020-05-01 04:30:47 +01:00
|
|
|
)
|
2020-03-14 03:53:58 +00:00
|
|
|
|
2022-10-24 06:07:25 +01:00
|
|
|
func init() {
|
|
|
|
newOSImpl = newWindowsImpl
|
2022-11-04 13:41:36 +00:00
|
|
|
// The portlist poller used to fork on Windows, which is insanely expensive,
|
|
|
|
// so historically we only did this every 5 seconds on Windows. Maybe we
|
|
|
|
// could reduce it down to 1 seconds like Linux, but nobody's benchmarked as
|
|
|
|
// of 2022-11-04.
|
|
|
|
pollInterval = 5 * time.Second
|
2022-10-24 06:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type famPort struct {
|
|
|
|
proto string
|
|
|
|
port uint16
|
2022-11-18 20:09:01 +00:00
|
|
|
pid uint32
|
2022-10-24 06:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type windowsImpl struct {
|
2023-05-24 17:52:45 +01:00
|
|
|
known map[famPort]*portMeta // inode string => metadata
|
|
|
|
includeLocalhost bool
|
2022-10-24 06:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type portMeta struct {
|
|
|
|
port Port
|
|
|
|
keep bool
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
2023-05-24 17:52:45 +01:00
|
|
|
func newWindowsImpl(includeLocalhost bool) osImpl {
|
2022-10-24 06:07:25 +01:00
|
|
|
return &windowsImpl{
|
2023-05-24 17:52:45 +01:00
|
|
|
known: map[famPort]*portMeta{},
|
|
|
|
includeLocalhost: includeLocalhost,
|
2022-10-24 06:07:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*windowsImpl) Close() error { return nil }
|
|
|
|
|
|
|
|
func (im *windowsImpl) AppendListeningPorts(base []Port) ([]Port, error) {
|
|
|
|
// TODO(bradfitz): netstat.Get makes a bunch of garbage. Add an Append-style
|
|
|
|
// API to that package instead/additionally.
|
|
|
|
tab, err := netstat.Get()
|
2021-04-12 17:23:49 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-10-24 06:07:25 +01:00
|
|
|
|
|
|
|
for _, pm := range im.known {
|
|
|
|
pm.keep = false
|
2020-10-28 16:18:18 +00:00
|
|
|
}
|
2022-10-24 06:07:25 +01:00
|
|
|
|
|
|
|
ret := base
|
|
|
|
for _, e := range tab.Entries {
|
|
|
|
if e.State != "LISTEN" {
|
|
|
|
continue
|
|
|
|
}
|
2023-05-24 17:52:45 +01:00
|
|
|
if !im.includeLocalhost && !e.Local.Addr().IsUnspecified() {
|
2022-10-24 06:07:25 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
fp := famPort{
|
|
|
|
proto: "tcp", // TODO(bradfitz): UDP too; add to netstat
|
|
|
|
port: e.Local.Port(),
|
2022-11-18 20:09:01 +00:00
|
|
|
pid: uint32(e.Pid),
|
2022-10-24 06:07:25 +01:00
|
|
|
}
|
|
|
|
pm, ok := im.known[fp]
|
|
|
|
if ok {
|
|
|
|
pm.keep = true
|
|
|
|
continue
|
|
|
|
}
|
2022-11-18 20:09:01 +00:00
|
|
|
var process string
|
|
|
|
if e.OSMetadata != nil {
|
|
|
|
if module, err := e.OSMetadata.GetModule(); err == nil {
|
|
|
|
process = module
|
|
|
|
}
|
|
|
|
}
|
2022-10-24 06:07:25 +01:00
|
|
|
pm = &portMeta{
|
|
|
|
keep: true,
|
|
|
|
port: Port{
|
|
|
|
Proto: "tcp",
|
|
|
|
Port: e.Local.Port(),
|
2022-11-18 20:09:01 +00:00
|
|
|
Process: process,
|
2023-05-24 17:52:45 +01:00
|
|
|
Pid: e.Pid,
|
2022-10-24 06:07:25 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
im.known[fp] = pm
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, m := range im.known {
|
|
|
|
if !m.keep {
|
|
|
|
delete(im.known, k)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ret = append(ret, m.port)
|
|
|
|
}
|
2020-06-08 17:04:31 +01:00
|
|
|
|
2022-11-18 20:09:01 +00:00
|
|
|
return sortAndDedup(ret), nil
|
2022-10-24 06:07:25 +01:00
|
|
|
}
|