@@ -39,14 +39,16 @@ func (c *ProxyClientConnection) send(pkt *agent.Packet) error {
39
39
stream := c .Grpc
40
40
return stream .Send (pkt )
41
41
} else if c .Mode == "http-connect" {
42
- if pkt .Type != agent .PacketType_DATA {
43
- return nil
42
+ if pkt .Type == agent .PacketType_CLOSE_RSP {
43
+ return c .Http .Close ()
44
+ } else if pkt .Type == agent .PacketType_DATA {
45
+ _ , err := c .Http .Write (pkt .GetData ().Data )
46
+ return err
47
+ } else {
48
+ return fmt .Errorf ("attempt to send via unrecognized connection type %v" , pkt .Type )
44
49
}
45
- writer := c .Http
46
- _ , err := writer .Write (pkt .GetData ().Data )
47
- return err
48
50
} else {
49
- return fmt .Errorf ("attempt to send via unrecognized connection type %q" , c .Mode )
51
+ return fmt .Errorf ("attempt to send via unrecognized connection mode %q" , c .Mode )
50
52
}
51
53
}
52
54
@@ -84,6 +86,7 @@ func (s *ProxyServer) Proxy(stream agent.ProxyService_ProxyServer) error {
84
86
close (recvCh )
85
87
}()
86
88
89
+ // Start goroutine to receive packets from frontend and push to recvCh
87
90
go func () {
88
91
for {
89
92
in , err := stream .Recv ()
@@ -92,7 +95,8 @@ func (s *ProxyServer) Proxy(stream agent.ProxyService_ProxyServer) error {
92
95
return
93
96
}
94
97
if err != nil {
95
- klog .Warningf ("stream read error: %v" , err )
98
+ klog .Warningf (">>> Stream read from frontend error: %v" , err )
99
+ close (stopCh )
96
100
return
97
101
}
98
102
@@ -104,57 +108,82 @@ func (s *ProxyServer) Proxy(stream agent.ProxyService_ProxyServer) error {
104
108
}
105
109
106
110
func (s * ProxyServer ) serveRecvFrontend (stream agent.ProxyService_ProxyServer , recvCh <- chan * agent.Packet ) {
107
- klog .Info ("start serve recv ..." )
111
+ klog .Info ("start serving frontend stream" )
112
+
113
+ var firstConnID int64
114
+
108
115
for pkt := range recvCh {
109
116
switch pkt .Type {
110
117
case agent .PacketType_DIAL_REQ :
111
- klog .Info ("received DIAL_REQ" )
118
+ klog .Info (">>> Received DIAL_REQ" )
112
119
if s .Backend == nil {
113
- klog .Info ("no backend found; drop" )
120
+ klog .Info (">>> No backend found; drop" )
114
121
continue
115
122
}
116
123
117
124
if err := s .Backend .Send (pkt ); err != nil {
118
- klog .Warningf ("send packet to Backend failed: %v" , err )
125
+ klog .Warningf (">>> DIAL_REQ to Backend failed: %v" , err )
119
126
}
120
127
s .PendingDial [pkt .GetDialRequest ().Random ] = & ProxyClientConnection {
121
128
Mode : "grpc" ,
122
129
Grpc : stream ,
123
130
connected : make (chan struct {}),
124
131
}
125
- klog .Info ("DIAL_REQ sent to backend" ) // got this. but backend didn't receive anything.
132
+ klog .Info (">>> DIAL_REQ sent to backend" ) // got this. but backend didn't receive anything.
126
133
127
134
case agent .PacketType_CLOSE_REQ :
128
- klog .Infof ("received CLOSE_REQ(id=%d)" , pkt .GetCloseRequest ().ConnectID )
135
+ klog .Infof (">>> Received CLOSE_REQ(id=%d)" , pkt .GetCloseRequest ().ConnectID )
129
136
if s .Backend == nil {
130
- klog .Info ("no backend found; drop" )
137
+ klog .Info (">>> No backend found; drop" )
131
138
continue
132
139
}
133
140
134
141
if err := s .Backend .Send (pkt ); err != nil {
135
- klog .Warningf ("send packet to Backend failed: %v" , err )
142
+ klog .Warningf (">>> CLOSE_REQ to Backend failed: %v" , err )
136
143
}
137
144
klog .Info ("CLOSE_REQ sent to backend" )
138
145
139
146
case agent .PacketType_DATA :
140
- klog .Infof ("received DATA(id=%d)" , pkt .GetData ().ConnectID )
147
+ connID := pkt .GetData ().ConnectID
148
+ klog .Infof (">>> Received DATA(id=%d)" , connID )
149
+ if firstConnID == 0 {
150
+ firstConnID = connID
151
+ } else if firstConnID != connID {
152
+ klog .Warningf (">>> Data(id=%d) doesn't match first connection id %d" , firstConnID , connID )
153
+ }
154
+
141
155
if s .Backend == nil {
142
- klog .Info ("no backend found; drop" )
156
+ klog .Info (">>> No backend found; drop" )
143
157
continue
144
158
}
145
159
146
160
if err := s .Backend .Send (pkt ); err != nil {
147
- klog .Warningf ("send packet to Backend failed: %v" , err )
161
+ klog .Warningf (">>> DATA to Backend failed: %v" , err )
148
162
}
149
- klog .Info ("DATA sent to backend" )
163
+ klog .Info (">>> DATA sent to backend" )
150
164
151
165
default :
152
- klog .Infof ("Ignore %v packet coming from frontend" , pkt .Type )
166
+ klog .Infof (">>> Ignore %v packet coming from frontend" , pkt .Type )
167
+ }
168
+ }
169
+
170
+ klog .Infof (">>> Close streaming (id=%d)" , firstConnID )
171
+
172
+ pkt := & agent.Packet {
173
+ Type : agent .PacketType_CLOSE_REQ ,
174
+ Payload : & agent.Packet_CloseRequest {
175
+ CloseRequest : & agent.CloseRequest {
176
+ ConnectID : firstConnID ,
177
+ },
178
+ },
179
+ }
180
+ if s .Backend != nil {
181
+ if err := s .Backend .Send (pkt ); err != nil {
182
+ klog .Warningf (">>> CLOSE_REQ to Backend failed: %v" , err )
153
183
}
154
184
}
155
185
}
156
186
157
- // Ignored now
158
187
func (s * ProxyServer ) serveSend (stream agent.ProxyService_ProxyServer , sendCh <- chan * agent.Packet ) {
159
188
klog .Info ("start serve send ..." )
160
189
for pkt := range sendCh {
@@ -206,40 +235,56 @@ func (s *ProxyServer) Connect(stream agent.AgentService_ConnectServer) error {
206
235
207
236
// route the packet back to the correct client
208
237
func (s * ProxyServer ) serveRecvBackend (stream agent.AgentService_ConnectServer , recvCh <- chan * agent.Packet ) {
238
+ var firstConnID int64
239
+
209
240
for pkt := range recvCh {
210
241
switch pkt .Type {
211
242
case agent .PacketType_DIAL_RSP :
212
243
resp := pkt .GetDialResponse ()
213
- klog .Warningf ("Received dial response for %d, connectID is %d" , resp .Random , resp .ConnectID )
244
+ firstConnID = resp .ConnectID
245
+ klog .Infof ("<<< Received DIAL_RSP(rand=%d, id=%d)" , resp .Random , resp .ConnectID )
246
+
214
247
if client , ok := s .PendingDial [resp .Random ]; ! ok {
215
- klog .Warning ("DialResp not recognized; dropped" )
248
+ klog .Warning ("<<< DialResp not recognized; dropped" )
216
249
} else {
217
250
err := client .send (pkt )
218
251
delete (s .PendingDial , resp .Random )
219
252
if err != nil {
220
- klog .Warningf ("dial response send to client stream error: %v" , err )
253
+ klog .Warningf ("<<< DIAL_RSP send to client stream error: %v" , err )
221
254
} else {
222
255
client .connectID = resp .ConnectID
223
256
s .Frontends [resp .ConnectID ] = client
224
257
close (client .connected )
225
258
}
226
259
}
260
+
227
261
case agent .PacketType_DATA :
228
262
resp := pkt .GetData ()
263
+ klog .Infof ("<<< Received DATA(id=%d)" , resp .ConnectID )
229
264
if client , ok := s .Frontends [resp .ConnectID ]; ok {
230
265
if err := client .send (pkt ); err != nil {
231
- klog .Warningf ("data send to client stream error: %v" , err )
266
+ klog .Warningf ("<<< DATA send to client stream error: %v" , err )
267
+ } else {
268
+ klog .Infof ("<<< DATA sent to frontend" )
232
269
}
233
270
}
271
+
234
272
case agent .PacketType_CLOSE_RSP :
235
273
resp := pkt .GetCloseResponse ()
274
+ klog .Infof ("<<< Received CLOSE_RSP(id=%d)" , resp .ConnectID )
236
275
if client , ok := s .Frontends [resp .ConnectID ]; ok {
237
276
if err := client .send (pkt ); err != nil {
238
- klog .Warningf ("close response send to client stream error: %v" , err )
277
+ // Normal when frontend closes it.
278
+ klog .Warningf ("<<< CLOSE_RSP send to client stream error: %v" , err )
279
+ } else {
280
+ klog .Infof ("<<< CLOSE_RSP sent to frontend" )
239
281
}
240
282
}
283
+
241
284
default :
242
- klog .Warningf ("unrecognized packet %+v" , pkt )
285
+ klog .Warningf ("<<< Unrecognized packet %+v" , pkt )
243
286
}
244
287
}
245
- }
288
+
289
+ klog .Infof ("<<< Close streaming (id=%d)" , firstConnID )
290
+ }
0 commit comments