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
214
215
216
217
218
|
package monitor
import (
"bufio"
"context"
"crypto/tls"
"fmt"
"net"
"strings"
"time"
"github.com/Fuwn/kaze/internal/config"
)
// GeminiMonitor monitors Gemini protocol endpoints
type GeminiMonitor struct {
id string
name string
group string
target string
interval time.Duration
timeout time.Duration
retries int
verifySSL bool
hideSSLDays bool
roundResponseTime bool
roundUptime bool
}
// NewGeminiMonitor creates a new Gemini monitor
func NewGeminiMonitor(cfg config.MonitorConfig) (*GeminiMonitor, error) {
// Parse target - should be gemini://host or host:port
target := cfg.Target
// Remove gemini:// prefix if present
target = strings.TrimPrefix(target, "gemini://")
// If no port specified, add default Gemini port 1965
if !strings.Contains(target, ":") {
target = target + ":1965"
}
// Validate host:port format
host, port, err := net.SplitHostPort(target)
if err != nil {
return nil, fmt.Errorf("invalid Gemini target %q: must be host:port format: %w", cfg.Target, err)
}
// Store as host:port for connection
target = net.JoinHostPort(host, port)
verifySSL := true
if cfg.VerifySSL != nil {
verifySSL = *cfg.VerifySSL
}
return &GeminiMonitor{
id: cfg.ID(),
name: cfg.Name,
group: cfg.Group,
target: target,
interval: cfg.Interval.Duration,
timeout: cfg.Timeout.Duration,
retries: cfg.Retries,
verifySSL: verifySSL,
hideSSLDays: cfg.HideSSLDays,
roundResponseTime: cfg.RoundResponseTime,
roundUptime: cfg.RoundUptime,
}, nil
}
// ID returns the unique identifier for this monitor
func (m *GeminiMonitor) ID() string {
return m.id
}
// Name returns the monitor's name
func (m *GeminiMonitor) Name() string {
return m.name
}
// Group returns the group this monitor belongs to
func (m *GeminiMonitor) Group() string {
return m.group
}
// Type returns the monitor type
func (m *GeminiMonitor) Type() string {
return "gemini"
}
// Target returns the monitor target
func (m *GeminiMonitor) Target() string {
return m.target
}
// Interval returns the check interval
func (m *GeminiMonitor) Interval() time.Duration {
return m.interval
}
// Retries returns the number of retry attempts
func (m *GeminiMonitor) Retries() int {
return m.retries
}
// HideSSLDays returns whether to hide SSL days from display
func (m *GeminiMonitor) HideSSLDays() bool {
return m.hideSSLDays
}
// RoundResponseTime returns whether to round response time
func (m *GeminiMonitor) RoundResponseTime() bool {
return m.roundResponseTime
}
// RoundUptime returns whether to round uptime percentage
func (m *GeminiMonitor) RoundUptime() bool {
return m.roundUptime
}
// Check performs the Gemini protocol check
func (m *GeminiMonitor) Check(ctx context.Context) *Result {
result := &Result{
MonitorName: m.id,
Timestamp: time.Now(),
}
// Extract hostname for SNI
host, _, _ := net.SplitHostPort(m.target)
// Configure TLS
tlsConfig := &tls.Config{
ServerName: host,
InsecureSkipVerify: !m.verifySSL,
MinVersion: tls.VersionTLS12,
}
// Create dialer with timeout
dialer := &net.Dialer{
Timeout: m.timeout,
}
// Measure connection and request time
start := time.Now()
// Connect with TLS
conn, err := tls.DialWithDialer(dialer, "tcp", m.target, tlsConfig)
if err != nil {
result.ResponseTime = time.Since(start)
result.Status = StatusDown
result.Error = fmt.Errorf("TLS connection failed: %w", err)
return result
}
defer conn.Close()
// Get SSL certificate info
if len(conn.ConnectionState().PeerCertificates) > 0 {
cert := conn.ConnectionState().PeerCertificates[0]
result.SSLExpiry = &cert.NotAfter
result.SSLDaysLeft = int(time.Until(cert.NotAfter).Hours() / 24)
}
// Send Gemini request (just the URL followed by CRLF)
// Format: gemini://host/path\r\n
geminiURL := fmt.Sprintf("gemini://%s/\r\n", host)
conn.SetDeadline(time.Now().Add(m.timeout))
if _, err := conn.Write([]byte(geminiURL)); err != nil {
result.ResponseTime = time.Since(start)
result.Status = StatusDown
result.Error = fmt.Errorf("failed to send request: %w", err)
return result
}
// Read response header (status code and meta)
reader := bufio.NewReader(conn)
responseLine, err := reader.ReadString('\n')
result.ResponseTime = time.Since(start)
if err != nil {
result.Status = StatusDown
result.Error = fmt.Errorf("failed to read response: %w", err)
return result
}
// Parse status code (first two characters)
if len(responseLine) < 2 {
result.Status = StatusDown
result.Error = fmt.Errorf("invalid Gemini response: too short")
return result
}
statusCode := responseLine[0:2]
// Gemini status codes:
// 1x = INPUT
// 2x = SUCCESS
// 3x = REDIRECT
// 4x = TEMPORARY FAILURE
// 5x = PERMANENT FAILURE
// 6x = CLIENT CERTIFICATE REQUIRED
switch statusCode[0] {
case '2':
result.Status = StatusUp
case '3':
result.Status = StatusUp // Redirects are ok
case '1', '6':
result.Status = StatusDegraded // Input or cert required
result.Error = fmt.Errorf("status %s: %s", statusCode, strings.TrimSpace(responseLine[3:]))
default:
result.Status = StatusDown
result.Error = fmt.Errorf("status %s: %s", statusCode, strings.TrimSpace(responseLine[3:]))
}
return result
}
|