Repository navigation
tunnel: report a connector that exits on its own, keep quick tunnels on their URL #949
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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 | ||||||
| } | ||||||
|
Comment on lines
+140
to
+142
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If if c.Token != "" {
t.namedURL = strings.TrimRight(strings.TrimSpace(c.ExternalURL), "/")
if t.namedURL == "" {
return nil, fmt.Errorf("cloudflare named tunnel requires a valid ExternalURL")
}
}
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, taken in 0d7885e, with one change: the check goes before |
||||||
| 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") | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using a more descriptive error message like
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd keep |
||||||
| } | ||||||
| p.err = err | ||||||
| } | ||||||
| p.mu.Unlock() | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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(), "%!") { | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Thanks for covering both the
Suggested change
|
||||||
| 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) | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for fixing the quick-tunnel URL! How about refusing
ExternalURLwithout aTokeninstead of ignoring it? The CLI already refuses that combination. The check at the top ofOpenalready refuses the opposite case,TokenwithoutExternalURL. Something like:an early exit pla
as an early exit placed right after the check in line 107