-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
144 lines (135 loc) · 4.72 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
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"encoding/json"
"fmt"
"flag"
"os"
"time"
"crypto/tls"
"errors"
"bufio"
"sync"
"net/smtp"
"io/ioutil"
"github.com/fatih/color"
)
type Config struct {
Email string `json:"sender_email"`
Password string `json:"sender_password"`
Host string `json:"smtp_host"`
Port string `json:"smtp_port"`
Receivers []string `json:"receivers"`
AlertBefore int `json:"alert_before"`
}
var (
domain = flag.String("d", "", "Single domain to be checked, in case you don't have a file containing domain names")
filename = flag.String("f", "", "Route to a file with domains to check")
left = flag.Bool("l", false, "Is going to return number of days left until the certificate expires instead of the expiry date")
email = flag.String("e", "", "Route to a JSON file with email configuration")
)
func sslCheck(url string) (time.Time, error) {
conn, err := tls.Dial("tcp", fmt.Sprintf("%s:443", url), nil)
if err != nil {
return time.Time{}, errors.New("Couldn't establish a connection with the site. It's possible that you misspelled it's name or it has no certificate")
}
err = conn.VerifyHostname(url)
if err != nil {
return time.Time{}, errors.New("Couldn't find ssl certificate on the site.")
}
expiry := conn.ConnectionState().PeerCertificates[0].NotAfter
return expiry, nil
}
func main() {
var wg sync.WaitGroup
flag.Parse()
var emailJson Config
if *domain != "" && *filename != "" {
fmt.Println("You can't choose both -d and -f flag at the same time.")
os.Exit(2)
} else if *domain != "" {
func(domain string, left bool) {
ex, err := sslCheck(domain)
if err != nil {
fmt.Printf("%s: %s\n", color.RedString(domain), err)
os.Exit(2)
} else {
l := int(ex.Sub(time.Now()).Hours() / 24)
if *email != "" {
emailJson = parseJsonFile(*email)
if l <= emailJson.AlertBefore {
sendMail(emailJson.Email, emailJson.Password, emailJson.Host, emailJson.Port, domain, emailJson.Receivers, l)
}
}
if left {
fmt.Printf("%s: %s | %d days left\n", color.GreenString(domain), color.YellowString(ex.String()), l)
} else {
fmt.Printf("%s: %s\n", color.GreenString(domain), color.YellowString(ex.String()))
}
}
}(*domain, *left)
} else if *domain == "" && *filename == "" {
fmt.Println("You have to provide a single domain (-d flag) or a file that contain domains (-f flag)")
} else if *filename != "" {
fmt.Println("Reading from file:", color.MagentaString(*filename))
readFile, err := os.Open(*filename)
if err != nil {
fmt.Println("No file named:", color.RedString(*filename))
}
fileScanner := bufio.NewScanner(readFile)
fileScanner.Split(bufio.ScanLines)
var lines []string
for fileScanner.Scan() {
lines = append(lines, fileScanner.Text())
}
readFile.Close()
wg.Add(len(lines))
for _, line := range lines {
go func(domain string, left bool) {
defer wg.Done()
ex, err := sslCheck(domain)
if err != nil {
fmt.Printf("%s: %s\n", color.RedString(domain), err)
} else {
l := int(ex.Sub(time.Now()).Hours() / 24)
if *email != "" {
emailJson = parseJsonFile(*email)
if l <= emailJson.AlertBefore {
sendMail(emailJson.Email, emailJson.Password, emailJson.Host, emailJson.Port, domain, emailJson.Receivers, l)
}
}
if left {
fmt.Printf("%s: %s | %d days left\n", color.GreenString(domain), color.YellowString(ex.String()), l)
} else {
fmt.Printf("%s: %s\n", color.GreenString(domain), color.YellowString(ex.String()))
}
}
}(line, *left)
}
wg.Wait()
}
}
func sendMail(sender, password, host, port, domain string, receiver []string, left int) string {
auth := smtp.PlainAuth("", sender, password, host)
msg := fmt.Sprintf("To: %s\r\nSubject: Your certificate on %s is going to expire\r\n Certificate on %s is going to expire in %d days\r\n", receiver, domain, domain, left)
body := []byte(msg)
err := smtp.SendMail(host + ":" + port, auth, sender, receiver, body)
if err != nil {
return "Couldn't send the email"
}
return "Email was sent successfully"
}
func parseJsonFile(file string) Config {
readFile, err := os.Open(file)
defer readFile.Close()
if err != nil {
fmt.Println("Couldn't find the email configuration file.")
os.Exit(2)
}
byteValue, _ := ioutil.ReadAll(readFile)
var config Config
if err := json.Unmarshal(byteValue, &config); err != nil {
fmt.Println("Failed to parse the email configuration file.")
os.Exit(2)
}
return config
}