forked from ericbarch/socket-tunnel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
90 lines (74 loc) · 2.74 KB
/
server.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
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
// config
var SERVER_PORT = 3000;
// libs
var bouncy = require('bouncy');
var tldjs = require('tldjs');
var ss = require('socket.io-stream');
var uuid = require('node-uuid');
// association between subdomains and socket.io sockets
var socketsByName = {};
// bounce incoming http requests to socket.io
var server = bouncy(function (req, res, bounce) {
// without a hostname, we won't know who the request is for
var hostname = req.headers.host;
if (!hostname) {
res.statusCode = 502;
return res.end('Invalid hostname');
}
// make sure we received a subdomain
var subdomain = tldjs.getSubdomain(hostname);
if (!subdomain) {
res.statusCode = 502;
return res.end('Invalid subdomain');
}
var clientId = subdomain.toLowerCase();
var client = socketsByName[clientId];
// no such subdomain
// we use a 502 error to the client to signify we can't service the request
if (!client) {
res.statusCode = 502;
res.end(clientId + ' is currently unregistered or offline.');
} else {
var requestGUID = uuid.v4();
client.emit('incomingClient', requestGUID);
ss(client).once(requestGUID, function (stream) {
bounce(stream);
});
}
});
// socket.io instance
var io = require('socket.io')(server);
io.on('connection', function (socket) {
socket.on('createTunnel', function (requestedName) {
if (socket.requestedName) {
// tunnel has already been created
return;
}
// domains are case insensitive
var reqNameNormalized = requestedName.toLowerCase();
// make sure the client is requesting an alphanumeric of reasonable length
if (/[^a-zA-Z0-9]/.test(reqNameNormalized) || reqNameNormalized.length === 0 || reqNameNormalized.length > 63) {
console.log(new Date() + ': ' + reqNameNormalized + ' -- bad subdomain. disconnecting client.');
return socket.disconnect();
}
// make sure someone else hasn't claimed this subdomain
if (socketsByName[reqNameNormalized]) {
console.log(new Date() + ': ' + reqNameNormalized + ' requested but already claimed. disconnecting client.');
return socket.disconnect();
}
// store a reference to this socket by the subdomain claimed
socketsByName[reqNameNormalized] = socket;
socket.requestedName = reqNameNormalized;
console.log(new Date() + ': ' + reqNameNormalized + ' registered successfully');
});
// when a client disconnects, we need to remove their association
socket.on('disconnect', function () {
if (socket.requestedName) {
delete socketsByName[socket.requestedName];
console.log(new Date() + ': ' + socket.requestedName + ' unregistered');
}
});
});
// http server
server.listen(SERVER_PORT);
console.log(new Date() + ': socket-tunnel server started on port ' + SERVER_PORT);