mirror of
https://github.com/tailscale/tailscale.git
synced 2024-11-25 19:15:34 +00:00
036f70b7b4
Add an osImpl interface that can be stateful and thus more efficient between calls. It will later be implemented by all OSes but for now this change only adds a Linux implementation. Remove Port.inode. It was only used by Linux and moves into its osImpl. Don't reopen /proc/net/* files on each run. Turns out you can just keep then open and seek to the beginning and reread and the contents are fresh. name old time/op new time/op delta GetListIncremental-8 7.29ms ± 2% 6.53ms ± 1% -10.50% (p=0.000 n=9+9) name old alloc/op new alloc/op delta GetListIncremental-8 1.30kB ±13% 0.70kB ± 5% -46.38% (p=0.000 n=9+10) name old allocs/op new allocs/op delta GetListIncremental-8 33.2 ±11% 18.0 ± 0% -45.82% (p=0.000 n=9+10) Updates #5958 Change-Id: I4be83463cbd23c2e2fa5d0bdf38560004f53401b Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
103 lines
2.8 KiB
Go
103 lines
2.8 KiB
Go
// 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.
|
|
|
|
//go:build !ios && !js
|
|
// +build !ios,!js
|
|
|
|
package portlist
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
func TestParsePort(t *testing.T) {
|
|
type InOut struct {
|
|
in string
|
|
expect int
|
|
}
|
|
tests := []InOut{
|
|
{"1.2.3.4:5678", 5678},
|
|
{"0.0.0.0.999", 999},
|
|
{"1.2.3.4:*", 0},
|
|
{"5.5.5.5:0", 0},
|
|
{"[1::2]:5", 5},
|
|
{"[1::2].5", 5},
|
|
{"gibberish", -1},
|
|
}
|
|
|
|
for _, io := range tests {
|
|
got := parsePort(io.in)
|
|
if got != io.expect {
|
|
t.Fatalf("input:%#v expect:%v got:%v\n", io.in, io.expect, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
const netstatOutput = `
|
|
// linux
|
|
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
|
|
udp 0 0 0.0.0.0:5353 0.0.0.0:*
|
|
udp6 0 0 :::5353 :::*
|
|
udp6 0 0 :::5354 :::*
|
|
|
|
// macOS
|
|
tcp4 0 0 *.23 *.* LISTEN
|
|
tcp6 0 0 *.24 *.* LISTEN
|
|
tcp4 0 0 *.8185 *.* LISTEN
|
|
tcp4 0 0 127.0.0.1.8186 *.* LISTEN
|
|
tcp6 0 0 ::1.8187 *.* LISTEN
|
|
tcp4 0 0 127.1.2.3.8188 *.* LISTEN
|
|
|
|
udp6 0 0 *.5453 *.*
|
|
udp4 0 0 *.5553 *.*
|
|
|
|
// Windows 10
|
|
Proto Local Address Foreign Address State
|
|
TCP 0.0.0.0:32 0.0.0.0:0 LISTENING
|
|
[sshd.exe]
|
|
UDP 0.0.0.0:5050 *:*
|
|
CDPSvc
|
|
[svchost.exe]
|
|
UDP 0.0.0.0:53 *:*
|
|
[chrome.exe]
|
|
UDP 10.0.1.43:9353 *:*
|
|
[iTunes.exe]
|
|
UDP [::]:53 *:*
|
|
UDP [::]:53 *:*
|
|
[funball.exe]
|
|
`
|
|
|
|
func TestParsePortsNetstat(t *testing.T) {
|
|
want := List{
|
|
Port{"tcp", 22, ""},
|
|
Port{"tcp", 23, ""},
|
|
Port{"tcp", 24, ""},
|
|
Port{"tcp", 32, "sshd"},
|
|
Port{"udp", 53, "chrome"},
|
|
Port{"udp", 53, "funball"},
|
|
Port{"udp", 5050, "CDPSvc"},
|
|
Port{"udp", 5353, ""},
|
|
Port{"udp", 5354, ""},
|
|
Port{"udp", 5453, ""},
|
|
Port{"udp", 5553, ""},
|
|
Port{"tcp", 8185, ""}, // but not 8186, 8187, 8188 on localhost
|
|
Port{"udp", 9353, "iTunes"},
|
|
}
|
|
|
|
pl := appendParsePortsNetstat(nil, netstatOutput)
|
|
jgot, _ := json.MarshalIndent(pl, "", "\t")
|
|
jwant, _ := json.MarshalIndent(want, "", "\t")
|
|
if len(pl) != len(want) {
|
|
t.Fatalf("Got:\n%s\n\nWant:\n%s\n", jgot, jwant)
|
|
}
|
|
for i := range pl {
|
|
if pl[i] != want[i] {
|
|
t.Errorf("row#%d\n got: %#v\n\nwant: %#v\n",
|
|
i, pl[i], want[i])
|
|
t.Fatalf("Got:\n%s\n\nWant:\n%s\n", jgot, jwant)
|
|
}
|
|
}
|
|
}
|