-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (69 loc) · 2.39 KB
/
main.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
package main
import (
"istio-botdetector/config"
"istio-botdetector/detector"
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
)
func main() {
proxywasm.SetVMContext(&vmContext{})
}
type vmContext struct {
types.DefaultVMContext
}
func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext {
return &pluginContext{}
}
type pluginContext struct {
types.DefaultPluginContext
contextID uint32
Configuration config.PluginConfiguration
Detector detector.Detector
CacheBucket detector.CacheBucket
}
func (ctx *pluginContext) NewHttpContext(contextID uint32) types.HttpContext {
return &httpContext{Configuration: ctx.Configuration, Detector: ctx.Detector, CacheBucket: ctx.CacheBucket}
}
type httpContext struct {
types.DefaultHttpContext
contextID uint32
Configuration config.PluginConfiguration
Detector detector.Detector
CacheBucket detector.CacheBucket
}
func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPluginStartStatus {
configDataByte, err := proxywasm.GetPluginConfiguration()
if err != nil {
proxywasm.LogCriticalf("error reading plugin configuration: %v", err)
return types.OnPluginStartStatusFailed
}
if config, err := config.ParsPluginConfiguration(configDataByte); err != nil {
proxywasm.LogCriticalf("error parsing plugin configuration: %v", err)
return types.OnPluginStartStatusFailed
} else {
ctx.Configuration = config
}
if d, err := detector.InitializeDetector(ctx.Configuration); err != nil {
proxywasm.LogCriticalf("error initializing detector: %v", err)
return types.OnPluginStartStatusFailed
} else {
ctx.Detector = d
}
if cacheB, err := detector.InitializeCacheBucket(ctx.Configuration); err != nil {
proxywasm.LogCriticalf("error initializing cache bucket: %v", err)
return types.OnPluginStartStatusFailed
} else {
ctx.CacheBucket = cacheB
}
return types.OnPluginStartStatusOK
}
func (ctx *httpContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action {
userAgent, _ := proxywasm.GetHttpRequestHeader("user-agent")
if detector.IsBot(userAgent, ctx.Detector, ctx.Configuration, ctx.CacheBucket) {
if err := proxywasm.SendHttpResponse(403, [][2]string{}, nil, -1); err != nil {
proxywasm.LogErrorf("failed to send the 403 response: %v", err)
}
return types.ActionPause
}
return types.ActionContinue
}