forked from rmasci/sslcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
115 lines (94 loc) · 2.51 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
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
serv "github.com/iobear/sslcheck/http"
sslcheck "github.com/iobear/sslcheck/lib"
logmodule "github.com/iobear/sslcheck/log"
)
var runAs = filepath.Base(os.Args[0])
var version string
var logger *logmodule.Logger
type CLIArgs struct {
IPAddress string
DomainName string
Port string
Help bool
LogLevel logmodule.LogLevel
IsServer bool
}
func parseCLIArgs() CLIArgs {
var args CLIArgs
var logLevelString string
flag.StringVar(&args.IPAddress, "ip", "", "IP Address")
flag.StringVar(&args.DomainName, "domain", "", "Domain Name")
flag.StringVar(&args.Port, "port", "443", "Port Number")
flag.BoolVar(&args.Help, "help", false, "Help")
flag.StringVar(&logLevelString, "loglevel", "error", "Log level (debug, info, warning, error, critical)")
flag.BoolVar(&args.IsServer, "serv", false, "Run as HTTP service")
flag.Parse()
switch logLevelString {
case "debug":
args.LogLevel = logmodule.DEBUG
case "info":
args.LogLevel = logmodule.INFO
case "warn":
args.LogLevel = logmodule.WARNING
case "warning":
args.LogLevel = logmodule.WARNING
case "error":
args.LogLevel = logmodule.ERROR
case "critical":
args.LogLevel = logmodule.CRITICAL
default:
log.Fatalf("Unknown log level: %s", logLevelString)
}
if args.DomainName == "" && len(flag.Args()) > 0 {
args.DomainName = flag.Args()[0]
} else if args.DomainName == "" && len(flag.Args()) == 0 {
args.IsServer = true
}
return args
}
func checkSSL(args CLIArgs) {
logger = logmodule.NewLogger(args.LogLevel)
checker := &sslcheck.SSLChecker{Logger: logger}
if args.DomainName == "" && args.IPAddress == "" {
logger.Error("Either domain name or IP address is required.")
os.Exit(1)
}
info := sslcheck.SSLInfo{
IPAddress: args.IPAddress,
DomainName: args.DomainName,
Port: args.Port,
}
result, err := checker.CheckSSL(info)
if err != nil {
logger.Error("Error: %v", err)
os.Exit(1)
}
logger.Info("Is Valid: %v", result.IsValid)
logger.Info("Details: %v", result.Details)
}
// Usage is what is run if the right parameters are not met upon startup.
func Usage() {
// To embed the bot user and password comment the line above and uncomment the line below
fmt.Printf("Usage: %v -i <ip address> -p <port> -d <domain name> -loglevel <log level>\n", runAs)
flag.PrintDefaults()
fmt.Println(version)
}
func main() {
args := parseCLIArgs()
if args.Help {
Usage()
os.Exit(0)
}
if args.IsServer {
serv.StartServer()
return
}
checkSSL(args)
}