This repository was archived by the owner on Oct 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
207 lines (173 loc) · 5.8 KB
/
main.go
File metadata and controls
207 lines (173 loc) · 5.8 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
package main
import (
"context"
"flag"
_ "fmt"
"log"
"os"
"os/signal"
_ "path/filepath"
"sync"
"syscall"
"time"
"neoprotect-notifier/config"
"neoprotect-notifier/integrations"
"neoprotect-notifier/neoprotect"
)
func main() {
configPath := flag.String("config", "config.json", "Path to configuration file")
flag.Parse()
log.SetOutput(os.Stdout)
log.SetFlags(log.LstdFlags | log.Lshortfile)
log.Println("Starting NeoProtect Attack Notifier")
cfg, err := config.LoadConfig(*configPath)
if err != nil {
log.Fatalf("Failed to load configuration: %v", err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
client, err := neoprotect.NewClient(cfg.APIKey, cfg.APIEndpoint)
if err != nil {
log.Fatalf("Failed to create NeoProtect client: %v", err)
}
integrationManager, err := integrations.NewManager("./integrations", cfg.EnabledIntegrations)
if err != nil {
log.Fatalf("Failed to initialize integration manager: %v", err)
}
if err := integrationManager.InitializeIntegrations(cfg); err != nil {
log.Fatalf("Failed to initialize integrations: %v", err)
}
log.Println("Setting NeoProtect API client on integrations...")
integrationManager.SetAPIClient(client)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
monitorAttacks(ctx, client, integrationManager, cfg.PollInterval, cfg)
}()
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
<-sigChan
log.Println("Received termination signal, shutting down...")
integrationManager.Shutdown()
cancel()
wg.Wait()
log.Println("Shutdown complete")
}
func monitorAttacks(ctx context.Context, client *neoprotect.Client, manager *integrations.Manager, pollInterval time.Duration, cfg *config.Config) {
ticker := time.NewTicker(pollInterval)
defer ticker.Stop()
knownAttacks := make(map[string]*neoprotect.Attack)
messageTracker := integrations.NewMessageTracker()
log.Println("Performing initial attack status fetch (active attacks only)")
fetchAndProcessActiveAttacks(ctx, client, manager, cfg.MonitorMode, cfg.SpecificIPs, knownAttacks, messageTracker, cfg)
for {
select {
case <-ctx.Done():
log.Println("Attack monitoring stopped")
return
case <-ticker.C:
fetchAndProcessActiveAttacks(ctx, client, manager, cfg.MonitorMode, cfg.SpecificIPs, knownAttacks, messageTracker, cfg)
}
}
}
func fetchAndProcessActiveAttacks(ctx context.Context, client *neoprotect.Client, manager *integrations.Manager, monitorMode string, ipsToMonitor []string, knownAttacks map[string]*neoprotect.Attack, messageTracker *integrations.MessageTracker, cfg *config.Config) {
attacks, err := client.GetAllAttacksAllPages(ctx, true)
if err != nil {
log.Printf("Error fetching active attacks: %v", err)
return
}
if monitorMode == "specific" {
var filteredAttacks []*neoprotect.Attack
for _, attack := range attacks {
for _, ip := range ipsToMonitor {
if attack.DstAddressString == ip {
if !cfg.IsBlacklisted(ip) {
filteredAttacks = append(filteredAttacks, attack)
}
break
}
}
}
attacks = filteredAttacks
} else if monitorMode == "all" {
var filteredAttacks []*neoprotect.Attack
for _, attack := range attacks {
if !cfg.IsBlacklisted(attack.DstAddressString) {
filteredAttacks = append(filteredAttacks, attack)
}
}
attacks = filteredAttacks
} else {
log.Printf("Invalid monitor mode: %s", monitorMode)
return
}
var validAttacks []*neoprotect.Attack
for _, attack := range attacks {
if !isValidAttack(attack) {
log.Printf("Skipping invalid attack: ID=%s, IP=%s", attack.ID, attack.DstAddressString)
continue
}
validAttacks = append(validAttacks, attack)
}
processActiveAttacks(ctx, client, manager, validAttacks, knownAttacks, messageTracker)
checkForEndedAttacks(ctx, manager, validAttacks, knownAttacks, messageTracker)
cleanupEndedAttacks(knownAttacks)
}
func isValidAttack(attack *neoprotect.Attack) bool {
if attack == nil {
return false
}
if attack.ID == "" {
return false
}
if attack.DstAddressString == "" {
return false
}
return true
}
func processActiveAttacks(ctx context.Context, client *neoprotect.Client, manager *integrations.Manager, attacks []*neoprotect.Attack, knownAttacks map[string]*neoprotect.Attack, messageTracker *integrations.MessageTracker) {
seenAttacks := make(map[string]bool)
for _, attack := range attacks {
seenAttacks[attack.ID] = true
existingAttack, exists := knownAttacks[attack.ID]
if !exists {
knownAttacks[attack.ID] = attack
err := manager.NotifyNewAttack(ctx, attack, messageTracker)
if err != nil {
log.Printf("Error notifying integrations about new attack: %v", err)
}
} else if !attack.Equal(existingAttack) {
previousState := *existingAttack
knownAttacks[attack.ID] = attack
err := manager.NotifyAttackUpdate(ctx, attack, &previousState, messageTracker)
if err != nil {
log.Printf("Error notifying integrations about attack update: %v", err)
}
}
}
}
func checkForEndedAttacks(ctx context.Context, manager *integrations.Manager, activeAttacks []*neoprotect.Attack, knownAttacks map[string]*neoprotect.Attack, messageTracker *integrations.MessageTracker) {
activeAttackIDs := make(map[string]bool)
for _, attack := range activeAttacks {
activeAttackIDs[attack.ID] = true
}
for id, attack := range knownAttacks {
if !activeAttackIDs[id] && attack.EndedAt == nil {
now := time.Now()
attack.EndedAt = &now
err := manager.NotifyAttackEnded(ctx, attack, messageTracker)
if err != nil {
log.Printf("Error notifying integrations about implicitly ended attack: %v", err)
}
knownAttacks[id] = attack
}
}
}
func cleanupEndedAttacks(knownAttacks map[string]*neoprotect.Attack) {
for id, attack := range knownAttacks {
if attack.EndedAt != nil && time.Since(*attack.EndedAt) > 24*time.Hour {
delete(knownAttacks, id)
}
}
}