forked from pete911/certinfo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
114 lines (94 loc) · 2.15 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
package main
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/pete911/certinfo/pkg/cert"
)
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.Expiry {
PrintCertificatesExpiry(certificatesFiles)
return
}
if flags.PemOnly {
PrintPemOnly(certificatesFiles, flags.Chains)
return
}
PrintCertificatesLocations(certificatesFiles, flags.Chains, flags.Pem)
}
func LoadCertificatesLocations(flags Flags) cert.CertificateLocations {
if len(flags.Args) > 0 {
var certificateLocations cert.CertificateLocations
for _, arg := range flags.Args {
var certificateLocation cert.CertificateLocation
var err error
if isTCPNetworkAddress(arg) {
certificateLocation, err = cert.LoadCertificatesFromNetwork(arg, flags.Insecure)
} else {
certificateLocation, err = cert.LoadCertificatesFromFile(arg)
}
if err != nil {
printCertFileError(arg, err)
continue
}
certificateLocations = append(certificateLocations, certificateLocation)
}
return certificateLocations
}
if isStdin() {
certificateLocation, err := cert.LoadCertificateFromStdin()
if err != nil {
printCertFileError("stdin", err)
return nil
}
return []cert.CertificateLocation{certificateLocation}
}
// no stdin and not args
flags.Usage()
os.Exit(0)
return nil
}
func printCertFileError(fileName string, err error) {
fmt.Printf("--- [%s] ---\n", fileName)
fmt.Println(err)
fmt.Println()
}
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
}