forked from floshodan/hrobot-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firewall.go
318 lines (267 loc) · 8.56 KB
/
firewall.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
package hrobot
import (
"context"
"fmt"
"reflect"
"github.com/Mats4k/hrobot-go/hrobot/schema"
"github.com/google/go-querystring/query"
)
type FirewallClient struct {
client *Client
}
type Firewall struct {
ServerIP string
ServerNumber int
Status string
Filter_IPv6 bool
WhitelistHos bool
Port string
Rules struct {
Input []struct {
IPVersion string
Name string
DstIP interface{}
SrcIP string
DstPort string
SrcPort interface{}
Protocol interface{}
TCPFlags interface{}
Action string
}
Output []struct {
IPVersion string
Name string
DstIP interface{}
SrcIP string
DstPort string
SrcPort interface{}
Protocol interface{}
TCPFlags interface{}
Action string
}
}
}
type FirewallRules struct {
Rules struct {
Input []struct {
IPVersion string `json:"ip_version"`
Name string `json:"name"`
DstIP interface{} `json:"dst_ip"`
SrcIP string `json:"src_ip"`
DstPort string `json:"dst_port"`
SrcPort interface{} `json:"src_port"`
Protocol interface{} `json:"protocol"`
TCPFlags interface{} `json:"tcp_flags"`
Action string `json:"action"`
} `json:"input"`
} `json:"rules"`
}
type InputRule struct {
IPVersion string `url:"ip_version"`
Name string `url:"name"`
DstIP string `url:"dst_ip"`
SrcIP string `url:"src_ip"`
DstPort string `url:"dst_port"`
SrcPort string `url:"src_port"`
Protocol string `url:"protocol"`
TCPFlags string `url:"tcp_flags"`
Action string `url:"action"`
}
type FirewallTemplate struct {
ID int
Name string
Filter_IPv6 bool
WhitelistHos bool
IsDefault bool
}
type FirewallTemplateWithRules struct {
ID int `json:"id"`
Name string
Filter_IPv6 bool
WhitelistHos bool `json:"whitelist_hos"`
IsDefault bool `json:"is_default"`
Rules struct {
Input []struct {
IPVersion string
Name string
DstIP interface{}
SrcIP string
DstPort string
SrcPort interface{}
Protocol interface{}
TCPFlags interface{}
PacketLength interface{}
Action string
}
Output []struct {
IPVersion string
Name string
DstIP interface{}
SrcIP string
DstPort string
SrcPort interface{}
Protocol interface{}
TCPFlags interface{}
PacketLength interface{}
Action string
}
}
}
func (c *FirewallClient) GetFirewallByServernumber(ctx context.Context, servernumber string) (*Firewall, *Response, error) {
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("/firewall/%s", servernumber), nil)
if err != nil {
return nil, nil, err
}
var body schema.Firewall
resp, err := c.client.Do(req, &body)
if err != nil {
return nil, nil, err
}
return FirewallFromSchema(body), resp, nil
}
func (c *FirewallClient) PostFirewallByServernumber(ctx context.Context, servernumber string, opt *FirewallOps) (*Firewall, *Response, error) {
params, _ := query.Values(opt)
//add rules to params
for index, service := range opt.Rules {
v := reflect.ValueOf(service)
for i := 0; i < v.NumField(); i++ {
//if empty string the field must be empty
if v.Field(i).String() != "" {
params.Add(fmt.Sprintf("rules[input][%v][%v]", index, v.Type().Field(i).Tag.Get("url")), v.Field(i).String())
}
}
}
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("/firewall/%s", servernumber), params)
if err != nil {
return nil, nil, err
}
var body schema.Firewall
resp, err := c.client.Do(req, &body)
if err != nil {
return nil, nil, err
}
return FirewallFromSchema(body), resp, nil
}
func (c *FirewallClient) DeleteFirewallByServerNumber(ctx context.Context, servernumber string) (*Response, error) {
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("/firewall/%s", servernumber), nil)
if err != nil {
return nil, err
}
var body schema.Firewall
resp, err := c.client.Do(req, &body)
if err != nil {
return nil, err
}
return resp, nil
}
func (c *FirewallClient) GetFirewallByIP(ctx context.Context, ip string) (*Firewall, *Response, error) {
return c.GetFirewallByServernumber(ctx, ip)
}
func (c *FirewallClient) PostFirewallByIP(ctx context.Context, ip string, opt *FirewallOps) (*Firewall, *Response, error) {
return c.PostFirewallByServernumber(ctx, ip, &FirewallOps{})
}
func (c *FirewallClient) DeleteFirewallByIP(ctx context.Context, ip string) (*Response, error) {
return c.DeleteFirewallByServerNumber(ctx, ip)
}
func (c *FirewallClient) GetFirewallTemplate(ctx context.Context) ([]*FirewallTemplate, *Response, error) {
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("/firewall/template"), nil)
if err != nil {
return nil, nil, err
}
var body schema.FirewallTemplateList
resp, err := c.client.Do(req, &body)
if err != nil {
return nil, nil, err
}
firewalltemplatelist := make([]*FirewallTemplate, 0, len(body))
for i := range body {
firewalltemplate := FirewallTemplateFromSchema(body[i])
firewalltemplatelist = append(firewalltemplatelist, firewalltemplate)
}
return firewalltemplatelist, resp, nil
}
func (c *FirewallClient) PostFirewallTemplate(ctx context.Context, opt *FirewallTemplateOps) (*FirewallTemplateWithRules, *Response, error) {
params, _ := query.Values(opt)
//add rules to params
for index, service := range opt.Rules {
v := reflect.ValueOf(service)
for i := 0; i < v.NumField(); i++ {
//if empty string the field must be empty
if v.Field(i).String() != "" {
params.Add(fmt.Sprintf("rules[input][%v][%v]", index, v.Type().Field(i).Tag.Get("url")), v.Field(i).String())
}
}
}
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("/firewall/template"), params)
if err != nil {
return nil, nil, err
}
var body schema.FirewallTemplateWithRules
resp, err := c.client.Do(req, &body)
if err != nil {
return nil, nil, err
}
return FirewallTemplateWithRulesFromSchema(body), resp, nil
}
func (c *FirewallClient) GetFirewallTemplateById(ctx context.Context, id string) (*FirewallTemplateWithRules, *Response, error) {
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("/firewall/template/%s", id), nil)
if err != nil {
return nil, nil, err
}
var body schema.FirewallTemplateWithRules
resp, err := c.client.Do(req, &body)
if err != nil {
return nil, nil, err
}
return FirewallTemplateWithRulesFromSchema(body), resp, nil
}
func (c *FirewallClient) UpdateFirewallTemplateById(ctx context.Context, id string, opt *FirewallTemplateOps) (*FirewallTemplateWithRules, *Response, error) {
params, _ := query.Values(opt)
//add rules to params
for index, service := range opt.Rules {
v := reflect.ValueOf(service)
for i := 0; i < v.NumField(); i++ {
//if empty string the field must be empty
if v.Field(i).String() != "" {
params.Add(fmt.Sprintf("rules[input][%v][%v]", index, v.Type().Field(i).Tag.Get("url")), v.Field(i).String())
}
}
}
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("/firewall/template/%s", id), params)
if err != nil {
return nil, nil, err
}
var body schema.FirewallTemplateWithRules
resp, err := c.client.Do(req, &body)
if err != nil {
return nil, nil, err
}
return FirewallTemplateWithRulesFromSchema(body), resp, nil
}
func (c *FirewallClient) DeleteFirewallTemplateById(ctx context.Context, id string) (*Response, error) {
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("/firewall/template/%s", id), nil)
if err != nil {
return nil, err
}
var body schema.Firewall
resp, err := c.client.Do(req, &body)
if err != nil {
return nil, err
}
return resp, nil
}
type FirewallOps struct {
Status string `url:"status"` //change the status of the firewall ('active' or 'disabled')
Filter_IPv6 string `url:"filter_ipv6"` // activate or deactivate the ipv6 filter ('true' or 'false', optional)
Whitelist_hos string `url:"whitelist_hos"` // change the flag of hetzner services whitelisting (true or false)
Rules []InputRule `url:"-"`
Template_id string `url:"template_id,omitempty"` // not possible in combination of whitelist_hos and rules
}
type FirewallTemplateOps struct {
Name string `url:"name"` //template name
Filter_IPv6 string `url:"filter_ipv6"` // activate or deactivate the ipv6 filter ('true' or 'false', optional)
Whitelist_hos bool `url:"whitelist_hos"` // change the flag of hetzner services whitelisting (true or false)
Is_Default bool `url:"is_default"`
Rules []InputRule `url:"-"`
}
type RulesJSONtoURLENCode func()