/
repo_handlers.go
859 lines (698 loc) · 19.6 KB
/
repo_handlers.go
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
package cmd
import (
"context"
"fmt"
"io/ioutil"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime/trace"
"strings"
"time"
log "github.com/Sirupsen/logrus"
"github.com/fatih/color"
e "github.com/pkg/errors"
"github.com/sahib/brig/client"
"github.com/sahib/brig/cmd/pwd"
"github.com/sahib/brig/cmd/tabwriter"
"github.com/sahib/brig/gateway"
"github.com/sahib/brig/server"
"github.com/sahib/brig/util"
"github.com/sahib/brig/util/pwutil"
"github.com/sahib/brig/version"
"github.com/urfave/cli"
)
const brigLogo = `
_____ / /\ ___ / /\
/ /::\ / /::\ / /\ / /:/_
/ /:/\:\ / /:/\:\ / /:/ / /:/ /\
/ /:/~/::\ / /:/~/:/ /__/::\ / /:/_/::\
/__/:/ /:/\:| /__/:/ /:/___ \__\/\:\__ /__/:/__\/\:\
\ \:\/:/~/:/ \ \:\/:::::/ \ \:\/\ \ \:\ /~~/:/
\ \::/ /:/ \ \::/~~~~ \__\::/ \ \:\ /:/
\ \:\/:/ \ \:\ /__/:/ \ \:\/:/
\ \::/ \ \:\ \__\/ \ \::/
\__\/ \__\/ \__\/
A new file README.md was automatically added.
Use 'brig cat README.md' to view it & get started.
`
func createInitialReadme(ctl *client.Client, folder string) error {
text := `Welcome to brig!
Here's what you can do next:
• Read the official documentation (Just type »brig docs«)
• Add a few remotes to sync with (See »brig help remote«)
• Mount your data somewhere convinient (See »brig help fstab«)
• Sync with the remotes you've added (See »brig help sync«)
• Have a relaxing day while exploring brig.
Please remember that brig is software in its very early stages,
and you should not rely on it yet for production purposes.
If you're done with this README, you can easily remove it:
$ brig rm README.md
We recommend highly to install a password manager and hook it up
with brig. For example, with "pass" you can execute the following
to avoid re-entering your password on every daemon startup:
$ brig cfg set repo.password_command "pass brig/my_pwd_key"
The next start of brig will then read the password from the
standard output of this process. Your repository is here:
%s
Have a nice day.
`
fd, err := ioutil.TempFile("", ".brig-init-readme-")
if err != nil {
return err
}
text = fmt.Sprintf(text, folder)
if _, err := fd.WriteString(text); err != nil {
return err
}
readmePath := fd.Name()
if err := fd.Close(); err != nil {
return err
}
if err := ctl.Stage(readmePath, "/README.md"); err != nil {
return err
}
return ctl.MakeCommit("Added initial README.md")
}
func handleInit(ctx *cli.Context, ctl *client.Client) error {
owner := ctx.Args().First()
backend := ctx.String("backend")
folder := ctx.GlobalString("repo")
if ctx.NArg() == 2 {
var err error
folder, err = filepath.Abs(ctx.Args().Get(1))
if err != nil {
return fmt.Errorf("failed to get absolute path for %s: %v", folder, err)
}
}
if ctx.NArg() > 2 {
return fmt.Errorf("too many arguments")
}
if folder == "" {
// Make sure that we do not lookup the global registry:
folder = guessRepoFolder(ctx, false)
fmt.Printf("Guessed folder for init: %s\n", folder)
}
// If a password helper is set, we should read the password from it directly.
password := readPasswordFromArgs(folder, ctx)
if pwHelper := ctx.String("pw-helper"); password == "" && pwHelper != "" {
var err error
password, err = pwutil.ReadPasswordFromHelper(folder, pwHelper)
if err != nil {
return fmt.Errorf("failed to read password from helper: %s", err)
}
}
// Check if the folder exists...
// doing init twice can easily break things.
isInitialized, err := repoIsInitialized(folder)
if err != nil {
return err
}
if isInitialized {
return fmt.Errorf("`%s` already exists and is not empty; refusing to do init", folder)
}
if password == "" {
pwdBytes, err := pwd.PromptNewPassword(20)
if err != nil {
msg := fmt.Sprintf("Failed to read password: %v", err)
fmt.Println(msg)
return ExitCode{UnknownError, msg}
}
password = string(pwdBytes)
}
if err := ctl.Init(folder, owner, password, backend); err != nil {
return ExitCode{UnknownError, fmt.Sprintf("init failed: %v", err)}
}
if !ctx.Bool("empty") {
if err := createInitialReadme(ctl, folder); err != nil {
return err
}
}
fmt.Println(brigLogo)
if ctx.Bool("no-password") {
// Set a command in the config that simply echoes a static password:
staticPasswordHelper := "echo no-password"
if err := ctl.ConfigSet("repo.password_command", staticPasswordHelper); err != nil {
return err
}
}
if pwHelper := ctx.String("pw-helper"); pwHelper != "" {
if err := ctl.ConfigSet("repo.password_command", pwHelper); err != nil {
return err
}
}
return nil
}
func printConfigDocEntry(entry client.ConfigEntry) {
val := entry.Val
if val == "" {
val = color.YellowString("(empty)")
}
defaultMarker := ""
if entry.Val == entry.Default {
defaultMarker = color.CyanString("(default)")
}
fmt.Printf("%s: %v %s\n", color.GreenString(entry.Key), val, defaultMarker)
needsRestart := yesify(entry.NeedsRestart)
defaultVal := entry.Default
if entry.Default == "" {
defaultVal = color.YellowString("(empty)")
}
fmt.Printf(" Default: %v\n", defaultVal)
fmt.Printf(" Documentation: %v\n", entry.Doc)
fmt.Printf(" Needs restart: %v\n", needsRestart)
}
func handleConfigList(cli *cli.Context, ctl *client.Client) error {
all, err := ctl.ConfigAll()
if err != nil {
return ExitCode{UnknownError, fmt.Sprintf("config list: %v", err)}
}
for _, entry := range all {
printConfigDocEntry(entry)
}
return nil
}
func handleConfigGet(ctx *cli.Context, ctl *client.Client) error {
key := ctx.Args().Get(0)
val, err := ctl.ConfigGet(key)
if err != nil {
return ExitCode{UnknownError, fmt.Sprintf("config get: %v", err)}
}
for _, elem := range strings.Split(val, " ;; ") {
fmt.Println(elem)
}
return nil
}
func handleConfigSet(ctx *cli.Context, ctl *client.Client) error {
key := ctx.Args().Get(0)
val := ctx.Args().Get(1)
if len(ctx.Args()) > 2 {
val = strings.Join(ctx.Args()[1:], " ;; ")
}
if err := ctl.ConfigSet(key, val); err != nil {
return ExitCode{UnknownError, fmt.Sprintf("config set: %v", err)}
}
entry, err := ctl.ConfigDoc(key)
if err != nil {
return ExitCode{UnknownError, fmt.Sprintf("config doc: %v", err)}
}
if entry.NeedsRestart {
fmt.Println("NOTE: You need to restart brig for this option to take effect.")
}
return nil
}
func handleConfigDoc(ctx *cli.Context, ctl *client.Client) error {
key := ctx.Args().Get(0)
entry, err := ctl.ConfigDoc(key)
if err != nil {
return ExitCode{UnknownError, fmt.Sprintf("config get: %v", err)}
}
printConfigDocEntry(entry)
return nil
}
func handleDaemonPing(ctx *cli.Context, ctl *client.Client) error {
if ctx.Bool("wait-for-init") {
if err := ctl.WaitForInit(); err != nil {
return err
}
}
count := ctx.Int("count")
for i := 0; i < count; i++ {
before := time.Now()
symbol := color.GreenString("✔")
if err := ctl.Ping(); err != nil {
symbol = color.RedString("✘")
}
delay := time.Since(before)
fmt.Printf("#%02d %s ➔ %s: %s (%v)\n",
i+1,
ctl.LocalAddr().String(),
ctl.RemoteAddr().String(),
symbol,
delay,
)
time.Sleep(1 * time.Second)
}
return nil
}
func handleDaemonQuit(ctx *cli.Context, ctl *client.Client) error {
if err := ctl.Quit(); err != nil {
return ExitCode{
DaemonNotResponding,
fmt.Sprintf("brigd not responding: %v", err),
}
}
return nil
}
func handleDaemonLaunch(ctx *cli.Context) error {
// Enable tracing (for profiling) if required.
if ctx.Bool("trace") {
tracePath := fmt.Sprintf("/tmp/brig-%d.trace", os.Getpid())
log.Debugf("Writing trace output to %s", tracePath)
fd, err := os.Create(tracePath)
if err != nil {
return err
}
defer util.Closer(fd)
if err := trace.Start(fd); err != nil {
return err
}
defer trace.Stop()
}
// If the repository was not initialized yet,
// we should not ask for a password, since init
// will already ask for one. If we recognize the repo
// wrongly as uninitialized, then it won't unlock without
// a password though.
brigPath := guessRepoFolder(ctx, true)
isInitialized, err := repoIsInitialized(brigPath)
if err != nil {
return err
}
port := guessPort(ctx)
bindHost := ctx.GlobalString("bind")
var password string
passwordFn := func() (string, error) {
if !isInitialized {
return "", nil
}
password, err = readPassword(ctx, brigPath)
if err != nil {
return "", ExitCode{
UnknownError,
fmt.Sprintf("Failed to read password: %v", err),
}
}
return password, nil
}
logToStdout := ctx.Bool("log-to-stdout")
server, err := server.BootServer(brigPath, passwordFn, bindHost, port, logToStdout)
if err != nil {
return ExitCode{
UnknownError,
fmt.Sprintf("Failed to boot brigd: %v", err),
}
}
defer util.Closer(server)
if err := server.Serve(); err != nil {
return ExitCode{
UnknownError,
fmt.Sprintf("Failed to serve: %v", err),
}
}
return nil
}
func handleMount(ctx *cli.Context, ctl *client.Client) error {
mountPath := ctx.Args().First()
absMountPath, err := filepath.Abs(mountPath)
if err != nil {
return err
}
if !ctx.Bool("no-mkdir") {
if _, err := os.Stat(absMountPath); os.IsNotExist(err) {
fmt.Printf(
"Mount directory »%s« does not exist. Will create it.\n",
absMountPath,
)
if err := os.MkdirAll(absMountPath, 0700); err != nil {
return e.Wrapf(err, "failed to mkdir mount point")
}
}
}
options := client.MountOptions{
ReadOnly: ctx.Bool("readonly"),
RootPath: ctx.String("root"),
}
if err := ctl.Mount(absMountPath, options); err != nil {
return ExitCode{
UnknownError,
fmt.Sprintf("Failed to mount: %v", err),
}
}
return nil
}
func handleUnmount(ctx *cli.Context, ctl *client.Client) error {
mountPath := ctx.Args().First()
absMountPath, err := filepath.Abs(mountPath)
if err != nil {
return err
}
if err := ctl.Unmount(absMountPath); err != nil {
return ExitCode{
UnknownError,
fmt.Sprintf("Failed to unmount: %v", err),
}
}
return nil
}
func handleVersion(ctx *cli.Context, ctl *client.Client) error {
vInfo, err := ctl.Version()
if err != nil {
return err
}
row := func(name, value string) {
fmt.Printf("%25s: %s\n", name, value)
}
row("Client Version", version.String())
row("Client Rev", version.GitRev)
row("Server Version", vInfo.ServerSemVer)
row("Server Rev", vInfo.ServerRev)
row("Backend (ipfs) Version", vInfo.BackendSemVer)
row("Backend (ipfs) Rev", vInfo.BackendRev)
row("Build time", version.BuildTime)
return nil
}
func handleGc(ctx *cli.Context, ctl *client.Client) error {
aggressive := ctx.Bool("aggressive")
freed, err := ctl.GarbageCollect(aggressive)
if err != nil {
return err
}
if len(freed) == 0 {
fmt.Println("Nothing freed.")
return nil
}
tabW := tabwriter.NewWriter(
os.Stdout, 0, 0, 2, ' ',
tabwriter.StripEscape,
)
fmt.Fprintln(tabW, "CONTENT\tHASH\tOWNER\t")
for _, gcItem := range freed {
fmt.Fprintf(
tabW,
"%s\t%s\t%s\t\n",
color.WhiteString(gcItem.Path),
color.RedString(gcItem.Content.ShortB58()),
color.CyanString(gcItem.Owner),
)
}
return tabW.Flush()
}
func handleFstabAdd(ctx *cli.Context, ctl *client.Client) error {
mountName := ctx.Args().Get(0)
mountPath := ctx.Args().Get(1)
options := client.MountOptions{
ReadOnly: ctx.Bool("readonly"),
RootPath: ctx.String("root"),
}
return ctl.FstabAdd(mountName, mountPath, options)
}
func handleFstabRemove(ctx *cli.Context, ctl *client.Client) error {
mountName := ctx.Args().Get(0)
return ctl.FstabRemove(mountName)
}
func handleFstabApply(ctx *cli.Context, ctl *client.Client) error {
if ctx.Bool("unmount") {
return ctl.FstabUnmountAll()
}
return ctl.FstabApply()
}
func handleFstabUnmounetAll(ctx *cli.Context, ctl *client.Client) error {
return ctl.FstabUnmountAll()
}
func handleFstabList(ctx *cli.Context, ctl *client.Client) error {
mounts, err := ctl.FsTabList()
if err != nil {
return ExitCode{UnknownError, fmt.Sprintf("config list: %v", err)}
}
if len(mounts) == 0 {
return nil
}
tabW := tabwriter.NewWriter(
os.Stdout, 0, 0, 2, ' ',
tabwriter.StripEscape,
)
tmpl, err := readFormatTemplate(ctx)
if err != nil {
return err
}
if tmpl == nil && len(mounts) != 0 {
fmt.Fprintln(tabW, "NAME\tPATH\tREAD_ONLY\tROOT\tACTIVE\t")
}
for _, entry := range mounts {
if tmpl != nil {
if err := tmpl.Execute(os.Stdout, entry); err != nil {
return err
}
continue
}
fmt.Fprintf(
tabW,
"%s\t%s\t%s\t%s\t%s\n",
entry.Name,
entry.Path,
yesify(entry.ReadOnly),
entry.Root,
checkmarkify(entry.Active),
)
}
return tabW.Flush()
}
func handleGatewayStart(ctx *cli.Context, ctl *client.Client) error {
isEnabled, err := ctl.ConfigGet("gateway.enabled")
if err != nil {
return err
}
if isEnabled == "false" {
if err := ctl.ConfigSet("gateway.enabled", "true"); err != nil {
return err
}
} else {
fmt.Println("Seems like we're running already.")
}
port, err := ctl.ConfigGet("gateway.port")
if err != nil {
return err
}
isHTTPS, err := gatewayIsHTTPS(ctl)
if err != nil {
return err
}
protocol := "http"
if isHTTPS {
protocol = "https"
}
fmt.Printf(
"The gateway is accessible via %s\n",
color.GreenString(protocol+"://localhost:"+port+"/get/README.md"),
)
return nil
}
func gatewayIsHTTPS(ctl *client.Client) (bool, error) {
certPath, err := ctl.ConfigGet("gateway.cert.certfile")
if err != nil {
return false, err
}
keyPath, err := ctl.ConfigGet("gateway.cert.keyfile")
if err != nil {
return false, err
}
return certPath != "" && keyPath != "", nil
}
func handleGatewayStatus(ctx *cli.Context, ctl *client.Client) error {
isEnabled, err := ctl.ConfigGet("gateway.enabled")
if err != nil {
return err
}
if isEnabled == "false" {
fmt.Println("The gateway is not running. Use `brig gateway start`.")
return nil
}
port, err := ctl.ConfigGet("gateway.port")
if err != nil {
return err
}
domain, err := ctl.ConfigGet("gateway.cert.domain")
if err != nil {
return err
}
if domain == "" {
domain = "localhost"
}
fmt.Printf("Running on %s\n", color.GreenString(fmt.Sprintf("https://%s:%s", domain, port)))
folders, err := ctl.ConfigGet("gateway.folders")
if err != nil {
return err
}
if folders == "" {
fmt.Println(color.RedString("No folders are accessible at all (empty folder config)."))
fmt.Println(color.RedString("You might want to change that by setting this config key:"))
fmt.Println()
fmt.Println(" $ brig cfg set gateway.folders <folder1> <folder2> ...")
fmt.Println()
fmt.Println("To enable access to everything, you can use the root folder:")
fmt.Println()
fmt.Println(" $ brig cfg set gateway.folders /")
fmt.Println()
} else {
fmt.Println("All files under the following folders are accessible:")
fmt.Println()
for _, folder := range strings.Split(folders, " ;; ") {
fmt.Println(" ", folder)
}
fmt.Println()
}
isHTTPS, err := gatewayIsHTTPS(ctl)
if isHTTPS {
fmt.Printf("Using %s for transmitting files. Nice.\n", color.GreenString("https"))
} else {
fmt.Printf("Using %s for transmitting files.", color.RedString("http"))
fmt.Println("Consider changing this (if possible) by using `brig gateway cert`.")
}
authIsEnabled, err := ctl.ConfigGet("gateway.auth.enabled")
if err != nil {
return err
}
if authIsEnabled == "true" {
fmt.Printf("Basic HTTP authentication with user and password is enabled.\n")
} else {
fmt.Printf("There is %s user authentication enabled.\n", color.YellowString("no"))
fmt.Printf("You can enable it by setting the following config keys:\n")
fmt.Printf("\n")
fmt.Printf(" $ brig config set gateway.auth.user <user>\n")
fmt.Printf(" $ brig config set gateway.auth.pass <pass>\n")
fmt.Printf("\n")
}
redirIsEnabled, err := ctl.ConfigGet("gateway.cert.redirect.enabled")
if redirIsEnabled == "true" {
redirPort, err := ctl.ConfigGet("gateway.cert.redirect.http_port")
if err != nil {
return err
}
fmt.Printf(
"All requests on HTTP port %s will be forwarded to HTTPS port %s.\n",
color.GreenString(redirPort),
color.GreenString(port),
)
} else if isHTTPS {
fmt.Printf("There is not HTTP port configured that forwards to HTTPS.\n")
}
return nil
}
func handleGatewayStop(ctx *cli.Context, ctl *client.Client) error {
isEnabled, err := ctl.ConfigGet("gateway.enabled")
if err != nil {
return err
}
if isEnabled == "true" {
if err := ctl.ConfigSet("gateway.enabled", "false"); err != nil {
return err
}
fmt.Println("The gateway will stop serving after handling all open requests.")
} else {
fmt.Println("It seems like the gateway is already stopped.")
}
return nil
}
func handleGatewayCert(ctx *cli.Context) error {
domain := ctx.Args().Get(0)
if domain == "" {
return fmt.Errorf("Usage: brig gateway cert your.domain.org ")
}
cacheDir := ctx.String("cache-dir")
if cacheDir == "" {
var err error
cacheDir, err = gateway.UserCacheDir()
if err != nil {
return err
}
cacheDir = filepath.Join(cacheDir, "brig")
}
if os.Geteuid() != 0 {
fmt.Println(
color.YellowString("You are not root. We need root rights to bind to port 80."),
)
fmt.Println(
color.YellowString("I will re-execute this command for you as:"),
)
fmt.Printf("$ sudo brig gateway cert %s --cache-dir %s\n", domain, cacheDir)
fmt.Println()
exePath, err := getExecutablePath()
if err != nil {
return err
}
sudoPath, err := exec.LookPath("sudo")
if err != nil {
return err
}
proc := exec.Command(
sudoPath,
exePath,
"gateway", "cert",
domain,
cacheDir,
)
proc.Stdin = os.Stdin
proc.Stdout = os.Stdout
proc.Stderr = os.Stderr
if err := proc.Start(); err != nil {
return err
}
return proc.Wait()
}
privPath, pubPath, err := gateway.FetchTLSCertificate(domain, cacheDir)
if err != nil {
fmt.Printf("Failed to download cert: %s\n", err)
return err
}
fmt.Println("A certificate was downloaded successfully.")
port := guessPort(ctx)
ctl, err := client.Dial(context.Background(), port)
if err != nil {
fmt.Println("There does not seem a daemon running currently.")
fmt.Println("Please execute the following commands when it is running:")
fmt.Println("")
fmt.Printf(" $ brig config set gateway.cert.certfile '%s'\n", pubPath)
fmt.Printf(" $ brig config set gateway.cert.keyfile '%s'\n", privPath)
fmt.Println("")
fmt.Println("Alternatively, just re-run this command again some time else.")
return nil
}
defer ctl.Close()
if err := ctl.ConfigSet("gateway.cert.domain", domain); err != nil {
return err
}
if err := ctl.ConfigSet("gateway.cert.certfile", pubPath); err != nil {
return err
}
if err := ctl.ConfigSet("gateway.cert.keyfile", privPath); err != nil {
return err
}
fmt.Println("Successfully set the gateway config to use the certificate.")
fmt.Println("Note that you have to re-run this command every 90 days currently.")
return nil
}
func handleGatewayURL(ctx *cli.Context, ctl *client.Client) error {
path := ctx.Args().First()
if _, err := ctl.Stat(path); err != nil {
return err
}
domain, err := ctl.ConfigGet("gateway.cert.domain")
if err != nil {
return err
}
if domain == "" {
domain = "localhost"
}
port, err := ctl.ConfigGet("gateway.port")
if err != nil {
return err
}
if port == "80" || port == "443" {
port = ""
} else {
port = ":" + port
}
isHTTPS, err := gatewayIsHTTPS(ctl)
if err != nil {
return err
}
protocol := "http"
if domain != "localhost" && isHTTPS {
protocol = "https"
}
escapedPath := url.PathEscape(strings.TrimLeft(path, "/"))
fmt.Printf("%s://%s%s/get/%s\n", protocol, domain, port, escapedPath)
return nil
}