-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatchcert.go
69 lines (53 loc) · 1.28 KB
/
catchcert.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
package main
import (
"flag"
"fmt"
"os"
"strconv"
"github.com/marcobellaccini/catchcert/certfun"
)
// program version
const version string = "v0.0.1"
// usage examples
const usageEx = `Connect to a server using TLS and get PEM-encoded server certificate.
Usage:
catchcert <SERVER> [<PORT>]
Examples:
catchcert wikipedia.org
catchcert server.contoso.com 8443
catchcert github.com > github.pem
catchcert dc.contoso.com 636 > contoso_dc.crt
Check https://github.com/marcobellaccini/catchcert for more information.`
func main() {
verFlag := flag.Bool("version", false, "print version")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, usageEx)
fmt.Fprintf(os.Stderr, "\n\nOptions:\n")
flag.PrintDefaults()
}
flag.Parse()
if *verFlag {
fmt.Println("catchcert", version)
os.Exit(0)
}
args := flag.Args()
var port string = "443" // default port
if len(args) == 2 {
port = args[1]
if _, err := strconv.Atoi(port); err != nil {
fmt.Println(usageEx)
os.Exit(101)
}
} else if len(args) > 2 || len(args) == 0 {
fmt.Println(usageEx)
os.Exit(102)
}
tgt := args[0] // target server
cert, err := certfun.GetCertsPEM(tgt + ":" + port)
if err != nil {
fmt.Println("Unable to get certificate for", tgt, "port", port)
os.Exit(201)
} else {
fmt.Println(cert)
}
}