2020-02-05 22:16:58 +00:00
|
|
|
// 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.
|
|
|
|
|
2022-10-23 04:10:13 +00:00
|
|
|
// This file contains the code related to the Poller type and its methods.
|
|
|
|
// The hot loop to keep efficient is Poller.Run.
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
package portlist
|
|
|
|
|
|
|
|
import (
|
2020-03-14 03:53:58 +00:00
|
|
|
"context"
|
2020-04-07 03:11:24 +00:00
|
|
|
"errors"
|
2022-10-23 04:10:13 +00:00
|
|
|
"fmt"
|
2020-02-05 22:16:58 +00:00
|
|
|
"time"
|
2020-04-07 03:11:24 +00:00
|
|
|
|
2022-10-23 04:10:13 +00:00
|
|
|
"tailscale.com/envknob"
|
2020-04-07 03:11:24 +00:00
|
|
|
"tailscale.com/version"
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
2022-10-23 04:10:13 +00:00
|
|
|
var debugDisablePortlist = envknob.RegisterBool("TS_DEBUG_DISABLE_PORTLIST")
|
|
|
|
|
2020-03-14 03:53:58 +00:00
|
|
|
// Poller scans the systems for listening ports periodically and sends
|
|
|
|
// the results to C.
|
2020-02-05 22:16:58 +00:00
|
|
|
type Poller struct {
|
2022-10-22 05:39:59 +00:00
|
|
|
c chan List // unbuffered
|
2020-03-14 03:53:58 +00:00
|
|
|
|
2022-10-22 05:39:59 +00:00
|
|
|
// closeCtx is the context that's canceled on Close.
|
|
|
|
closeCtx context.Context
|
|
|
|
closeCtxCancel context.CancelFunc
|
|
|
|
|
|
|
|
runDone chan struct{} // closed when Run completes
|
2020-03-14 03:53:58 +00:00
|
|
|
|
2022-10-22 04:30:40 +00:00
|
|
|
// scatch is memory for Poller.getList to reuse between calls.
|
|
|
|
scratch []Port
|
|
|
|
|
2022-10-22 05:39:59 +00:00
|
|
|
prev List // most recent data
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 03:53:58 +00:00
|
|
|
// NewPoller returns a new portlist Poller. It returns an error
|
2020-04-07 03:11:24 +00:00
|
|
|
// if the portlist couldn't be obtained.
|
2020-02-05 22:16:58 +00:00
|
|
|
func NewPoller() (*Poller, error) {
|
2020-11-11 17:04:34 +00:00
|
|
|
if version.OS() == "iOS" {
|
2020-04-07 03:11:24 +00:00
|
|
|
return nil, errors.New("not available on iOS")
|
|
|
|
}
|
2022-10-23 04:10:13 +00:00
|
|
|
if debugDisablePortlist() {
|
|
|
|
return nil, errors.New("portlist disabled by envknob")
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
p := &Poller{
|
2022-10-22 05:39:59 +00:00
|
|
|
c: make(chan List),
|
|
|
|
runDone: make(chan struct{}),
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2022-10-22 05:39:59 +00:00
|
|
|
p.closeCtx, p.closeCtxCancel = context.WithCancel(context.Background())
|
2020-03-14 03:53:58 +00:00
|
|
|
|
|
|
|
// Do one initial poll synchronously so we can return an error
|
|
|
|
// early.
|
|
|
|
var err error
|
2022-10-22 04:30:40 +00:00
|
|
|
p.prev, err = p.getList()
|
2020-03-14 03:53:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return p, nil
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 05:39:59 +00:00
|
|
|
// Updates return the channel that receives port list updates.
|
|
|
|
//
|
|
|
|
// The channel is closed when the Poller is closed.
|
|
|
|
func (p *Poller) Updates() <-chan List { return p.c }
|
|
|
|
|
|
|
|
// Close closes the Poller.
|
|
|
|
// Run will return with a nil error.
|
2020-03-14 03:53:58 +00:00
|
|
|
func (p *Poller) Close() error {
|
2022-10-22 05:39:59 +00:00
|
|
|
p.closeCtxCancel()
|
|
|
|
<-p.runDone
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// send sends pl to p.c and returns whether it was successfully sent.
|
|
|
|
func (p *Poller) send(ctx context.Context, pl List) (sent bool, err error) {
|
2020-03-14 03:53:58 +00:00
|
|
|
select {
|
2022-10-22 05:39:59 +00:00
|
|
|
case p.c <- pl:
|
|
|
|
return true, nil
|
|
|
|
case <-ctx.Done():
|
|
|
|
return false, ctx.Err()
|
|
|
|
case <-p.closeCtx.Done():
|
|
|
|
return false, nil
|
2020-03-14 03:53:58 +00:00
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 03:53:58 +00:00
|
|
|
// Run runs the Poller periodically until either the context
|
|
|
|
// is done, or the Close is called.
|
2022-10-22 05:39:59 +00:00
|
|
|
//
|
|
|
|
// Run may only be called once.
|
2020-03-14 03:53:58 +00:00
|
|
|
func (p *Poller) Run(ctx context.Context) error {
|
2022-10-22 05:39:59 +00:00
|
|
|
defer close(p.runDone)
|
2020-03-14 03:53:58 +00:00
|
|
|
defer close(p.c)
|
2022-10-22 05:39:59 +00:00
|
|
|
|
2020-03-14 03:53:58 +00:00
|
|
|
tick := time.NewTicker(pollInterval)
|
2020-02-05 22:16:58 +00:00
|
|
|
defer tick.Stop()
|
|
|
|
|
2022-10-22 05:39:59 +00:00
|
|
|
// Send out the pre-generated initial value.
|
|
|
|
if sent, err := p.send(ctx, p.prev); !sent {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-tick.C:
|
2022-10-22 04:30:40 +00:00
|
|
|
pl, err := p.getList()
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
2020-03-14 03:53:58 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-04-09 22:16:36 +00:00
|
|
|
if pl.sameInodes(p.prev) {
|
2020-03-14 03:53:58 +00:00
|
|
|
continue
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2020-03-14 03:53:58 +00:00
|
|
|
p.prev = pl
|
2022-10-22 05:39:59 +00:00
|
|
|
if sent, err := p.send(ctx, p.prev); !sent {
|
|
|
|
return err
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2020-03-14 03:53:58 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
2022-10-22 05:39:59 +00:00
|
|
|
case <-p.closeCtx.Done():
|
2020-02-05 22:16:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-23 04:10:13 +00:00
|
|
|
|
|
|
|
func (p *Poller) getList() (List, error) {
|
|
|
|
if debugDisablePortlist() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
p.scratch, err = appendListeningPorts(p.scratch[:0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("listPorts: %s", err)
|
|
|
|
}
|
|
|
|
pl := sortAndDedup(p.scratch)
|
|
|
|
if pl.sameInodes(p.prev) {
|
|
|
|
// Nothing changed, skip inode lookup
|
|
|
|
return p.prev, nil
|
|
|
|
}
|
|
|
|
pl, err = addProcesses(pl)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("addProcesses: %s", err)
|
|
|
|
}
|
|
|
|
return pl, nil
|
|
|
|
}
|