-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.go
210 lines (170 loc) · 5.02 KB
/
runtime.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
package runtime
import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"github.com/MindHunter86/addie/blocklist"
"github.com/MindHunter86/addie/utils"
"github.com/rs/zerolog"
)
type RuntimePatchType uint8
const (
RuntimePatchLottery RuntimePatchType = iota
RuntimePatchQuality
RuntimePatchBlocklist
RuntimePatchBlocklistIps
RuntimePatchLimiter
RuntimePatchAccessStdout
RuntimePatchAccessLevel
)
var (
ErrRuntimeUndefinedPatch = errors.New("given patch payload is undefined")
RuntimeUtilsBindings = map[string]RuntimePatchType{
utils.CfgLotteryChance: RuntimePatchLottery,
utils.CfgQualityLevel: RuntimePatchQuality,
utils.CfgBlockList: RuntimePatchBlocklistIps,
utils.CfgBlockListSwitcher: RuntimePatchBlocklist,
utils.CfgLimiterSwitcher: RuntimePatchLimiter,
utils.CfgAccessLogStdout: RuntimePatchAccessStdout,
utils.CfgAccessLogLevel: RuntimePatchAccessLevel,
}
// intenal
log *zerolog.Logger
runtimeChangesHumanize = map[RuntimePatchType]string{
RuntimePatchLottery: "lottery chance",
RuntimePatchQuality: "quality level",
RuntimePatchBlocklist: "blocklist switch",
RuntimePatchBlocklistIps: "blocklist ips",
RuntimePatchLimiter: "limiter switch",
RuntimePatchAccessStdout: "access_log stdout switcher",
RuntimePatchAccessLevel: "access_log loglevel",
}
)
type (
Runtime struct {
Config *Storage
// todo - refactor
blocklist *blocklist.Blocklist // temporary;
}
RuntimePatch struct {
Type RuntimePatchType
Patch []byte
}
)
func NewRuntime(c context.Context) (r *Runtime, e error) {
blist := c.Value(utils.ContextKeyBlocklist).(*blocklist.Blocklist)
log = c.Value(utils.ContextKeyLogger).(*zerolog.Logger)
r = &Runtime{
blocklist: blist,
}
if r.Config, e = NewStorage(c); e != nil {
return
}
return
}
func (m *Runtime) ApplyPatch(patch *RuntimePatch) (e error) {
if len(patch.Patch) == 0 {
return ErrRuntimeUndefinedPatch
}
switch patch.Type {
case RuntimePatchLottery:
e = patch.ApplyLotteryChance(m.Config)
case RuntimePatchQuality:
e = patch.ApplyQualityLevel(m.Config)
case RuntimePatchBlocklistIps:
e = patch.ApplyBlocklistIps(m.Config, m.blocklist)
case RuntimePatchBlocklist:
e = patch.ApplySwitch(m.Config, ParamBlocklist)
case RuntimePatchLimiter:
e = patch.ApplySwitch(m.Config, ParamLimiter)
case RuntimePatchAccessStdout:
e = patch.ApplySwitch(m.Config, ParamAccessStdout)
case RuntimePatchAccessLevel:
e = patch.ApplyLogLevel(m.Config, ParamAccessLevel)
default:
panic("internal error - undefined runtime patch type")
}
if e != nil {
log.Error().Err(e).
Msgf("could not apply runtime configuration (%s)", runtimeChangesHumanize[patch.Type])
}
return
}
func (m *RuntimePatch) ApplyLogLevel(st *Storage, param StorageParam) (e error) {
buf, level := strings.TrimSpace(string(m.Patch)), zerolog.NoLevel
switch buf {
case "trace":
level = zerolog.TraceLevel
case "debug":
level = zerolog.DebugLevel
case "info":
level = zerolog.InfoLevel
case "warn":
level = zerolog.WarnLevel
case "error":
level = zerolog.ErrorLevel
default:
e = fmt.Errorf("unknown level received from consul for %s - %s", GetNameByParam[param], buf)
return
}
st.Set(ParamAccessLevel, level)
log.Info().Msgf("runtime patch has been applied for %s with %s", GetNameByParam[param], buf)
return
}
func (m *RuntimePatch) ApplyBlocklistIps(_ *Storage, bl *blocklist.Blocklist) (e error) {
buf := string(m.Patch)
if buf == "_" {
bl.Reset()
log.Info().Msg("runtime patch has been for Blocklist.Reset")
return
}
lastsize := bl.Size()
ips := strings.Split(buf, ",")
bl.Push(ips...)
// dummy code
// ???
// st.SetValue(ParamBlocklistIps, ips)
log.Info().Msgf("runtime patch has been for Blocklist, applied %d ips", len(ips))
log.Debug().Msgf("apply blocklist: last size - %d, new - %d", lastsize, bl.Size())
return
}
func (m *RuntimePatch) ApplySwitch(st *Storage, param StorageParam) (e error) {
buf := string(m.Patch)
switch buf {
case "0":
st.Set(param, 0)
case "1":
st.Set(param, 1)
default:
e = fmt.Errorf("invalid value in runtime bool patch for %s : %s", GetNameByParam[param], buf)
return
}
log.Info().Msgf("runtime patch has been applied for %s with %s", GetNameByParam[param], buf)
return
}
func (m *RuntimePatch) ApplyQualityLevel(st *Storage) (e error) {
buf := string(m.Patch)
quality, ok := utils.GetTitleQualityByString[buf]
if !ok {
e = fmt.Errorf("quality is invalid; 480, 720, 1080 values are permited only, current - %s", buf)
return
}
log.Info().Msgf("runtime patch has been applied for QualityLevel with %s", buf)
st.SetSmoothly(ParamQuality, quality)
return
}
func (m *RuntimePatch) ApplyLotteryChance(st *Storage) (e error) {
var chance int
if chance, e = strconv.Atoi(string(m.Patch)); e != nil {
return
}
if chance < 0 || chance > 100 {
e = fmt.Errorf("chance could not be less than 0 and more than 100, current %d", chance)
return
}
log.Info().Msgf("runtime patch has been applied for LotteryChance with %d", chance)
st.SetSmoothly(ParamLottery, chance)
return
}