-
Notifications
You must be signed in to change notification settings - Fork 10
/
requests_validator.go
84 lines (67 loc) · 1.84 KB
/
requests_validator.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
package plugins
import (
"github.com/BlockPILabs/aggregator/aggregator"
"github.com/BlockPILabs/aggregator/middleware"
"github.com/BlockPILabs/aggregator/rpc"
"strings"
)
var (
defaultWriteMethods = []string{
strings.ToLower("_call"),
strings.ToLower("_sendRawTransaction"),
strings.ToLower("_sendTransaction"),
strings.ToLower("_sendTransactionAsFeePayer"),
}
)
type RequestValidatorMiddleware struct {
nextMiddleware middleware.Middleware
enabled bool
}
func NewRequestValidatorMiddleware() *RequestValidatorMiddleware {
return &RequestValidatorMiddleware{enabled: true}
}
func (m *RequestValidatorMiddleware) Name() string {
return "RequestValidatorMiddleware"
}
func (m *RequestValidatorMiddleware) Enabled() bool {
return m.enabled
}
func (m *RequestValidatorMiddleware) Next() middleware.Middleware {
return m.nextMiddleware
}
func (m *RequestValidatorMiddleware) SetNext(middleware middleware.Middleware) {
m.nextMiddleware = middleware
}
func (m *RequestValidatorMiddleware) OnRequest(session *rpc.Session) error {
if session.Method == "OPTIONS" {
return aggregator.ErrMustReturn
}
//if session.Method != "POST" {
// return aggregator.ErrInvalidMethod
//}
session.IsWriteRpcMethod = m.isWriteMethod(session.RpcMethod())
return nil
}
func (m *RequestValidatorMiddleware) OnProcess(session *rpc.Session) error {
if session.Method == "OPTIONS" {
//if ctx, ok := session.RequestCtx.(*fasthttp.RequestCtx); ok {
//
//}
return aggregator.ErrMustReturn
}
return nil
}
func (m *RequestValidatorMiddleware) OnResponse(session *rpc.Session) error {
return nil
}
func (m *RequestValidatorMiddleware) isWriteMethod(method string) bool {
if len(method) > 0 {
method := strings.ToLower(method)
for _, m := range defaultWriteMethods {
if strings.HasSuffix(method, m) {
return true
}
}
}
return false
}