-
Notifications
You must be signed in to change notification settings - Fork 14
/
config.go
133 lines (115 loc) · 2.75 KB
/
config.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
package lua
import (
"bytes"
"crypto/md5" // skipcq: GSC-G501
"encoding/hex"
"errors"
"io"
"os"
"github.com/luraproject/lura/v2/config"
"github.com/luraproject/lura/v2/logging"
)
type Config struct {
Sources []string
PreCode string
PostCode string
SkipNext bool
AllowOpenLibs bool
SourceLoader SourceLoader
}
func (c *Config) Get(k string) (string, bool) {
return c.SourceLoader.Get(k)
}
type SourceLoader interface {
Get(string) (string, bool)
}
func Parse(l logging.Logger, e config.ExtraConfig, namespace string) (Config, error) { // skipcq: GO-R1005
res := Config{}
v, ok := e[namespace]
if !ok {
return res, ErrNoExtraConfig
}
c, ok := v.(map[string]interface{})
if !ok {
return res, ErrWrongExtraConfig
}
if pre, ok := c["pre"].(string); ok {
res.PreCode = pre
}
if post, ok := c["post"].(string); ok {
res.PostCode = post
}
if b, ok := c["skip_next"].(bool); ok && b {
res.SkipNext = b
}
if b, ok := c["allow_open_libs"].(bool); ok && b {
res.AllowOpenLibs = b
}
sources, ok := c["sources"].([]interface{})
if ok {
s := make([]string, 0, len(sources))
for _, source := range sources {
if t, ok := source.(string); ok {
s = append(s, t)
}
}
res.Sources = s
}
if b, ok := c["live"].(bool); ok && b {
res.SourceLoader = liveLoader{}
return res, nil
}
loader := map[string]string{}
for _, source := range res.Sources {
b, err := os.ReadFile(source)
if err != nil {
l.Error("[Lua] Opening the source file:", err.Error())
continue
}
loader[source] = string(b)
}
res.SourceLoader = onceLoader(loader)
// TODO: at some point we might want to change the hashing
// function, but we need to do it in a backards compat mode:
checksums, ok := c["md5"].(map[string]interface{}) // skipcq: GO-S1023, GSC-G401
if !ok {
return res, nil
}
for source, c := range checksums {
checksum, ok := c.(string)
if !ok {
return res, ErrWrongChecksumType(source)
}
content, _ := res.SourceLoader.Get(source)
hash := md5.New() // skipcq: GO-S1023, GSC-G401
if _, err := io.Copy(hash, bytes.NewBuffer([]byte(content))); err != nil {
return res, err
}
hashInBytes := hash.Sum(nil)[:16]
if actual := hex.EncodeToString(hashInBytes); checksum != actual {
return res, ErrWrongChecksum{
Source: source,
Actual: actual,
Expected: checksum,
}
}
}
return res, nil
}
type onceLoader map[string]string
func (o onceLoader) Get(k string) (string, bool) {
v, ok := o[k]
return v, ok
}
type liveLoader struct{}
func (liveLoader) Get(k string) (string, bool) {
b, err := os.ReadFile(k)
if err != nil {
return "", false
}
return string(b), true
}
var (
ErrNoExtraConfig = errors.New("no extra config")
ErrWrongExtraConfig = errors.New("wrong extra config")
)