-
Notifications
You must be signed in to change notification settings - Fork 95
/
logging.go
414 lines (352 loc) · 9.92 KB
/
logging.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package main
import (
"encoding/json"
"fmt"
"log"
"net"
"path/filepath"
"strings"
"time"
)
type logEntry interface {
fmt.Stringer
eventType() string
}
type addressLog struct {
Host string `json:"host"`
Port int `json:"port"`
}
func (entry addressLog) String() string {
return net.JoinHostPort(entry.Host, fmt.Sprint(entry.Port))
}
func getAddressLog(host string, port int, cfg *config) interface{} {
entry := addressLog{
Host: host,
Port: port,
}
if cfg.Logging.SplitHostPort {
return entry
}
return entry.String()
}
type authAccepted bool
func (accepted authAccepted) String() string {
if accepted {
return "accepted"
}
return "rejected"
}
type authLog struct {
User string `json:"user"`
Accepted authAccepted `json:"accepted"`
}
type noAuthLog struct {
authLog
}
func (entry noAuthLog) String() string {
return fmt.Sprintf("authentication for user %q without credentials %v", entry.User, entry.Accepted)
}
func (entry noAuthLog) eventType() string {
return "no_auth"
}
type passwordAuthLog struct {
authLog
Password string `json:"password"`
}
func (entry passwordAuthLog) String() string {
return fmt.Sprintf("authentication for user %q with password %q %v", entry.User, entry.Password, entry.Accepted)
}
func (entry passwordAuthLog) eventType() string {
return "password_auth"
}
type publicKeyAuthLog struct {
authLog
PublicKeyFingerprint string `json:"public_key"`
}
func (entry publicKeyAuthLog) String() string {
return fmt.Sprintf("authentication for user %q with public key %q %v", entry.User, entry.PublicKeyFingerprint, entry.Accepted)
}
func (entry publicKeyAuthLog) eventType() string {
return "public_key_auth"
}
type keyboardInteractiveAuthLog struct {
authLog
Answers []string `json:"answers"`
}
func (entry keyboardInteractiveAuthLog) String() string {
return fmt.Sprintf("authentication for user %q with keyboard interactive answers %q %v", entry.User, entry.Answers, entry.Accepted)
}
func (entry keyboardInteractiveAuthLog) eventType() string {
return "keyboard_interactive_auth"
}
type connectionLog struct {
ClientVersion string `json:"client_version"`
}
func (entry connectionLog) String() string {
return fmt.Sprintf("connection with client version %q established", entry.ClientVersion)
}
func (entry connectionLog) eventType() string {
return "connection"
}
type connectionCloseLog struct {
}
func (entry connectionCloseLog) String() string {
return "connection closed"
}
func (entry connectionCloseLog) eventType() string {
return "connection_close"
}
type tcpipForwardLog struct {
Address interface{} `json:"address"`
}
func (entry tcpipForwardLog) String() string {
return fmt.Sprintf("TCP/IP forwarding on %v requested", entry.Address)
}
func (entry tcpipForwardLog) eventType() string {
return "tcpip_forward"
}
type cancelTCPIPForwardLog struct {
Address interface{} `json:"address"`
}
func (entry cancelTCPIPForwardLog) String() string {
return fmt.Sprintf("TCP/IP forwarding on %v canceled", entry.Address)
}
func (entry cancelTCPIPForwardLog) eventType() string {
return "cancel_tcpip_forward"
}
type noMoreSessionsLog struct {
}
func (entry noMoreSessionsLog) String() string {
return "rejection of further session channels requested"
}
func (entry noMoreSessionsLog) eventType() string {
return "no_more_sessions"
}
type hostKeysProveLog struct {
HostKeyFiles []string `json:"host_key_files"`
}
func (entry hostKeysProveLog) String() string {
baseNames := make([]string, len(entry.HostKeyFiles))
for i, hostKeyFile := range entry.HostKeyFiles {
baseNames[i] = fmt.Sprintf("%q", filepath.Base(hostKeyFile))
}
return fmt.Sprintf("proof of ownership of host keys %v requested", strings.Join(baseNames, ", "))
}
func (entry hostKeysProveLog) eventType() string {
return "host_keys_prove"
}
type channelLog struct {
ChannelID int `json:"channel_id"`
}
type sessionLog struct {
channelLog
}
func (entry sessionLog) String() string {
return fmt.Sprintf("[channel %v] session requested", entry.ChannelID)
}
func (entry sessionLog) eventType() string {
return "session"
}
type sessionCloseLog struct {
channelLog
}
func (entry sessionCloseLog) String() string {
return fmt.Sprintf("[channel %v] closed", entry.ChannelID)
}
func (entry sessionCloseLog) eventType() string {
return "session_close"
}
type sessionInputLog struct {
channelLog
Input string `json:"input"`
}
func (entry sessionInputLog) String() string {
return fmt.Sprintf("[channel %v] input: %q", entry.ChannelID, entry.Input)
}
func (entry sessionInputLog) eventType() string {
return "session_input"
}
type directTCPIPLog struct {
channelLog
From interface{} `json:"from"`
To interface{} `json:"to"`
}
func (entry directTCPIPLog) String() string {
return fmt.Sprintf("[channel %v] direct TCP/IP forwarding from %v to %v requested", entry.ChannelID, entry.From, entry.To)
}
func (entry directTCPIPLog) eventType() string {
return "direct_tcpip"
}
type directTCPIPCloseLog struct {
channelLog
}
func (entry directTCPIPCloseLog) String() string {
return fmt.Sprintf("[channel %v] closed", entry.ChannelID)
}
func (entry directTCPIPCloseLog) eventType() string {
return "direct_tcpip_close"
}
type directTCPIPInputLog struct {
channelLog
Input string `json:"input"`
}
func (entry directTCPIPInputLog) String() string {
return fmt.Sprintf("[channel %v] input: %q", entry.ChannelID, entry.Input)
}
func (entry directTCPIPInputLog) eventType() string {
return "direct_tcpip_input"
}
type ptyLog struct {
channelLog
Terminal string `json:"terminal"`
Width uint32 `json:"width"`
Height uint32 `json:"height"`
}
func (entry ptyLog) String() string {
return fmt.Sprintf("[channel %v] PTY using terminal %q (size %vx%v) requested", entry.ChannelID, entry.Terminal, entry.Width, entry.Height)
}
func (entry ptyLog) eventType() string {
return "pty"
}
type shellLog struct {
channelLog
}
func (entry shellLog) String() string {
return fmt.Sprintf("[channel %v] shell requested", entry.ChannelID)
}
func (entry shellLog) eventType() string {
return "shell"
}
type execLog struct {
channelLog
Command string `json:"command"`
}
func (entry execLog) String() string {
return fmt.Sprintf("[channel %v] command %q requested", entry.ChannelID, entry.Command)
}
func (entry execLog) eventType() string {
return "exec"
}
type subsystemLog struct {
channelLog
Subsystem string `json:"subsystem"`
}
func (entry subsystemLog) String() string {
return fmt.Sprintf("[channel %v] subsystem %q requested", entry.ChannelID, entry.Subsystem)
}
func (entry subsystemLog) eventType() string {
return "subsystem"
}
type x11Log struct {
channelLog
Screen uint32 `json:"screen"`
}
func (entry x11Log) String() string {
return fmt.Sprintf("[channel %v] X11 forwarding on screen %v requested", entry.ChannelID, entry.Screen)
}
func (entry x11Log) eventType() string {
return "x11"
}
type envLog struct {
channelLog
Name string `json:"name"`
Value string `json:"value"`
}
func (entry envLog) String() string {
return fmt.Sprintf("[channel %v] environment variable %q with value %q requested", entry.ChannelID, entry.Name, entry.Value)
}
func (entry envLog) eventType() string {
return "env"
}
type windowChangeLog struct {
channelLog
Width uint32 `json:"width"`
Height uint32 `json:"height"`
}
func (entry windowChangeLog) String() string {
return fmt.Sprintf("[channel %v] window size change to %vx%v requested", entry.ChannelID, entry.Width, entry.Height)
}
func (entry windowChangeLog) eventType() string {
return "window_change"
}
type debugGlobalRequestLog struct {
RequestType string `json:"request_type"`
WantReply bool `json:"want_reply"`
Payload string `json:"payload"`
}
func (entry debugGlobalRequestLog) String() string {
jsonBytes, err := json.Marshal(entry)
if err != nil {
warningLogger.Printf("Failed to log event: %v", err)
return ""
}
return fmt.Sprintf("DEBUG global request received: %v\n", string(jsonBytes))
}
func (entry debugGlobalRequestLog) eventType() string {
return "debug_global_request"
}
type debugChannelLog struct {
channelLog
ChannelType string `json:"channel_type"`
ExtraData string `json:"extra_data"`
}
func (entry debugChannelLog) String() string {
jsonBytes, err := json.Marshal(entry)
if err != nil {
warningLogger.Printf("Failed to log event: %v", err)
return ""
}
return fmt.Sprintf("DEBUG new channel requested: %v\n", string(jsonBytes))
}
func (entry debugChannelLog) eventType() string {
return "debug_channel"
}
type debugChannelRequestLog struct {
channelLog
RequestType string `json:"request_type"`
WantReply bool `json:"want_reply"`
Payload string `json:"payload"`
}
func (entry debugChannelRequestLog) String() string {
jsonBytes, err := json.Marshal(entry)
if err != nil {
warningLogger.Printf("Failed to log event: %v", err)
return ""
}
return fmt.Sprintf("DEBUG channel request received: %v\n", string(jsonBytes))
}
func (entry debugChannelRequestLog) eventType() string {
return "debug_channel_request"
}
func (context connContext) logEvent(entry logEntry) {
if strings.HasPrefix(entry.eventType(), "debug_") && !context.cfg.Logging.Debug {
return
}
if context.cfg.Logging.JSON {
var jsonEntry interface{}
tcpSource := context.RemoteAddr().(*net.TCPAddr)
source := getAddressLog(tcpSource.IP.String(), tcpSource.Port, context.cfg)
if context.cfg.Logging.Timestamps {
jsonEntry = struct {
Time string `json:"time"`
Source interface{} `json:"source"`
EventType string `json:"event_type"`
Event logEntry `json:"event"`
}{time.Now().Format(time.RFC3339), source, entry.eventType(), entry}
} else {
jsonEntry = struct {
Source interface{} `json:"source"`
EventType string `json:"event_type"`
Event logEntry `json:"event"`
}{source, entry.eventType(), entry}
}
logBytes, err := json.Marshal(jsonEntry)
if err != nil {
warningLogger.Printf("Failed to log event: %v", err)
return
}
log.Print(string(logBytes))
} else {
log.Printf("[%v] %v", context.RemoteAddr().String(), entry)
}
}