This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathmain.go
77 lines (67 loc) · 1.66 KB
/
main.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
package main
import (
//"crypto/x509"
"crypto/x509"
"fmt"
"net/url"
"os"
"github.com/Sirupsen/logrus"
"github.com/gravitational/teleconsole/clt"
"github.com/gravitational/teleconsole/version"
"github.com/gravitational/trace"
)
func main() {
app, err := clt.NewApp(nil)
fatalIf(err)
conf := app.GetConfig()
if conf.Verbosity == 0 {
logrus.SetLevel(logrus.PanicLevel)
} else {
logrus.SetFormatter(&trace.TextFormatter{})
app.DebugDump()
}
// we have CLI args?
if len(app.Args) > 0 {
switch app.Args[0] {
case "help":
app.Usage()
case "join":
err = app.Join()
case "version":
version.Print("Teleconsole", conf.Verbosity > 0)
os.Exit(0)
default:
app.Usage()
}
// no CLI args? Start a new broadcast!
} else {
err = app.Start()
}
fatalIf(err)
}
func fatalIf(err error) {
if err != nil {
// see if it's untrusted HTTPS certificate error?
if badCert, url := IsUntrustedCertError(err); badCert {
fmt.Fprintf(os.Stderr, "\033[1mWARNING:\033[0m The SSL certificate for %s cannot be trusted!\n", url)
fmt.Fprintf(os.Stderr, "Either you are being attacked, or try -insecure flag if you know what you're doing\n")
// HTTP reponse error:
} else {
fmt.Fprintf(os.Stderr, "%s\n", err)
}
logrus.Debug(trace.DebugReport(err))
os.Exit(1)
}
}
// returns true if the error indicates
// "x509: certificate signed by unknown authority" error when talking to HTTPS server
//
// Also returns URL which caused the error
func IsUntrustedCertError(err error) (bool, string) {
switch t := trace.Unwrap(err).(interface{}).(type) {
case *url.Error:
_, ok := t.Err.(x509.UnknownAuthorityError)
return ok, t.URL
}
return false, ""
}