diff --git a/cmd/agentmesh-one/main.go b/cmd/agentmesh-one/main.go index cf2147b51..f390569f3 100644 --- a/cmd/agentmesh-one/main.go +++ b/cmd/agentmesh-one/main.go @@ -189,6 +189,12 @@ func main() { if tun != nil { go func() { <-tun.Done() + // The connector shares the terminal's process group, so the + // signal that stops this process stops it too: an exit during + // shutdown is expected, not a lost tunnel. + if cmd.Context().Err() != nil { + return + } if err := tun.Err(); err != nil { logger.Errorf("Tunnel %s closed: %v; remote devices can no longer reach %s", tunnelProvider, err, tun.URL()) } diff --git a/internal/tunnel/cloudflare.go b/internal/tunnel/cloudflare.go index 9834e917b..9d56519a3 100644 --- a/internal/tunnel/cloudflare.go +++ b/internal/tunnel/cloudflare.go @@ -103,7 +103,8 @@ func (c *Cloudflare) Name() string { return "cloudflare" } // Open implements Provider. func (c *Cloudflare) Open(ctx context.Context, target string) (Tunnel, error) { - if c.Token != "" && strings.TrimSpace(c.ExternalURL) == "" { + namedURL := strings.TrimRight(strings.TrimSpace(c.ExternalURL), "/") + if c.Token != "" && namedURL == "" { return nil, fmt.Errorf("cloudflare named tunnel requires ExternalURL (--external-url)") } binary, err := c.resolveBinary(ctx) @@ -135,7 +136,10 @@ func (c *Cloudflare) Open(ctx context.Context, target string) (Tunnel, error) { cancel() return nil, fmt.Errorf("failed to start cloudflared: %w", err) } - t := &process{cancel: cancel, done: make(chan struct{}), namedURL: strings.TrimRight(strings.TrimSpace(c.ExternalURL), "/")} + t := &process{cancel: cancel, done: make(chan struct{})} + if c.Token != "" { + t.namedURL = namedURL + } urlCh := make(chan string, 1) go t.scan(pr, urlCh) go func() { @@ -308,10 +312,13 @@ func (p *process) Close() error { } // finish records the exit and releases Done; an exit caused by Close is -// not an error. +// not an error, any other exit is, even with status 0. func (p *process) finish(err error) { p.mu.Lock() - if !p.closed && err != nil { + if !p.closed { + if err == nil { + err = errors.New("exit status 0") + } p.err = err } p.mu.Unlock() diff --git a/internal/tunnel/cloudflare_test.go b/internal/tunnel/cloudflare_test.go index d3ee86747..9c5ed10c4 100644 --- a/internal/tunnel/cloudflare_test.go +++ b/internal/tunnel/cloudflare_test.go @@ -181,6 +181,39 @@ func TestCloudflareOpenReportsEarlyExit(t *testing.T) { } } +// A connector that stops on its own reports why even when its exit status +// is 0: only Close leaves Err nil, and the caller logs the outage only when +// Err is set. +func TestCloudflareReportsUnrequestedCleanExit(t *testing.T) { + bin := stubCloudflared(t, `echo "INF starting" >&2; exit 0`) + _, err := (&Cloudflare{Binary: bin, Timeout: 5 * time.Second}).Open(context.Background(), "http://127.0.0.1:1") + if err == nil || strings.Contains(err.Error(), "%!") { + t.Errorf("Open error = %v, want an early-exit error with a reason", err) + } + + gate := filepath.Join(t.TempDir(), "exit") + bin = stubCloudflared(t, `echo "INF | https://installed-quick-1234.trycloudflare.com |" >&2 +while [ ! -e "`+gate+`" ]; do sleep 0.01; done +exit 0 +`) + tun, err := (&Cloudflare{Binary: bin, Timeout: 5 * time.Second, LookupHost: resolved}).Open(context.Background(), "http://127.0.0.1:1") + if err != nil { + t.Fatalf("Open: %v", err) + } + t.Cleanup(func() { _ = tun.Close() }) + if err := os.WriteFile(gate, nil, 0o600); err != nil { + t.Fatal(err) + } + select { + case <-tun.Done(): + case <-time.After(10 * time.Second): + t.Fatal("Done not closed after the connector exited") + } + if tun.Err() == nil { + t.Fatal("Err = nil after the connector exited without Close, want an error") + } +} + func TestCloudflareOpenTimesOutWithoutURL(t *testing.T) { bin := stubCloudflared(t, `trap 'exit 0' TERM; while :; do sleep 1; done`) p := &Cloudflare{Binary: bin, Timeout: 200 * time.Millisecond} @@ -434,3 +467,35 @@ func TestLookup(t *testing.T) { t.Fatalf("Lookup unknown = %v", err) } } + +// ExternalURL names the hostname of a named tunnel; without a Token the +// connector runs a quick tunnel, and the URL published is the one it was +// assigned, not ExternalURL. +func TestCloudflareQuickTunnelIgnoresExternalURL(t *testing.T) { + bin := stubCloudflared(t, `echo "INF | https://quick-ext-1234.trycloudflare.com |" >&2 +echo "INF Registered tunnel connection connIndex=0 location=ams01 protocol=quic" >&2 +trap 'exit 0' TERM +while :; do sleep 1; done +`) + p := &Cloudflare{Binary: bin, Timeout: 5 * time.Second, LookupHost: resolved, ExternalURL: "https://mesh.example.com"} + tun, err := p.Open(context.Background(), "http://127.0.0.1:1") + if err != nil { + t.Fatalf("Open: %v", err) + } + t.Cleanup(func() { _ = tun.Close() }) + if got, want := tun.URL(), "https://quick-ext-1234.trycloudflare.com"; got != want { + t.Fatalf("URL = %q, want the assigned quick-tunnel URL %q", got, want) + } +} + +// A named tunnel needs a hostname: an ExternalURL that trims to nothing is +// refused before cloudflared is started. +func TestCloudflareNamedTunnelRefusesAnEmptyExternalURL(t *testing.T) { + for _, externalURL := range []string{"", " ", "/", " // "} { + p := &Cloudflare{Binary: filepath.Join(t.TempDir(), "never-started"), Token: "token", ExternalURL: externalURL} + _, err := p.Open(context.Background(), "http://127.0.0.1:1") + if err == nil || !strings.Contains(err.Error(), "requires ExternalURL") { + t.Errorf("Open with ExternalURL %q: err = %v, want the ExternalURL refusal", externalURL, err) + } + } +}