-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
124 lines (99 loc) · 2.6 KB
/
app.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
package main
import (
"crypto/tls"
"io"
"log"
"net/http"
"net/url"
"strconv"
"strings"
"text/template"
"time"
)
type Domain struct {
Name string
StatusCode string
CertificateExpiry string
}
func isValidURL(inputURL string) bool {
_, err := url.ParseRequestURI(inputURL)
if err != nil {
return false
}
resp, err := http.Get(inputURL)
if err != nil {
return false
}
defer resp.Body.Close()
return true
}
func checkDomainStatus(domainName string) string {
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
}
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
},
}
fullUrl := "https://" + domainName
if isValidURL(fullUrl) == false {
return "Invalid URL."
}
resp, _ := client.Get(fullUrl)
defer resp.Body.Close()
statusCode := resp.StatusCode
statusText := http.StatusText(statusCode)
return strconv.Itoa(statusCode) + " - " + statusText
}
func checkCertificateExpiry(domainName string) string {
conn, err := tls.Dial("tcp", domainName+":443", nil)
if err != nil {
switch err.(type) {
case *tls.CertificateVerificationError:
return "Certificate verification failed."
default:
return "Connection failed."
}
}
defer conn.Close()
expiry := conn.ConnectionState().PeerCertificates[0].NotAfter
expiryStr := expiry.Format("January 2, 2006")
if expiry.After(time.Now()) {
return expiryStr + " \u2705"
} else {
return expiryStr + " \u274C"
}
}
func main() {
port := ":8080"
log.Println("Starting server on port", strings.Split(port, ":")[1])
healthCheck := func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "It works!")
}
http.HandleFunc("/healthcheck", healthCheck)
handler1 := func(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("templates/index.html"))
// domains := map[string][]Domain{
// "Domains": {
// {
// Name: "example.com",
// StatusCode: "200",
// CertificateExpiry: "June 20, 2024",
// },
// },
// }
// tmpl.Execute(w, domains)
tmpl.Execute(w, nil)
}
handler2 := func(w http.ResponseWriter, r *http.Request) {
domainName := r.PostFormValue("domain-name")
statusCode := checkDomainStatus(domainName)
certificateExpiry := checkCertificateExpiry(domainName)
tmpl := template.Must(template.ParseFiles("templates/index.html"))
tmpl.ExecuteTemplate(w, "domain-list-element", Domain{Name: domainName, StatusCode: statusCode, CertificateExpiry: certificateExpiry})
}
http.HandleFunc("/", handler1)
http.HandleFunc("/add-domain/", handler2)
log.Fatal(http.ListenAndServe(port, nil))
}