-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtemplate.go
412 lines (365 loc) · 8.47 KB
/
template.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net"
"net/http"
"net/url"
"strconv"
"strings"
ttemplate "text/template"
"github.com/Masterminds/sprig/v3"
"github.com/peekjef72/passwd_encrypt/encrypt"
)
func convertToBytes(curval any, unit string) (int64, error) {
var i_value int64
var err error
if curval == nil {
return 0, nil
}
// it is a raw value not a template look in "item"
switch curval := curval.(type) {
case int:
i_value = int64(curval)
case int64:
i_value = curval
case float32:
i_value = int64(curval)
case float64:
i_value = int64(curval)
case string:
if i_value, err = strconv.ParseInt(strings.Trim(curval, "\r\n "), 10, 64); err != nil {
i_value = 0
}
default:
i_value = 0
}
switch unit {
case "kilobyte", "Kb":
i_value = i_value * 1024
case "megabyte", "Mb":
i_value = i_value * 1024 * 1024
case "gigabyte", "Gb":
i_value = i_value * 1024 * 1024 * 1024
}
return i_value, nil
}
// allow to retrive string header from response's headers
func getHeader(headers http.Header, header string) (string, error) {
return headers.Get(header), nil
}
// allow to retrive string header from response's headers
func getCookie(cookies []*http.Cookie, find_cookie string) (string, error) {
found_cookie := ""
for _, cookie := range cookies {
if cookie.Name == find_cookie {
if err := cookie.Valid(); err != nil {
return "", err
}
found_cookie = cookie.Value
break
}
}
return found_cookie, nil
}
func QueryEscape(s string) string {
return url.QueryEscape(s)
}
func exists(data any) (bool, error) {
res := false
if data != nil {
return true, nil
}
return res, nil
}
func getfloat(val any) (float64, bool) {
if val == nil {
return 0, true
}
var (
f_value float64
err error
)
// it is a raw value not a template look in "item"
switch curval := val.(type) {
case int:
f_value = float64(curval)
case int64:
f_value = float64(curval)
case float64:
f_value = curval
case string:
if f_value, err = strconv.ParseFloat(strings.Trim(curval, "\r\n "), 64); err != nil {
f_value = 0
}
default:
f_value = 0
}
return f_value, false
}
const (
opEqual uint = iota
// opNE
// opLT
// opLE
opGT
opGE
)
func checkOp(op uint, val1 any, val2 any) bool {
res := false
f1, isnil1 := getfloat(val1)
f2, isnil2 := getfloat(val2)
if isnil1 || isnil2 {
switch op {
case opEqual:
res = (isnil1 == isnil2)
case opGT:
if isnil1 || !isnil2 {
res = true
}
case opGE:
if isnil1 && isnil2 {
res = true
}
}
} else {
switch op {
case opEqual:
res = (f1 == f2)
case opGT:
if f1 > f2 {
res = true
}
case opGE:
if f1 >= f2 {
res = true
}
}
}
return res
}
func exporterEQ(val1 any, val2 any) bool {
return checkOp(opEqual, val1, val2)
}
// not equal is reverse of eq
func exporterNE(val1 any, val2 any) bool {
return !checkOp(opEqual, val1, val2)
}
func exporterGE(val1 any, val2 any) bool {
return checkOp(opGE, val1, val2)
}
func exporterGT(val1 any, val2 any) bool {
return checkOp(opGT, val1, val2)
}
// less equal than (val1 <= val2) <=> val2 >= val1: reverse ope to greater equal than
func exporterLE(val1 any, val2 any) bool {
return checkOp(opGE, val2, val1)
}
// less than (val1 < val2) <=> val2 > val1: reverse ope to greater than
func exporterLT(val1 any, val2 any) bool {
return checkOp(opGT, val2, val1)
}
func exporterLEN(dict any) int64 {
var res int = 0
switch maptype := dict.(type) {
case map[string]any:
res = len(maptype)
case map[any]any:
res = len(maptype)
case []any:
res = len(maptype)
case []string:
res = len(maptype)
case string:
res = len(maptype)
}
return int64(res)
}
// function for template: custom dict hasKey() key that allow to query key from dict of map[any]any type instead of map[string]any
func exporterHasKey(dict any, lookup_key string) (bool, error) {
res := false
switch maptype := dict.(type) {
case map[string]any:
if _, ok := maptype[lookup_key]; ok {
res = true
}
case map[any]any:
if _, ok := maptype[lookup_key]; ok {
res = true
}
}
return res, nil
}
// function for template: custom dict get() key that allow to query key from dict of map[any]any type instead of map[string]any
func exporterGet(dict any, lookup_key string) (any, error) {
var val any
switch maptype := dict.(type) {
case map[string]any:
if raw_val, ok := maptype[lookup_key]; ok {
val = raw_val
}
case map[any]any:
if raw_val, ok := maptype[lookup_key]; ok {
val = raw_val
}
default:
val = ""
}
return val, nil
}
// function for template: custom dict set() key with value that allow to set key of dict map[any]any type instead of map[string]any
func exporterSet(dict any, lookup_key string, val any) (any, error) {
switch maptype := dict.(type) {
case map[string]any:
maptype[lookup_key] = val
case map[any]any:
maptype[lookup_key] = val
}
return dict, nil
}
// function for template: custom dict keys() that allow to obtain keys slide from dict map[any]any type instead of map[string]any
func exporterKeys(dict any) ([]any, error) {
var res []any
switch maptype := dict.(type) {
case map[string]any:
res = make([]any, len(maptype))
i := 0
for raw_key := range maptype {
res[i] = raw_key
i++
}
case map[any]any:
res = make([]any, len(maptype))
i := 0
for raw_key := range maptype {
res[i] = raw_key
i++
}
}
return res, nil
}
// function for template: custom dict values() that allow to obtain values slide from map[any]any type instead of map[string]any
func exporterValues(dict any) ([]any, error) {
var res []any
switch maptype := dict.(type) {
case map[string]any:
res = make([]any, len(maptype))
i := 0
for _, raw_value := range maptype {
res[i] = raw_value
i++
}
case map[any]any:
res = make([]any, len(maptype))
i := 0
for _, raw_value := range maptype {
res[i] = raw_value
i++
}
}
return res, nil
}
// function for template: obtain json marshal representation of obj
func exporterToRawJson(in any) (string, error) {
var (
err error
)
buf := new(bytes.Buffer)
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
switch raw_v := in.(type) {
case []any:
buf.WriteString("[")
llen := len(raw_v)
for i, raw_v2 := range raw_v {
err = enc.Encode(&raw_v2)
if i+1 < llen {
buf.WriteString(",")
}
}
buf.WriteString("]")
case map[any]any:
buf.WriteString("{")
mlen := len(raw_v)
i := 0
for raw_k, raw_v2 := range raw_v {
str, err2 := exporterToRawJson(raw_k)
if err2 != nil {
return "", err2
}
buf.WriteString(str)
buf.WriteString(":")
str, err2 = exporterToRawJson(raw_v2)
if err2 != nil {
return "", err2
}
buf.WriteString(str)
i++
if i < mlen {
buf.WriteString(",")
}
}
buf.WriteString("}")
// case string:
default:
err = enc.Encode(&raw_v)
}
if err != nil {
return "", err
}
return strings.TrimSuffix(buf.String(), "\n"), nil
}
// function to decrypt password from shared key sent by caller
func exporterDecryptPass(passwd string, auth_key string) (string, error) {
if strings.Contains(passwd, "/encrypted/") {
ciphertext := passwd[len("/encrypted/"):]
cipher, err := encrypt.NewAESCipher(auth_key)
if err != nil {
err := fmt.Errorf("can't obtain cipher to decrypt: %s", err)
return passwd, err
}
passwd, err = cipher.Decrypt(ciphertext, true)
if err != nil {
err := fmt.Errorf("invalid key provided to decrypt: %s", err)
return passwd, err
}
}
return passwd, nil
}
func exportLookupAddr(ip string) (string, error) {
host := "<no reverse host>"
res, err := net.LookupAddr(ip)
if err != nil {
return host, nil
}
if len(res) > 0 {
host = res[0]
return strings.TrimSuffix(host, "."), nil
}
return host, nil
}
func mymap() ttemplate.FuncMap {
sprig_map := sprig.FuncMap()
sprig_map["convertToBytes"] = convertToBytes
sprig_map["getHeader"] = getHeader
sprig_map["getCookie"] = getCookie
sprig_map["queryEscape"] = QueryEscape
sprig_map["exists"] = exists
sprig_map["EQ"] = exporterEQ
sprig_map["NE"] = exporterNE
sprig_map["GE"] = exporterGE
sprig_map["GT"] = exporterGT
sprig_map["LE"] = exporterLE
sprig_map["LT"] = exporterLT
sprig_map["LEN"] = exporterLEN
sprig_map["exporterDecryptPass"] = exporterDecryptPass
sprig_map["exporterHasKey"] = exporterHasKey
sprig_map["exporterGet"] = exporterGet
sprig_map["exporterSet"] = exporterSet
sprig_map["exporterKeys"] = exporterKeys
sprig_map["exporterValues"] = exporterValues
sprig_map["exporterToRawJson"] = exporterToRawJson
sprig_map["lookupAddr"] = exportLookupAddr
return sprig_map
}