38
38
public class ReverseTcpProxyTunnelClient extends TunnelClient {
39
39
private static final Logger log = LoggerFactory .getLogger (ReverseTcpProxyTunnelClient .class );
40
40
protected static final char [] ID_CHARACTERS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" .toCharArray ();
41
- protected static final long HEARTBEAT_DELAY_DEFAULT = 5000 ; // 毫秒
41
+
42
42
protected static final long MIN_DELAY_DEFAULT = 1000 ;// 毫秒
43
43
protected static final long MAX_DELAY_DEFAULT = 64000 ;// 毫秒
44
44
45
45
46
- protected final long heartbeatDelay ;
47
46
protected final String secret ;
48
47
protected final String name ;
49
48
50
-
49
+ protected long heartbeatDelay ;
51
50
protected String backendHost = "meethigher.top" ;
52
51
protected int backendPort = 22 ;
53
52
protected String dataProxyHost = "127.0.0.1" ;
@@ -75,10 +74,9 @@ protected static String generateName() {
75
74
}
76
75
77
76
protected ReverseTcpProxyTunnelClient (Vertx vertx , NetClient netClient ,
78
- long minDelay , long maxDelay , long heartbeatDelay ,
77
+ long minDelay , long maxDelay ,
79
78
String secret , String name ) {
80
79
super (vertx , netClient , minDelay , maxDelay );
81
- this .heartbeatDelay = heartbeatDelay ;
82
80
this .secret = secret ;
83
81
this .name = name ;
84
82
addMessageHandler ();
@@ -106,24 +104,25 @@ public ReverseTcpProxyTunnelClient dataProxyPort(int dataProxyPort) {
106
104
107
105
public ReverseTcpProxyTunnelClient dataProxyName (String dataProxyName ) {
108
106
this .dataProxyName = dataProxyName ;
107
+ super .name = this .dataProxyName ;
109
108
return this ;
110
109
}
111
110
112
- public static ReverseTcpProxyTunnelClient create (Vertx vertx , NetClient netClient , long minDelay , long maxDelay , long heartbeatDelay , String secret , String name ) {
113
- return new ReverseTcpProxyTunnelClient (vertx , netClient , minDelay , maxDelay , heartbeatDelay , secret , name );
111
+ public static ReverseTcpProxyTunnelClient create (Vertx vertx , NetClient netClient , long minDelay , long maxDelay , String secret , String name ) {
112
+ return new ReverseTcpProxyTunnelClient (vertx , netClient , minDelay , maxDelay , secret , name );
114
113
}
115
114
116
115
public static ReverseTcpProxyTunnelClient create (Vertx vertx , NetClient netClient , String secret ) {
117
- return new ReverseTcpProxyTunnelClient (vertx , netClient , MIN_DELAY_DEFAULT , MAX_DELAY_DEFAULT , HEARTBEAT_DELAY_DEFAULT , secret , generateName ());
116
+ return new ReverseTcpProxyTunnelClient (vertx , netClient , MIN_DELAY_DEFAULT , MAX_DELAY_DEFAULT , secret , generateName ());
118
117
}
119
118
120
119
121
120
public static ReverseTcpProxyTunnelClient create (Vertx vertx , NetClient netClient ) {
122
- return new ReverseTcpProxyTunnelClient (vertx , netClient , MIN_DELAY_DEFAULT , MAX_DELAY_DEFAULT , HEARTBEAT_DELAY_DEFAULT , SECRET_DEFAULT , generateName ());
121
+ return new ReverseTcpProxyTunnelClient (vertx , netClient , MIN_DELAY_DEFAULT , MAX_DELAY_DEFAULT , SECRET_DEFAULT , generateName ());
123
122
}
124
123
125
124
public static ReverseTcpProxyTunnelClient create (Vertx vertx ) {
126
- return new ReverseTcpProxyTunnelClient (vertx , vertx .createNetClient (), MIN_DELAY_DEFAULT , MAX_DELAY_DEFAULT , HEARTBEAT_DELAY_DEFAULT , SECRET_DEFAULT , generateName ());
125
+ return new ReverseTcpProxyTunnelClient (vertx , vertx .createNetClient (), MIN_DELAY_DEFAULT , MAX_DELAY_DEFAULT , SECRET_DEFAULT , generateName ());
127
126
}
128
127
129
128
@@ -135,6 +134,7 @@ protected void addMessageHandler() {
135
134
this .onConnected ((vertx , netSocket , buffer ) -> netSocket .write (encode (TunnelMessageType .OPEN_DATA_PORT ,
136
135
TunnelMessage .OpenDataPort .newBuilder ()
137
136
.setSecret (secret )
137
+ .setDataProxyHost (dataProxyHost )
138
138
.setDataProxyPort (dataProxyPort )
139
139
.setDataProxyName (dataProxyName )
140
140
.build ().toByteArray ())));
@@ -149,6 +149,7 @@ protected boolean doHandle(Vertx vertx, NetSocket netSocket, TunnelMessageType t
149
149
if (parsed .getSuccess ()) {
150
150
// 如果认证 + 开通端口成功,那么就需要进行长连接保持,并开启定期心跳。
151
151
result = true ;
152
+ heartbeatDelay = parsed .getHeartbeatDelay ();
152
153
vertx .setTimer (heartbeatDelay , id -> netSocket .write (encode (TunnelMessageType .HEARTBEAT ,
153
154
TunnelMessage .Heartbeat .newBuilder ().setTimestamp (System .currentTimeMillis ()).build ().toByteArray ())));
154
155
} else {
@@ -186,45 +187,55 @@ protected boolean doHandle(Vertx vertx, NetSocket netSocket, TunnelMessageType t
186
187
if (ar .succeeded ()) {
187
188
final NetSocket dataSocket = ar .result ();
188
189
dataSocket .pause ();
190
+ // 连接建立成功后,立马发送消息告诉数据服务,我是数据连接,并与用户连接进行绑定
191
+ dataSocket .write (Buffer .buffer ()
192
+ .appendBytes (DATA_CONN_FLAG )
193
+ .appendInt (sessionId ));
194
+ log .debug ("{}: data connection {} established, notify data proxy server of current session id {}. wait for backend connection" ,
195
+ dataProxyName ,
196
+ dataSocket .remoteAddress (),
197
+ sessionId );
189
198
netClient .connect (backendPort , backendHost ).onComplete (rst -> {
190
199
if (rst .succeeded ()) {
191
200
atomicResult .set (rst .succeeded ());
192
201
final NetSocket backendSocket = rst .result ();
193
202
backendSocket .pause ();
194
- dataSocket .write (Buffer .buffer ()
195
- .appendBytes (DATA_CONN_FLAG )
196
- .appendInt (sessionId ));
203
+ log .debug ("{}: backend connection {} established" , dataProxyName , backendSocket .remoteAddress ());
197
204
// 双向生命周期绑定、双向数据转发
198
205
dataSocket .closeHandler (v -> {
199
- log .debug ("data connection {} closed" , dataSocket .remoteAddress ());
206
+ log .debug ("{}: data connection {} closed" , dataProxyName , dataSocket .remoteAddress ());
200
207
backendSocket .close ();
201
208
}).pipeTo (backendSocket ).onFailure (e -> {
202
- log .error ("data connection {} pipe to backend connection {} failed, connection will be closed" ,
209
+ log .error ("{}: data connection {} pipe to backend connection {} failed, connection will be closed" ,
210
+ dataProxyName ,
203
211
dataSocket .remoteAddress (), backendSocket .remoteAddress (), e );
204
212
dataSocket .close ();
205
213
});
206
214
backendSocket .closeHandler (v -> {
207
- log .debug ("backend connection {} closed" , backendSocket .remoteAddress ());
215
+ log .debug ("{}: backend connection {} closed" , dataProxyName , backendSocket .remoteAddress ());
208
216
dataSocket .close ();
209
217
}).pipeTo (dataSocket ).onFailure (e -> {
210
- log .error ("backend connection {} pipe to data connection {} failed, connection will be closed" ,
218
+ log .error ("{}: backend connection {} pipe to data connection {} failed, connection will be closed" ,
219
+ dataProxyName ,
211
220
backendSocket .remoteAddress (), dataSocket .remoteAddress (), e );
212
221
backendSocket .close ();
213
222
});
214
223
backendSocket .resume ();
215
224
dataSocket .resume ();
216
- log .debug ("data connection {} bound to backend connection {} for session id {}" ,
225
+ log .debug ("{}: data connection {} bound to backend connection {} for session id {}" ,
226
+ dataProxyName ,
217
227
dataSocket .remoteAddress (),
218
228
backendSocket .remoteAddress (),
219
229
sessionId );
220
230
} else {
221
- log .error ("client open backend connection to {}:{} failed" ,
231
+ log .error ("{}: client open backend connection to {}:{} failed" ,
232
+ dataProxyName ,
222
233
backendHost , backendPort , rst .cause ());
223
234
}
224
235
latch .countDown ();
225
236
});
226
237
} else {
227
- log .error ("client open data connection to {}:{} failed" , dataProxyHost , dataProxyPort , ar .cause ());
238
+ log .error ("{}: client open data connection to {}:{} failed" , dataProxyName , dataProxyHost , dataProxyPort , ar .cause ());
228
239
latch .countDown ();
229
240
}
230
241
0 commit comments