-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
433 lines (375 loc) Β· 11.7 KB
/
app.go
File metadata and controls
433 lines (375 loc) Β· 11.7 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
// Copyright (c) 2026 threatvec & talkdedsec. All Rights Reserved.
// This software is proprietary and confidential.
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"time"
"vaultx/internal/ai"
"vaultx/internal/db"
"vaultx/internal/projects"
"vaultx/internal/scanner"
"vaultx/internal/scheduler"
"vaultx/internal/threat"
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
)
// App is the main application struct that holds all module references.
type App struct {
ctx context.Context
db *db.Database
aiManager *ai.Manager
clipboard *ClipboardWatcher
updater *Updater
nightWatcher *threat.NightWatcher
scheduler *scheduler.Scheduler
projects *projects.Manager
}
// NewApp creates a new App application struct.
func NewApp() *App {
return &App{}
}
// startup is called when the app starts.
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
database, err := db.NewDatabase()
if err != nil {
log.Printf("Database init error: %v", err)
}
a.db = database
a.aiManager = ai.NewManager(a.db)
a.updater = NewUpdater()
updateAvailable, latestVersion, err := a.updater.CheckUpdate()
if err != nil {
log.Printf("Update check error: %v", err)
}
if updateAvailable {
wailsRuntime.EventsEmit(ctx, "update:available", latestVersion)
}
a.clipboard = NewClipboardWatcher(ctx)
go a.clipboard.Start()
ollamaAvailable := a.aiManager.DetectOllama()
wailsRuntime.EventsEmit(ctx, "ai:ollama-status", ollamaAvailable)
// Initialize NightWatcher (only if DB is available)
var settings map[string]string
if a.db != nil {
settings, _ = a.db.GetAllSettings()
}
if settings == nil {
settings = make(map[string]string)
}
discordWebhook := settings["discord_webhook"]
a.nightWatcher = threat.NewNightWatcher(discordWebhook, func(result *threat.BreachResult) {
wailsRuntime.EventsEmit(ctx, "nightwatch:alert", result)
})
go a.nightWatcher.Start(ctx)
// Initialize scheduler
a.scheduler = scheduler.NewScheduler(func(tool, target string) (string, error) {
return fmt.Sprintf("scheduled scan: tool=%s target=%s", tool, target), nil
})
go a.scheduler.Start(ctx)
// Initialize projects manager
a.projects = projects.NewManager()
}
// shutdown is called when the app is closing.
func (a *App) shutdown(ctx context.Context) {
if a.clipboard != nil {
a.clipboard.Stop()
}
if a.nightWatcher != nil {
a.nightWatcher.Stop()
}
if a.scheduler != nil {
a.scheduler.Stop()
}
if a.db != nil {
a.db.Close()
}
}
// GetVersion returns the current application version.
func (a *App) GetVersion() string {
return CurrentVersion
}
// CheckForUpdate checks if a new version is available.
func (a *App) CheckForUpdate() (bool, string, error) {
return a.updater.CheckUpdate()
}
// GetOllamaStatus returns whether Ollama is available.
func (a *App) GetOllamaStatus() bool {
return a.aiManager.DetectOllama()
}
// QueryAI sends a prompt to the active AI provider and returns the response.
func (a *App) QueryAI(prompt string) (string, error) {
return a.aiManager.Query(prompt)
}
// AnalyzeWithAI sends scan results to AI for analysis.
func (a *App) AnalyzeWithAI(toolName, resultJSON string) (string, error) {
return a.aiManager.AnalyzeScanResult(toolName, resultJSON)
}
// GenerateWeeklyReport generates a weekly security report using AI.
func (a *App) GenerateWeeklyReport() (string, error) {
// Collect last 7 days of scan history
tools := []string{
"shadowscan", "urlscanner", "whoislookup", "dnsanalyzer", "sslinspector",
"httpheaders", "webfingerprint", "phishing", "ipreputaton", "cvesearch",
"ipintelligence", "portscan", "metadata", "exif", "hashlookup",
"usernamesearch", "emailbreach", "passwordanalyzer", "emailheader",
}
var history []map[string]string
for _, tool := range tools {
rows, err := a.db.GetQueryHistory(tool, 20)
if err != nil {
continue
}
for _, row := range rows {
history = append(history, map[string]string{
"tool": row.Tool,
"query": row.Query,
"result": row.Result,
})
}
}
return a.aiManager.GenerateWeeklyReport(history)
}
// GetAIProvider returns the currently active AI provider name.
func (a *App) GetAIProvider() string {
return a.aiManager.GetActiveProvider()
}
// TestOllamaConnection tests if Ollama is reachable at the given URL via Go backend.
func (a *App) TestOllamaConnection(url string) bool {
if url == "" {
url = "http://localhost:11434"
}
return a.aiManager.TestOllamaURL(url)
}
// ListOllamaModels returns available Ollama models using the configured URL.
func (a *App) ListOllamaModels() ([]ai.OllamaModelInfo, error) {
url := "http://localhost:11434"
if a.db != nil {
if settings, err := a.db.GetAllSettings(); err == nil {
if u := settings["ollama_url"]; u != "" {
url = u
}
}
}
return a.aiManager.ListOllamaModelsFromURL(url)
}
// SetAIProvider saves the preferred AI provider and credentials.
func (a *App) SetAIProvider(provider, apiKey, model string) error {
return a.aiManager.SetProvider(provider, apiKey, model)
}
// TestAIProvider tests a specific AI provider connection.
func (a *App) TestAIProvider(provider, apiKey, model string) (*ai.ProviderStatus, error) {
return a.aiManager.TestProvider(provider, apiKey, model)
}
// GetAIHistory returns the last N AI messages from history.
func (a *App) GetAIHistory(limit int) ([]map[string]string, error) {
if a.db == nil {
return nil, nil
}
rows, err := a.db.GetQueryHistory("ai_chat", limit)
if err != nil {
return nil, err
}
var messages []map[string]string
for _, r := range rows {
messages = append(messages, map[string]string{
"role": r.Query,
"content": r.Result,
"timestamp": r.CreatedAt.Format("2006-01-02 15:04:05"),
})
}
return messages, nil
}
// SaveAIMessage saves an AI chat message to history.
func (a *App) SaveAIMessage(role, content string) error {
if a.db != nil {
return a.db.SaveQueryHistory("ai_chat", role, content)
}
return nil
}
// RunSelfScan performs a comprehensive self-scan with live progress.
func (a *App) RunSelfScan() (*scanner.SelfScanResult, error) {
ctx, cancel := context.WithTimeout(a.ctx, 60*time.Second)
defer cancel()
progressFn := func(step scanner.ScanStep) {
wailsRuntime.EventsEmit(a.ctx, "scanme:progress", step)
}
result, err := scanner.RunSelfScan(ctx, progressFn)
if err != nil {
return nil, err
}
if a.db != nil {
raw, _ := json.Marshal(result)
a.db.SaveQueryHistory("scanme", "self", string(raw))
a.checkBadges("scanme")
}
return result, nil
}
// GetBadges returns all earned badges.
func (a *App) GetBadges() ([]db.Badge, error) {
if a.db == nil {
return nil, nil
}
return a.db.GetBadges()
}
// GetDashboardStats returns aggregated stats for the dashboard.
func (a *App) GetDashboardStats() map[string]interface{} {
stats := map[string]interface{}{
"totalScans": 0,
"threatsDetected": 0,
"securityScore": 0,
"activeMonitors": 0,
"badgeCount": 0,
}
if a.db == nil {
return stats
}
total := a.db.GetTotalQueryCount()
stats["totalScans"] = total
threats := a.db.GetToolQueryCount("ipreputaton") + a.db.GetToolQueryCount("cvesearch") + a.db.GetToolQueryCount("emailbreach")
stats["threatsDetected"] = threats
// Get last scan-me score
history, err := a.db.GetQueryHistory("scanme", 1)
if err == nil && len(history) > 0 {
var scanResult scanner.SelfScanResult
if json.Unmarshal([]byte(history[0].Result), &scanResult) == nil {
stats["securityScore"] = scanResult.SecurityScore
}
}
// Active monitors
monitors := a.db.GetToolQueryCount("nightwatch")
stats["activeMonitors"] = monitors
badges, _ := a.db.GetBadges()
stats["badgeCount"] = len(badges)
return stats
}
// GetRecentActivity returns the last N activities.
func (a *App) GetRecentActivity(limit int) ([]map[string]string, error) {
if a.db == nil {
return nil, nil
}
history, err := a.db.GetRecentActivity(limit)
if err != nil {
return nil, err
}
var items []map[string]string
for _, h := range history {
items = append(items, map[string]string{
"tool": h.Tool,
"query": h.Query,
"timestamp": h.CreatedAt.Format("2006-01-02 15:04"),
})
}
return items, nil
}
// checkBadges checks and awards badges based on activity.
func (a *App) checkBadges(tool string) {
if a.db == nil {
return
}
total := a.db.GetTotalQueryCount()
// First Scan badge
if total >= 1 {
earned, _ := a.db.EarnBadge("First Step", "Completed your first scan", "π°")
if earned {
wailsRuntime.EventsEmit(a.ctx, "badge:earned", map[string]string{"name": "First Step", "icon": "π°"})
}
}
// Researcher
if total >= 50 {
earned, _ := a.db.EarnBadge("Researcher", "Completed 50 scans", "π")
if earned {
wailsRuntime.EventsEmit(a.ctx, "badge:earned", map[string]string{"name": "Researcher", "icon": "π"})
}
}
// Security Expert
if total >= 500 {
earned, _ := a.db.EarnBadge("Security Expert", "Completed 500 scans", "π‘οΈ")
if earned {
wailsRuntime.EventsEmit(a.ctx, "badge:earned", map[string]string{"name": "Security Expert", "icon": "π‘οΈ"})
}
}
// Breach Hunter
breachCount := a.db.GetToolQueryCount("emailbreach")
if breachCount >= 10 {
earned, _ := a.db.EarnBadge("Breach Hunter", "Checked 10 email breaches", "π―")
if earned {
wailsRuntime.EventsEmit(a.ctx, "badge:earned", map[string]string{"name": "Breach Hunter", "icon": "π―"})
}
}
// Network Detective
portCount := a.db.GetToolQueryCount("portscan")
if portCount >= 20 {
earned, _ := a.db.EarnBadge("Network Detective", "Performed 20 port scans", "π")
if earned {
wailsRuntime.EventsEmit(a.ctx, "badge:earned", map[string]string{"name": "Network Detective", "icon": "π"})
}
}
// OSINT Master
osintCount := a.db.GetToolQueryCount("usernamesearch")
if osintCount >= 5 {
earned, _ := a.db.EarnBadge("OSINT Master", "Performed 5 username searches", "ποΈ")
if earned {
wailsRuntime.EventsEmit(a.ctx, "badge:earned", map[string]string{"name": "OSINT Master", "icon": "ποΈ"})
}
}
// NightWatch Guardian
watchCount := a.db.GetToolQueryCount("nightwatch")
if watchCount >= 1 {
earned, _ := a.db.EarnBadge("NightWatch Guardian", "Set up NightWatch monitoring", "π¦")
if earned {
wailsRuntime.EventsEmit(a.ctx, "badge:earned", map[string]string{"name": "NightWatch Guardian", "icon": "π¦"})
}
}
// AI Analyst
aiCount := a.db.GetToolQueryCount("ai_chat")
if aiCount >= 10 {
earned, _ := a.db.EarnBadge("AI Analyst", "Asked 10 AI questions", "π€")
if earned {
wailsRuntime.EventsEmit(a.ctx, "badge:earned", map[string]string{"name": "AI Analyst", "icon": "π€"})
}
}
// Self-Aware
if tool == "scanme" {
earned, _ := a.db.EarnBadge("Self-Aware", "Ran your first self-scan", "πͺ")
if earned {
wailsRuntime.EventsEmit(a.ctx, "badge:earned", map[string]string{"name": "Self-Aware", "icon": "πͺ"})
}
}
}
// GetSettings returns all settings from the database.
func (a *App) GetSettings() (map[string]string, error) {
if a.db == nil {
return make(map[string]string), nil
}
return a.db.GetAllSettings()
}
// SaveSetting saves a key-value setting to the database.
func (a *App) SaveSetting(key, value string) error {
if a.db == nil {
return fmt.Errorf("database not available")
}
return a.db.SaveSetting(key, value)
}
// GetQueryHistory returns the last N queries from history.
func (a *App) GetQueryHistory(toolName string, limit int) ([]db.QueryHistory, error) {
if a.db == nil {
return nil, nil
}
return a.db.GetQueryHistory(toolName, limit)
}
// GetClipboardWatcherEnabled returns the clipboard watcher state.
func (a *App) GetClipboardWatcherEnabled() bool {
if a.clipboard == nil {
return false
}
return a.clipboard.enabled
}
// SetClipboardWatcherEnabled enables or disables the clipboard watcher.
func (a *App) SetClipboardWatcherEnabled(enabled bool) {
if a.clipboard != nil {
a.clipboard.enabled = enabled
}
}