-
Notifications
You must be signed in to change notification settings - Fork 3
/
net.go
213 lines (183 loc) · 4.69 KB
/
net.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package net
import (
"crypto/tls"
"flag"
"fmt"
"io"
"log"
"net"
"net/http"
"net/http/httptrace"
"net/url"
"os"
"strings"
"time"
)
type WebsiteCheckResult struct {
URL string
IsUp bool
ResponseTime time.Duration
TraceInfo *HttpTraceInfo
AssertionPassed bool
LastCheckTime time.Time
AssertText string
}
type HttpTraceInfo struct {
Wait time.Duration
DNSLookup time.Duration
TCPConnection time.Duration
TimeToFirstByte time.Duration
DownloadDuration time.Duration
}
type NetworkConfig struct {
Timeout time.Duration
ShouldFail bool
FollowRedirects bool
SkipSSL bool
AssertText string
RefreshInterval time.Duration
}
func CheckWebsite(url string, config NetworkConfig) WebsiteCheckResult {
result := WebsiteCheckResult{
URL: url,
LastCheckTime: time.Now(),
}
var start, connect, dnsStart, dnsDone, gotFirstByte time.Time
trace := &httptrace.ClientTrace{
DNSStart: func(_ httptrace.DNSStartInfo) { dnsStart = time.Now() },
DNSDone: func(_ httptrace.DNSDoneInfo) { dnsDone = time.Now() },
GotConn: func(_ httptrace.GotConnInfo) { connect = time.Now() },
GotFirstResponseByte: func() { gotFirstByte = time.Now() },
}
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: config.SkipSSL || isIPAddress(url)},
}
client := &http.Client{
Timeout: config.Timeout,
Transport: transport,
}
if !config.FollowRedirects {
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
}
req, _ := http.NewRequest("GET", url, nil)
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
start = time.Now()
resp, err := client.Do(req)
result.ResponseTime = time.Since(start)
if err != nil {
return result
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return result
}
success := resp.StatusCode >= 200 && resp.StatusCode < 300
if config.ShouldFail {
success = !success
}
bodyString := string(body)
result.AssertionPassed = true
result.AssertText = config.AssertText
if config.AssertText != "" {
result.AssertionPassed = strings.Contains(bodyString, config.AssertText)
if !result.AssertionPassed {
success = false
}
}
result.IsUp = success
result.TraceInfo = &HttpTraceInfo{
Wait: dnsStart.Sub(start),
DNSLookup: dnsDone.Sub(dnsStart),
TCPConnection: connect.Sub(dnsDone),
TimeToFirstByte: gotFirstByte.Sub(connect),
DownloadDuration: time.Since(gotFirstByte),
}
return result
}
func GetSSLCertExpiry(siteUrl string) int {
u, err := url.Parse(siteUrl)
if err != nil {
return -1
}
host := u.Host
if !strings.Contains(host, ":") {
host += ":443"
}
conn, err := tls.Dial("tcp", host, &tls.Config{})
if err != nil {
return -1
}
defer conn.Close()
if len(conn.ConnectionState().PeerCertificates) > 0 {
cert := conn.ConnectionState().PeerCertificates[0]
daysUntilExpiry := int(time.Until(cert.NotAfter).Hours() / 24)
return daysUntilExpiry
}
return -1
}
func isIPAddress(host string) bool {
u, err := url.Parse(host)
if err != nil {
return false
}
hostname := u.Hostname()
return net.ParseIP(hostname) != nil
}
func TryHTTPSConnection(urlString string) (*http.Response, error) {
const defaultTimeout = 5 * time.Second
client := http.Client{
Timeout: defaultTimeout,
}
resp, err := client.Head(urlString)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return resp, nil
}
func formatURL(inputURL string) (string, error) {
inputURL = strings.TrimSpace(inputURL)
if strings.Contains(inputURL, "://") {
return inputURL, nil
}
return "https://" + inputURL, nil
}
func isUrl(str string) bool {
url, err := url.ParseRequestURI(str)
if err != nil {
return false
}
address := net.ParseIP(url.Host)
if address == nil {
return strings.Contains(url.Host, ".")
}
return true
}
func AutoDetectProtocol(inputURL string) string {
formattedURL, err := formatURL(inputURL)
if err != nil {
log.Printf("Error normalizing URL: %v, fallback to input URL\n", err)
return inputURL
}
if !isUrl(formattedURL) {
fmt.Printf("Error: Invalid URL provided. Please ensure the URL is correct.\n\n")
flag.Usage()
os.Exit(1)
}
resp, err := TryHTTPSConnection(formattedURL)
if err == nil && resp.StatusCode < 400 {
return formattedURL
}
if strings.HasPrefix(formattedURL, "https://") {
fallbackURL := strings.Replace(formattedURL, "https://", "http://", 1)
resp, err := TryHTTPSConnection(fallbackURL)
if err == nil && resp.StatusCode < 400 {
log.Println("Fallback to HTTP successful.")
return fallbackURL
}
}
return formattedURL
}