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
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,32 @@ $ tailcat serve 5555:10.2.200.213:5555

Then on the client, `tailcat forward tcXXXXXXXXX 5555` followed by `adb connect 127.0.0.1:5555`. Write IPv6 targets in brackets: `5555:[fd7a::1]:5555`.

### Expose a local Unix-domain socket

A serve argument starting with `/` (or `unix:`, which also allows a
relative path) proxies connections to a local Unix-domain stream
socket. Without a port, the socket gets every port not otherwise
served, so a client needs no port number:

```sh
$ tailcat serve /run/git-annex/socket
# Proxying all other ports to unix:/run/git-annex/socket
# 🐈 Server listening with new address: tcXXXXXXXXX
```

```sh
$ tailcat tcXXXXXXXXX
```

Each client connection gets its own connection to the socket. tailcat
doesn't create the socket or change its permissions, and refuses to
start if it doesn't exist. To serve several sockets, or a socket
alongside other services, give each one a port with a mapping:

```sh
$ tailcat serve 22,8022:/run/app.sock
```

### Show the address as a QR code

Pass `--qr` to `serve` to also print the tailcat address as a QR code,
Expand Down
70 changes: 62 additions & 8 deletions cmd/tailcat/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func TestServeSSHAuthorizedKeysFlag(t *testing.T) {
if len(args) != 1 || args[0] != "ssh" {
t.Errorf("serve args = %q; want [ssh]", args)
}
_, services, _, err := parsePortSet("ssh")
_, services, _, _, err := parsePortSet("ssh")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -596,14 +596,17 @@ func TestGenkeyEmbedDERPMapUnknownRegion(t *testing.T) {

// TestParsePortSetTargets covers the "port:target" serve mappings:
// a bare port target means that port on localhost, a host:port
// target is kept as given (IPv6 in brackets), and conflicting
// mappings of one port are rejected, as is port 0 anywhere.
// target is kept as given (IPv6 in brackets), a socket target is
// canonicalized to "unix:" and its path, and conflicting mappings of
// one port are rejected, as is port 0 anywhere. It also covers
// catch-all Unix sockets, given without a port.
func TestParsePortSetTargets(t *testing.T) {
for _, tt := range []struct {
spec string
wantPorts []uint16
wantTargets map[uint16]string
wantErr string
spec string
wantPorts []uint16
wantTargets map[uint16]string
wantCatchAll string
wantErr string
}{
{
spec: "5555:10.2.200.213:5555",
Expand Down Expand Up @@ -643,8 +646,56 @@ func TestParsePortSetTargets(t *testing.T) {
{spec: "5555:0", wantErr: "not a valid port"},
{spec: "5555:65536", wantErr: "not a valid port"},
{spec: "5555:99999", wantErr: "not a valid port"},
{
spec: "8022:/run/annex.sock",
wantPorts: []uint16{8022},
wantTargets: map[uint16]string{8022: "unix:/run/annex.sock"},
},
{
// A path may contain colons; only the first one splits.
spec: "8022:/run/a:b.sock",
wantPorts: []uint16{8022},
wantTargets: map[uint16]string{8022: "unix:/run/a:b.sock"},
},
{
spec: "8022:unix:rel.sock",
wantPorts: []uint16{8022},
wantTargets: map[uint16]string{8022: "unix:rel.sock"},
},
{
// Any target starting with "unix:" is a socket, even
// one that looks like a host named "unix" and a port.
spec: "8022:unix:80",
wantPorts: []uint16{8022},
wantTargets: map[uint16]string{8022: "unix:80"},
},
{
spec: "/run/annex.sock",
wantPorts: []uint16{},
wantTargets: map[uint16]string{},
wantCatchAll: "unix:/run/annex.sock",
},
{
spec: "unix:rel.sock",
wantPorts: []uint16{},
wantTargets: map[uint16]string{},
wantCatchAll: "unix:rel.sock",
},
{
spec: "22, /run/a:b.sock ,8080:80",
wantPorts: []uint16{22, 8080},
wantTargets: map[uint16]string{8080: "localhost:80"},
wantCatchAll: "unix:/run/a:b.sock",
},
{spec: "unix:", wantErr: "no Unix socket path"},
{spec: "8022:unix:", wantErr: "no Unix socket path"},
{spec: "/a.sock,/b.sock", wantErr: "more than one Unix socket"},
{spec: "/a.sock,exec", wantErr: "exec service"},
{spec: "exit-node,/a.sock", wantErr: "exit-node service"},
{spec: "all,/a.sock", wantErr: `"all"`},
{spec: "run/a.sock", wantErr: "is not a port"},
} {
ports, _, targets, err := parsePortSet(tt.spec)
ports, _, targets, catchAll, err := parsePortSet(tt.spec)
if tt.wantErr != "" {
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Errorf("parsePortSet(%q) error = %v; want one containing %q", tt.spec, err, tt.wantErr)
Expand All @@ -661,6 +712,9 @@ func TestParsePortSetTargets(t *testing.T) {
if !maps.Equal(targets, tt.wantTargets) {
t.Errorf("parsePortSet(%q) targets = %v; want %v", tt.spec, targets, tt.wantTargets)
}
if catchAll != tt.wantCatchAll {
t.Errorf("parsePortSet(%q) catch-all = %q; want %q", tt.spec, catchAll, tt.wantCatchAll)
}
}
}

Expand Down
102 changes: 102 additions & 0 deletions cmd/tailcat/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import (
"io"
"net"
"net/netip"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -476,3 +478,103 @@ func TestServePortMapping(t *testing.T) {
t.Errorf("mapped port echoed %q; want %q", got, payload)
}
}

// startUnixEchoListener starts a Unix-domain socket server in dir,
// closed with the test, that writes name and a colon and then echoes
// what it reads. It returns the socket's path.
func startUnixEchoListener(t *testing.T, dir, name string) string {
t.Helper()
path := filepath.Join(dir, name+".sock")
ln, err := net.Listen("unix", path)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { ln.Close() })
go func() {
for {
c, err := ln.Accept()
if err != nil {
return
}
go func() {
defer c.Close()
io.WriteString(c, name+":")
io.Copy(c, c)
}()
}
}()
return path
}

// TestServeUnixSocket checks that a serve spec can proxy to Unix
// sockets: a bare socket path takes connections to every port not
// otherwise served, including the client's default port, and a
// "port:unix:path" mapping takes priority over it for its port.
func TestServeUnixSocket(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("test needs Unix-domain socket mode checks, which are skipped on Windows")
}
t.Parallel()
e := newTestEnv(t)
dir := t.TempDir()
catchAll := startUnixEchoListener(t, dir, "catchall")
mapped := startUnixEchoListener(t, dir, "mapped")

_, addr, serverStderr := e.startServer("serve", catchAll+",8022:unix:"+mapped)
waitForLog(t, serverStderr, "# Proxying all other ports to unix:"+catchAll+"\n")
waitForLog(t, serverStderr, "# Proxying port 8022 to unix:"+mapped+"\n")

for _, tt := range []struct {
port string // client destination port, or "" for the default
want string // name of the socket that should answer
}{
{"", "catchall"},
{"8080", "catchall"},
{"8022", "mapped"},
} {
args := []string{"--key=new", "--derpmap-url=" + e.derpMapURL, addr}
if tt.port != "" {
args = append(args, tt.port)
}
const payload = "unix socket echo"
got, err := runClient(t, e.cmd(args...), serverStderr, payload)
if err != nil {
t.Fatalf("client to port %q: %v", tt.port, err)
}
if want := tt.want + ":" + payload; got != want {
t.Errorf("client to port %q got %q; want %q", tt.port, got, want)
}
}
}

// TestServeUnixSocketChecked checks that the server refuses to start
// if a socket in its serve spec is missing or isn't a socket, rather
// than failing each connection later.
func TestServeUnixSocketChecked(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("test needs Unix-domain socket mode checks, which are skipped on Windows")
}
t.Parallel()
e := newTestEnv(t)
dir := t.TempDir()
notSocket := filepath.Join(dir, "plain-file")
if err := os.WriteFile(notSocket, nil, 0o600); err != nil {
t.Fatal(err)
}
for _, tt := range []struct {
spec string
want string
}{
{filepath.Join(dir, "missing.sock"), "no such file or directory"},
{"8022:" + notSocket, "is not a Unix-domain socket"},
} {
out, err := e.cmd("serve", tt.spec).CombinedOutput()
if err == nil {
t.Errorf("serve %q succeeded; want failure", tt.spec)
continue
}
if !strings.Contains(string(out), "invalid Unix socket to serve") || !strings.Contains(string(out), tt.want) {
t.Errorf("serve %q output = %q; want an invalid Unix socket error containing %q", tt.spec, out, tt.want)
}
}
}
Loading
Loading