-
Notifications
You must be signed in to change notification settings - Fork 4
/
dotnetclient.js
77 lines (65 loc) · 2.69 KB
/
dotnetclient.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
module.exports = function (data, callback) {
var net = require('net');
var urlParse = require('url').parse;
var WebSocket = require('hyco-websocket');
var WebSocketServer = require('hyco-websocket').relayedServer;
var wsServer = new WebSocketServer({
server: WebSocket.createRelayListenUri(data.ns, data.path),
token: function () {
return WebSocket.createRelayToken('https://' + data.ns, data.keyrule, data.key);
}
});
wsServer.on('request', function (request) {
var url = urlParse(request.resource, true);
var args = url.pathname.split('/').slice(1);
var action = args.shift();
var params = url.query;
data.onMessage(request, function(err, res){
});
console.log(request);
// if (action == 'tunnel') {
console.log('tunnel');
//createTunnel(request, params.port, params.host);
createTunnel(request, data.port, data.host);
/* } else {
request.reject(404);
}*/
});
function createTunnel(request, port, host) {
console.log('authentictted');
// if (!authenticate(request.httpRequest)) {
// request.reject(403);
// return;
// }
request.accept(null, null, null, function (webSock) {
console.log(webSock.remoteAddress + ' connected - Protocol Version ' + webSock.webSocketVersion);
var tcpSock = new net.Socket();
tcpSock.on('error', function (err) {
webSock.send(JSON.stringify({ status: 'error', details: 'Upstream socket error; ' + err }));
});
tcpSock.on('data', function (data) {
console.log('data');
webSock.send(data);
});
tcpSock.on('close', function () {
webSock.close();
});
console.log('connection on ' + host + ": " + port);
tcpSock.connect(port, host || '127.0.0.1', function () {
webSock.on('message', function (msg) {
if (msg.type === 'utf8') {
console.log('received utf message: ' + msg.utf8Data);
} else {
console.log('received binary message of length ' + msg.binaryData.length);
tcpSock.write(msg.binaryData);
}
});
webSock.send(JSON.stringify({ status: 'ready', details: 'Upstream socket connected' }));
});
webSock.on('close', function () {
tcpSock.destroy();
console.log(webSock.remoteAddress + ' disconnected');
});
});
}
}