-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblacklist.go
171 lines (149 loc) · 4.38 KB
/
blacklist.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
package wallarm
import (
"encoding/json"
"net/url"
"strconv"
)
type (
// Blacklist contains operations available on Blacklist resource
Blacklist interface {
BlacklistRead(clientID int) ([]BlacklistRead, error)
BlacklistCreate(blacklistBody *BlacklistCreate) error
BlacklistDelete(clientID int, ids []int) error
}
// Bulk is used to define IP address, applications, time and reason
Bulk struct {
IP string `json:"ip"`
ExpireAt int `json:"expire_at"`
Reason string `json:"reason"`
Poolid int `json:"poolid"`
Clientid int `json:"clientid"`
}
// BlacklistCreate is a root object to fill the blacklist
BlacklistCreate struct {
Bulks *[]Bulk `json:"bulk"`
}
// BlacklistRead is used to unmarshal blacklist Read function
BlacklistRead struct {
Body struct {
Result string `json:"result"`
Total int `json:"total"`
Continuation interface{} `json:"continuation"`
EventContinuation string `json:"event_continuation"`
Objects []struct {
ID int `json:"id"`
Clientid int `json:"clientid"`
Country string `json:"country"`
Poolid int `json:"poolid"`
StillAttacks bool `json:"still_attacks"`
IP string `json:"ip"`
ExpireAt int `json:"expire_at"`
Tags []interface{} `json:"tags"`
BlockedAt int `json:"blocked_at"`
Reason string `json:"reason"`
Tor interface{} `json:"tor"`
Datacenter interface{} `json:"datacenter"`
ProxyType interface{} `json:"proxy_type"`
} `json:"objects"`
} `json:"body"`
}
)
// BlacklistRead requests the current blacklist for the future purposes.
// It is going to respond with the list of IP addresses.
// API reference: https://apiconsole.eu1.wallarm.com
func (api *api) BlacklistRead(clientID int) ([]BlacklistRead, error) {
uri := "/v3/blacklist"
q := url.Values{}
q.Add("filter[clientid]", strconv.Itoa(clientID))
q.Add("filter[attack_delay]", "300")
q.Add("limit", "1000")
query := q.Encode()
respBody, err := api.makeRequest("GET", uri, "", query)
if err != nil {
return nil, err
}
var data BlacklistRead
if err = json.Unmarshal(respBody, &data); err != nil {
return nil, err
}
var resp []BlacklistRead
resp = append(resp, data)
for {
var data BlacklistRead
if err = json.Unmarshal(respBody, &data); err != nil {
return nil, err
}
if data.Body.Continuation != nil {
if q.Get("continuation") == "" {
q.Add("continuation", data.Body.Continuation.(string))
} else {
q.Set("continuation", data.Body.Continuation.(string))
}
query = q.Encode()
respBody, err = api.makeRequest("GET", uri, "", query)
if err != nil {
return nil, err
}
resp = append(resp, data)
} else {
return resp, nil
}
}
}
// BlacklistCreate creates a blacklist in the Wallarm Cloud.
// API reference: https://apiconsole.eu1.wallarm.com
func (api *api) BlacklistCreate(blacklistBody *BlacklistCreate) error {
uri := "/v3/blacklist/bulk"
_, err := api.makeRequest("POST", uri, "", blacklistBody)
if err != nil {
return err
}
return nil
}
// BlacklistDelete deletes a blacklist for the client.
// Currently, it will flush the entire blacklist, then it will be changed for granular deletion.
// API reference: https://apiconsole.eu1.wallarm.com
func (api *api) BlacklistDelete(clientID int, ids []int) error {
var rest int
uri := "/v3/blacklist/all"
q := url.Values{}
q.Add("filter[clientid]", strconv.Itoa(clientID))
for k, id := range ids {
lengthID := len([]byte(strconv.Itoa(id)))
lengthQuery := len([]byte(q.Encode()))
if lengthQuery+lengthID > 7000 {
rest = k
break
}
q.Add("filter[id][]", strconv.Itoa(id))
}
query := q.Encode()
_, err := api.makeRequest("DELETE", uri, "", query)
if err != nil {
return err
}
idRest := ids
for rest != 0 {
q := url.Values{}
q.Add("filter[clientid]", strconv.Itoa(clientID))
idRest = idRest[rest:]
for k, id := range idRest {
lengthID := len([]byte(strconv.Itoa(id)))
lengthQuery := len([]byte(q.Encode()))
if lengthQuery+lengthID > 7000 {
rest = k
break
}
q.Add("filter[id][]", strconv.Itoa(id))
if k == len(idRest)-1 {
rest = 0
}
}
query := q.Encode()
_, err := api.makeRequest("DELETE", uri, "", query)
if err != nil {
return err
}
}
return nil
}