Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,14 +375,15 @@ are always refused. `tailcat --json perf` prints the results as JSON.

By default a server prints its address and then stays quiet. `--log`
adds an access log on stderr: one line when a client is allowed in,
and one when each of its TCP connections opens and closes.
and one when each of its TCP connections or UDP flows opens and
closes. A UDP flow closes after two minutes without traffic.

```sh
$ tailcat serve --log 8000
# 🐈 Server listening with new address: tcXXXXXXXXX
2026/01/30 11:04:02 [peer] allowed key=nodekey:abc123...
2026/01/30 11:04:02 [conn] open peer=nodekey:abc123... port=8000 service=forward via=derp:sfo
2026/01/30 11:04:11 [conn] close peer=nodekey:abc123... port=8000 service=forward duration=8.104s
2026/01/30 11:04:02 [conn] open peer=nodekey:abc123... proto=tcp port=8000 service=forward via=derp:sfo
2026/01/30 11:04:11 [conn] close peer=nodekey:abc123... proto=tcp port=8000 service=forward duration=8.104s
```

Clients are identified by public key, the same value `--allow` takes.
Expand Down
51 changes: 37 additions & 14 deletions cmd/tailcat/connlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (

// connLog writes the server-mode access log enabled by
// --log: one line per client admitted or refused and per
// incoming connection, naming the client key the tunnel authenticated
// and the port or service it reached.
// incoming TCP connection or UDP flow, naming the client key the
// tunnel authenticated and the port or service it reached.
//
// It is deliberately separate from --verbose, which turns on the
// library's debug logging. An operator who wants a record of who
Expand Down Expand Up @@ -94,24 +94,47 @@ func (l *connLog) firstRefusal(k key.NodePublic) bool {
// [tailcat.ProxyConns] needs to shut a tunnel connection down without
// losing its FIN.
func (l *connLog) wrapTCP(dst string, next func(net.Conn)) func(net.Conn) {
dst = "proto=tcp " + dst
if next == nil {
l.logf("[conn] refused %v", dst)
return nil
}
return func(c net.Conn) {
peer := "unknown"
via := "unknown"
if k, ok := l.srv.PeerKey(c.RemoteAddr()); ok {
peer = k.String()
via = l.path(k)
}
l.logf("[conn] open peer=%v %v via=%v", peer, dst, via)
start := time.Now()
defer func() {
l.logf("[conn] close peer=%v %v duration=%v", peer, dst, time.Since(start).Round(time.Millisecond))
}()
next(c)
l.logOpenClose(dst, c.RemoteAddr(), func() { next(c) })
}
}

// wrapUDP is like wrapTCP, but for a UDP flow, which closes once it
// has been idle for the server's UDP idle timeout.
//
// Unlike wrapTCP, it doesn't log a nil next. Nothing refuses a UDP
// flow; its packets are just dropped, and each one can ask for a
// handler again, so logging them could flood the log.
func (l *connLog) wrapUDP(dst string, next func(tailcat.ConnPacketConn)) func(tailcat.ConnPacketConn) {
if next == nil {
return nil
}
dst = "proto=udp " + dst
return func(c tailcat.ConnPacketConn) {
l.logOpenClose(dst, c.RemoteAddr(), func() { next(c) })
}
}

// logOpenClose logs the opening of a connection or flow to dst from
// remote, runs serve, and then logs its closing.
func (l *connLog) logOpenClose(dst string, remote net.Addr, serve func()) {
peer := "unknown"
via := "unknown"
if k, ok := l.srv.PeerKey(remote); ok {
peer = k.String()
via = l.path(k)
}
l.logf("[conn] open peer=%v %v via=%v", peer, dst, via)
start := time.Now()
defer func() {
l.logf("[conn] close peer=%v %v duration=%v", peer, dst, time.Since(start).Round(time.Millisecond))
}()
serve()
}

// path reports how packets currently reach peer k: the direct UDP
Expand Down
50 changes: 47 additions & 3 deletions cmd/tailcat/connlog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package main

import (
"bytes"
"context"
"fmt"
"io"
"net"
Expand All @@ -16,6 +17,9 @@ import (
"strings"
"testing"
"time"

"github.com/tailscale/tailcat"
"tailscale.com/types/key"
)

// clientIdentity generates a saved client key and returns its path and
Expand Down Expand Up @@ -53,7 +57,7 @@ func TestServeLog(t *testing.T) {
t.Errorf("server echoed %q; want %q", got, payload)
}

wantOpen := fmt.Sprintf("[conn] open peer=%v port=%d service=forward via=", cpub, port)
wantOpen := fmt.Sprintf("[conn] open peer=%v proto=tcp port=%d service=forward via=", cpub, port)
waitForLog(t, serverStderr, wantOpen)
logs := serverStderr.String()

Expand All @@ -69,7 +73,7 @@ func TestServeLog(t *testing.T) {
t.Errorf("server stderr lacks %q:\n%s", want, logs)
}
// The close line carries a duration, so match its shape.
wantClose := regexp.MustCompile(fmt.Sprintf(`\[conn\] close peer=%v port=%d service=forward duration=\S+`, regexp.QuoteMeta(cpub), port))
wantClose := regexp.MustCompile(fmt.Sprintf(`\[conn\] close peer=%v proto=tcp port=%d service=forward duration=\S+`, regexp.QuoteMeta(cpub), port))
waitForLog(t, serverStderr, "[conn] close peer="+cpub)
logs = serverStderr.String()
if !wantClose.MatchString(logs) {
Expand Down Expand Up @@ -132,7 +136,7 @@ func TestServeLogExitNode(t *testing.T) {
t.Fatal(err)
}

want := fmt.Sprintf("[conn] open peer=%v forward=%v via=", cpub, dst)
want := fmt.Sprintf("[conn] open peer=%v proto=tcp forward=%v via=", cpub, dst)
waitForLog(t, serverStderr, want)
}

Expand Down Expand Up @@ -196,3 +200,43 @@ func TestServeWithoutLog(t *testing.T) {
t.Errorf("server logged connections without --log:\n%s", logs)
}
}

// TestServeLogUDP verifies that --log records UDP flows an exit node
// relays, tagged proto=udp, with the client's key and the address
// they were relayed to.
func TestServeLogUDP(t *testing.T) {
t.Parallel()
e := newTestEnv(t)
port := startUDPEcho(t)
dst := netip.AddrPortFrom(netip.MustParseAddr("127.0.0.1"), port)

_, addr, serverStderr := e.startServer("serve", "--log", "exit-node")

clientKey := key.NewNode()
cl := &tailcat.Client{
Server: tailcat.Addr(addr),
Key: clientKey,
DERPMapURL: e.derpMapURL,
Logf: testLogger(t, "client"),
}
t.Cleanup(func() { cl.Close() })

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
conn, err := cl.DialUDP(ctx, dst)
if err != nil {
t.Fatalf("DialUDP %v: %v", dst, err)
}
defer conn.Close()
if _, err := conn.Write([]byte("logged udp")); err != nil {
t.Fatalf("udp write: %v", err)
}
buf := make([]byte, 64<<10)
conn.SetReadDeadline(time.Now().Add(15 * time.Second))
if _, err := conn.Read(buf); err != nil {
t.Fatalf("udp read: %v\nserver log:\n%s", err, serverStderr.String())
}

want := fmt.Sprintf("[conn] open peer=%v proto=udp forward=%v via=", clientKey.Public(), dst)
waitForLog(t, serverStderr, want)
}
39 changes: 25 additions & 14 deletions cmd/tailcat/tailcat.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func newRootCommand() *ff.Command {
flagSSHAuthorizedKeys = serveFS.StringLong("ssh-authorized-keys", "", "comma-separated SSH public key sources for the 'ssh' service: authorized_keys file paths, literal OpenSSH public key lines, or names like 'alice@github' (fetched from https://github.com/alice.keys). All sources are loaded and validated at startup.")
flagPSK = serveFS.BoolLongDefault("psk", true, "include a WireGuard pre-shared key in the tailcat address (recommended). Set false only for shorter addresses and compatibility with tailcat clients v0.5.0 and earlier; this weakens security.")
flagHybrid = serveFS.BoolLong("hybrid", "EXPERIMENTAL: use the hybrid post-quantum (ML-KEM-768) WireGuard handshake, which doesn't depend on the tailcat address staying secret for its post-quantum protection. The address is shorter, with a 128-bit pre-shared key, but only works with clients that support the hybrid handshake; older clients reject it.")
flagLog = serveFS.BoolLong("log", "log an access record to stderr for each client that connects or is refused and each TCP connection it opens: the client's public key, the port or service reached, and whether it arrived directly or over a relay. Unlike --verbose, which turns on the library's debug logging, this logs only these events, tagged [peer] and [conn].")
flagLog = serveFS.BoolLong("log", "log an access record to stderr for each client that connects or is refused and each TCP connection or UDP flow it opens: the client's public key, the port or service reached, and whether it arrived directly or over a relay. Unlike --verbose, which turns on the library's debug logging, this logs only these events, tagged [peer] and [conn].")

recvFS := ff.NewFlagSet("recv").SetParent(serveFS)
flagRecvAcceptDirs := recvFS.BoolLong("accept-dirs", "accept directory trees (tailcat cp -r), keeping requested file names when available. The trade-off: senders can then make and stat directories and learn whether some names already exist in the drop box. The default flat mode reveals nothing about existing files, but accepts only single files, each saved under a server-chosen unique name.")
Expand Down Expand Up @@ -551,8 +551,8 @@ Keep an access log of who connects and what they reach:
which writes lines like these to stderr, independent of --verbose:

[peer] allowed key=nodekey:abc...
[conn] open peer=nodekey:abc... port=22 service=forward via=derp:sfo
[conn] close peer=nodekey:abc... port=22 service=forward duration=8.104s
[conn] open peer=nodekey:abc... proto=tcp port=22 service=forward via=derp:sfo
[conn] close peer=nodekey:abc... proto=tcp port=22 service=forward duration=8.104s

Environment:

Expand Down Expand Up @@ -1652,26 +1652,37 @@ func server(logf logger.Logf, serveSpec string, execArgs []string) {
return tcpForwardTo(net.JoinHostPort("localhost", strconv.Itoa(int(port)))), "forward"
}

var clog *connLog // nil unless --log
s.OnTCP = func(port uint16) func(net.Conn) {
h, _ := tcpHandler(port)
return h
}
if *flagLog {
clog = newConnLog()
clog := newConnLog()
clog.attach(s)
s.OnTCP = func(port uint16) func(net.Conn) {
h, service := tcpHandler(port)
dst := fmt.Sprintf("port=%d", port)
if service != "" {
dst += " service=" + service
}
return clog.wrapTCP(dst, h)
}
if next := s.OnTCPForward; next != nil {
s.OnTCPForward = func(dst netip.AddrPort) func(net.Conn) {
return clog.wrapTCP("forward="+dst.String(), next(dst))
}
}
}
s.OnTCP = func(port uint16) func(net.Conn) {
h, service := tcpHandler(port)
if clog != nil {
dst := fmt.Sprintf("port=%d", port)
if service != "" {
dst += " service=" + service
if next := s.OnUDPForward; next != nil {
s.OnUDPForward = func(dst netip.AddrPort) func(tailcat.ConnPacketConn) {
return clog.wrapUDP("forward="+dst.String(), next(dst))
}
}
// The perf service is the only one served over UDP.
if next := s.OnUDP; next != nil {
s.OnUDP = func(port uint16) func(tailcat.ConnPacketConn) {
return clog.wrapUDP(fmt.Sprintf("port=%d service=perf", port), next(port))
}
return clog.wrapTCP(dst, h)
}
return h
}

if err := s.Start(); err != nil {
Expand Down
Loading