-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.go
More file actions
193 lines (176 loc) · 5.26 KB
/
Copy pathrunner.go
File metadata and controls
193 lines (176 loc) · 5.26 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
package main
import (
"context"
"fmt"
"log"
"time"
)
func runAmbient(parent context.Context, cfg RuntimeAmbient) error {
ctx := parent
var cancel context.CancelFunc
if cfg.Duration > 0 {
ctx, cancel = context.WithTimeout(parent, cfg.Duration)
defer cancel()
}
model, err := NewAmbientModel(cfg)
if err != nil {
return err
}
exporter, err := NewSFlowExporter(cfg.Common)
if err != nil {
return err
}
defer func() {
if err := exporter.Close(); err != nil {
log.Printf("close exporter: %v", err)
}
}()
log.Printf("mode=ambient collector=%s agent=%s sub_agent=%d sampling=%d networks=%d duration=%s",
cfg.Common.Collector, cfg.Common.AgentIP, cfg.Common.SubAgentID,
cfg.Common.SamplingRate, len(cfg.Networks), printableDuration(cfg.Duration))
ticker := time.NewTicker(cfg.Common.Tick)
defer ticker.Stop()
lastLog := time.Now()
for {
select {
case <-ctx.Done():
if parent.Err() != nil {
return parent.Err()
}
return nil
case <-ticker.C:
result, err := model.Tick(cfg.Common.Tick, cfg.Common.MaxSamplesPerTick)
if err != nil {
return err
}
for _, packet := range result.Packets {
if err := exporter.Add(packet); err != nil {
return err
}
}
if err := exporter.Flush(); err != nil {
return err
}
if len(result.Packets) >= cfg.Common.MaxSamplesPerTick {
log.Printf("warning: ambient reached max_samples_per_tick=%d; generated rate may be clipped", cfg.Common.MaxSamplesPerTick)
}
if time.Since(lastLog) >= cfg.Common.LogInterval {
interval := time.Since(lastLog)
stats := exporter.SnapshotAndReset()
top := sortedTopHosts(model.Networks, 1)
topText := "none"
if len(top) > 0 {
topText = fmt.Sprintf("%s@%s", top[0].Address, formatRate(top[0].LastRate))
}
log.Printf("mode=ambient target=%s target_in=%s target_out=%s estimated=%s estimated_in=%s estimated_out=%s samples=%d datagrams=%d top=%s",
formatRate(result.TargetBPS), formatRate(result.TargetIncomingBPS), formatRate(result.TargetOutgoingBPS),
measuredRate(stats.EstimatedBits, interval), measuredRate(stats.EstimatedIncomingBits, interval), measuredRate(stats.EstimatedOutgoingBits, interval),
stats.Samples, stats.Datagrams, topText)
lastLog = time.Now()
}
}
}
}
func runAttack(ctx context.Context, cfg RuntimeAttack) error {
model, err := NewAttackModel(cfg)
if err != nil {
return err
}
exporter, err := NewSFlowExporter(cfg.Common)
if err != nil {
return err
}
defer func() {
if err := exporter.Close(); err != nil {
log.Printf("close exporter: %v", err)
}
}()
totalDuration := model.TotalDuration()
log.Printf("mode=attack collector=%s agent=%s sub_agent=%d sampling=%d victim=%s peak=%s duration=%s",
cfg.Common.Collector, cfg.Common.AgentIP, cfg.Common.SubAgentID,
cfg.Common.SamplingRate, cfg.Victim, formatRate(cfg.PeakRateBPS), totalDuration)
start := time.Now()
lastTick := start
lastLog := start
ticker := time.NewTicker(cfg.Common.Tick)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case now := <-ticker.C:
elapsed := now.Sub(start)
interval := now.Sub(lastTick)
lastTick = now
if elapsed > totalDuration {
if err := exporter.Flush(); err != nil {
return err
}
stats := exporter.SnapshotAndReset()
if stats.Samples > 0 {
log.Printf("mode=attack target=0bps estimated=%s samples=%d datagrams=%d phase=done",
measuredRate(stats.EstimatedBits, now.Sub(lastLog)), stats.Samples, stats.Datagrams)
}
return nil
}
result, err := model.Tick(elapsed, interval, cfg.Common.MaxSamplesPerTick)
if err != nil {
return err
}
for _, packet := range result.Packets {
if err := exporter.Add(packet); err != nil {
return err
}
}
if err := exporter.Flush(); err != nil {
return err
}
if len(result.Packets) >= cfg.Common.MaxSamplesPerTick {
log.Printf("warning: attack reached max_samples_per_tick=%d; generated rate may be clipped", cfg.Common.MaxSamplesPerTick)
}
if now.Sub(lastLog) >= cfg.Common.LogInterval {
stats := exporter.SnapshotAndReset()
log.Printf("mode=attack victim=%s target=%s estimated=%s samples=%d datagrams=%d phase=%s",
cfg.Victim, formatRate(result.TargetBPS), measuredRate(stats.EstimatedBits, now.Sub(lastLog)),
stats.Samples, stats.Datagrams, attackPhase(elapsed, cfg))
lastLog = now
}
}
}
}
func attackPhase(elapsed time.Duration, cfg RuntimeAttack) string {
if elapsed < cfg.RampUp {
return "ramp-up"
}
elapsed -= cfg.RampUp
if elapsed < cfg.Hold {
return "hold"
}
return "ramp-down"
}
func measuredRate(bits uint64, interval time.Duration) string {
if interval <= 0 {
return "0bps"
}
return formatRate(float64(bits) / interval.Seconds())
}
func formatRate(bitsPerSecond float64) string {
switch {
case bitsPerSecond >= 1e12:
return fmt.Sprintf("%.2fTbps", bitsPerSecond/1e12)
case bitsPerSecond >= 1e9:
return fmt.Sprintf("%.2fGbps", bitsPerSecond/1e9)
case bitsPerSecond >= 1e6:
return fmt.Sprintf("%.2fMbps", bitsPerSecond/1e6)
case bitsPerSecond >= 1e3:
return fmt.Sprintf("%.2fKbps", bitsPerSecond/1e3)
default:
return fmt.Sprintf("%.0fbps", bitsPerSecond)
}
}
func printableDuration(duration time.Duration) string {
if duration == 0 {
return "infinite"
}
return duration.String()
}