-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
131 lines (110 loc) · 2.61 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
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
package main
import (
"fmt"
"github.com/pete911/certinfo/pkg/cert"
"os"
"strconv"
"strings"
"sync"
)
var Version = "dev"
func main() {
flags, err := ParseFlags()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
if flags.Version {
fmt.Println(Version)
os.Exit(0)
}
certificatesFiles := LoadCertificatesLocations(flags)
if flags.NoExpired {
certificatesFiles = certificatesFiles.RemoveExpired()
}
if flags.NoDuplicate {
certificatesFiles = certificatesFiles.RemoveDuplicates()
}
if flags.SortExpiry {
certificatesFiles = certificatesFiles.SortByExpiry()
}
if flags.Expiry {
PrintCertificatesExpiry(certificatesFiles)
return
}
if flags.PemOnly {
PrintPemOnly(certificatesFiles, flags.Chains)
return
}
PrintCertificatesLocations(certificatesFiles, flags.Chains, flags.Pem)
}
func LoadCertificatesLocations(flags Flags) cert.CertificateLocations {
var certificateLocations cert.CertificateLocations
if flags.Clipboard {
certificateLocations = append(certificateLocations, cert.LoadCertificateFromClipboard())
}
if len(flags.Args) > 0 {
certificateLocations = append(certificateLocations, loadFromArgs(flags.Args, flags.Insecure)...)
}
if isStdin() {
certificateLocations = append(certificateLocations, cert.LoadCertificateFromStdin())
}
if len(certificateLocations) > 0 {
return certificateLocations
}
// no stdin and no args
flags.Usage()
os.Exit(0)
return nil
}
func loadFromArgs(args []string, insecure bool) cert.CertificateLocations {
out := make(chan cert.CertificateLocation)
go func() {
var wg sync.WaitGroup
for _, arg := range args {
wg.Add(1)
go func() {
defer wg.Done()
if isTCPNetworkAddress(arg) {
out <- cert.LoadCertificatesFromNetwork(arg, insecure)
return
}
out <- cert.LoadCertificatesFromFile(arg)
}()
}
wg.Wait()
close(out)
}()
// load certificates from the channel
certsByArgs := make(map[string]cert.CertificateLocation)
for location := range out {
certsByArgs[location.Path] = location
}
// sort certificates by input arguments
var certsSortedByArgs cert.CertificateLocations
for _, arg := range args {
certsSortedByArgs = append(certsSortedByArgs, certsByArgs[arg])
}
return certsSortedByArgs
}
func isTCPNetworkAddress(arg string) bool {
parts := strings.Split(arg, ":")
if len(parts) != 2 {
return false
}
if _, err := strconv.Atoi(parts[1]); err != nil {
return false
}
return true
}
func isStdin() bool {
info, err := os.Stdin.Stat()
if err != nil {
fmt.Printf("checking stdin: %v\n", err)
return false
}
if (info.Mode() & os.ModeCharDevice) == 0 {
return true
}
return false
}