forked from MatiasDesuu/ThinkDashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatus.go
More file actions
156 lines (138 loc) · 3.55 KB
/
Copy pathstatus.go
File metadata and controls
156 lines (138 loc) · 3.55 KB
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
package main
import (
"crypto/tls"
"encoding/json"
"net"
"net/http"
"net/url"
"time"
)
// PingURL checks the status and response time of a bookmark URL
func (h *Handlers) PingURL(w http.ResponseWriter, r *http.Request) {
// Set CORS headers first
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
// Get URL from query parameter
urlParam := r.URL.Query().Get("url")
if urlParam == "" {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]interface{}{
"error": "URL parameter is required",
"status": "offline",
"ping": nil,
})
return
}
// Parse and validate URL
parsedURL, err := url.Parse(urlParam)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]interface{}{
"error": "Invalid URL",
"status": "offline",
"ping": nil,
})
return
}
// Validate that the URL belongs to a registered bookmark
allBookmarks := h.store.GetAllBookmarks()
isValidBookmark := false
for _, bookmark := range allBookmarks {
if bookmark.URL == urlParam {
isValidBookmark = true
break
}
}
if !isValidBookmark {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]interface{}{
"error": "URL is not a registered bookmark",
"status": "offline",
"ping": nil,
})
return
}
// Extract host and port
host := parsedURL.Hostname()
port := parsedURL.Port()
if port == "" {
if parsedURL.Scheme == "https" {
port = "443"
} else {
port = "80"
}
}
// Get skipFastPing query parameter
skipFastPing := r.URL.Query().Get("skipFastPing")
// Start timing
start := time.Now()
if skipFastPing == "" {
// Try TCP connection first (fast ping)
address := net.JoinHostPort(host, port)
conn, err := net.DialTimeout("tcp", address, 2*time.Second)
if err == nil {
conn.Close()
elapsed := time.Since(start).Milliseconds()
// Ensure minimum of 1ms for display purposes
if elapsed < 1 {
elapsed = 1
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "online",
"ping": elapsed,
})
return
}
}
// If TCP fails (or fast ping disabled), try a quick HTTP request as fallback
client := &http.Client{
Timeout: 3 * time.Second,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
DialContext: (&net.Dialer{
Timeout: 2 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 2 * time.Second,
ResponseHeaderTimeout: 2 * time.Second,
},
}
req, err := http.NewRequest("GET", urlParam, nil)
if err != nil {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "offline",
"ping": nil,
})
return
}
// Add User-Agent header to avoid being blocked by some servers
req.Header.Set("User-Agent", "ThinkDashboard-Ping/1.0")
resp, err := client.Do(req)
if resp != nil {
defer resp.Body.Close()
}
elapsed := time.Since(start).Milliseconds()
// Ensure minimum of 1ms for display purposes
if elapsed < 1 {
elapsed = 1
}
if err == nil && resp != nil && resp.StatusCode >= 200 && resp.StatusCode < 500 {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "online",
"ping": elapsed,
})
return
}
// Offline
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "offline",
"ping": nil,
})
}