Skip to content

Commit 8d9c7f0

Browse files
committed
feat: 调整日志格式
1 parent a814395 commit 8d9c7f0

File tree

2 files changed

+50
-30
lines changed

2 files changed

+50
-30
lines changed

src/main/java/top/meethigher/proxy/tcp/tunnel/ReverseTcpProxyTunnelClient.java

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -195,51 +195,61 @@ protected boolean doHandle(Vertx vertx, NetSocket netSocket, TunnelMessageType t
195195
dataSocket.write(Buffer.buffer()
196196
.appendBytes(DATA_CONN_FLAG)
197197
.appendInt(sessionId));
198-
log.debug("{}: data connection {} established, notify data proxy server of current session id {}. wait for backend connection",
198+
log.debug("{}: sessionId {}, data connection {} -- {} established. wait for backend connection",
199199
dataProxyName,
200-
dataSocket.remoteAddress(),
201-
sessionId);
200+
sessionId,
201+
dataSocket.remoteAddress(), dataSocket.localAddress());
202202
netClient.connect(backendPort, backendHost).onComplete(rst -> {
203203
if (rst.succeeded()) {
204204
atomicResult.set(rst.succeeded());
205205
final NetSocket backendSocket = rst.result();
206206
backendSocket.pause();
207-
log.debug("{}: backend connection {} established", dataProxyName, backendSocket.remoteAddress());
207+
log.debug("{}: sessionId {}, backend connection {} -- {} established", dataProxyName, sessionId, backendSocket.remoteAddress(), backendSocket.localAddress());
208208
// 双向生命周期绑定、双向数据转发
209209
// feat: v1.0.5以前的版本,在closeHandler里面,将对端连接也关闭。比如targetSocket关闭时,则将sourceSocket也关闭。
210210
// 结果导致在转发短连接时,出现了bug。参考https://github.com/meethigher/tcp-reverse-proxy/issues/6
211211
dataSocket.closeHandler(v -> {
212-
log.debug("{}: data connection {} closed", dataProxyName, dataSocket.remoteAddress());
212+
log.debug("{}: sessionId {}, data connection {} -- {} closed", dataProxyName, sessionId, dataSocket.remoteAddress(), dataSocket.localAddress());
213213
}).pipeTo(backendSocket).onFailure(e -> {
214-
log.error("{}: data connection {} pipe to backend connection {} failed, connection will be closed",
214+
log.error("{}: sessionId {}, data connection {} -- {} pipe to backend connection {} -- {} failed",
215215
dataProxyName,
216-
dataSocket.remoteAddress(), backendSocket.remoteAddress(), e);
216+
sessionId,
217+
dataSocket.remoteAddress(), dataSocket.localAddress(),
218+
backendSocket.remoteAddress(), backendSocket.localAddress(),
219+
e);
217220
});
218221
backendSocket.closeHandler(v -> {
219-
log.debug("{}: backend connection {} closed", dataProxyName, backendSocket.remoteAddress());
222+
log.debug("{}: sessionId {}, backend connection {} -- {} closed", dataProxyName, sessionId, backendSocket.remoteAddress(), backendSocket.localAddress());
220223
}).pipeTo(dataSocket).onFailure(e -> {
221-
log.error("{}: backend connection {} pipe to data connection {} failed, connection will be closed",
224+
log.error("{}: sessionId {}, backend connection {} -- {} pipe to data connection {} -- {} failed",
222225
dataProxyName,
223-
backendSocket.remoteAddress(), dataSocket.remoteAddress(), e);
226+
sessionId,
227+
backendSocket.remoteAddress(), backendSocket.localAddress(),
228+
dataSocket.remoteAddress(), dataSocket.localAddress(),
229+
e);
224230
});
225231
backendSocket.resume();
226232
dataSocket.resume();
227-
log.debug("{}: data connection {} bound to backend connection {} for session id {}",
233+
log.debug("{}: sessionId {}, data connection {} -- {} bound to backend connection {} -- {} for session id {}",
228234
dataProxyName,
229-
dataSocket.remoteAddress(),
230-
backendSocket.remoteAddress(),
235+
sessionId,
236+
dataSocket.remoteAddress(), dataSocket.localAddress(),
237+
backendSocket.remoteAddress(), backendSocket.localAddress(),
231238
sessionId);
232239
} else {
233240
// 建立连接失败,那么数据连接就要关闭
234241
dataSocket.close();
235-
log.error("{}: client open backend connection to {}:{} failed",
242+
log.error("{}: sessionId {}, client open backend connection to {}:{} failed",
236243
dataProxyName,
244+
sessionId,
237245
backendHost, backendPort, rst.cause());
238246
}
239247
latch.countDown();
240248
});
241249
} else {
242-
log.error("{}: client open data connection to {}:{} failed", dataProxyName, dataProxyHost, dataProxyPort, ar.cause());
250+
log.error("{}: sessionId {}, client open data connection to {}:{} failed", dataProxyName,
251+
sessionId,
252+
dataProxyHost, dataProxyPort, ar.cause());
243253
latch.countDown();
244254
}
245255

src/main/java/top/meethigher/proxy/tcp/tunnel/ReverseTcpProxyTunnelServer.java

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ protected void handleConnect(NetSocket socket) {
217217
* 第一种:用户建立连接后,主动发送数据请求,此时直接通过数据包即可判定用户连接还是数据连接。如HTTP
218218
* 第二种:用户建立连接后,等待服务端主动发送请求,此时就需要使用到延迟判定他是一个数据连接。如SSH
219219
*/
220+
log.debug("{}: connection {} -- {} established", name, socket.remoteAddress(), socket.localAddress());
220221
final long timerId = vertx.setTimer(judgeDelay, id -> handleUserConnection(socket, null, -1));
221222
// 创建缓冲区
222223
final Buffer buf = Buffer.buffer();
@@ -235,7 +236,7 @@ protected void handleConnect(NetSocket socket) {
235236
handleUserConnection(socket, buf, timerId);
236237
}
237238
});
238-
log.debug("{}: connection {} established, is it a data connection or user connection?", name, socket.remoteAddress());
239+
239240
socket.resume();
240241
}
241242

