tsnet: add opt-in SSH support (Server.ListenSSH)

This adds tsnet.Server.ListenSSH which, if the SSH feature is linked,
returns a net.Listener whose Accept yields *tailssh.Session values (as
net.Conn). This lets tsnet apps accept incoming SSH connections to
implement custom TUI applications.

Basic apps can use net.Conn directly (Read/Write/Close). Rich apps
import ssh/tailssh and type-assert for peer identity, PTY, signals,
etc. If feature/ssh isn't imported, ListenSSH returns an error.

Includes a demo guess-the-number game in tsnet/example/ssh-game.

Updates tailscale/corp#37839

Change-Id: I4e7c3c96afb030cdf4da8f2d8b2253820628129a
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2026-05-30 14:17:50 -07:00
committed by Brad Fitzpatrick
parent c9333854fb
commit 3e34e721e8
7 changed files with 618 additions and 0 deletions
+27
View File
@@ -1264,6 +1264,33 @@ func (s *Server) Listen(network, addr string) (net.Listener, error) {
return s.listen(network, addr, listenOnTailnet)
}
// ListenSSH listens on the Tailscale network for SSH connections at the given
// addr (e.g. ":2222"). The returned listener's Accept method yields net.Conn
// values that are actually *tailssh.Session, providing access to the
// connecting peer's Tailscale identity, PTY information, signals, and more.
//
// Basic applications can use the returned connections as plain net.Conn
// (Read/Write/Close). Applications that need richer SSH semantics should
// type-assert to *tailssh.Session.
//
// SSH support must be linked into the binary by importing
// _ "tailscale.com/feature/ssh". Without that import, ListenSSH returns an
// error.
//
// If s has not been started yet, it will be started.
func (s *Server) ListenSSH(addr string) (net.Listener, error) {
rawLn, err := s.Listen("tcp", addr)
if err != nil {
return nil, err
}
sshLn, err := s.lb.ListenSSH(rawLn, s.logf)
if err != nil {
rawLn.Close()
return nil, err
}
return sshLn, nil
}
// ListenPacket announces on the Tailscale network.
//
// The network must be "udp", "udp4" or "udp6". The addr must be of the form