forked from mse-group/wasm-extensions-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
153 lines (143 loc) · 4.4 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
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
// Copyright (c) 2022 Alibaba Group Holding Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"errors"
"fmt"
"strings"
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
"github.com/tidwall/gjson"
"github.com/mse-group/wasm-extensions-go/pkg/wrapper"
)
func main() {
wrapper.SetCtx(
"request-block",
wrapper.ParseConfigBy(parseConfig),
wrapper.ProcessRequestHeadersBy(onHttpRequestHeaders),
wrapper.ProcessRequestBodyBy(onHttpRequestBody),
)
}
type RequestBlockConfig struct {
blockedCode uint32
blockedMessage string
caseSensitive bool
blockUrls []string
blockHeaders []string
blockBodys []string
}
func parseConfig(json gjson.Result, config *RequestBlockConfig, log wrapper.LogWrapper) error {
code := json.Get("blocked_code").Int()
if code != 0 && code > 100 && code < 600 {
config.blockedCode = uint32(code)
} else {
config.blockedCode = 403
}
config.blockedMessage = json.Get("blocked_message").String()
config.caseSensitive = json.Get("case_sensitive").Bool()
for _, item := range json.Get("block_urls").Array() {
url := item.String()
if url == "" {
continue
}
if config.caseSensitive {
config.blockUrls = append(config.blockUrls, url)
} else {
config.blockUrls = append(config.blockUrls, strings.ToLower(url))
}
}
for _, item := range json.Get("block_headers").Array() {
header := item.String()
if header == "" {
continue
}
if config.caseSensitive {
config.blockHeaders = append(config.blockHeaders, header)
} else {
config.blockHeaders = append(config.blockHeaders, strings.ToLower(header))
}
}
for _, item := range json.Get("block_bodys").Array() {
body := item.String()
if body == "" {
continue
}
if config.caseSensitive {
config.blockBodys = append(config.blockBodys, body)
} else {
config.blockBodys = append(config.blockBodys, strings.ToLower(body))
}
}
if len(config.blockUrls) == 0 && len(config.blockHeaders) == 0 &&
len(config.blockBodys) == 0 {
return errors.New("there is no block rules")
}
return nil
}
func onHttpRequestHeaders(contextID uint32, config RequestBlockConfig, needBody *bool, log wrapper.LogWrapper) types.Action {
if len(config.blockUrls) > 0 {
requestUrl, err := proxywasm.GetHttpRequestHeader(":path")
if err != nil {
log.Warnf("get path failed: %v", err)
return types.ActionContinue
}
if !config.caseSensitive {
requestUrl = strings.ToLower(requestUrl)
}
for _, blockUrl := range config.blockUrls {
if strings.Contains(requestUrl, blockUrl) {
proxywasm.SendHttpResponse(config.blockedCode, nil, []byte(config.blockedMessage), -1)
return types.ActionContinue
}
}
}
if len(config.blockHeaders) > 0 {
headers, err := proxywasm.GetHttpRequestHeaders()
if err != nil {
log.Warnf("get headers failed: %v", err)
return types.ActionContinue
}
var headerPairs []string
for _, kv := range headers {
headerPairs = append(headerPairs, fmt.Sprintf("%s\n%s", kv[0], kv[1]))
}
headerStr := strings.Join(headerPairs, "\n")
if !config.caseSensitive {
headerStr = strings.ToLower(headerStr)
}
for _, blockHeader := range config.blockHeaders {
if strings.Contains(headerStr, blockHeader) {
proxywasm.SendHttpResponse(config.blockedCode, nil, []byte(config.blockedMessage), -1)
return types.ActionContinue
}
}
}
if len(config.blockBodys) == 0 {
*needBody = false
}
return types.ActionContinue
}
func onHttpRequestBody(contextID uint32, config RequestBlockConfig, body []byte, log wrapper.LogWrapper) types.Action {
bodyStr := string(body)
if !config.caseSensitive {
bodyStr = strings.ToLower(bodyStr)
}
for _, blockBody := range config.blockBodys {
if strings.Contains(bodyStr, blockBody) {
proxywasm.SendHttpResponse(config.blockedCode, nil, []byte(config.blockedMessage), -1)
return types.ActionContinue
}
}
return types.ActionContinue
}