@@ -247,18 +248,18 @@ protected void handleConnect(NetSocket socket) {
247248
* @param timerId 延时判定数据连接的定时器id,-1表示不存在定时器
248249
*/
249250
protected void handleDataConnection(NetSocket socket, Buffer buf, long timerId) {
250-
log.debug("{}: oh, connection {} is a data connection!", name, socket.remoteAddress());
251251
// 取消延迟判定的逻辑
252252
if (timerId != -1) {
253253
vertx.cancelTimer(timerId);
254254
}
255255
// 数据连接
256256
int sessionId = buf.getInt(4);
257+
log.debug("{}: sessionId {}, connection {} -- {} is a data connection!", name, sessionId, socket.remoteAddress(), socket.localAddress());
257258
UserConnection userConn = unboundUserConnections.remove(sessionId);
258259
if (userConn != null) {
259260
bindConnections(userConn, socket, sessionId);
260261
} 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());
262263
socket.close();
263264
}
264265
}
@@ -271,20 +272,21 @@ protected void handleDataConnection(NetSocket socket, Buffer buf, long timerId)
271272
* @param timerId 延时判定数据连接的定时器id,-1表示不存在定时器
272273
*/
273274
protected void handleUserConnection(NetSocket socket, Buffer buf, long timerId) {
274-
log.debug("{}: oh, connection {} is a user connection!", name, socket.remoteAddress());
275275
// 取消延迟判定的逻辑
276276
if (timerId != -1) {
277277
vertx.cancelTimer(timerId);
278278
}
279279
// 用户连接
280280
int sessionId = IdGenerator.nextId();
281+
log.debug("{}: sessionId {}, connection {} -- {} is a user connection!", name, sessionId, socket.remoteAddress(), socket.localAddress());
281282
UserConnection userConn = new UserConnection(sessionId, socket, new ArrayList<>());
282283
if (buf != null) {
283284
userConn.buffers.add(buf.copy());
284285
}
285286
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());
288290
// 通过控制连接通知TunnelClient主动建立数据连接。服务端不需要通知客户端需要连接的端口,因为数据端口的启动是由客户端通知服务端开启的。
289291
controlSocket.write(TunnelMessageCodec.encode(TunnelMessageType.OPEN_DATA_CONN.code(),
290292
TunnelMessage.OpenDataConn.newBuilder().setSessionId(sessionId).build().toByteArray()));
@@ -303,23 +305,31 @@ protected void bindConnections(UserConnection userConn, NetSocket dataSocket, in
303305
// feat: v1.0.5以前的版本,在closeHandler里面,将对端连接也关闭。比如targetSocket关闭时,则将sourceSocket也关闭。
304306
// 结果导致在转发短连接时,出现了bug。参考https://github.com/meethigher/tcp-reverse-proxy/issues/6
305307
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());
307309
}).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);
310314
});
311315
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());
313320
}).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);
316325
});
317326
// 将用户连接中的缓存数据发出。
318327
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 {}",
320329
name,
321-
dataSocket.remoteAddress(),
322-
userSocket.remoteAddress(),
330+
sessionId,
331+
dataSocket.remoteAddress(), dataSocket.localAddress(),
332+
userSocket.remoteAddress(), userSocket.localAddress(),
323333
sessionId);
324334
}
325335

0 commit comments

Comments
 (0)