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.
|
|
|
|
|
2021-02-19 18:03:00 +00:00
|
|
|
// +build darwin,amd64,!go1.16 darwin,go1.16
|
|
|
|
// +build !ios
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
package portlist
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2020-11-18 16:38:31 +00:00
|
|
|
"os/exec"
|
2020-02-05 22:16:58 +00:00
|
|
|
"strings"
|
2020-05-12 00:12:26 +01:00
|
|
|
"sync/atomic"
|
2020-03-14 03:53:58 +00:00
|
|
|
"time"
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// We have to run netstat, which is a bit expensive, so don't do it too often.
|
2020-03-14 03:53:58 +00:00
|
|
|
const pollInterval = 5 * time.Second
|
2020-02-05 22:16:58 +00:00
|
|
|
|
|
|
|
func listPorts() (List, error) {
|
|
|
|
return listPortsNetstat("-na")
|
|
|
|
}
|
|
|
|
|
2020-05-12 00:12:26 +01:00
|
|
|
var lsofFailed int64 // atomic bool
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
// In theory, lsof could replace the function of both listPorts() and
|
|
|
|
// addProcesses(), since it provides a superset of the netstat output.
|
|
|
|
// However, "netstat -na" runs ~100x faster than lsof on my machine, so
|
|
|
|
// we should do it only if the list of open ports has actually changed.
|
|
|
|
//
|
|
|
|
// TODO(apenwarr): this fails in a macOS sandbox (ie. our usual case).
|
|
|
|
// We might as well just delete this code if we can't find a solution.
|
|
|
|
func addProcesses(pl []Port) ([]Port, error) {
|
2020-05-12 00:12:26 +01:00
|
|
|
if atomic.LoadInt64(&lsofFailed) != 0 {
|
|
|
|
// This previously failed in the macOS sandbox, so don't try again.
|
|
|
|
return pl, nil
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
exe, err := exec.LookPath("lsof")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("lsof: lookup: %v", err)
|
|
|
|
}
|
2020-03-18 03:19:39 +00:00
|
|
|
output, err := exec.Command(exe, "-F", "-n", "-P", "-O", "-S2", "-T", "-i4", "-i6").Output()
|
2020-02-05 22:16:58 +00:00
|
|
|
if err != nil {
|
2020-05-12 00:12:26 +01:00
|
|
|
var stderr []byte
|
|
|
|
if xe, ok := err.(*exec.ExitError); ok {
|
|
|
|
stderr = xe.Stderr
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
// fails when run in a macOS sandbox, so make this non-fatal.
|
2020-05-12 00:12:26 +01:00
|
|
|
if atomic.CompareAndSwapInt64(&lsofFailed, 0, 1) {
|
|
|
|
log.Printf("portlist: can't run lsof in Mac sandbox; omitting process names from service list. Error details: %v, %s", err, bytes.TrimSpace(stderr))
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
return pl, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProtoPort struct {
|
|
|
|
proto string
|
|
|
|
port uint16
|
|
|
|
}
|
|
|
|
m := map[ProtoPort]*Port{}
|
|
|
|
for i := range pl {
|
|
|
|
pp := ProtoPort{pl[i].Proto, pl[i].Port}
|
|
|
|
m[pp] = &pl[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
r := bytes.NewReader(output)
|
|
|
|
scanner := bufio.NewScanner(r)
|
|
|
|
|
|
|
|
var cmd, proto string
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
2020-03-18 03:19:39 +00:00
|
|
|
if line == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
field, val := line[0], line[1:]
|
|
|
|
switch field {
|
|
|
|
case 'p':
|
2020-02-05 22:16:58 +00:00
|
|
|
// starting a new process
|
|
|
|
cmd = ""
|
|
|
|
proto = ""
|
2020-03-18 03:19:39 +00:00
|
|
|
case 'c':
|
|
|
|
cmd = val
|
|
|
|
case 'P':
|
|
|
|
proto = strings.ToLower(val)
|
|
|
|
case 'n':
|
|
|
|
if strings.Contains(val, "->") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// a listening port
|
|
|
|
port := parsePort(val)
|
|
|
|
if port > 0 {
|
|
|
|
pp := ProtoPort{proto, uint16(port)}
|
|
|
|
p := m[pp]
|
2020-09-02 23:42:33 +01:00
|
|
|
switch {
|
|
|
|
case p != nil:
|
2020-03-18 03:19:39 +00:00
|
|
|
p.Process = cmd
|
2020-09-02 23:42:33 +01:00
|
|
|
case isLoopbackAddr(val):
|
|
|
|
// ignore
|
|
|
|
default:
|
2020-03-18 03:19:39 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "weird: missing %v\n", pp)
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pl, nil
|
|
|
|
}
|