Repository navigation
Expand file tree
/
Copy pathstart.go
More file actions
396 lines (349 loc) · 10.6 KB
/
Copy pathstart.go
File metadata and controls
396 lines (349 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package cmd
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"os"
"strings"
"sync"
"time"
"github.com/spf13/cobra"
"github.com/Devehab/linko/cloudflare"
"github.com/Devehab/linko/config"
"github.com/Devehab/linko/internal/cloudflared"
"github.com/Devehab/linko/internal/naming"
"github.com/Devehab/linko/internal/target"
"github.com/Devehab/linko/internal/ui"
)
type startOptions struct {
name string
keep bool
replace bool
yes bool
verbose bool
open bool
logLevel string
}
func newStartCmd() *cobra.Command {
opts := &startOptions{}
cmd := &cobra.Command{
Use: "start <port|host:port|url>",
Aliases: []string{"run", "up"},
Short: "Publish a local service on a public HTTPS URL",
Long: `start publishes a locally running service through your Cloudflare tunnel.
linko 3000 random subdomain -> http://localhost:3000
linko start 3000 the same thing, written out
linko 3000 --name crm crm.<base domain> -> http://localhost:3000
linko 8080 --name api --keep keep the hostname after you quit
The tunnel stays up until you press Ctrl+C.`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cancel := signalContext(cmd.Context())
defer cancel()
return runStart(ctx, args[0], opts)
},
}
f := cmd.Flags()
f.StringVarP(&opts.name, "name", "n", "", "subdomain to use (default: random)")
f.BoolVar(&opts.keep, "keep", false, "keep the hostname after the tunnel stops")
f.BoolVar(&opts.replace, "replace", false, "replace the hostname if it already points somewhere else")
f.BoolVarP(&opts.yes, "yes", "y", false, "do not ask questions")
f.BoolVarP(&opts.verbose, "verbose", "v", false, "stream cloudflared logs")
f.BoolVarP(&opts.open, "open", "o", false, "open the public URL in your browser once connected")
f.StringVar(&opts.logLevel, "loglevel", "info", "cloudflared log level: debug, info, warn, error, fatal")
return cmd
}
func runStart(ctx context.Context, rawTarget string, opts *startOptions) error {
cfg, client, err := loadClient()
if err != nil {
return err
}
service, port, err := target.Parse(rawTarget)
if err != nil {
return err
}
// Pick the subdomain label.
label := strings.ToLower(strings.TrimSpace(opts.name))
ephemeral := label == ""
if ephemeral {
label, err = naming.Random(naming.DefaultLength)
if err != nil {
return err
}
}
if err := naming.ValidateLabel(label); err != nil {
return err
}
hostname := cfg.Hostname(label)
// What is already published under this hostname?
tunnelCfg, err := client.GetTunnelConfig(ctx, cfg.TunnelID)
if err != nil {
return fmt.Errorf("reading the tunnel configuration: %w", err)
}
if existing, ok := cloudflare.FindIngress(tunnelCfg.Ingress, hostname); ok && existing.Service != service {
action, err := resolveConflict(hostname, existing.Service, service, opts)
if err != nil {
return err
}
switch action {
case conflictCancel:
ui.Info("Cancelled.")
return nil
case conflictNewHostname:
suffix, rerr := naming.Random(4)
if rerr != nil {
return rerr
}
label = label + "-" + suffix
hostname = cfg.Hostname(label)
ephemeral = true
ui.Info("Using %s instead", hostname)
case conflictReplace:
}
}
// DNS
dnsTarget := cloudflare.TunnelCNAMETarget(cfg.TunnelID)
rec, created, err := client.EnsureCNAME(ctx, hostname, dnsTarget)
if err != nil {
explainDNSFailure(err, cfg.Domain)
return fmt.Errorf("setting up DNS for %s: %w", hostname, err)
}
if created {
ui.Success("DNS record created (%s)", hostname)
}
// Ingress
tunnelCfg.Ingress = cloudflare.UpsertIngress(tunnelCfg.Ingress, hostname, service)
if err := client.PutTunnelConfig(ctx, cfg.TunnelID, tunnelCfg); err != nil {
return fmt.Errorf("updating the tunnel routes: %w", err)
}
ui.Success("Route published (%s -> %s)", hostname, service)
route := config.Route{
Name: label,
Hostname: hostname,
Service: service,
Port: port,
Ephemeral: ephemeral && !opts.keep,
}
if rec != nil {
route.DNSRecordID = rec.ID
}
cfg.UpsertRoute(route)
if err := cfg.Save(); err != nil {
ui.Warn("could not update %s: %v", config.Path(), err)
}
// Clean up the ephemeral hostname when we exit.
if route.Ephemeral {
defer cleanupRoute(client, cfg, route)
}
// cloudflared
mgr := cloudflared.New(config.BinDir())
binary, err := mgr.Ensure(ctx, os.Stdout)
if err != nil {
return err
}
return runTunnel(ctx, mgr, binary, cfg, route, opts)
}
func runTunnel(ctx context.Context, mgr *cloudflared.Manager, binary string, cfg *config.Config, route config.Route, opts *startOptions) error {
cmd := mgr.Command(ctx, binary, cloudflared.RunOptions{
Token: cfg.TunnelToken,
LogLevel: opts.logLevel,
})
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return fmt.Errorf("starting cloudflared: %w", err)
}
watcher := newLogWatcher(opts.verbose, func() {
printBanner(route, cfg)
if opts.open {
if err := openBrowser("https://" + route.Hostname); err != nil {
ui.Warn("could not open a browser: %v", err)
}
}
})
var wg sync.WaitGroup
wg.Add(2)
go func() { defer wg.Done(); watcher.scan(stdout) }()
go func() { defer wg.Done(); watcher.scan(stderr) }()
// Drain both pipes before Wait(): Wait closes them, which would truncate
// cloudflared's output and race with the scanners.
wg.Wait()
waitErr := cmd.Wait()
if ctx.Err() != nil {
ui.Blank()
ui.Info("Tunnel stopped.")
return nil
}
if waitErr != nil {
if tail := watcher.tail(); tail != "" {
ui.Blank()
fmt.Fprintln(os.Stderr, ui.Dim(tail))
}
return fmt.Errorf("cloudflared exited: %w", waitErr)
}
return nil
}
func printBanner(route config.Route, cfg *config.Config) {
url := "https://" + route.Hostname
ui.Blank()
ui.Success("Tunnel connected")
ui.Blank()
ui.Line(" %s %s", ui.Dim("Public URL"), ui.Link(url, ui.Bold(ui.Cyan(url))))
ui.Line(" %s %s", ui.Dim("Forwards"), route.Service)
ui.Line(" %s %s", ui.Dim("Tunnel"), cfg.TunnelName)
if route.Ephemeral {
ui.Line(" %s %s", ui.Dim("Lifetime"), "removed when you quit (use --keep to persist)")
}
ui.Blank()
if hint := ui.ClickHint(); hint != "" {
ui.Line(" %s", ui.Dim(hint+" · or start with --open next time"))
} else {
ui.Line(" %s", ui.Dim("start with --open to launch the browser automatically"))
}
ui.Line(" %s", ui.Dim("Press Ctrl+C to stop"))
ui.Blank()
}
// logWatcher turns cloudflared's chatty output into a single "connected" line,
// while keeping the last few lines around in case it dies.
type logWatcher struct {
verbose bool
onConnect func()
mu sync.Mutex
announced bool
recent []string
}
func newLogWatcher(verbose bool, onConnect func()) *logWatcher {
return &logWatcher{verbose: verbose, onConnect: onConnect}
}
func (w *logWatcher) scan(r io.Reader) {
sc := bufio.NewScanner(r)
sc.Buffer(make([]byte, 0, 64*1024), 1024*1024)
for sc.Scan() {
line := sc.Text()
w.mu.Lock()
w.recent = append(w.recent, line)
if len(w.recent) > 15 {
w.recent = w.recent[len(w.recent)-15:]
}
connected := false
if !w.announced && isConnectedLine(line) {
w.announced = true
connected = true
}
verbose := w.verbose
w.mu.Unlock()
if verbose {
fmt.Fprintln(os.Stderr, ui.Dim(line))
}
if connected && w.onConnect != nil {
w.onConnect()
}
}
}
func (w *logWatcher) tail() string {
w.mu.Lock()
defer w.mu.Unlock()
return strings.Join(w.recent, "\n")
}
func isConnectedLine(line string) bool {
l := strings.ToLower(line)
return strings.Contains(l, "registered tunnel connection") ||
strings.Contains(l, "connection established") ||
strings.Contains(l, "connection registered")
}
// explainDNSFailure turns Cloudflare's opaque "Authentication error (code
// 10000)" into the one thing the user actually has to change. Finding the zone
// only needs Zone:Read, so a token can get all the way through `linko init`
// and still be unable to write a record.
func explainDNSFailure(err error, domain string) {
var apiErr *cloudflare.Error
if !errors.As(err, &apiErr) || !apiErr.IsAuth() {
return
}
ui.Blank()
ui.Fail("Cloudflare refused to create the DNS record.")
ui.Info("The token can read this zone but not change it.")
ui.Info("Open https://dash.cloudflare.com/profile/api-tokens, edit the token and set:")
ui.Info(" Zone → DNS → %s (not Read)", ui.Bold("Edit"))
ui.Info(" Zone Resources → Include → Specific zone → %s", domain)
ui.Blank()
}
type conflictAction int
const (
conflictReplace conflictAction = iota
conflictNewHostname
conflictCancel
)
func resolveConflict(hostname, current, next string, opts *startOptions) (conflictAction, error) {
if opts.replace {
return conflictReplace, nil
}
ui.Blank()
ui.Warn("Hostname already in use: %s", ui.Bold(hostname))
ui.Line(" %s %s", ui.Dim("currently ->"), current)
ui.Line(" %s %s", ui.Dim(" new ->"), next)
ui.Blank()
if opts.yes {
return conflictReplace, nil
}
p := ui.NewPrompter()
choice, err := p.Choose("What would you like to do?", []string{
"Replace the existing route",
"Create another hostname",
"Cancel",
})
if err != nil {
return conflictCancel, err
}
return conflictAction(choice), nil
}
// cleanupRoute removes an ephemeral hostname from the tunnel and DNS.
func cleanupRoute(client *cloudflare.Client, cfg *config.Config, route config.Route) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := removeRoute(ctx, client, cfg, route); err != nil {
ui.Warn("could not clean up %s: %v", route.Hostname, err)
ui.Info("Remove it later with: linko remove %s", route.Name)
return
}
if err := cfg.Save(); err != nil {
ui.Warn("could not update %s: %v", config.Path(), err)
}
ui.Info("Removed %s", route.Hostname)
}
// removeRoute deletes the ingress rule, the DNS record and the local entry.
func removeRoute(ctx context.Context, client *cloudflare.Client, cfg *config.Config, route config.Route) error {
tunnelCfg, err := client.GetTunnelConfig(ctx, cfg.TunnelID)
if err != nil {
return err
}
tunnelCfg.Ingress = cloudflare.RemoveIngress(tunnelCfg.Ingress, route.Hostname)
if err := client.PutTunnelConfig(ctx, cfg.TunnelID, tunnelCfg); err != nil {
return err
}
recordID := route.DNSRecordID
if recordID == "" {
rec, ferr := client.FindDNSRecord(ctx, route.Hostname)
if ferr != nil {
return ferr
}
if rec != nil && cloudflare.IsTunnelTarget(rec.Content) {
recordID = rec.ID
}
}
if recordID != "" {
if err := client.DeleteDNSRecord(ctx, recordID); err != nil {
return err
}
}
cfg.RemoveRoute(route.Hostname)
return nil
}