-
Notifications
You must be signed in to change notification settings - Fork 33
/
HttpTunnelClientDriver.java
437 lines (346 loc) · 12.4 KB
/
HttpTunnelClientDriver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*
* Copyright (c) 2000, 2017 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2022 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.messaging.jmq.httptunnel.tunnel.client;
import com.sun.messaging.jmq.httptunnel.tunnel.HttpTunnelConnection;
import com.sun.messaging.jmq.httptunnel.tunnel.HttpTunnelDriver;
import com.sun.messaging.jmq.httptunnel.tunnel.HttpTunnelPacket;
import com.sun.messaging.jmq.httptunnel.api.share.HttpTunnelDefaults;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Hashtable;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* This class provides unreliable packet delivery mechanism on the client side. It also uses a dedicated thread to
* continuously send HTTP pull requests to fetch packets sent by the server.
*/
public class HttpTunnelClientDriver extends Thread implements HttpTunnelDefaults, HttpTunnelDriver {
private static boolean DEBUG = Boolean.getBoolean("httptunnel.debug");
private boolean stopThread = false;
private String urlString = null;
private String urlParam = null;
private URL pushUrl = null;
private URL pullUrl = null;
private URLConnection uc = null;
private HttpTunnelPush pushWorker = null;
private int connId;
private HttpTunnelConnection conn = null;
private long lastConnectTime = 0;
private Logger logger = Logger.getLogger("Http Tunneling");
public HttpTunnelClientDriver(String urlString) {
int index = urlString.lastIndexOf('?');
if (index >= 0) {
this.urlString = urlString.substring(0, index);
urlParam = "&" + urlString.substring(index + 1);
} else {
this.urlString = urlString;
urlParam = "";
}
setName("HttpTunnelClientDriver");
setDaemon(true);
pushWorker = new HttpTunnelPush();
}
private void handleConnInitAck() {
if (conn != null) {
return;
}
conn = new HttpTunnelConnection(connId, this);
}
private void handleConnClose(HttpTunnelPacket p) {
if (conn == null) {
return;
}
conn.handleClose(p);
}
private void handleConnAbort(HttpTunnelPacket p) {
if (conn == null) {
return;
}
conn.handleAbort(p);
}
private void handleConnOption(HttpTunnelPacket p) {
if (conn == null) {
return;
}
conn.handleConnOption(p);
}
/**
* Handles data packets from the network.
*/
private void handlePacket(HttpTunnelPacket p, boolean moreData) {
if (conn == null) {
// TBD : Error - Tell the client ???
return;
}
conn.receivePacket(p, moreData);
}
private void handleDummyPacket(HttpTunnelPacket p) {
if (DEBUG) {
log("#### Received dummy packet :");
log(p + "\n");
}
}
/**
* Implements the connection establishment protocol.
*/
public HttpTunnelConnection doConnect() throws IOException {
URL connUrl = new URL(urlString + "?Type=connect" + urlParam);
HttpTunnelPacket p = new HttpTunnelPacket();
p.setPacketType(CONN_INIT_PACKET);
p.setPacketBody(null);
p.setConnId(0);
p.setSequence(0);
p.setWinsize(0);
p.setChecksum(0);
try {
HttpTunnelPacket resp = pushWorker.sendPacketDirect(connUrl, p, true);
connId = resp.getConnId();
String serverName = new String(resp.getPacketBody(), "UTF8");
String newurlParam = "&" + serverName;
if (!urlParam.equals("") && !newurlParam.equals(urlParam)) {
throw new IOException("Unexpected new ServerName: " + serverName);
}
if (urlParam.equals("")) {
urlParam = newurlParam;
}
pushUrl = new URL(urlString + "?Type=push" + urlParam);
pullUrl = new URL(urlString + "?Type=pull&ConnId=" + connId + urlParam);
while (conn == null) {
Vector v = pullPackets();
if (v == null || v.isEmpty()) {
continue;
}
HttpTunnelPacket ack = (HttpTunnelPacket) v.elementAt(0);
if (ack != null) {
switch (ack.getPacketType()) {
case CONN_SHUTDOWN:
throw new IOException("Connection refused");
case CONN_INIT_ACK:
handleConnInitAck();
break;
case CONN_CLOSE_PACKET:
handleConnClose(p);
break;
}
}
}
} catch (Exception e) {
String message = "Connection refused : ";
if (e instanceof EOFException) {
message = "Connection refused : " + "Make sure that the broker is running and " + "its HTTP service is active...";
}
// Concatenate the underlying exception's message.
if (e.getMessage() != null) {
message = message + e.getMessage();
}
ConnectException ce = new ConnectException(message);
try {
ce.initCause(e);
} catch (Throwable t) {
// e.g. NoSuchMethodError when running with ancient
// JDKs that do not have Throwable.initCause(). In
// this case we will not be able to provide the full
// stack trace of the underlying cause. The users will
// have to rely on the concatenated message string.
// Too bad. They should upgrade...
}
throw ce;
}
pushWorker.startPushThread(pushUrl);
start();
return conn;
}
/**
* Sends a packet.
*/
@Override
public void sendPacket(HttpTunnelPacket p) {
pushWorker.sendPacket(p);
if (DEBUG) {
log("Sending packet" + p);
}
}
/**
* Shutdown the driver. Stops accepting packets from the peer.
*/
@Override
public void shutdown(int connId) {
pushWorker.shutdown();
stopThread = true;
if ((uc != null) && uc instanceof HttpURLConnection) {
try {
((HttpURLConnection) uc).disconnect();
} catch (Throwable t) {
}
}
conn = null;
}
/**
* Send a HTTP pull request to fetch any packets sent by the server. The server-side driver can send multiple packets
* with each HTTP pull response.
*
* @return A <code>Vector</code> containing received packets.
*/
private Vector pullPackets() throws Exception {
Vector v = null;
int responseCode = HttpURLConnection.HTTP_OK;
try {
uc = pullUrl.openConnection();
uc.setDoInput(true);
uc.setDoOutput(false);
uc.setUseCaches(false);
uc.connect();
if (uc instanceof HttpURLConnection) {
responseCode = ((HttpURLConnection) uc).getResponseCode();
} else {
uc.getContentType();
// We don't really need the content type. This just forces
// the uc to do its job.
}
} catch (IOException e) {
handleHTTPConnectError();
throw e;
}
if (responseCode != HttpURLConnection.HTTP_OK) {
handleHTTPConnectError();
} else {
lastConnectTime = System.currentTimeMillis();
v = new Vector();
InputStream is = uc.getInputStream();
while (true) {
try {
HttpTunnelPacket p = new HttpTunnelPacket();
p.readPacket(is);
v.addElement(p);
} catch (Exception e) {
if (v.size() > 1) {
break;
}
if (v.size() == 1) {
HttpTunnelPacket p = (HttpTunnelPacket) v.elementAt(0);
if (p.getPacketType() != NO_OP_PACKET) {
break;
}
}
// The 'pull' request came back empty handed..
int pullPeriod = conn.getPullPeriod();
if (pullPeriod > 0) {
try {
Thread.sleep(pullPeriod * 1000L);
} catch (Exception se) {
}
break;
} else {
throw e;
}
}
}
is.close();
}
uc = null;
return v;
}
private void handleHTTPConnectError() {
if (conn.getConnectionTimeout() <= 0) {
return;
}
if (lastConnectTime == 0) {
return;
}
if ((System.currentTimeMillis() - lastConnectTime) > (conn.getConnectionTimeout() * 1000L)) {
// Abort the connection by generating a CONN_ABORT_PACKET.
HttpTunnelPacket p = new HttpTunnelPacket();
p.setPacketType(CONN_ABORT_PACKET);
p.setConnId(connId);
p.setSequence(0);
p.setWinsize(0);
p.setChecksum(0);
p.setPacketBody(null);
handleConnAbort(p);
}
}
private void handleHttpPullError() {
}
@Override
public void run() {
while (!stopThread) {
try {
Vector v = pullPackets();
if (v == null || v.isEmpty()) {
handleHttpPullError();
continue;
}
int i;
int j;
for (j = v.size() - 1; j >= 0; j--) {
HttpTunnelPacket p = (HttpTunnelPacket) v.elementAt(j);
if (p.getPacketType() == DATA_PACKET) {
break;
}
}
// Now j points to the last data packet in v
for (i = 0; i < v.size(); i++) {
HttpTunnelPacket p = (HttpTunnelPacket) v.elementAt(i);
if (DEBUG) {
log("Received packet:" + p);
}
switch (p.getPacketType()) {
case CONN_CLOSE_PACKET:
handleConnClose(p);
break;
case CONN_ABORT_PACKET:
handleConnAbort(p);
break;
case CONN_OPTION_PACKET:
handleConnOption(p);
break;
case DATA_PACKET:
case ACK:
handlePacket(p, (i != j));
// i == j is true for the last data packet in v
break;
case DUMMY_PACKET:
handleDummyPacket(p);
break;
default:
break;
}
// receivePacket(p);
}
} catch (Exception e) {
try {
Thread.sleep(1000);
} catch (Exception se) {
}
handleHttpPullError();
}
}
}
@Override
public Hashtable getDebugState() {
return new Hashtable();
}
private void log(String msg) {
logger.log(Level.INFO, msg);
}
}