mirror of
https://github.com/tailscale/tailscale.git
synced 2024-11-25 19:15:34 +00:00
portlist: add package doc, file comments, move a method to the right file
And respect envknob earlier. NewPoller has one caller and ignores errors; they just signal ipnlocal to log a warning and not use the portlist poller. Change-Id: I4a33af936fe780cca8c7197d4d74ac31a1dc01e3 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
parent
774fa72d32
commit
70dde89c34
@ -2,16 +2,23 @@
|
|||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// This file contains the code related to the Poller type and its methods.
|
||||||
|
// The hot loop to keep efficient is Poller.Run.
|
||||||
|
|
||||||
package portlist
|
package portlist
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"tailscale.com/envknob"
|
||||||
"tailscale.com/version"
|
"tailscale.com/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var debugDisablePortlist = envknob.RegisterBool("TS_DEBUG_DISABLE_PORTLIST")
|
||||||
|
|
||||||
// Poller scans the systems for listening ports periodically and sends
|
// Poller scans the systems for listening ports periodically and sends
|
||||||
// the results to C.
|
// the results to C.
|
||||||
type Poller struct {
|
type Poller struct {
|
||||||
@ -35,6 +42,9 @@ func NewPoller() (*Poller, error) {
|
|||||||
if version.OS() == "iOS" {
|
if version.OS() == "iOS" {
|
||||||
return nil, errors.New("not available on iOS")
|
return nil, errors.New("not available on iOS")
|
||||||
}
|
}
|
||||||
|
if debugDisablePortlist() {
|
||||||
|
return nil, errors.New("portlist disabled by envknob")
|
||||||
|
}
|
||||||
p := &Poller{
|
p := &Poller{
|
||||||
c: make(chan List),
|
c: make(chan List),
|
||||||
runDone: make(chan struct{}),
|
runDone: make(chan struct{}),
|
||||||
@ -113,3 +123,24 @@ func (p *Poller) Run(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
@ -2,14 +2,16 @@
|
|||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// This file is just the types. The bulk of the code is in poller.go.
|
||||||
|
|
||||||
|
// The portlist package contains code that checks what ports are open and
|
||||||
|
// listening on the current machine.
|
||||||
package portlist
|
package portlist
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"tailscale.com/envknob"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Port is a listening port on the machine.
|
// Port is a listening port on the machine.
|
||||||
@ -74,29 +76,6 @@ func (pl List) String() string {
|
|||||||
return strings.TrimRight(sb.String(), "\n")
|
return strings.TrimRight(sb.String(), "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
var debugDisablePortlist = envknob.RegisterBool("TS_DEBUG_DISABLE_PORTLIST")
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// sortAndDedup sorts ps in place (by Port.lessThan) and then returns
|
// sortAndDedup sorts ps in place (by Port.lessThan) and then returns
|
||||||
// a subset of it with duplicate (Proto, Port) removed.
|
// a subset of it with duplicate (Proto, Port) removed.
|
||||||
func sortAndDedup(ps List) List {
|
func sortAndDedup(ps List) List {
|
||||||
|
Loading…
Reference in New Issue
Block a user