@@ -217,6 +217,7 @@ protected void handleConnect(NetSocket socket) {
217
217
* 第一种:用户建立连接后,主动发送数据请求,此时直接通过数据包即可判定用户连接还是数据连接。如HTTP
218
218
* 第二种:用户建立连接后,等待服务端主动发送请求,此时就需要使用到延迟判定他是一个数据连接。如SSH
219
219
*/
220
+ log .debug ("{}: connection {} -- {} established" , name , socket .remoteAddress (), socket .localAddress ());
220
221
final long timerId = vertx .setTimer (judgeDelay , id -> handleUserConnection (socket , null , -1 ));
221
222
// 创建缓冲区
222
223
final Buffer buf = Buffer .buffer ();
@@ -235,7 +236,7 @@ protected void handleConnect(NetSocket socket) {
235
236
handleUserConnection (socket , buf , timerId );
236
237
}
237
238
});
238
- log . debug ( "{}: connection {} established, is it a data connection or user connection?" , name , socket . remoteAddress ());
239
+
239
240
socket .resume ();
240
241
}
241
242
@@ -247,18 +248,18 @@ protected void handleConnect(NetSocket socket) {
247
248
* @param timerId 延时判定数据连接的定时器id,-1表示不存在定时器
248
249
*/
249
250
protected void handleDataConnection (NetSocket socket , Buffer buf , long timerId ) {
250
- log .debug ("{}: oh, connection {} is a data connection!" , name , socket .remoteAddress ());
251
251
// 取消延迟判定的逻辑
252
252
if (timerId != -1 ) {
253
253
vertx .cancelTimer (timerId );
254
254
}
255
255
// 数据连接
256
256
int sessionId = buf .getInt (4 );
257
+ log .debug ("{}: sessionId {}, connection {} -- {} is a data connection!" , name , sessionId , socket .remoteAddress (), socket .localAddress ());
257
258
UserConnection userConn = unboundUserConnections .remove (sessionId );
258
259
if (userConn != null ) {
259
260
bindConnections (userConn , socket , sessionId );
260
261
} else {
261
- log .debug ("{}: invalid session id {}, connection {} will be closed" , name , sessionId , socket .remoteAddress ());
262
+ log .debug ("{}: sessionId {}, invalid session id, connection {} -- {} will be closed" , name , sessionId , socket .remoteAddress (), socket . localAddress ());
262
263
socket .close ();
263
264
}
264
265
}
@@ -271,20 +272,21 @@ protected void handleDataConnection(NetSocket socket, Buffer buf, long timerId)
271
272
* @param timerId 延时判定数据连接的定时器id,-1表示不存在定时器
272
273
*/
273
274
protected void handleUserConnection (NetSocket socket , Buffer buf , long timerId ) {
274
- log .debug ("{}: oh, connection {} is a user connection!" , name , socket .remoteAddress ());
275
275
// 取消延迟判定的逻辑
276
276
if (timerId != -1 ) {
277
277
vertx .cancelTimer (timerId );
278
278
}
279
279
// 用户连接
280
280
int sessionId = IdGenerator .nextId ();
281
+ log .debug ("{}: sessionId {}, connection {} -- {} is a user connection!" , name , sessionId , socket .remoteAddress (), socket .localAddress ());
281
282
UserConnection userConn = new UserConnection (sessionId , socket , new ArrayList <>());
282
283
if (buf != null ) {
283
284
userConn .buffers .add (buf .copy ());
284
285
}
285
286
unboundUserConnections .put (sessionId , userConn );
286
- log .debug ("{}: user connection {} create session id {}, wait for data connection ..." ,
287
- name , socket .remoteAddress (), sessionId );
287
+ log .debug ("{}: sessionId {}, user connection {} -- {} wait for data connection ..." ,
288
+ name , sessionId ,
289
+ socket .remoteAddress (), socket .localAddress ());
288
290
// 通过控制连接通知TunnelClient主动建立数据连接。服务端不需要通知客户端需要连接的端口,因为数据端口的启动是由客户端通知服务端开启的。
289
291
controlSocket .write (TunnelMessageCodec .encode (TunnelMessageType .OPEN_DATA_CONN .code (),
290
292
TunnelMessage .OpenDataConn .newBuilder ().setSessionId (sessionId ).build ().toByteArray ()));
@@ -303,23 +305,31 @@ protected void bindConnections(UserConnection userConn, NetSocket dataSocket, in
303
305
// feat: v1.0.5以前的版本,在closeHandler里面,将对端连接也关闭。比如targetSocket关闭时,则将sourceSocket也关闭。
304
306
// 结果导致在转发短连接时,出现了bug。参考https://github.com/meethigher/tcp-reverse-proxy/issues/6
305
307
userSocket .closeHandler (v -> {
306
- log .debug ("{}: user connection {} closed" , name , userSocket .remoteAddress ());
308
+ log .debug ("{}: sessionId {}, user connection {} -- {} closed" , name , sessionId , userSocket .remoteAddress (), userSocket . localAddress ());
307
309
}).pipeTo (dataSocket ).onFailure (e -> {
308
- log .error ("{}: user connection {} pipe to data connection {} failed, connection will be closed" ,
309
- name , userSocket .remoteAddress (), dataSocket .remoteAddress (), e );
310
+ log .error ("{}: sessionId {}, user connection {} -- {} pipe to data connection {} -- {} failed, connection will be closed" ,
311
+ name ,
312
+ sessionId ,
313
+ userSocket .remoteAddress (), userSocket .localAddress (), dataSocket .remoteAddress (), dataSocket .localAddress (), e );
310
314
});
311
315
dataSocket .closeHandler (v -> {
312
- log .debug ("{}: data connection {} closed" , name , dataSocket .remoteAddress ());
316
+ log .debug ("{}: sessionId {}, data connection {} -- {} closed" ,
317
+ name ,
318
+ sessionId ,
319
+ dataSocket .remoteAddress (), dataSocket .localAddress ());
313
320
}).pipeTo (userSocket ).onFailure (e -> {
314
- log .error ("{}: data connection {} pipe to user connection {} failed, connection will be closed" ,
315
- name , dataSocket .remoteAddress (), userSocket .remoteAddress (), e );
321
+ log .error ("{}: sessionId {}, data connection {} -- {} pipe to user connection {} -- {} failed, connection will be closed" ,
322
+ name ,
323
+ sessionId ,
324
+ dataSocket .remoteAddress (), dataSocket .localAddress (), userSocket .remoteAddress (), userSocket .localAddress (), e );
316
325
});
317
326
// 将用户连接中的缓存数据发出。
318
327
userConn .buffers .forEach (dataSocket ::write );
319
- log .debug ("{}: data connection {} bound to user connection {} for session id {}" ,
328
+ log .debug ("{}: sessionId {}, data connection {} -- {} bound to user connection {} -- {} for session id {}" ,
320
329
name ,
321
- dataSocket .remoteAddress (),
322
- userSocket .remoteAddress (),
330
+ sessionId ,
331
+ dataSocket .remoteAddress (), dataSocket .localAddress (),
332
+ userSocket .remoteAddress (), userSocket .localAddress (),
323
333
sessionId );
324
334
}
325
335
0 commit comments