ports

package
v0.4.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ports answers "is anything listening on this port, and who is it".

Index

Constants

This section is empty.

Variables

View Source
var ErrNoOwner = errors.New("the other end of the connection was not found")

ErrNoOwner means the client end of a connection was not found, as for a connection from a process of another account that lsof cannot see.

View Source
var ErrUnsupported = errors.New("connection owners cannot be looked up on this platform")

ErrUnsupported means this platform cannot tell who holds a connection.

Functions

func Cwds

func Cwds(ctx context.Context, pids []int) map[int]string

Cwds returns the working directory of each pid, where it can be read.

func IPv6Loopback

func IPv6Loopback() bool

IPv6Loopback reports whether the system has IPv6 loopback.

func Listening

func Listening(port int) bool

Listening reports whether a TCP connection to the port on loopback succeeds. Both IPv4 and IPv6 are tried because dev servers bind to either.

func LoopbackListeners

func LoopbackListeners(ctx context.Context, port int) map[string]int

LoopbackListeners returns the pid listening on port at each loopback address ("127.0.0.1", "::1"), where lsof or ss can see it.

Types

type ConnOwner

type ConnOwner struct {
	UID int
	// Self is true when the client end belongs to this process too, as for
	// a connection the daemon opened to itself.
	Self bool
}

ConnOwner is who holds the client end of a TCP connection to this process.

func ConnectionOwner

func ConnectionOwner(_ context.Context, client, server netip.AddrPort) (ConnOwner, error)

ConnectionOwner returns who holds the client end of a loopback TCP connection from client to server, from /proc/net/tcp and tcp6, which list the sockets of every account with their uid.

type Exposure added in v0.4.0

type Exposure string

Exposure says from where a listener can be reached, judged by the addresses it listens on.

const (
	// Loopback listeners answer only on this computer.
	Loopback Exposure = "loopback"
	// Tailnet listeners answer only on this machine's own Tailscale
	// addresses.
	Tailnet Exposure = "tailnet"
	// Network listeners answer on all interfaces or on another address,
	// such as one of the local network.
	Network Exposure = "network"
)

func Classify added in v0.4.0

func Classify(addrs []string, own TailnetAddrs) Exposure

Classify returns the widest exposure of listening addresses as lsof or ss write them ("127.0.0.1", "[::1]", "*", "0.0.0.0", "[fe80::1%lo0]"), or "" for none. own gives this machine's Tailscale addresses; any other address, also one in Tailscale's ranges, and one it cannot parse count as reachable from the network.

func Widest added in v0.4.0

func Widest(es ...Exposure) Exposure

Widest returns the widest of the exposures.

func (Exposure) Wider added in v0.4.0

func (e Exposure) Wider(o Exposure) bool

Wider reports whether e reaches further than o; "" (unknown) is the narrowest.

type Listener

type Listener struct {
	Port    int    `json:"port"`
	PID     int    `json:"pid"`
	Command string `json:"command"`
	Cwd     string `json:"cwd,omitempty"`
	// Addresses are the local addresses the process listens on at Port, as
	// lsof or ss write them ("127.0.0.1", "[::1]", "*"), in the order found.
	Addresses []string `json:"addresses,omitempty"`
}

Listener is a process listening on a TCP port.

func List

func List(ctx context.Context) []Listener

List returns all TCP listeners visible to the current user, one entry per (port, pid) with all of its addresses, sorted by port, with working directories. It uses lsof and falls back to ss on Linux.

func Owner

func Owner(ctx context.Context, port int) (Listener, bool)

Owner returns the first listener on port, if lsof or ss can see it.

func Sockets added in v0.4.0

func Sockets(ctx context.Context) []Listener

Sockets is List without working directories: one lsof (or ss) call.

func (Listener) Same added in v0.4.0

func (l Listener) Same(o Listener) bool

Same reports whether two listeners describe the same sockets.

type TailnetAddrs added in v0.4.2

type TailnetAddrs func() []netip.Addr

TailnetAddrs returns this machine's own Tailscale addresses, or none when Tailscale is unavailable. Classify calls it only for an address in Tailscale's ranges; a nil TailnetAddrs knows no addresses.

func (TailnetAddrs) Resolved added in v0.4.2

func (own TailnetAddrs) Resolved(lists ...[]string) TailnetAddrs

Resolved calls own at most once, when an address of the lists is in Tailscale's ranges, and returns a TailnetAddrs that answers that result, so a caller can classify under a lock without waiting for Tailscale.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL