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"
|
2022-10-24 01:02:02 +00:00
|
|
|
"sync"
|
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-24 01:02:02 +00:00
|
|
|
// os, if non-nil, is an OS-specific implementation of the portlist getting
|
|
|
|
// code. When non-nil, it's responsible for getting the complete list of
|
|
|
|
// cached ports complete with the process name. That is, when set,
|
|
|
|
// addProcesses is not used.
|
|
|
|
//
|
|
|
|
// This is part of a multi-step migration (starting 2022-10-22) to move to
|
|
|
|
// using osImpl for all of Linux, macOS (unsandboxed), and Windows. But
|
|
|
|
// during the transition period, we support this being nil.
|
|
|
|
// TODO(bradfitz): finish that migration.
|
|
|
|
os osImpl
|
|
|
|
osOnce sync.Once // guards init of os
|
|
|
|
|
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-24 01:02:02 +00:00
|
|
|
prev List // most recent data, not aliasing scratch
|
|
|
|
}
|
|
|
|
|
|
|
|
// osImpl is the OS-specific implementation of getting the open listening ports.
|
|
|
|
type osImpl interface {
|
|
|
|
Close() error
|
|
|
|
|
|
|
|
// AppendListeningPorts appends to base (which must have length 0 but
|
|
|
|
// optional capacity) the list of listening ports. The Port struct should be
|
|
|
|
// populated as completely as possible. Another pass will not add anything
|
|
|
|
// to it.
|
|
|
|
//
|
|
|
|
// The appended ports should be in a sorted (or at least stable) order so
|
|
|
|
// the caller can cheaply detect when there are no changes.
|
|
|
|
AppendListeningPorts(base []Port) ([]Port, error)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
2022-10-24 01:02:02 +00:00
|
|
|
// newOSImpl, if non-nil, constructs a new osImpl.
|
|
|
|
var newOSImpl func() osImpl
|
|
|
|
|
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())
|
2022-10-24 01:02:02 +00:00
|
|
|
p.osOnce.Do(p.initOSField)
|
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-24 01:02:02 +00:00
|
|
|
func (p *Poller) initOSField() {
|
|
|
|
if newOSImpl != nil {
|
|
|
|
p.os = newOSImpl()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2022-10-24 01:02:02 +00:00
|
|
|
if p.os != nil {
|
|
|
|
p.os.Close()
|
|
|
|
}
|
2022-10-22 05:39:59 +00:00
|
|
|
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
|
|
|
|
}
|
2022-10-24 01:02:02 +00:00
|
|
|
if pl.equal(p.prev) {
|
2020-03-14 03:53:58 +00:00
|
|
|
continue
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2022-10-24 01:02:02 +00:00
|
|
|
// New value. Make a copy, as pl might alias pl.scratch
|
|
|
|
// and prev must not.
|
|
|
|
p.prev = append([]Port(nil), 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
|
|
|
|
}
|
2022-10-24 01:02:02 +00:00
|
|
|
p.osOnce.Do(p.initOSField)
|
2022-10-23 04:10:13 +00:00
|
|
|
var err error
|
2022-10-24 01:02:02 +00:00
|
|
|
if p.os != nil {
|
|
|
|
p.scratch, err = p.os.AppendListeningPorts(p.scratch[:0])
|
|
|
|
return p.scratch, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Old path for OSes that don't have osImpl yet.
|
|
|
|
// TODO(bradfitz): delete these when macOS and Windows are converted.
|
2022-10-23 04:10:13 +00:00
|
|
|
p.scratch, err = appendListeningPorts(p.scratch[:0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("listPorts: %s", err)
|
|
|
|
}
|
|
|
|
pl := sortAndDedup(p.scratch)
|
2022-10-24 01:02:02 +00:00
|
|
|
if pl.equal(p.prev) {
|
2022-10-23 04:10:13 +00:00
|
|
|
// 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
|
|
|
|
}
|