Skip to content

Commit d12cfbb

Browse files
committed
feat: 针对issue-12反馈问题,记录更详细的日志
1 parent 0fcddcd commit d12cfbb

File tree

1 file changed

+34
-40
lines changed

1 file changed

+34
-40
lines changed

src/main/java/top/meethigher/proxy/tcp/ReverseTcpProxy.java

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -48,47 +48,41 @@ protected ReverseTcpProxy(NetServer netServer, NetClient netClient,
4848
sourceSocket.pause();
4949
SocketAddress sourceRemote = sourceSocket.remoteAddress();
5050
SocketAddress sourceLocal = sourceSocket.localAddress();
51-
log.debug("{} <-- {} connected", sourceLocal, sourceRemote);
52-
sourceSocket.closeHandler(v -> log.debug("{} <-- {} closed", sourceLocal, sourceRemote));
53-
netClient.connect(targetPort, targetHost).onComplete(ar -> {
54-
if (ar.succeeded()) {
55-
NetSocket targetSocket = ar.result();
56-
targetSocket.pause();
57-
SocketAddress targetRemote = targetSocket.remoteAddress();
58-
SocketAddress targetLocal = targetSocket.localAddress();
59-
log.debug("{} --> {} connected", targetLocal, targetRemote);
60-
// feat: v1.0.5以前的版本,在closeHandler里面,将对端连接也关闭。比如targetSocket关闭时,则将sourceSocket也关闭。
61-
// 结果导致在转发短连接时,出现了bug。参考https://github.com/meethigher/tcp-reverse-proxy/issues/6
62-
targetSocket.closeHandler(v -> log.debug("{} --> {} closed", targetLocal, targetRemote));
63-
sourceSocket.pipeTo(targetSocket).onComplete(ar1 -> {
64-
if (ar1.succeeded()) {
65-
log.debug("pipeTo successful. {} --> {} --> {} --> {}",
66-
sourceRemote, sourceLocal, targetLocal, targetRemote);
67-
} else {
68-
log.error("pipeTo failed. {} --> {} --> {} --> {}",
69-
sourceRemote, sourceLocal, targetLocal, targetRemote,
70-
ar1.cause());
71-
}
51+
log.debug("source {} -- {} connected", sourceLocal, sourceRemote);
52+
sourceSocket.closeHandler(v -> log.debug("source {} -- {} closed", sourceLocal, sourceRemote));
53+
netClient.connect(targetPort, targetHost)
54+
.onFailure(e -> {
55+
log.error("failed to connect to {}:{}", targetHost, targetPort, e);
56+
// 若连接目标服务失败,需要断开源头服务
57+
sourceSocket.close();
58+
})
59+
.onSuccess(targetSocket -> {
60+
targetSocket.pause();
61+
SocketAddress targetRemote = targetSocket.remoteAddress();
62+
SocketAddress targetLocal = targetSocket.localAddress();
63+
log.debug("target {} -- {} connected", targetLocal, targetRemote);
64+
65+
// feat: v1.0.5以前的版本,在closeHandler里面,将对端连接也关闭。比如targetSocket关闭时,则将sourceSocket也关闭。
66+
// 结果导致在转发短连接时,出现了bug。参考https://github.com/meethigher/tcp-reverse-proxy/issues/6
67+
targetSocket.closeHandler(v -> log.debug("target {} -- {} closed", targetLocal, targetRemote));
68+
69+
// https://github.com/meethigher/tcp-reverse-proxy/issues/12
70+
// 将日志记录详细,便于排查问题
71+
sourceSocket.pipeTo(targetSocket)
72+
.onSuccess(v -> log.debug("source {} -- {} pipe to target {} -- {} succeeded",
73+
sourceLocal, sourceRemote, targetLocal, targetRemote))
74+
.onFailure(e -> log.error("source {} -- {} pipe to target {} -- {} failed",
75+
sourceLocal, sourceRemote, targetLocal, targetRemote, e));
76+
targetSocket.pipeTo(sourceSocket)
77+
.onSuccess(v -> log.debug("target {} -- {} pipe to source {} -- {} succeeded",
78+
targetLocal, targetRemote, sourceLocal, sourceRemote))
79+
.onFailure(e -> log.error("target {} -- {} pipe to source {} -- {} failed",
80+
targetLocal, targetRemote, sourceLocal, sourceRemote, e));
81+
log.debug("source {} -- {} bound to target {} -- {}",
82+
sourceLocal, sourceRemote, targetLocal, targetRemote);
83+
sourceSocket.resume();
84+
targetSocket.resume();
7285
});
73-
targetSocket.pipeTo(sourceSocket).onComplete(ar1 -> {
74-
if (ar1.succeeded()) {
75-
log.debug("pipeTo successful. {} <-- {} <-- {} <-- {}",
76-
sourceRemote, sourceLocal, targetLocal, targetRemote);
77-
} else {
78-
log.error("pipeTo failed. {} <-- {} <-- {} <-- {}",
79-
sourceRemote, sourceLocal, targetLocal, targetRemote,
80-
ar1.cause());
81-
}
82-
});
83-
sourceSocket.resume();
84-
targetSocket.resume();
85-
86-
} else {
87-
log.error("failed to connect to {}:{}", targetHost, targetPort, ar.cause());
88-
// 若连接目标服务失败,需要断开源头服务
89-
sourceSocket.close();
90-
}
91-
});
9286
};
9387
}
9488

0 commit comments

Comments
 (0)