Skip to content

Commit 9c9a23f

Browse files
authored
Merge pull request #2 from web-tunnel/multiple-clients
2 parents 2fdeb2b + 9c66ed0 commit 9c9a23f

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,34 @@ Please replace your_local_server_port with your local HTTP server port, eg: `300
5757

5858
After that you can access your local HTTP server by access `your_public_server_domain`.
5959

60+
## Multiple Clients
61+
62+
The server steams HTTP request to WebSocket connection which has same host value in request headers.
63+
64+
So if you have multiple domains for the proxy server, you can have multiple clients.
65+
66+
For example, you have `https://app1.test.com` and `https://app2.test.com` for this proxy server.
67+
68+
In client 1:
69+
70+
```
71+
$ lite-http-tunnel config server https://app1.test.com -p app1
72+
$ lite-http-tunnel start your_local_server_port -p app1
73+
```
74+
75+
In client 2:
76+
77+
```
78+
$ lite-http-tunnel config server https://app2.test.com -p app2
79+
$ lite-http-tunnel start your_local_server_port -p app2
80+
```
81+
6082
## Related
6183

62-
A introduce article: [Building a HTTP Tunnel with WebSocket and Node.JS](https://medium.com/@embbnux/building-a-http-tunnel-with-websocket-and-node-js-98068b0225d3)
84+
A introduce article: [Building a HTTP Tunnel with WebSocket and Node.JS](https://medium.com/@embbnux/building-a-http-tunnel-with-websocket-and-node-js-98068b0225d3?source=friends_link&sk=985d90ec9f512928b34ed38b7ddcb378)
85+
86+
## TODO
87+
88+
- [ ] Add tests
89+
- [ ] Support multiple clients based on request path prefix
90+
- [ ] Support to stream WebSocket request

server.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ const app = express();
1313
const httpServer = http.createServer(app);
1414
const io = new Server(httpServer);
1515

16-
let connectedSocket = null;
16+
let connectedSockets = {};
1717

1818
io.use((socket, next) => {
19-
if (connectedSocket) {
20-
return next(new Error('Connected error'));
19+
const connectHost = socket.handshake.headers.host;
20+
if (connectedSockets[connectHost]) {
21+
return next(new Error(`${connectHost} has a existing connection`));
2122
}
2223
if (!socket.handshake.auth || !socket.handshake.auth.token){
2324
next(new Error('Authentication error'));
@@ -34,21 +35,22 @@ io.use((socket, next) => {
3435
});
3536

3637
io.on('connection', (socket) => {
37-
console.log('client connected');
38-
connectedSocket = socket;
38+
const connectHost = socket.handshake.headers.host;
39+
connectedSockets[connectHost] = socket;
40+
console.log(`client connected at ${connectHost}`);
3941
const onMessage = (message) => {
4042
if (message === 'ping') {
4143
socket.send('pong');
4244
}
4345
}
4446
const onDisconnect = (reason) => {
4547
console.log('client disconnected: ', reason);
46-
connectedSocket = null;
48+
delete connectedSockets[connectHost];
4749
socket.off('message', onMessage);
4850
socket.off('error', onError);
4951
};
5052
const onError = (e) => {
51-
connectedSocket = null;
53+
delete connectedSockets[connectHost];
5254
socket.off('message', onMessage);
5355
socket.off('disconnect', onDisconnect);
5456
};
@@ -78,6 +80,7 @@ app.get('/tunnel_jwt_generator', (req, res) => {
7880
});
7981

8082
app.use('/', (req, res) => {
83+
const connectedSocket = connectedSockets[req.headers.host];
8184
if (!connectedSocket) {
8285
res.status(404);
8386
res.send('Not Found');

0 commit comments

Comments
 (0)