-
Notifications
You must be signed in to change notification settings - Fork 31
/
iterator.go
316 lines (283 loc) · 7.05 KB
/
iterator.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
// Copyright © 2017 The vt-go authors. All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package vt
import (
"bytes"
"compress/flate"
"encoding/base64"
"encoding/json"
"net/url"
"strconv"
"time"
)
const (
ok = iota
retry
stop
)
type cursor struct {
Link string
Offset int
}
func (c *cursor) encode() string {
if c.Link == "" {
return ""
}
var b bytes.Buffer
b64 := base64.NewEncoder(base64.RawURLEncoding, &b)
fw, _ := flate.NewWriter(b64, flate.BestCompression)
json.NewEncoder(fw).Encode(c)
fw.Close()
return b.String()
}
func (c *cursor) decode(s string) error {
if s == "" {
c.Link = ""
c.Offset = 0
return nil
}
b := bytes.NewBufferString(s)
fr := flate.NewReader(base64.NewDecoder(base64.RawURLEncoding, b))
err := json.NewDecoder(fr).Decode(&c)
if err != nil {
return err
}
return nil
}
type collectionObject struct {
object *Object
cursor cursor
}
// IteratorOption represents an option passed to an iterator.
type IteratorOption func(*Iterator) error
// IteratorCursor specifies a cursor for the iterator. The iterator will start
// at the point indicated by the cursor.
func IteratorCursor(cursor string) IteratorOption {
return func(it *Iterator) error {
it.cursor = cursor
return nil
}
}
// IteratorFilter specifies a filtering query that is sent to the backend. The
// backend will return items that comply with the condition imposed by the
// filter. The filter syntax varies depending on the collection being iterated.
func IteratorFilter(filter string) IteratorOption {
return func(it *Iterator) error {
it.filter = filter
return nil
}
}
// IteratorBatchSize specifies the number of items that are retrieved in a
// single call to the backend.
func IteratorBatchSize(n int) IteratorOption {
return func(it *Iterator) error {
it.batchSize = n
return nil
}
}
// IteratorLimit specifies a maximum number of items that will be returned by
// the iterator.
func IteratorLimit(n int) IteratorOption {
return func(it *Iterator) error {
it.limit = n
return nil
}
}
// IteratorDescriptorsOnly receives a boolean that indicate whether or not we want
// the backend to respond with object descriptors instead of the full objects.
func IteratorDescriptorsOnly(b bool) IteratorOption {
return func(it *Iterator) error {
it.descriptorsOnly = b
return nil
}
}
// Iterator represents a iterator over a collection of VirusTotal objects.
type Iterator struct {
client *Client
ch chan interface{}
done chan bool
next *Object
err error
closed bool
limit int
count int
batchSize int
filter string
cursor string
descriptorsOnly bool
links Links
meta map[string]interface{}
}
func newIterator(cli *Client, u *url.URL, options ...IteratorOption) (*Iterator, error) {
skip := 0
it := &Iterator{
client: cli,
ch: make(chan interface{}, 50),
done: make(chan bool)}
for _, opt := range options {
if err := opt(it); err != nil {
return nil, err
}
}
if it.cursor != "" {
c := cursor{}
err := c.decode(it.cursor)
if err != nil {
return nil, err
}
it.links.Next = c.Link
skip = c.Offset
} else {
q := u.Query()
if it.batchSize > 0 {
q.Add("limit", strconv.Itoa(it.batchSize))
}
if it.filter != "" {
q.Add("filter", it.filter)
}
if it.descriptorsOnly {
q.Add("descriptors_only", "true")
}
u.RawQuery = q.Encode()
it.links.Next = u.String()
}
go it.iterate(skip)
return it, nil
}
// Next advances the iterator to the next object and returns true if there are
// more objects or false if the end of the collection has been reached.
func (it *Iterator) Next() bool {
if it.limit > 0 && it.count == it.limit {
return false
}
item, ok := <-it.ch
if ok {
switch v := item.(type) {
case collectionObject:
it.next = v.object
it.cursor = v.cursor.encode()
it.count++
case error:
it.next = nil
it.err = v
}
}
return ok && it.next != nil
}
// Get returns the current object in the collection iterator.
func (it *Iterator) Get() *Object {
return it.next
}
// Cursor returns a token indicating the current iterator's position.
func (it *Iterator) Cursor() string {
return it.cursor
}
// Close closes a collection iterator.
func (it *Iterator) Close() {
if !it.closed {
it.closed = true
it.done <- true
}
}
// Meta returns the metadata returned by the server during the last call to
// the collection's endpoint.
func (it *Iterator) Meta() map[string]interface{} {
return it.meta
}
// Error returns any error occurred during the iteration of a collection.
func (it *Iterator) Error() error {
return it.err
}
func (it *Iterator) trySendToChannel(item interface{}) int {
select {
case <-it.done:
return stop
case it.ch <- item:
return ok
default:
return retry
}
}
func (it *Iterator) sendToChannel(item interface{}) int {
sent := false
for !sent {
switch it.trySendToChannel(item) {
case ok:
sent = true
case retry:
time.Sleep(10 * time.Millisecond)
case stop:
return stop
}
}
return ok
}
func (it *Iterator) getMoreObjects() (objs []*Object, err error) {
nextURL, err := url.Parse(it.links.Next)
if err != nil {
return nil, err
}
var resp *Response
var data json.RawMessage
if resp, err = it.client.GetData(nextURL, &data); err != nil {
return nil, err
}
// Try to unmarshall the data into an object, if it succeeds is because the
// user passed and endpoint that returns a single object to the iterator.
// This case is handled as a collection that returns a single object.
obj := &Object{}
if err = json.Unmarshal(data, obj); err == nil {
objs = append(objs, obj)
} else if err = json.Unmarshal(data, &objs); err != nil {
return nil, err
}
it.links = resp.Links
it.meta = resp.Meta
return objs, nil
}
func (it *Iterator) iterate(skip int) {
sent := 0
loop:
for it.limit == 0 || sent < it.limit {
// Send request to the API to get more objects.
objects, err := it.getMoreObjects()
if err != nil {
// If an error occurred send it through the channel
if it.sendToChannel(err) == stop {
break loop
}
}
objects = objects[skip:]
for i, object := range objects {
co := collectionObject{object: object}
if i == len(objects)-1 {
co.cursor.Link = it.links.Next
co.cursor.Offset = 0
} else {
co.cursor.Link = it.links.Self
co.cursor.Offset = skip + i + 1
}
if it.sendToChannel(co) == stop {
break loop
}
sent++
}
if len(objects) == 0 || it.links.Next == "" {
break loop
}
skip = 0
}
it.closed = true
close(it.ch)
close(it.done)
}