-
Notifications
You must be signed in to change notification settings - Fork 13
/
connect.js
executable file
·88 lines (81 loc) · 2.32 KB
/
connect.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
var https = require('https');
var tunnel = require('./lib/tunnel');
var remoteHost = process.argv[2] || 'localhost';
var credentials, tunnels = [];
var shell = global.shell = require('./lib/shell');
shell.on('command', function(cmd, args) {
if (cmd == 'help') {
shell.echo('Commands:');
shell.echo('tunnel [localhost:]port [remotehost:]port');
shell.echo('close [tunnel-id]');
shell.echo('exit');
shell.prompt();
} else
if (cmd == 'tunnel') {
tunnel.createTunnel(remoteHost, credentials, args[0], args[1], function(err, server) {
if (err) {
shell.echo(String(err));
} else {
var id = tunnels.push(server);
shell.echo('Tunnel created with id: ' + id);
}
shell.prompt();
});
} else
if (cmd == 'close') {
var id = parseInt(args[0], 10) - 1;
if (tunnels[id]) {
tunnels[id].close();
tunnels[id] = null;
shell.echo('Tunnel ' + (id + 1) + ' closed.');
} else {
shell.echo('Invalid tunnel id.');
}
shell.prompt();
} else
if (cmd == 'exit') {
shell.exit();
} else {
shell.echo('Invalid command. Type `help` for more information.');
shell.prompt();
}
});
shell.echo('WebSocket Tunnel Console v0.1');
shell.echo('Remote Host: https://' + remoteHost);
authenticate(function() {
shell.prompt();
});
function authenticate(callback) {
shell.prompt('Username: ', function(user) {
shell.prompt('Password: ', function(pw) {
credentials = user + ':' + pw;
shell.echo('Authenticating ...');
checkAuth(function(success) {
if (success) {
shell.echo('Authenticated Successfully.');
callback();
} else {
shell.echo('Error: invalid credentials.');
authenticate(callback);
}
});
}, {passwordMode: true});
});
}
function checkAuth(callback) {
var encoded = new Buffer(String(credentials)).toString('base64');
var opts = {
host: remoteHost,
path: '/auth',
headers: {'Authorization': 'Basic ' + encoded}
};
var req = https.request(opts, function(res) {
callback(res.statusCode == 204);
});
req.on('error', function(err) {
shell.echo('Unable to authenticate.');
shell.echo(String(err));
shell.exit();
});
req.end();
}