portlist: de-dup services on same (proto, port) on both IPv4/IPv6

Fixes #1703

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick 2021-04-13 14:40:33 -07:00
parent a8d95a18b2
commit 2df6372b67
2 changed files with 21 additions and 6 deletions

View File

@ -6,6 +6,7 @@
import (
"fmt"
"sort"
"strings"
)
@ -76,6 +77,7 @@ func GetList(prev List) (List, error) {
if err != nil {
return nil, fmt.Errorf("listPorts: %s", err)
}
pl = sortAndDedup(pl)
if pl.sameInodes(prev) {
// Nothing changed, skip inode lookup
return prev, nil
@ -86,3 +88,22 @@ func GetList(prev List) (List, error) {
}
return pl, nil
}
// 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 {
protoPort := Port{Proto: p.Proto, Port: p.Port}
if last == protoPort {
continue
}
out = append(out, p)
last = protoPort
}
return out
}

View File

@ -12,7 +12,6 @@
"os"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
"syscall"
@ -71,11 +70,6 @@ func listPorts() (List, error) {
l = append(l, ports...)
}
sort.Slice(l, func(i, j int) bool {
return (&l[i]).lessThan(&l[j])
})
return l, nil
}