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.
|
|
|
|
|
|
|
|
package portlist
|
|
|
|
|
|
|
|
import (
|
2020-03-14 03:53:58 +00:00
|
|
|
"context"
|
2020-04-07 04:11:24 +01:00
|
|
|
"errors"
|
2020-02-05 22:16:58 +00:00
|
|
|
"time"
|
2020-04-07 04:11:24 +01:00
|
|
|
|
|
|
|
"tailscale.com/version"
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
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 {
|
2020-03-14 03:53:58 +00:00
|
|
|
// C received the list of ports periodically. It's closed when
|
|
|
|
// Run completes, after which Err can be checked.
|
|
|
|
C <-chan List
|
|
|
|
|
|
|
|
c chan List
|
|
|
|
|
|
|
|
// Err is the error from the final GetList call. It is only
|
|
|
|
// valid to read once C has been closed. Err is nil if Close
|
|
|
|
// is called or the context is canceled.
|
|
|
|
Err error
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
quitCh chan struct{} // close this to force exit
|
|
|
|
prev List // most recent data
|
|
|
|
}
|
|
|
|
|
2020-03-14 03:53:58 +00:00
|
|
|
// NewPoller returns a new portlist Poller. It returns an error
|
2020-04-07 04:11:24 +01: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 04:11:24 +01:00
|
|
|
return nil, errors.New("not available on iOS")
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
p := &Poller{
|
2020-03-14 03:53:58 +00:00
|
|
|
c: make(chan List),
|
2020-02-05 22:16:58 +00:00
|
|
|
quitCh: make(chan struct{}),
|
|
|
|
}
|
2020-03-14 03:53:58 +00:00
|
|
|
p.C = p.c
|
|
|
|
|
|
|
|
// Do one initial poll synchronously so we can return an error
|
|
|
|
// early.
|
|
|
|
var err error
|
|
|
|
p.prev, err = GetList(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return p, nil
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 03:53:58 +00:00
|
|
|
func (p *Poller) Close() error {
|
|
|
|
select {
|
|
|
|
case <-p.quitCh:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
close(p.quitCh)
|
|
|
|
<-p.C
|
2020-03-14 03:53:58 +00:00
|
|
|
return nil
|
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.
|
|
|
|
func (p *Poller) Run(ctx context.Context) error {
|
|
|
|
defer close(p.c)
|
|
|
|
tick := time.NewTicker(pollInterval)
|
2020-02-05 22:16:58 +00:00
|
|
|
defer tick.Stop()
|
|
|
|
|
|
|
|
// Send out the pre-generated initial value
|
2020-03-14 03:53:58 +00:00
|
|
|
p.c <- p.prev
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-tick.C:
|
|
|
|
pl, err := GetList(p.prev)
|
|
|
|
if err != nil {
|
|
|
|
p.Err = err
|
2020-03-14 03:53:58 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-04-09 23:16:36 +01: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
|
|
|
|
select {
|
|
|
|
case p.c <- pl:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
case <-p.quitCh:
|
|
|
|
return nil
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2020-03-14 03:53:58 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
2020-02-05 22:16:58 +00:00
|
|
|
case <-p.quitCh:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|