Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

portdisco: switch to upstream portlist package #10

Merged
merged 1 commit into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/mitchellh/go-ps v1.0.0
golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53
golang.org/x/sync v0.2.0
tailscale.com v1.1.1-0.20230525175241-fb13df273a24
tailscale.com v1.1.1-0.20230607142209-f8f0b981ac9c
)

require (
Expand All @@ -27,9 +27,9 @@ require (
go4.org/netipx v0.0.0-20230303233057-f1b76eb4bb35 // indirect
golang.org/x/crypto v0.8.0 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/tools v0.8.0 // indirect
golang.org/x/tools v0.9.1 // indirect
golang.zx2c4.com/wireguard/windows v0.5.3 // indirect
)
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 h1:5llv2sWeaMSnA3w2kS57ouQQ4
golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk=
golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM=
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI=
golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.4.1-0.20230131160137-e7d7f63158de/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/tools v0.8.0 h1:vSDcovVPld282ceKgDimkRSC8kpaH1dgyc9UMzlt84Y=
golang.org/x/tools v0.8.0/go.mod h1:JxBZ99ISMI5ViVkT1tr6tdNmXeTrcpVSD3vZ1RsRdN4=
golang.org/x/tools v0.9.1 h1:8WMNJAz3zrtPmnYC7ISf5dEn3MT0gY7jBJfw27yrrLo=
golang.org/x/tools v0.9.1/go.mod h1:owI94Op576fPu3cIGQeHs3joujW/2Oc6MtlxbF5dfNc=
golang.zx2c4.com/wireguard/windows v0.5.3 h1:On6j2Rpn3OEMXqBq00QEDC7bWSZrPIHKIus8eIuExIE=
golang.zx2c4.com/wireguard/windows v0.5.3/go.mod h1:9TEe8TJmtwyQebdFwAkEWOPr3prrtqm+REGFifP60hI=
tailscale.com v1.1.1-0.20230525175241-fb13df273a24 h1:LmWHgdVP1DLbLAOkk90KC4s52HcOEjS+VvvGiXqpL7Q=
tailscale.com v1.1.1-0.20230525175241-fb13df273a24/go.mod h1:6WD9//Juq/UWwV9Pfpfqo1/n0t59U6MBrhZLVLip1zw=
tailscale.com v1.1.1-0.20230607142209-f8f0b981ac9c h1:w69HAs+pB67hX+o3pvP7RSnOU8CaiPY9s1XkqwXkBaU=
tailscale.com v1.1.1-0.20230607142209-f8f0b981ac9c/go.mod h1:1t0LGzRAX3TBG4k5XtXaAEmYxKnx6b2k18m7j71G3Po=
12 changes: 3 additions & 9 deletions tsrelay/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,20 +207,14 @@ func (h *httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
portMap := map[uint16]string{}
go func() {
defer wg.Done()
p := &portlist.Poller{
Interval: 10 * time.Second,
IncludeLocalhost: true,
}
p := &portlist.Poller{IncludeLocalhost: true}
defer p.Close()
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
ch, err := p.Run(ctx)
ports, _, err := p.Poll()
if err != nil {
h.l.Printf("error polling for serve: %v", err)
return
}
update := <-ch
for _, p := range update.List {
for _, p := range ports {
portMap[p.Port] = p.Process
}
}()
Expand Down
50 changes: 26 additions & 24 deletions tsrelay/portdisco.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,6 @@ type wsMessage struct {
func (h *httpHandler) runPortDisco(ctx context.Context, c *websocket.Conn) error {
defer c.Close()

p := &portlist.Poller{
Interval: 3 * time.Second,
IncludeLocalhost: true,
}
ch, err := p.Run(ctx)
if err != nil {
return fmt.Errorf("error running poller: %w", err)
}

go func() {
for {
if ctx.Err() != nil {
Expand Down Expand Up @@ -71,37 +62,48 @@ func (h *httpHandler) runPortDisco(ctx context.Context, c *websocket.Conn) error
}
}()

isFirst := true
p := &portlist.Poller{
IncludeLocalhost: true,
}
ticker := time.NewTicker(3 * time.Second)
defer ticker.Stop()

// eagerly load already open ports to avoid spam notifications
ports, _, err := p.Poll()
if err != nil {
return fmt.Errorf("error running initial poll: %w", err)
}
for _, p := range ports {
if p.Proto != "tcp" {
continue
}
h.l.VPrintln("pre-setting", p.Port, p.Pid, p.Process)
h.prev[p.Port] = p
}
h.l.Println("initial ports are set")

for {
select {
case <-ctx.Done():
return ctx.Err()
case up := <-ch:
if up.Error != nil {
case <-ticker.C:
ports, changed, err := p.Poll()
if err != nil {
h.l.Printf("error receiving portlist update: %v", err)
continue
}
if isFirst {
isFirst = false
for _, p := range up.List {
if p.Proto != "tcp" {
continue
}
h.l.VPrintln("pre-setting", p.Port, p.Pid, p.Process)
h.prev[p.Port] = p
}
h.l.Println("initial ports are set")
if !changed {
continue
}
err := h.handlePortUpdates(c, up.List)
err = h.handlePortUpdates(c, ports)
if err != nil {
return fmt.Errorf("error handling port updates: %w", err)
}
}
}
}

func (h *httpHandler) handlePortUpdates(c *websocket.Conn, up portlist.List) error {
func (h *httpHandler) handlePortUpdates(c *websocket.Conn, up []portlist.Port) error {
h.l.VPrintln("ports were updated")
h.Lock()
h.l.VPrintln("up is", len(up))
Expand Down