-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
emulation.go
161 lines (141 loc) · 4.29 KB
/
emulation.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
154
155
156
157
158
159
160
161
package centrifuge
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"github.com/centrifugal/centrifuge/internal/readerpool"
"github.com/centrifugal/protocol"
)
// EmulationConfig is a config for EmulationHandler.
type EmulationConfig struct {
// MaxRequestBodySize limits request body size (in bytes). By default we accept 64kb max.
MaxRequestBodySize int
}
// EmulationHandler allows receiving client protocol commands from client and proxy
// them to the right node (where client session lives). This makes it possible to use
// unidirectional transports for server-to-clients data flow but still emulate
// bidirectional connection - thanks to this handler. Redirection to the correct node
// works over Survey.
type EmulationHandler struct {
node *Node
config EmulationConfig
emuLayer *emulationLayer
}
// NewEmulationHandler creates new EmulationHandler.
func NewEmulationHandler(node *Node, config EmulationConfig) *EmulationHandler {
return &EmulationHandler{
node: node,
config: config,
emuLayer: newEmulationLayer(node),
}
}
func (s *EmulationHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodOptions {
// For pre-flight browser requests.
rw.WriteHeader(http.StatusOK)
return
}
maxBytesSize := s.config.MaxRequestBodySize
if maxBytesSize == 0 {
maxBytesSize = 64 * 1024
}
r.Body = http.MaxBytesReader(rw, r.Body, int64(maxBytesSize))
data, err := io.ReadAll(r.Body)
if err != nil {
s.node.logger.log(newLogEntry(LogLevelInfo, "error reading emulation request body", map[string]any{"error": err.Error()}))
if len(data) >= maxBytesSize {
rw.WriteHeader(http.StatusRequestEntityTooLarge)
return
}
rw.WriteHeader(http.StatusInternalServerError)
return
}
var req protocol.EmulationRequest
if r.Header.Get("Content-Type") == "application/octet-stream" {
err = req.UnmarshalVT(data)
} else {
err = json.Unmarshal(data, &req)
}
if err != nil {
if s.node.LogEnabled(LogLevelInfo) {
s.node.logger.log(newLogEntry(LogLevelInfo, "can't unmarshal emulation request", map[string]any{"error": err.Error(), "data": string(data)}))
}
rw.WriteHeader(http.StatusBadRequest)
return
}
err = s.emuLayer.Emulate(&req)
if err != nil {
s.node.logger.log(newLogEntry(LogLevelError, "error processing emulation request", map[string]any{"req": &req, "error": err.Error()}))
if err == errNodeNotFound {
rw.WriteHeader(http.StatusNotFound)
} else {
rw.WriteHeader(http.StatusInternalServerError)
}
return
}
rw.WriteHeader(http.StatusNoContent)
}
type emulationLayer struct {
node *Node
}
func newEmulationLayer(node *Node) *emulationLayer {
return &emulationLayer{node: node}
}
func (l *emulationLayer) Emulate(req *protocol.EmulationRequest) error {
return l.node.sendEmulation(req)
}
const emulationOp = "centrifuge_emulation"
var errNodeNotFound = errors.New("node not found")
func (n *Node) sendEmulation(req *protocol.EmulationRequest) error {
_, ok := n.nodes.get(req.Node)
if !ok {
return errNodeNotFound
}
data, err := req.MarshalVT()
if err != nil {
return err
}
_, err = n.Survey(context.Background(), emulationOp, data, req.Node)
return err
}
type emulationSurveyHandler struct {
node *Node
}
func newEmulationSurveyHandler(node *Node) *emulationSurveyHandler {
return &emulationSurveyHandler{node: node}
}
func (h *emulationSurveyHandler) HandleEmulation(e SurveyEvent, cb SurveyCallback) {
var req protocol.EmulationRequest
err := req.UnmarshalVT(e.Data)
if err != nil {
h.node.logger.log(newLogEntry(LogLevelError, "error unmarshal emulation request", map[string]any{"data": string(e.Data), "error": err.Error()}))
cb(SurveyReply{Code: 1})
return
}
client, ok := h.node.Hub().clientBySession(req.Session)
if !ok {
cb(SurveyReply{Code: 2})
return
}
var data []byte
if client.transport.Protocol() == ProtocolTypeJSON {
var d string
err = json.Unmarshal(req.Data, &d)
if err != nil {
h.node.logger.log(newLogEntry(LogLevelError, "error unmarshal emulation request data", map[string]any{"data": string(req.Data), "error": err.Error()}))
cb(SurveyReply{Code: 3})
return
}
data = []byte(d)
} else {
data = req.Data
}
go func() {
reader := readerpool.GetBytesReader(data)
_ = HandleReadFrame(client, reader)
readerpool.PutBytesReader(reader)
cb(SurveyReply{})
}()
}