-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
117 lines (93 loc) · 2.99 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
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
package main
import (
"log"
"github.com/ikethecoder/proxy-wasm-go-filter-template/config"
"github.com/ikethecoder/proxy-wasm-go-filter-template/jq"
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
)
func main() {
proxywasm.SetVMContext(&myPluginVMContext{})
}
type (
myPluginVMContext struct {
types.DefaultVMContext
}
myPluginContext struct {
conf config.Config
types.DefaultPluginContext
}
myPluginHTTPContext struct {
conf *config.Config
types.DefaultHttpContext
}
)
// -----------------------------------------------------------------------------
// VM Context
// -----------------------------------------------------------------------------
//
// func (*myPluginVMContext) OnVMStart(vmConfigurationSize int) types.OnVMStartStatus {
// return types.OnVMStartStatusOK
// }
func (*myPluginVMContext) NewPluginContext(vmID uint32) types.PluginContext {
return &myPluginContext{}
}
// -----------------------------------------------------------------------------
// Plugin Context
// -----------------------------------------------------------------------------
func (ctx *myPluginContext) OnPluginStart(confSize int) types.OnPluginStartStatus {
data, err := proxywasm.GetPluginConfiguration()
if err != nil && err != types.ErrorStatusNotFound {
proxywasm.LogCriticalf("error reading plugin configuration: %v", err)
return types.OnPluginStartStatusFailed
}
err = config.Load(data, &ctx.conf)
if err != nil {
proxywasm.LogCriticalf("error parsing plugin configuration: %v", err)
return types.OnPluginStartStatusFailed
}
proxywasm.LogInfo("OnPluginStart from Go!")
return types.OnPluginStartStatusOK
}
func (ctx *myPluginContext) NewHttpContext(pluginID uint32) types.HttpContext {
return &myPluginHTTPContext{
conf: &ctx.conf,
}
}
// func (ctx *myPluginContext) OnTick() {
// proxywasm.LogInfo("[plugin] OnTick")
// }
//
// func (*myPluginContext) OnPluginDone() bool {
// return true
// }
//
// -----------------------------------------------------------------------------
// HTTP Context
// -----------------------------------------------------------------------------
// func (*myPluginHTTPContext) OnHttpRequestHeaders(int, bool) types.Action {
// return types.ActionContinue
// }
//
// func (*myPluginHTTPContext) OnHttpRequestBody(int, bool) types.Action {
// return types.ActionContinue
// }
func (ctx *myPluginHTTPContext) OnHttpResponseHeaders(int, bool) types.Action {
proxywasm.RemoveHttpResponseHeader("Content-Length")
return types.ActionContinue
}
func (ctx *myPluginHTTPContext) OnHttpResponseBody(int, bool) types.Action {
body, err := proxywasm.GetHttpResponseBody(0, 100000000)
if err != nil {
log.Fatalf("Failed to get response body")
}
parsedDocument, err := jq.ParseJQ(body, ctx.conf.Query)
if err != nil {
log.Fatalf("Failed to get response body")
}
proxywasm.ReplaceHttpResponseBody(parsedDocument)
return types.ActionContinue
}
//
// func (*myPluginHTTPContext) OnHttpStreamDone() {
// }