-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
572 lines (489 loc) · 13.9 KB
/
app.go
File metadata and controls
572 lines (489 loc) · 13.9 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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
package main
import (
"context"
"encoding/csv"
"fmt"
"log/slog"
"net"
"os"
"strings"
"SyslogStudio/internal/event"
"SyslogStudio/internal/models"
"SyslogStudio/internal/pki"
"SyslogStudio/internal/storage"
"SyslogStudio/internal/syslog"
"SyslogStudio/internal/updater"
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
)
// App struct is the main Wails binding facade.
type App struct {
ctx context.Context
server *syslog.SyslogServer
tlsManager *pki.TLSManager
configStore *storage.ConfigStore
logStore *storage.LogStore
encryptionPassword string // in-memory only for session
unlockAttempts int
}
const maxUnlockAttempts = 5
// NewApp creates a new App application struct.
func NewApp() *App {
return &App{
tlsManager: pki.NewTLSManager(),
configStore: storage.NewConfigStore(),
}
}
// startup is called when the app starts.
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
emitter := event.NewWailsEventEmitter(ctx)
a.server = syslog.NewSyslogServer(emitter, a.tlsManager)
// Initialize log store
storageCfg := a.configStore.LoadStorage()
ls, err := storage.NewLogStore(storageCfg, emitter)
if err != nil {
slog.Warn("failed to initialize log store, persistence disabled", "error", err)
} else {
a.logStore = ls
a.server.LogStore = ls
}
// Restore alert rules
rules := a.configStore.LoadAlertRules()
if len(rules) > 0 {
a.server.AlertManager.SetRules(rules)
slog.Info("restored alert rules", "count", len(rules))
}
}
// shutdown is called when the app is closing.
func (a *App) shutdown(ctx context.Context) {
if a.server != nil {
a.server.Stop()
}
a.configStore.SaveAlertRules(a.server.AlertManager.GetRules())
if a.logStore != nil {
a.logStore.Close()
}
}
// --- Server Control Methods ---
func (a *App) StartServer(config models.ServerConfig) error {
err := a.server.Start(config)
if err == nil {
a.configStore.Save(config)
}
return err
}
func (a *App) StopServer() error {
return a.server.Stop()
}
func (a *App) GetServerStatus() models.ServerStatus {
return a.server.GetStatus()
}
func (a *App) GetDefaultConfig() models.ServerConfig {
return a.configStore.Load()
}
// --- Log Methods ---
func (a *App) GetMessages(filter models.FilterCriteria) []models.SyslogMessage {
return a.server.GetMessages(filter)
}
func (a *App) ClearMessages() {
a.server.ClearMessages()
}
func (a *App) GetStats() models.ServerStats {
return a.server.GetStats()
}
// IsStorageReady returns true if the database is fully initialized (FTS index built).
func (a *App) IsStorageReady() bool {
if a.logStore == nil {
return false
}
return a.logStore.IsReady()
}
// --- Storage Methods ---
func (a *App) GetStorageConfig() models.StorageConfig {
cfg := a.configStore.LoadStorage()
if cfg.Path == "" {
resolved, err := storage.ResolveDBPath("")
if err == nil {
cfg.Path = resolved
}
}
return cfg
}
func (a *App) SetStorageConfig(cfg models.StorageConfig) {
a.configStore.SaveStorage(cfg)
if a.logStore != nil {
a.logStore.UpdateConfig(cfg)
}
}
func (a *App) GetStorageStats() models.StorageStats {
if a.logStore != nil {
return a.logStore.GetStats()
}
return models.StorageStats{}
}
func (a *App) QueryMessages(opts models.QueryOptions) models.PagedResult {
if a.logStore != nil {
return a.logStore.QueryMessages(opts)
}
return models.PagedResult{Page: opts.Page, PageSize: opts.PageSize}
}
func (a *App) QueryMessageGroups(filter models.FilterCriteria, groupField string) []models.GroupSummary {
if a.logStore != nil {
return a.logStore.QueryGroups(filter, groupField)
}
return nil
}
func (a *App) CompactDatabase() error {
if a.logStore != nil {
return a.logStore.Compact()
}
return nil
}
func (a *App) ClearDatabase() error {
if a.logStore != nil {
return a.logStore.ClearAll()
}
return nil
}
// --- PKI / Certificate Methods ---
func (a *App) GenerateCA(opts models.CertOptions) (models.CertInfo, error) {
return a.tlsManager.GenerateCA(opts)
}
func (a *App) GenerateServerCert(opts models.CertOptions) (models.CertInfo, error) {
return a.tlsManager.GenerateServerCertSignedByCA(opts)
}
func (a *App) GenerateCertificate(opts models.CertOptions) (models.CertInfo, error) {
_, info, err := a.tlsManager.GenerateSelfSignedWithOptions(opts)
if err != nil {
return models.CertInfo{}, err
}
return info, nil
}
func (a *App) GetCACertInfo() (models.CertInfo, error) {
return a.tlsManager.GetCACertificateInfo()
}
func (a *App) GetServerCertInfo() (models.CertInfo, error) {
return a.tlsManager.GetServerCertificateInfo()
}
func (a *App) GetCertificateInfo(config models.ServerConfig) (models.CertInfo, error) {
return a.tlsManager.GetCertificateInfo(config)
}
func (a *App) GetDefaultCertOptions() models.CertOptions {
return models.DefaultCertOptions()
}
func (a *App) ExportCACertificate() (string, error) {
if !a.tlsManager.HasCA() {
return "", fmt.Errorf("no CA certificate available to export; generate a CA first")
}
path, err := wailsRuntime.SaveFileDialog(a.ctx, wailsRuntime.SaveDialogOptions{
Title: "Export CA Certificate (for device)",
DefaultFilename: "ca-cert.pem",
Filters: []wailsRuntime.FileFilter{
{DisplayName: "PEM Files (*.pem, *.crt)", Pattern: "*.pem;*.crt"},
},
})
if err != nil {
return "", err
}
if path == "" {
return "", nil
}
if err := a.tlsManager.SaveCACertificateToFile(path); err != nil {
return "", err
}
return path, nil
}
func (a *App) ExportServerCertificate() (string, error) {
if !a.tlsManager.HasServerCert() {
return "", fmt.Errorf("no server certificate available to export")
}
certPath, err := wailsRuntime.SaveFileDialog(a.ctx, wailsRuntime.SaveDialogOptions{
Title: "Export Server Certificate",
DefaultFilename: "server-cert.pem",
Filters: []wailsRuntime.FileFilter{
{DisplayName: "PEM Files (*.pem)", Pattern: "*.pem"},
},
})
if err != nil {
return "", err
}
if certPath == "" {
return "", nil
}
keyPath, err := wailsRuntime.SaveFileDialog(a.ctx, wailsRuntime.SaveDialogOptions{
Title: "Export Server Private Key",
DefaultFilename: "server-key.pem",
Filters: []wailsRuntime.FileFilter{
{DisplayName: "PEM Files (*.pem)", Pattern: "*.pem"},
},
})
if err != nil {
return "", err
}
if keyPath == "" {
return "", nil
}
if err := a.tlsManager.SaveServerCertificateToFile(certPath, keyPath); err != nil {
return "", err
}
return fmt.Sprintf("Exported:\n %s\n %s", certPath, keyPath), nil
}
func (a *App) ExportCertificate() (string, error) {
return a.ExportServerCertificate()
}
func (a *App) GetLocalIPs() []string {
var ips []string
addrs, err := net.InterfaceAddrs()
if err != nil {
return ips
}
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
ips = append(ips, ipnet.IP.String())
}
}
}
return ips
}
// --- Alert Methods ---
func (a *App) GetAlertRules() []models.AlertRule {
return a.server.AlertManager.GetRules()
}
func (a *App) AddAlertRule(rule models.AlertRule) models.AlertRule {
r := a.server.AlertManager.AddRule(rule)
a.configStore.SaveAlertRules(a.server.AlertManager.GetRules())
return r
}
func (a *App) UpdateAlertRule(rule models.AlertRule) bool {
ok := a.server.AlertManager.UpdateRule(rule)
if ok {
a.configStore.SaveAlertRules(a.server.AlertManager.GetRules())
}
return ok
}
func (a *App) DeleteAlertRule(id string) bool {
ok := a.server.AlertManager.DeleteRule(id)
if ok {
a.configStore.SaveAlertRules(a.server.AlertManager.GetRules())
}
return ok
}
func (a *App) GetAlertHistory() []models.AlertEvent {
return a.server.AlertManager.GetHistory()
}
func (a *App) ClearAlertHistory() {
a.server.AlertManager.ClearHistory()
}
// --- Encryption Methods ---
// GetUnlockAttemptsRemaining returns how many unlock attempts are left.
func (a *App) GetUnlockAttemptsRemaining() int {
return maxUnlockAttempts - a.unlockAttempts
}
// IsEncryptionEnabled returns whether encryption is configured.
func (a *App) IsEncryptionEnabled() bool {
return a.configStore.LoadStorage().EncryptionEnabled
}
// IsEncryptionLocked returns true if the database is encrypted and not yet unlocked.
func (a *App) IsEncryptionLocked() bool {
return a.logStore != nil && a.logStore.IsLocked()
}
// UnlockDatabase decrypts the database with the given password and opens it.
// After maxUnlockAttempts failed attempts, the application is closed.
func (a *App) UnlockDatabase(password string) error {
if a.logStore == nil {
return fmt.Errorf("log store not initialized")
}
if a.unlockAttempts >= maxUnlockAttempts {
go func() {
wailsRuntime.Quit(a.ctx)
}()
return fmt.Errorf("too many failed attempts")
}
if err := a.logStore.UnlockAndOpen(password); err != nil {
a.unlockAttempts++
remaining := maxUnlockAttempts - a.unlockAttempts
slog.Warn("unlock failed", "attempt", a.unlockAttempts, "remaining", remaining)
if a.unlockAttempts >= maxUnlockAttempts {
slog.Error("max unlock attempts reached, shutting down")
go func() {
wailsRuntime.Quit(a.ctx)
}()
return fmt.Errorf("too many failed attempts — application will close")
}
return fmt.Errorf("wrong password (%d attempts remaining)", remaining)
}
a.unlockAttempts = 0
a.encryptionPassword = password
a.server.LogStore = a.logStore
// Restore alert rules now that the store is available
rules := a.configStore.LoadAlertRules()
if len(rules) > 0 {
a.server.AlertManager.SetRules(rules)
}
return nil
}
// EnableEncryption enables at-rest encryption with the given password.
func (a *App) EnableEncryption(password string) error {
if password == "" {
return fmt.Errorf("password cannot be empty")
}
cfg := a.configStore.LoadStorage()
cfg.EncryptionEnabled = true
a.configStore.SaveStorage(cfg)
a.encryptionPassword = password
if a.logStore != nil {
a.logStore.SetEncryptionPassword(password)
a.logStore.UpdateConfig(cfg)
}
return nil
}
// DisableEncryption disables at-rest encryption after verifying the password.
func (a *App) DisableEncryption(password string) error {
if a.encryptionPassword != "" && password != a.encryptionPassword {
return fmt.Errorf("incorrect password")
}
cfg := a.configStore.LoadStorage()
cfg.EncryptionEnabled = false
a.configStore.SaveStorage(cfg)
a.encryptionPassword = ""
if a.logStore != nil {
a.logStore.SetEncryptionPassword("")
a.logStore.UpdateConfig(cfg)
}
return nil
}
// ChangeEncryptionPassword changes the encryption password.
func (a *App) ChangeEncryptionPassword(oldPassword, newPassword string) error {
if oldPassword != a.encryptionPassword {
return fmt.Errorf("incorrect current password")
}
if newPassword == "" {
return fmt.Errorf("new password cannot be empty")
}
a.encryptionPassword = newPassword
if a.logStore != nil {
a.logStore.SetEncryptionPassword(newPassword)
}
return nil
}
// --- Update Check ---
func (a *App) CheckForUpdate() models.UpdateInfo {
return updater.CheckForUpdate()
}
func (a *App) GetAppVersion() string {
return updater.AppVersion
}
// --- File Selection Dialogs ---
func (a *App) SelectCertFile() (string, error) {
return wailsRuntime.OpenFileDialog(a.ctx, wailsRuntime.OpenDialogOptions{
Title: "Select TLS Certificate",
Filters: []wailsRuntime.FileFilter{
{DisplayName: "PEM Files (*.pem, *.crt)", Pattern: "*.pem;*.crt"},
{DisplayName: "All Files", Pattern: "*.*"},
},
})
}
func (a *App) SelectKeyFile() (string, error) {
return wailsRuntime.OpenFileDialog(a.ctx, wailsRuntime.OpenDialogOptions{
Title: "Select TLS Private Key",
Filters: []wailsRuntime.FileFilter{
{DisplayName: "PEM Files (*.pem, *.key)", Pattern: "*.pem;*.key"},
{DisplayName: "All Files", Pattern: "*.*"},
},
})
}
func (a *App) SelectCAFile() (string, error) {
return wailsRuntime.OpenFileDialog(a.ctx, wailsRuntime.OpenDialogOptions{
Title: "Select CA Certificate",
Filters: []wailsRuntime.FileFilter{
{DisplayName: "PEM Files (*.pem, *.crt)", Pattern: "*.pem;*.crt"},
{DisplayName: "All Files", Pattern: "*.*"},
},
})
}
// --- Export Logs ---
func (a *App) ExportLogs(filter models.FilterCriteria, format string) (string, error) {
var defaultFilename string
var filters []wailsRuntime.FileFilter
if format == "csv" {
defaultFilename = "syslog_export.csv"
filters = []wailsRuntime.FileFilter{
{DisplayName: "CSV Files (*.csv)", Pattern: "*.csv"},
}
} else {
defaultFilename = "syslog_export.txt"
filters = []wailsRuntime.FileFilter{
{DisplayName: "Text Files (*.txt)", Pattern: "*.txt"},
}
}
path, err := wailsRuntime.SaveFileDialog(a.ctx, wailsRuntime.SaveDialogOptions{
Title: "Export Logs",
DefaultFilename: defaultFilename,
Filters: filters,
})
if err != nil {
return "", err
}
if path == "" {
return "", nil
}
messages := a.server.GetMessages(filter)
if format == "csv" {
err = writeCSV(path, messages)
} else {
err = writeText(path, messages)
}
if err != nil {
return "", fmt.Errorf("failed to write export: %w", err)
}
return path, nil
}
func writeCSV(path string, messages []models.SyslogMessage) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
f.Write([]byte{0xEF, 0xBB, 0xBF})
w := csv.NewWriter(f)
defer w.Flush()
w.Write([]string{
"Timestamp", "Severity", "Facility", "Hostname",
"AppName", "ProcID", "Message", "SourceIP", "Protocol",
})
for _, msg := range messages {
w.Write([]string{
msg.Timestamp.Format("2006-01-02 15:04:05"),
msg.SeverityLabel,
msg.FacilityLabel,
msg.Hostname,
msg.AppName,
msg.ProcID,
msg.Message,
msg.SourceIP,
msg.Protocol,
})
}
return w.Error()
}
func writeText(path string, messages []models.SyslogMessage) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
var sb strings.Builder
for _, msg := range messages {
sb.WriteString(fmt.Sprintf("%s [%s] %s %s %s: %s\n",
msg.Timestamp.Format("2006-01-02 15:04:05"),
msg.SeverityLabel,
msg.FacilityLabel,
msg.Hostname,
msg.AppName,
msg.Message,
))
}
_, err = f.WriteString(sb.String())
return err
}