-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelheim.go
453 lines (329 loc) · 10.2 KB
/
helheim.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
package helheim_go
/*
char *auth(char apiKey[], int discover);
char *getBalance();
char *bifrost(int sessionID, char libraryPath[]);
char *wokou(int sessionID, char browser[]);
char *createSession(char options[]);
char *deleteSession(int sessionID);
char *debug(int sessionID, int state);
char *request(int sessionID, char payload[]);
char *setProxy(int sessionID, char proxy[]);
char *setHeaders(int sessionID, char headers[]);
char *setKasada(int sessionID, char kasada[]);
char *setKasadaHooks(int sessionID, char kasadaHooks[]);
char *setCookie(int sessionID, char cookie[]);
char *delCookie(int sessionID, char cookie[]);
#include "Python.h"
*/
import "C"
import (
"encoding/json"
"fmt"
"sync"
"time"
"unsafe"
)
const authValidMinutes = 30
type Helheim interface {
Auth() (*AuthResponse, error)
GetBalance() (*BalanceResponse, error)
CreateSession(options CreateSessionOptions) (*SessionResponse, error)
DeleteSession(sessionId int) (*SessionDeleteResponse, error)
Debug(sessionId int, state int) (interface{}, error)
Request(sessionId int, options RequestOptions) (*RequestResponse, error)
Wokou(sessionId int, browser string) (*WokouResponse, error)
SetProxy(sessionId int, proxy string) (*SetProxyResponse, error)
SetHeaders(sessionId int, headers map[string]string) (*SetHeadersResponse, error)
SetCookie(sessionId int, cookie SessionCookie) (*ModifyCookiesResponse, error)
DelCookie(sessionId int, cookieName string) (*ModifyCookiesResponse, error)
SetKasada(sessionId int, options KasadaOptions) (interface{}, error)
SetKasadaHooks(sessionId int, options KasadaHooksOptions) (interface{}, error)
SetLogger(logger Logger)
}
type helheim struct {
logger Logger
apiKey string
authLck sync.Mutex
lastAuth *time.Time
discover bool
withAutoReAuth bool
}
func newHelheim(apiKey string, discover bool, withAutoReAuth bool, logger Logger) (Helheim, error) {
if logger == nil {
logger = NewNoopLogger()
}
h := &helheim{
logger: logger,
apiKey: apiKey,
withAutoReAuth: withAutoReAuth,
discover: discover,
authLck: sync.Mutex{},
}
auth, err := h.Auth()
if err != nil {
return nil, err
}
if auth.Response != "authenticated" {
return nil, fmt.Errorf("could not authenticate against helheim")
}
logger.Info("initiated helheim")
return h, nil
}
func (h *helheim) Auth() (*AuthResponse, error) {
h.authLck.Lock()
defer h.authLck.Unlock()
if !h.needReAuth() {
return nil, nil
}
discover := 0
if h.discover {
discover = 1
}
apiKey := C.CString(h.apiKey)
d := C.int(discover)
authResp := C.auth(apiKey, d)
jsonPayload := C.GoString(authResp)
C.free(unsafe.Pointer(apiKey))
h.logger.Debug("helheim response for Auth: %s", jsonPayload)
authResponse := AuthResponse{}
err := h.handleResponse(jsonPayload, &authResponse)
now := time.Now()
h.lastAuth = &now
return &authResponse, err
}
func (h *helheim) CreateSession(options CreateSessionOptions) (*SessionResponse, error) {
err := h.reAuth()
if err != nil {
return nil, err
}
optionsString, err := json.Marshal(options)
if err != nil {
return nil, err
}
opt := C.CString(string(optionsString))
jsonPayload := C.GoString(C.createSession(opt))
C.free(unsafe.Pointer(opt))
h.logger.Debug("helheim response for CreateSession: %s", jsonPayload)
sessionResponse := SessionResponse{}
err = h.handleResponse(jsonPayload, &sessionResponse)
return &sessionResponse, err
}
func (h *helheim) GetBalance() (*BalanceResponse, error) {
err := h.reAuth()
if err != nil {
return nil, err
}
jsonPayload := C.GoString(C.getBalance())
h.logger.Debug("helheim response for GetBalance: %s", jsonPayload)
balanceResponse := BalanceResponse{}
err = h.handleResponse(jsonPayload, &balanceResponse)
return &balanceResponse, err
}
func (h *helheim) DeleteSession(sessionId int) (*SessionDeleteResponse, error) {
err := h.reAuth()
if err != nil {
return nil, err
}
sId := C.int(sessionId)
jsonPayload := C.GoString(C.deleteSession(sId))
h.logger.Debug("helheim response for DeleteSession: %s", jsonPayload)
deleteResponse := SessionDeleteResponse{}
err = h.handleResponse(jsonPayload, &deleteResponse)
return &deleteResponse, err
}
func (h *helheim) Request(sessionId int, options RequestOptions) (*RequestResponse, error) {
err := h.reAuth()
if err != nil {
return nil, err
}
ops := options.Options
requestOptionsInternal := requestOptionsInternal{
Method: options.Method,
Url: options.Url,
Options: ops,
}
optionsString, err := json.Marshal(requestOptionsInternal)
if err != nil {
return nil, err
}
opt := C.CString(string(optionsString))
sId := C.int(sessionId)
jsonPayload := C.GoString(C.request(sId, opt))
C.free(unsafe.Pointer(opt))
requestResponse := RequestResponse{}
h.logger.Debug("helheim response for Request: %s", jsonPayload)
err = h.handleResponse(jsonPayload, &requestResponse)
return &requestResponse, err
}
func (h *helheim) Wokou(sessionId int, browser string) (*WokouResponse, error) {
err := h.reAuth()
if err != nil {
return nil, err
}
b := C.CString(browser)
sId := C.int(sessionId)
jsonPayload := C.GoString(C.wokou(sId, b))
C.free(unsafe.Pointer(b))
h.logger.Debug("helheim response for Wokou: %s", jsonPayload)
wokouResponse := WokouResponse{}
err = h.handleResponse(jsonPayload, &wokouResponse)
return &wokouResponse, err
}
func (h *helheim) SetProxy(sessionId int, proxy string) (*SetProxyResponse, error) {
err := h.reAuth()
if err != nil {
return nil, err
}
p := C.CString(proxy)
sId := C.int(sessionId)
jsonPayload := C.GoString(C.setProxy(sId, p))
C.free(unsafe.Pointer(p))
h.logger.Debug("helheim response for SetProxy: %s", jsonPayload)
setProxyResponse := SetProxyResponse{}
err = h.handleResponse(jsonPayload, &setProxyResponse)
return &setProxyResponse, err
}
func (h *helheim) SetHeaders(sessionId int, headers map[string]string) (*SetHeadersResponse, error) {
err := h.reAuth()
if err != nil {
return nil, err
}
headersString, err := json.Marshal(headers)
if err != nil {
return nil, err
}
headersParam := C.CString(string(headersString))
sId := C.int(sessionId)
jsonPayload := C.GoString(C.setHeaders(sId, headersParam))
C.free(unsafe.Pointer(headersParam))
h.logger.Debug("helheim response for SetHeaders: %s", jsonPayload)
setHeadersResponse := SetHeadersResponse{}
err = h.handleResponse(jsonPayload, &setHeadersResponse)
return &setHeadersResponse, err
}
func (h *helheim) SetCookie(sessionId int, cookie SessionCookie) (*ModifyCookiesResponse, error) {
err := h.reAuth()
if err != nil {
return nil, err
}
cookiePayload, err := json.Marshal(cookie)
if err != nil {
return nil, err
}
c := C.CString(string(cookiePayload))
sId := C.int(sessionId)
jsonPayload := C.GoString(C.setCookie(sId, c))
h.logger.Debug("helheim response for SetCookie: %s", jsonPayload)
C.free(unsafe.Pointer(c))
setCookiesResponse := ModifyCookiesResponse{}
err = h.handleResponse(jsonPayload, &setCookiesResponse)
return &setCookiesResponse, err
}
func (h *helheim) DelCookie(sessionId int, cookieName string) (*ModifyCookiesResponse, error) {
err := h.reAuth()
if err != nil {
return nil, err
}
c := C.CString(cookieName)
sId := C.int(sessionId)
jsonPayload := C.GoString(C.delCookie(sId, c))
h.logger.Debug("helheim response for DelCookie: %s", jsonPayload)
C.free(unsafe.Pointer(c))
setCookiesResponse := ModifyCookiesResponse{}
err = h.handleResponse(jsonPayload, &setCookiesResponse)
return &setCookiesResponse, err
}
func (h *helheim) Debug(sessionId int, state int) (interface{}, error) {
err := h.reAuth()
if err != nil {
return nil, err
}
sId := C.int(sessionId)
stateInt := C.int(state)
jsonPayload := C.GoString(C.debug(sId, stateInt))
h.logger.Debug("helheim response for Debug: %s", jsonPayload)
return jsonPayload, nil
}
func (h *helheim) SetKasada(sessionId int, options KasadaOptions) (interface{}, error) {
err := h.reAuth()
if err != nil {
return nil, err
}
optionsString, err := json.Marshal(options)
if err != nil {
return nil, err
}
opt := C.CString(string(optionsString))
sId := C.int(sessionId)
jsonPayload := C.GoString(C.setKasada(sId, opt))
h.logger.Debug("helheim response for SetKasada: %s", jsonPayload)
C.free(unsafe.Pointer(opt))
return jsonPayload, err
}
func (h *helheim) SetKasadaHooks(sessionId int, options KasadaHooksOptions) (interface{}, error) {
err := h.reAuth()
if err != nil {
return nil, err
}
optionsString, err := json.Marshal(options)
if err != nil {
return nil, err
}
opt := C.CString(string(optionsString))
sId := C.int(sessionId)
jsonPayload := C.GoString(C.setKasadaHooks(sId, opt))
C.free(unsafe.Pointer(opt))
h.logger.Debug("helheim response for SetKasadaHooks: %s", jsonPayload)
return jsonPayload, err
}
func (h *helheim) SetLogger(logger Logger) {
h.logger = logger
}
func (h *helheim) handleResponse(jsonPayload string, ret interface{}) error {
errorResponse := ErrorAwareResponse{}
err := json.Unmarshal([]byte(jsonPayload), &errorResponse)
if err != nil {
e := fmt.Errorf("could not unmarshall helheim response to error aware response: %w", err)
h.logger.Error("error while unmarshalling helheim response: %w", e)
return e
}
if errorResponse.Error {
e := fmt.Errorf("helheim error: %s", errorResponse.ErrorMsg)
h.logger.Error("error received in helheim response: %w", e)
return e
}
err = json.Unmarshal([]byte(jsonPayload), ret)
if err != nil {
e := fmt.Errorf("could not unmarshall helheim response to response type: %w", err)
h.logger.Error("error while unmarshalling helheim response: %w", e)
return e
}
return nil
}
func (h *helheim) needReAuth() bool {
if h.lastAuth == nil {
return true
}
now := time.Now()
minutes := now.Sub(*h.lastAuth).Minutes()
h.logger.Info("%.2f minutes since last authentication", minutes)
needsReAuth := minutes >= authValidMinutes
h.logger.Debug("%.2f minutes since last auth. need re auth: %v", minutes, needsReAuth)
return needsReAuth
}
func (h *helheim) reAuth() error {
if !h.withAutoReAuth {
h.logger.Debug("auto re auth not enabled. skipping")
return nil
}
if !h.needReAuth() {
return nil
}
_, err := h.Auth()
if err != nil {
h.logger.Error("failed to authenticate helheim: %w", err)
return err
}
return nil
}