This repository has been archived by the owner on Jun 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cmd_tls.go
71 lines (59 loc) · 1.75 KB
/
cmd_tls.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
package main
import (
"crypto/tls"
"errors"
"fmt"
"os"
"gopkg.in/alecthomas/kingpin.v2"
)
type TLSCommand struct {
}
func configureTLSCommand(app *kingpin.Application) {
tc := &TLSCommand{}
app.Command("tls", "Connects to broker using Transport Layer Security which can be useful for TLS handshake debugging.").Action(tc.runTLS)
}
func printConnectionState(connState tls.ConnectionState) {
switch connState.Version {
case tls.VersionSSL30:
fmt.Printf("Version: %s\n", "SSLv3")
break
case tls.VersionTLS10:
fmt.Printf("Version: %s\n", "TLSv1.0")
break
case tls.VersionTLS11:
fmt.Printf("Version: %s\n", "TLSv1.1")
break
case tls.VersionTLS12:
fmt.Printf("Version: %s\n", "TLSv1.2")
break
default:
fmt.Printf("Version: %d\n", connState.Version)
break
}
fmt.Printf("HandshakeComplete: %t\n", connState.HandshakeComplete)
fmt.Printf("NegotiatedProtocol: %s\n", connState.NegotiatedProtocol)
fmt.Printf("NegotiatedProtocolIsMutual: %t\n", connState.NegotiatedProtocolIsMutual)
fmt.Printf("CipherSuite: %#x\n", connState.CipherSuite)
}
func (tc *TLSCommand) runTLS(ctx *kingpin.ParseContext) error {
useTLS, tlsConfig, err := tlsConfig()
must(err)
brokers := brokers(useTLS)
if tlsConfig == nil || len(tlsConfig.Certificates) == 0 {
return errors.New("No certificates were loaded")
}
fmt.Printf("Number of Certificates: %d\n", len(tlsConfig.Certificates))
conn, err := tls.Dial("tcp", brokers[0], tlsConfig)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to connect with TCP client: %v\n", err)
return err
}
defer conn.Close()
if err := conn.Handshake(); err != nil {
fmt.Fprintf(os.Stderr, "Failed to handshake: %v\n", err)
return err
}
printConnectionState(conn.ConnectionState())
fmt.Println("TLS check done.")
return nil
}