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
|
|
|
|
2022-10-23 04:10:13 +00:00
|
|
|
// This file is just the types. The bulk of the code is in poller.go.
|
|
|
|
|
|
|
|
// The portlist package contains code that checks what ports are open and
|
|
|
|
// listening on the current machine.
|
2020-02-05 22:16:58 +00:00
|
|
|
package portlist
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-04-13 21:40:33 +00:00
|
|
|
"sort"
|
2020-02-05 22:16:58 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-03-14 03:53:58 +00:00
|
|
|
// Port is a listening port on the machine.
|
2020-02-05 22:16:58 +00:00
|
|
|
type Port struct {
|
2020-03-14 03:53:58 +00:00
|
|
|
Proto string // "tcp" or "udp"
|
|
|
|
Port uint16 // port number
|
|
|
|
Process string // optional process name, if found
|
2023-05-24 16:52:45 +00:00
|
|
|
Pid int // process id, if known
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 03:53:58 +00:00
|
|
|
// List is a list of Ports.
|
2020-02-05 22:16:58 +00:00
|
|
|
type List []Port
|
|
|
|
|
|
|
|
func (a *Port) lessThan(b *Port) bool {
|
2022-10-24 01:02:02 +00:00
|
|
|
if a.Port != b.Port {
|
|
|
|
return a.Port < b.Port
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2022-10-24 01:02:02 +00:00
|
|
|
if a.Proto != b.Proto {
|
|
|
|
return a.Proto < b.Proto
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2022-10-24 01:02:02 +00:00
|
|
|
return a.Process < b.Process
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
|
2022-10-24 01:02:02 +00:00
|
|
|
func (a *Port) equal(b *Port) bool {
|
|
|
|
return a.Port == b.Port &&
|
|
|
|
a.Proto == b.Proto &&
|
|
|
|
a.Process == b.Process
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
2022-10-24 01:02:02 +00:00
|
|
|
func (a List) equal(b List) bool {
|
2022-10-22 16:29:37 +00:00
|
|
|
if len(a) != len(b) {
|
2020-02-05 22:16:58 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := range a {
|
2022-10-24 01:02:02 +00:00
|
|
|
if !a[i].equal(&b[i]) {
|
2020-02-05 22:16:58 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pl List) String() string {
|
2020-03-14 03:53:58 +00:00
|
|
|
var sb strings.Builder
|
2020-02-05 22:16:58 +00:00
|
|
|
for _, v := range pl {
|
2022-10-24 01:02:02 +00:00
|
|
|
fmt.Fprintf(&sb, "%-3s %5d %#v\n",
|
|
|
|
v.Proto, v.Port, v.Process)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2020-03-14 03:53:58 +00:00
|
|
|
return strings.TrimRight(sb.String(), "\n")
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
2021-04-13 21:40:33 +00:00
|
|
|
// sortAndDedup sorts ps in place (by Port.lessThan) and then returns
|
|
|
|
// a subset of it with duplicate (Proto, Port) removed.
|
|
|
|
func sortAndDedup(ps List) List {
|
|
|
|
sort.Slice(ps, func(i, j int) bool {
|
|
|
|
return (&ps[i]).lessThan(&ps[j])
|
|
|
|
})
|
|
|
|
out := ps[:0]
|
|
|
|
var last Port
|
|
|
|
for _, p := range ps {
|
2023-05-24 16:52:45 +00:00
|
|
|
if last.Proto == p.Proto && last.Port == p.Port {
|
2021-04-13 21:40:33 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
out = append(out, p)
|
2023-05-24 16:52:45 +00:00
|
|
|
last = p
|
2021-04-13 21:40:33 +00:00
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|