-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhostgroup.go
More file actions
250 lines (180 loc) · 6.53 KB
/
Copy pathhostgroup.go
File metadata and controls
250 lines (180 loc) · 6.53 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
package fastnetmon
import (
"fmt"
"strings"
"github.com/levigross/grequests"
)
// Retrieves all host groups
func (client *FastNetMonClient) GetAllHostgroups() ([]Ban_settings_t, error) {
resp, err := grequests.Get(client.Prefix+"/hostgroup", grequests.FromRequestOptions(client.Ro))
if err != nil {
return nil, fmt.Errorf("Cannot connect to API: %w", err)
}
if !resp.Ok {
return nil, apiStatusError(resp)
}
hostgroups_response := ResponseHostGroupConfigurationJson{}
err = decodeResponseJSON(resp, &hostgroups_response)
if err != nil {
return nil, err
}
return hostgroups_response.Values, nil
}
// Creates host groups with specified name
func (client *FastNetMonClient) CreateHostGroup(name string) (bool, error) {
resp, err := grequests.Put(client.Prefix+"/hostgroup/"+name, grequests.FromRequestOptions(client.Ro))
if err != nil {
return false, fmt.Errorf("Cannot connect to API: %w", err)
}
if !resp.Ok {
return false, apiStatusError(resp)
}
response := ErrorJson{}
err = decodeResponseJSON(resp, &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, grequests.FromRequestOptions(client.Ro))
if err != nil {
return false, fmt.Errorf("Cannot connect to API: %w", err)
}
if !resp.Ok {
return false, apiStatusError(resp)
}
response := ErrorJson{}
err = decodeResponseJSON(resp, &response)
if err != nil {
return false, err
}
return response.Success, nil
}
// 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, grequests.FromRequestOptions(client.Ro))
if err != nil {
return false, fmt.Errorf("Cannot connect to API: %w", err)
}
if !resp.Ok {
return false, apiStatusError(resp)
}
response := ErrorJson{}
err = decodeResponseJSON(resp, &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, grequests.FromRequestOptions(client.Ro))
if err != nil {
return false, fmt.Errorf("Cannot connect to API: %w", err)
}
if !resp.Ok {
return false, apiStatusError(resp)
}
response := ErrorJson{}
err = decodeResponseJSON(resp, &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), grequests.FromRequestOptions(client.Ro))
if err != nil {
return false, fmt.Errorf("Cannot connect to API: %w", err)
}
if !resp.Ok {
return false, apiStatusError(resp)
}
response := ErrorJson{}
err = decodeResponseJSON(resp, &response)
if err != nil {
return false, err
}
return response.Success, 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
}