95 lines
2.0 KiB
JavaScript
Executable File
95 lines
2.0 KiB
JavaScript
Executable File
'use strict';
|
|
|
|
var pkg = require('../package.json');
|
|
var remote;
|
|
var local;
|
|
var isPiped = !process.stdin.isTTY;
|
|
var localAddress;
|
|
var localPort;
|
|
var rejectUnauthorized;
|
|
var servername;
|
|
|
|
function usage() {
|
|
console.info("");
|
|
console.info("sclient.js v" + pkg.version);
|
|
console.info("Usage: sclient [--servername <string>] [-k | --insecure] <servername:port> <port>");
|
|
console.info(" ex: sclient whatever.com 3000");
|
|
console.info(" (whatever.com:443 localhost:3000)");
|
|
console.info(" ex: sclient whatever.com:4080 0.0.0.0:3000");
|
|
console.info("");
|
|
}
|
|
|
|
function parseFlags() {
|
|
process.argv.some(function (arg, i) {
|
|
if (/^-k|--?insecure$/.test(arg)) {
|
|
rejectUnauthorized = false;
|
|
process.argv.splice(i, 1);
|
|
return true;
|
|
}
|
|
});
|
|
process.argv.some(function (arg, i) {
|
|
if (/^--?servername$/.test(arg)) {
|
|
servername = process.argv[i + 1];
|
|
if (!servername || /^-/.test(servername)) {
|
|
usage();
|
|
process.exit(2);
|
|
}
|
|
process.argv.splice(i, 2);
|
|
return true;
|
|
}
|
|
});
|
|
}
|
|
|
|
parseFlags();
|
|
|
|
remote = (process.argv[2]||'').split(':');
|
|
local = (process.argv[3]||'').split(':');
|
|
|
|
// arg 0 is node
|
|
// arg 1 is sclient
|
|
// arg 2 is remote
|
|
// arg 3 is local
|
|
if (4 !== process.argv.length) {
|
|
if (isPiped) {
|
|
local = ['|'];
|
|
} else {
|
|
usage();
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
// Check for the first argument (what to connect to)
|
|
if (!remote[0]) {
|
|
usage();
|
|
return;
|
|
}
|
|
|
|
if (!local[0]) {
|
|
usage();
|
|
return;
|
|
}
|
|
if (local[0] === String(parseInt(local[0], 10))) {
|
|
localPort = parseInt(local[0], 10);
|
|
localAddress = 'localhost';
|
|
} else {
|
|
localAddress = local[0]; // potentially '-' or '|'
|
|
localPort = parseInt(local[1], 10);
|
|
}
|
|
|
|
if ('-' === localAddress || '|' === localAddress) {
|
|
// no need for port
|
|
} else if (!localPort) {
|
|
usage();
|
|
return;
|
|
}
|
|
|
|
var opts = {
|
|
remoteAddr: remote[0]
|
|
, remotePort: remote[1]
|
|
, localAddress: localAddress
|
|
, localPort: localPort
|
|
, rejectUnauthorized: rejectUnauthorized
|
|
, servername: servername
|
|
};
|
|
require('../')(opts);
|