forked from ericbarch/socket-tunnel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
36 lines (29 loc) · 938 Bytes
/
client.js
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
// CONFIG
var REQUESTED_SUBDOMAIN = 'myname';
var TUNNEL_SERVER = 'http://mydomain.com';
var LOCAL_PORT = 80;
// require the things we need
var net = require('net');
var ss = require('socket.io-stream');
var socket = require('socket.io-client')(TUNNEL_SERVER);
socket.on('connect', function () {
console.log(new Date() + ': connected');
console.log(new Date() + ': requesting subdomain ' + REQUESTED_SUBDOMAIN + ' via ' + TUNNEL_SERVER);
socket.emit('createTunnel', REQUESTED_SUBDOMAIN);
});
socket.on('incomingClient', function (clientId) {
var client = net.connect(LOCAL_PORT, '127.0.0.1', function () {
var s = ss.createStream();
s.pipe(client).pipe(s);
s.on('end', function () {
client.destroy();
});
ss(socket).emit(clientId, s);
});
client.on('error', function () {
// handle connection refusal
var s = ss.createStream();
ss(socket).emit(clientId, s);
s.end();
});
});