-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathrpcinspector.go
125 lines (104 loc) · 2.9 KB
/
rpcinspector.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
package sigsci
import (
"fmt"
"net"
"net/rpc"
"time"
)
// RPCInspector is an Inspector implemented as RPC calls to the agent
type RPCInspector struct {
Network string
Address string
Timeout time.Duration
Debug bool
InitRPCClientFunc func() (*rpc.Client, error)
FiniRPCClientFunc func(*rpc.Client, error)
}
// ModuleInit sends a RPC.ModuleInit message to the agent
func (ri *RPCInspector) ModuleInit(in *RPCMsgIn, out *RPCMsgOut) error {
client, err := ri.GetRPCClient()
if err == nil {
err = client.Call("RPC.ModuleInit", in, out)
ri.CloseRPCClient(client, err)
}
if err != nil {
return fmt.Errorf("RPC.ModuleInit call failed: %s", err)
}
return nil
}
// PreRequest sends a RPC.PreRequest message to the agent
func (ri *RPCInspector) PreRequest(in *RPCMsgIn, out *RPCMsgOut) error {
client, err := ri.GetRPCClient()
if err == nil {
err = client.Call("RPC.PreRequest", in, out)
ri.CloseRPCClient(client, err)
}
if err != nil {
return fmt.Errorf("RPC.PreRequest call failed: %s", err)
}
return nil
}
// PostRequest sends a RPC.PostRequest message to the agent
func (ri *RPCInspector) PostRequest(in *RPCMsgIn, out *RPCMsgOut) error {
client, err := ri.GetRPCClient()
if err == nil {
var rpcout int
err = client.Call("RPC.PostRequest", in, &rpcout)
ri.CloseRPCClient(client, err)
// Always success as the rpcout is not currently used
out.WAFResponse = 200
out.RequestID = ""
out.RequestHeaders = nil
}
if err != nil {
return fmt.Errorf("RPC.PostRequest call failed: %s", err)
}
return nil
}
// UpdateRequest sends a RPC.UpdateRequest message to the agent
func (ri *RPCInspector) UpdateRequest(in *RPCMsgIn2, out *RPCMsgOut) error {
client, err := ri.GetRPCClient()
if err == nil {
var rpcout int
err = client.Call("RPC.UpdateRequest", in, &rpcout)
ri.CloseRPCClient(client, err)
// Always success as the rpcout is not currently used
out.WAFResponse = 200
out.RequestID = ""
out.RequestHeaders = nil
}
return err
}
// GetRPCClient gets a RPC client
func (ri *RPCInspector) GetRPCClient() (*rpc.Client, error) {
if ri.InitRPCClientFunc != nil {
return ri.InitRPCClientFunc()
}
conn, err := ri.getConnection()
if err != nil {
return nil, err
}
rpcCodec := NewMsgpClientCodec(conn)
return rpc.NewClientWithCodec(rpcCodec), nil
}
// CloseRPCClient closes a RPC client
func (ri *RPCInspector) CloseRPCClient(client *rpc.Client, err error) {
if ri.FiniRPCClientFunc != nil {
ri.FiniRPCClientFunc(client, err)
return
}
client.Close()
}
func (ri *RPCInspector) makeConnection() (net.Conn, error) {
deadline := time.Now().Add(ri.Timeout)
conn, err := net.DialTimeout(ri.Network, ri.Address, ri.Timeout)
if err != nil {
return nil, err
}
conn.SetDeadline(deadline)
return conn, nil
}
func (ri *RPCInspector) getConnection() (net.Conn, error) {
// here for future expansion to use pools, etc.
return ri.makeConnection()
}