-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
756 lines (576 loc) · 23 KB
/
main.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
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
package fastnetmon
import (
"fmt"
"strings"
"errors"
"github.com/levigross/grequests"
)
// JSON based callback script: https://fastnetmon.com/fastnetmon-json-formats/
type CallbackDetails struct {
// For IPv4 "127.0.0.1"
// For IPv6 "2a03:5131....:1"
IP string `json:"ip"`
// ban, unban, attack_status, partial_block, partial_unblock
Action string `json:"action"`
AttackDetails CallbackAttackDetails `json:"attack_details"`
// Callback type: host or hostgroup
AlertScope string `json:"alert_scope"`
// We populate it only for AlertScope set to hostgroup
HostGroup string `json:"hostgroup_name"`
// We populate it only for AlertScope set to hostgroup
ParentHostGroup string `json:"parent_hostgroup_name"`
// List of networks which belong to hostgroup
HostGroupNetworks []string `json:"hostgroup_networks"`
// Packet dump in string format for "ban" action only:
// "2018-12-15 19:16:39.376373 127.0.0.10:0 > 127.0.0.1:8842 protocol: tcp flags: rst,ack frag: 0 packets: 1 size: 54 bytes ip size: 40 bytes ttl: 64 sample ratio: 1 ",
PacketDump []string `json:"packet_dump"`
// Detailed packet dump in parsed format
PacketDumpDetailed []CallbackPacketDumpEntry `json:"packet_dump_detailed"`
// BGP Flow Spec rules
FlowSpecRules []FlowSpecRule `json:"flow_spec_rules"`
}
type FlowSpecAction struct {
// Rate
Rate uint `json:"rate"`
}
type FlowSpecRule struct {
// IPv4 network in CIDR format, optional field
SourcePrefix string `json:"source_prefix,omitempty"`
// IPv4 network in CIDR format, optional field
DestinationPrefix string `json:"destination_prefix,omitempty"`
// List of destination ports (from 0 to 65535), optional field
DestinationPorts []uint `json:"destination_ports,omitempty"`
// List of source ports (from 0 to 65535), optional field
SourcePorts []uint `json:"source_ports,omitempty"`
// List of packet sizes (from 0 to 1500), optional field
PacketLengths []uint `json:"packet_lengths,omitempty"`
// List of protocols (allowed options udp, tcp, icmp, gre), optional field
Protocols []string `json:"protocols,omitempty"`
// List of fragmentation flags (allowed values: dont-fragment, is-fragment, first-fragment, last-fragment, not-a-fragment), optional field
FragmentationFlags []string `json:"fragmentation_flags,omitempty"`
// List of TCP flags, allowed only when TCP used in protocols list. Allowed values: syn, ack, fin, urgent, push, rst. Flags also could be mixed with "|" sign (tcp|push). Optional field
TcpFlags []string `json:"tcp_flags,omitempty"`
// List of TTLs for traffic matching, non RFC compliant addition
Ttls []uint `json:"ttls,omitempty"`
// List of vlans for traffic matching, non RFC compliant addition
Vlans []uint `json:"vlans,omitempty"`
// Action type, allowed values: accept, discard, rate-limit, redirect, mark
ActionType string `json:"action_type,omitempty"`
// Optional details specific for action
Action FlowSpecAction `json:"action,omitempty"`
// List of IPs which can be used when action is set to "accept" to forward traffic to them
IPv4NextHops []string `json:"ipv4_nexthops,omitempty"`
}
// Keeps fields specific for threshold
type ThresholdStructure struct {
Flows bool `json:"flows"`
Mbits bool `json:"mbits"`
Packets bool `json:"packets"`
}
// FlexibleThresholdsDetails keeps details about which flexible thresholds triggered attack
// In some cases incoming and outgoing both can be true when attack was triggered in both directions in same time
type FlexibleThresholdsDetails struct {
// Set when attack was triggered in incoming direction
Incoming bool `json:"incoming"`
// Set when attack was triggered in outgoing direction
Outgoing bool `json:"outgoing"`
IncomingDetails ThresholdStructure `json:"incoming_details"`
OutgoingDetails ThresholdStructure `json:"outgoing_details"`
}
// Key information about attack
type CallbackAttackDetails struct {
// Example: 041eb504-2b33-4ff7-a6b7-8235408d5062
AttackUUID string `json:"attack_uuid"`
// low, middle, high, unknown
AttackSeverity string `json:"attack_severity"`
// Hostgroup name, only for per host callbacks
HostGroup string `json:"host_group"`
// Parent hostgroup name, only for per host callbacks
ParentHostGroup string `json:"parent_host_group"`
// Host's network, only for per host callbacks
HostNetwork string `json:"host_network"`
// IPv4 or IPv6
ProtocolVersion string `json:"protocol_version"`
// Set to true when attack was triggered by flexible threshold
AttackDetectionTriggeredByFlexibleThreshold bool `json:"attack_detection_triggered_by_flexible_threshold"`
// List of flexible thresholds which triggered attack
AttackDetectionFlexibleThresholds []string `json:"attack_detection_flexible_thresholds"`
// Detailed information about thresholds which triggered attack
Attack_DetectionFlexibleThresholdsDetailed map[string]FlexibleThresholdsDetails `json:"attack_detection_flexible_thresholds_detailed"`
AttackDetectionThreshold string `json:"attack_detection_threshold"`
AttackDetectionThresholdDirection string `json:"attack_detection_threshold_direction"`
// automatic, manual, other
AttackDetectionSource string `json:"attack_detection_source"`
TotalIncomingTraffic uint64 `json:"total_incoming_traffic"`
TotalOutgoingTraffic uint64 `json:"total_outgoing_traffic"`
TotalIncomingPps uint64 `json:"total_incoming_pps"`
TotalOutgoingPps uint64 `json:"total_outgoing_pps"`
TotalIncomingFlows uint64 `json:"total_incoming_flows"`
TotalOutgoingFlows uint64 `json:"total_outgoing_flows"`
IncomingIPFragmentedTraffic uint64 `json:"incoming_ip_fragmented_traffic"`
OutgoingIPFragmentedTraffic uint64 `json:"outgoing_ip_fragmented_traffic"`
IncomingIPFragmentedPps uint64 `json:"incoming_ip_fragmented_pps"`
OutgoingIPFragmentedPps uint64 `json:"outgoing_ip_fragmented_pps"`
IncomingTCPTraffic uint64 `json:"incoming_tcp_traffic"`
OutgoingTCPTraffic uint64 `json:"outgoing_tcp_traffic"`
IncomingTCPPps uint64 `json:"incoming_tcp_pps"`
OutgoingTCPPps uint64 `json:"outgoing_tcp_pps"`
IncomingSYNTCPTraffic uint64 `json:"incoming_syn_tcp_traffic"`
OutgoingSYNTCPTraffic uint64 `json:"outgoing_syn_tcp_traffic"`
IncomingSYNTCPPps uint64 `json:"incoming_syn_tcp_pps"`
OutgoingSYNTCPPps uint64 `json:"outgoing_syn_tcp_pps"`
IncomingUDPTraffic uint64 `json:"incoming_udp_traffic"`
OutgoingUDPTraffic uint64 `json:"outgoing_udp_traffic"`
IncomingUDPPps uint64 `json:"incoming_udp_pps"`
OutgoingUDPPps uint64 `json:"outgoing_udp_pps"`
IncomingICMPTraffic uint64 `json:"incoming_icmp_traffic"`
OutgoingICMPTraffic uint64 `json:"outgoing_icmp_traffic"`
IncomingICMPPps uint64 `json:"incoming_icmp_pps"`
OutgoingICMPPps uint64 `json:"outgoing_icmp_pps"`
}
// Detailed per field packet dump entry
type CallbackPacketDumpEntry struct {
// ipv4 or ipv6
IPVersion string `json:"ip_version"`
// IPs represented as strings
SourceIP string `json:"source_ip"`
DestinationIP string `json:"destination_ip"`
// Applicable only for TCP and UDP
SourcePort uint64 `json:"source_port"`
DestinationPort uint64 `json:"destination_port"`
// TCP Flags as string
TCPFlags string `json:"tcp_flags"`
Fragmentation bool `json:"fragmentation"`
Packets uint64 `json:"packets"`
Length uint64 `json:"length"`
IPLength uint64 `json:"ip_length"`
TTL uint64 `json:"ttl"`
SampleRatio uint64 `json:"sample_ratio"`
// tcp, udp, icmp and unknown
Protocol string `json"protocol"`
// Address of device which sent this flow / packet
AgentAddress string `json:"agent_address"`
}
type ResponseArrayJson struct {
Success bool `json:"success"`
ErrorText string `json:"error_text"`
Values []string `json:"values"`
}
type ErrorJson struct {
Success bool `json:"success"`
ErrorText string `json:"error_text"`
}
type ResponseJson struct {
Success bool `json:"success"`
ErrorText string `json:"error_text"`
Value string `json:"value"`
}
// Wrapper structure to carry Flow Spec announce and it's UUID
type ResponseFlowSpecAnnounce struct {
UUID string `json:"uuid"`
Announce FlowSpecRule `json:"announce"`
}
type ResponseFlowSpecJson struct {
Success bool `json:"success"`
Values []ResponseFlowSpecAnnounce `json:"values"`
}
type ResponseHostGroupConfigurationJson struct {
Success bool `json:"success"`
ErrorText string `json:"error_text"`
Values []Ban_settings_t `json:"values"`
}
type BlackholeAnnounces struct {
UUID string `json:"uuid"`
IP string `json:"ip"`
}
type ResponseRemoteBlackholeListJson struct {
Success bool `json:"success"`
ErrorText string `json:"error_text"`
Values []BlackholeAnnounces `json:"values"`
}
type Ban_settings_t struct {
Name string `bson:"name" json:"name" fastnetmon_type:"string"`
Description string `bson:"description" json:"description" fastnetmon_type:"string"`
Networks []string `bson:"networks" json:"networks" fastnetmon_type:"cidr_networks_list"`
Enable_ban bool `bson:"enable_ban" json:"enable_ban" fastnetmon_type:"bool"`
Ban_for_pps bool `bson:"ban_for_pps" json:"ban_for_pps" fastnetmon_type:"bool"`
Ban_for_bandwidth bool `bson:"ban_for_bandwidth" json:"ban_for_bandwidth" fastnetmon_type:"bool"`
Ban_for_flows bool `bson:"ban_for_flows" json:"ban_for_flows" fastnetmon_type:"bool"`
Threshold_pps uint `bson:"threshold_pps" json:"threshold_pps" fastnetmon_type:"positive_integer_without_zero"`
Threshold_mbps uint `bson:"threshold_mbps" json:"threshold_mbps" fastnetmon_type:"positive_integer_without_zero"`
Threshold_flows uint `bson:"threshold_flows" json:"threshold_flows" fastnetmon_type:"positive_integer_without_zero"`
Ban_for_tcp_bandwidth bool `bson:"ban_for_tcp_bandwidth" json:"ban_for_tcp_bandwidth" fastnetmon_type:"bool"`
Ban_for_udp_bandwidth bool `bson:"ban_for_udp_bandwidth" json:"ban_for_udp_bandwidth" fastnetmon_type:"bool"`
Ban_for_icmp_bandwidth bool `bson:"ban_for_icmp_bandwidth" json:"ban_for_icmp_bandwidth" fastnetmon_type:"bool"`
Ban_for_tcp_pps bool `bson:"ban_for_tcp_pps" json:"ban_for_tcp_pps" fastnetmon_type:"bool"`
Ban_for_udp_pps bool `bson:"ban_for_udp_pps" json:"ban_for_udp_pps" fastnetmon_type:"bool"`
Ban_for_icmp_pps bool `bson:"ban_for_icmp_pps" json:"ban_for_icmp_pps" fastnetmon_type:"bool"`
Threshold_tcp_mbps uint `bson:"threshold_tcp_mbps" json:"threshold_tcp_mbps" fastnetmon_type:"positive_integer_without_zero"`
Threshold_udp_mbps uint `bson:"threshold_udp_mbps" json:"threshold_udp_mbps" fastnetmon_type:"positive_integer_without_zero"`
Threshold_icmp_mbps uint `bson:"threshold_icmp_mbps" json:"threshold_icmp_mbps" fastnetmon_type:"positive_integer_without_zero"`
Threshold_tcp_pps uint `bson:"threshold_tcp_pps" json:"threshold_tcp_pps" fastnetmon_type:"positive_integer_without_zero"`
Threshold_udp_pps uint `bson:"threshold_udp_pps" json:"threshold_udp_pps" fastnetmon_type:"positive_integer_without_zero"`
Threshold_icmp_pps uint `bson:"threshold_icmp_pps" json:"threshold_icmp_pps" fastnetmon_type:"positive_integer_without_zero"`
}
type FastNetMonClient struct {
User string `json:"api_user"`
Password string `json:"api_password"`
Host string `json:"api_host"`
Port uint32 `json:"api_port"`
Ro *grequests.RequestOptions
Prefix string
}
// Creates new client, just checks input, does not execute connection attemps
func NewClient(host string, port uint32, user, password string) (*FastNetMonClient, error) {
client := FastNetMonClient{}
client.User = user
client.Password = password
client.Host = host
client.Port = port
if user != "" && password != "" && host != "" && port != 0 {
client.Ro = &grequests.RequestOptions{Auth: []string{client.User, client.Password}}
client.Prefix = fmt.Sprintf("http://%s:%d", client.Host, client.Port)
return &client, nil
}
return nil, errors.New("Please provide all fields")
}
// Set specified bool option for host group
func (client *FastNetMonClient) SetBoolOptionHostGroup(hostgroup_name string, option_name string, value bool) (bool, error) {
value_string := "disable"
if value {
value_string = "enable"
}
resp, err := grequests.Put(client.Prefix+"/hostgroup/"+hostgroup_name+"/"+option_name+"/"+value_string, client.Ro)
if err != nil {
return false, fmt.Errorf("Cannot connect to API: %w", err)
}
if !resp.Ok {
if resp.StatusCode == 401 {
return false, errors.New("Auth denied")
} else {
return false, fmt.Errorf("Did not return OK: %d", resp.StatusCode)
}
}
response := ErrorJson{}
err = resp.JSON(&response)
if err != nil {
return false, err
}
return response.Success, nil
}
// Set specified string list option for host group
func (client *FastNetMonClient) SetStringListOptionHostGroup(hostgroup_name string, option_name string, value string) (bool, error) {
// Replace prohibited symbols
value = strings.Replace(value, "/", "%2f", -1)
resp, err := grequests.Put(client.Prefix+"/hostgroup/"+hostgroup_name+"/"+option_name+"/"+value, client.Ro)
if err != nil {
return false, fmt.Errorf("Cannot connect to API: %w", err)
}
if !resp.Ok {
if resp.StatusCode == 401 {
return false, errors.New("Auth denied")
} else {
return false, fmt.Errorf("Did not return OK: %d", resp.StatusCode)
}
}
response := ErrorJson{}
err = resp.JSON(&response)
if err != nil {
return false, err
}
return response.Success, nil
}
// Set specified int option for host group
func (client *FastNetMonClient) SetUnsignedIntegerOptionHostGroup(hostgroup_name string, option_name string, value uint) (bool, error) {
resp, err := grequests.Put(client.Prefix+"/hostgroup/"+hostgroup_name+"/"+option_name+"/"+fmt.Sprintf("%d", value), client.Ro)
if err != nil {
return false, fmt.Errorf("Cannot connect to API: %w", err)
}
if !resp.Ok {
if resp.StatusCode == 401 {
return false, errors.New("Auth denied")
} else {
return false, fmt.Errorf("Did not return OK: %d", resp.StatusCode)
}
}
response := ErrorJson{}
err = resp.JSON(&response)
if err != nil {
return false, err
}
return response.Success, nil
}
// Creates host groups with specified name
func (client *FastNetMonClient) CreateHostGroup(name string) (bool, error) {
resp, err := grequests.Put(client.Prefix+"/hostgroup/"+name, client.Ro)
if err != nil {
return false, fmt.Errorf("Cannot connect to API: %w", err)
}
if !resp.Ok {
if resp.StatusCode == 401 {
return false, errors.New("Auth denied")
} else {
return false, fmt.Errorf("Did not return OK: %d", resp.StatusCode)
}
}
response := ErrorJson{}
err = resp.JSON(&response)
if err != nil {
return false, err
}
return response.Success, nil
}
// Removes host group by name
func (client *FastNetMonClient) RemoveHostGroup(name string) (bool, error) {
resp, err := grequests.Delete(client.Prefix+"/hostgroup/"+name, client.Ro)
if err != nil {
return false, fmt.Errorf("Cannot connect to API: %w", err)
}
if !resp.Ok {
if resp.StatusCode == 401 {
return false, errors.New("Auth denied")
} else {
return false, fmt.Errorf("Did not return OK: %d", resp.StatusCode)
}
}
response := ErrorJson{}
err = resp.JSON(&response)
if err != nil {
return false, err
}
return response.Success, nil
}
// Blocks some specified blackhole host
func (client *FastNetMonClient) BlackholeRemote(ip_address string) (bool, error) {
resp, err := grequests.Put(client.Prefix+"/remote_blackhole/"+ip_address, client.Ro)
if err != nil {
return false, fmt.Errorf("Cannot connect to API: %w", err)
}
if !resp.Ok {
if resp.StatusCode == 401 {
return false, errors.New("Auth denied")
} else {
return false, fmt.Errorf("Did not return OK: %d", resp.StatusCode)
}
}
response := ErrorJson{}
err = resp.JSON(&response)
if err != nil {
return false, err
}
return response.Success, nil
}
// Returns all IPs blocked using remote blackhole
func (client *FastNetMonClient) GetRemoteBlackhole() ([]BlackholeAnnounces, error) {
resp, err := grequests.Get(client.Prefix+"/remote_blackhole", client.Ro)
if err != nil {
return nil, fmt.Errorf("Cannot connect to API: %w", err)
}
if !resp.Ok {
if resp.StatusCode == 401 {
return nil, errors.New("Auth denied")
} else {
return nil, fmt.Errorf("Did not return OK: %d", resp.StatusCode)
}
}
ban_list_response := ResponseRemoteBlackholeListJson{}
err = resp.JSON(&ban_list_response)
if err != nil {
return nil, err
}
return ban_list_response.Values, nil
}
// Removes remote blackhole entry using UUID
func (client *FastNetMonClient) RemoveRemoteBlackhole(mitigation_uuid string) (bool, error) {
resp, err := grequests.Delete(client.Prefix+"/remote_blackhole/"+mitigation_uuid, client.Ro)
if err != nil {
return false, fmt.Errorf("Cannot connect to API: %w", err)
}
if !resp.Ok {
if resp.StatusCode == 401 {
return false, errors.New("Auth denied")
} else {
return false, fmt.Errorf("Did not return OK: %d", resp.StatusCode)
}
}
response := ErrorJson{}
err = resp.JSON(&response)
if err != nil {
return false, err
}
return response.Success, nil
}
// Returns all networks known by FastNetMon
func (client *FastNetMonClient) GetNetworks() ([]string, error) {
resp, err := grequests.Get(client.Prefix+"/main/networks_list", client.Ro)
if err != nil {
return nil, fmt.Errorf("Cannot connect to API: %w", err)
}
if !resp.Ok {
if resp.StatusCode == 401 {
return nil, errors.New("Auth denied")
} else {
return nil, fmt.Errorf("Did not return OK: %d", resp.StatusCode)
}
}
networks_response := ResponseArrayJson{}
err = resp.JSON(&networks_response)
if err != nil {
return nil, err
}
return networks_response.Values, nil
}
// Retrieves all host groups
func (client *FastNetMonClient) GetAllHostgroups() ([]Ban_settings_t, error) {
resp, err := grequests.Get(client.Prefix+"/hostgroup", client.Ro)
if err != nil {
return nil, fmt.Errorf("Cannot connect to API: %w", err)
}
if !resp.Ok {
if resp.StatusCode == 401 {
return nil, errors.New("Auth denied")
} else {
return nil, fmt.Errorf("Did not return OK: %d", resp.StatusCode)
}
}
hostgroups_response := ResponseHostGroupConfigurationJson{}
err = resp.JSON(&hostgroups_response)
if err != nil {
return nil, err
}
return hostgroups_response.Values, nil
}
// Creates specified host group with all fields
// TODO: it does not implement all options, only required subset
func Create_host_group_with_all_options(fastnetmon_client *FastNetMonClient, new_host_group Ban_settings_t) error {
new_host_group_name := new_host_group.Name
add_success, err := fastnetmon_client.CreateHostGroup(new_host_group_name)
if err != nil {
return fmt.Errorf("Cannot create hostgroup '%+v' with error: %w", new_host_group, err)
}
if !add_success {
return fmt.Errorf("Cannot create host group for some reasons")
}
enable_ban_res, err := fastnetmon_client.SetBoolOptionHostGroup(new_host_group_name, "enable_ban", new_host_group.Enable_ban)
if err != nil {
return fmt.Errorf("Cannot set bool variable: %+v", err)
}
if !enable_ban_res {
return fmt.Errorf("Cannot set bool variable for some reasons")
}
// Add networks
for _, network := range new_host_group.Networks {
add_network, err := fastnetmon_client.SetStringListOptionHostGroup(new_host_group_name, "networks", network)
if err != nil {
return fmt.Errorf("Cannot add network: %w", err)
}
if !add_network {
return fmt.Errorf("Cannot add network")
}
}
enable_bandwidth_ban, err := fastnetmon_client.SetBoolOptionHostGroup(new_host_group_name, "ban_for_bandwidth", new_host_group.Ban_for_bandwidth)
if err != nil {
return fmt.Errorf("Cannot set bool variable: %+v", err)
}
if !enable_bandwidth_ban {
return fmt.Errorf("Cannot set bool variable for some reasons")
}
enable_pps_ban, err := fastnetmon_client.SetBoolOptionHostGroup(new_host_group_name, "ban_for_pps", new_host_group.Ban_for_pps)
if err != nil {
return fmt.Errorf("Cannot set bool variable: %+v", err)
}
if !enable_pps_ban {
return fmt.Errorf("Cannot set bool variable for some reasons")
}
enable_flow_ban, err := fastnetmon_client.SetBoolOptionHostGroup(new_host_group_name, "ban_for_flows", new_host_group.Ban_for_flows)
if err != nil {
return fmt.Errorf("Cannot set bool variable: %+v", err)
}
if !enable_flow_ban {
return fmt.Errorf("Cannot set bool variable for some reasons")
}
bandwidth_threshold, err := fastnetmon_client.SetUnsignedIntegerOptionHostGroup(new_host_group_name, "threshold_mbps", new_host_group.Threshold_mbps)
if err != nil {
return fmt.Errorf("Cannot set threshold value variable: %+v", err)
}
if !bandwidth_threshold {
return fmt.Errorf("Cannot set bandwidth threshold")
}
packet_threshold, err := fastnetmon_client.SetUnsignedIntegerOptionHostGroup(new_host_group_name, "threshold_pps", new_host_group.Threshold_pps)
if err != nil {
return fmt.Errorf("Cannot set threshold value variable: %+v", err)
}
if !packet_threshold {
return fmt.Errorf("Cannot set packet per second threshold")
}
flow_threshold, err := fastnetmon_client.SetUnsignedIntegerOptionHostGroup(new_host_group_name, "threshold_flows", new_host_group.Threshold_flows)
if err != nil {
return fmt.Errorf("Cannot set threshold value variable: %+v", err)
}
if !flow_threshold {
return fmt.Errorf("Cannot set flow threshold")
}
return nil
}
// Adds Flow Spec announce
func (client *FastNetMonClient) AddFlowSpecRule(flow_spec_rule FlowSpecRule) (bool, error) {
request_options := client.Ro
request_options.JSON = flow_spec_rule
resp, err := grequests.Put(client.Prefix+"/flowspec", client.Ro)
if err != nil {
return false, fmt.Errorf("Cannot connect to API: %w", err)
}
if !resp.Ok {
if resp.StatusCode == 401 {
return false, errors.New("Auth denied")
} else {
return false, fmt.Errorf("Did not return OK: %d", resp.StatusCode)
}
}
response := ErrorJson{}
err = resp.JSON(&response)
if err != nil {
return false, err
}
return response.Success, nil
}
// Returns all active flow spec announces
func (client *FastNetMonClient) GetFlowSpecRules() ([]ResponseFlowSpecAnnounce, error) {
resp, err := grequests.Get(client.Prefix+"/flowspec", client.Ro)
if err != nil {
return nil, fmt.Errorf("Cannot connect to API: %w", err)
}
if !resp.Ok {
if resp.StatusCode == 401 {
return nil, errors.New("Auth denied")
} else {
return nil, fmt.Errorf("Did not return OK: %d", resp.StatusCode)
}
}
flow_spec_response := ResponseFlowSpecJson{}
err = resp.JSON(&flow_spec_response)
if err != nil {
return nil, err
}
return flow_spec_response.Values, nil
}
// Removes Flow Spec entry using UUID
func (client *FastNetMonClient) RemoveFlowSpecRule(mitigation_uuid string) (bool, error) {
resp, err := grequests.Delete(client.Prefix+"/flowspec/"+mitigation_uuid, client.Ro)
if err != nil {
return false, fmt.Errorf("Cannot connect to API: %w", err)
}
if !resp.Ok {
if resp.StatusCode == 401 {
return false, errors.New("Auth denied")
} else {
return false, fmt.Errorf("Did not return OK: %d", resp.StatusCode)
}
}
response := ErrorJson{}
err = resp.JSON(&response)
if err != nil {
return false, err
}
return response.Success, nil
}