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

Fixes #1703

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/1704/head
Brad Fitzpatrick 4 years ago
parent a8d95a18b2
commit 2df6372b67

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

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

Loading…
Cancel
Save