Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to execute multiple commands and keep an interactive console open #114

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 65 additions & 40 deletions bin/wscat
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,15 @@ program
.option('-c, --connect <url>', 'connect to a WebSocket server')
.option('-p, --protocol <version>', 'optional protocol version')
.option('-o, --origin <origin>', 'optional origin')
.option('-x, --execute <command>', 'execute command after connecting')
.option('-w, --wait <seconds>', 'wait given seconds after executing command')
.option('-x, --execute <command>',
'execute command after connecting. Repeat to set multiple',
collect, []
)
.option(
'--execute-interval <millis>',
'Optional interval in millis between sending subsequent commands. Initial delay for first command is 0 millis.'
)
.option('-w, --wait <seconds>', 'optional: wait given seconds after executing command before closing')
.option(
'-P, --show-ping-pong',
'print a notification when a ping or pong is received'
Expand All @@ -126,8 +133,7 @@ program
.option(
'-H, --header <header:value>',
'set an HTTP header. Repeat to set multiple (--connect only)',
collect,
[]
collect, []
)
.option(
'--auth <username:password>',
Expand All @@ -142,13 +148,17 @@ program
.option(
'--passphrase [passphrase]',
"specify a Client SSL Certificate Key's passphrase (--connect only). " +
"If you don't provide a value, it will be prompted for"
"If you don't provide a value, it will be prompted for"
)
.option(
'--non-interactive',
'do not open an interactive console after executing commands'
)
.option('--no-color', 'run without color')
.option(
'--slash',
'enable slash commands for control frames (/ping, /pong, /close ' +
'[code [, reason]])'
'[code [, reason]])'
)
.option(
'--proxy <[protocol://]host[:port]>',
Expand All @@ -166,7 +176,9 @@ if (program.listen) {
wsConsole.pause();

let ws = null;
const wss = new WebSocket.Server({ port: program.listen }, () => {
const wss = new WebSocket.Server({
port: program.listen
}, () => {
wsConsole.print(
Console.Types.Control,
`Listening on port ${program.listen} (press CTRL+C to quit)`,
Expand Down Expand Up @@ -240,6 +252,8 @@ if (program.listen) {
headers.Authorization =
'Basic ' + Buffer.from(program.auth).toString('base64');
}
if (program.execute) options.commands = program.execute;
options.interactive = !program.nonInteractive;
if (program.host) headers.Host = program.host;
if (program.protocol) options.protocolVersion = +program.protocol;
if (program.origin) options.origin = program.origin;
Expand All @@ -261,16 +275,29 @@ if (program.listen) {
options.headers = headers;
const ws = new WebSocket(connectUrl, program.subprotocol, options);

var scheduleSendCommand = (command, position) => {
setTimeout(
() => {
ws.send(command)
},
program.executeInterval ? program.executeInterval * position : 0
);
};

ws.on('open', () => {
if (program.execute) {
ws.send(program.execute);
setTimeout(
() => {
ws.close();
},
program.wait ? program.wait * 1000 : 2000
);
} else {
if (options.commands) {
options.commands.forEach(scheduleSendCommand);
if (program.wait) {
setTimeout(
() => {
ws.close();
},
program.wait * 1000
);
}
}

if (options.interactive) {
wsConsole.print(
Console.Types.Control,
'Connected (press CTRL+C to quit)',
Expand All @@ -287,22 +314,23 @@ if (program.listen) {
case 'pong':
ws.pong(noop);
break;
case 'close': {
let closeStatusCode = 1000;
let closeReason = '';
if (toks.length >= 2) {
closeStatusCode = parseInt(toks[1]);
}
if (toks.length >= 3) {
closeReason = toks.slice(2).join(' ');
case 'close':
{
let closeStatusCode = 1000;
let closeReason = '';
if (toks.length >= 2) {
closeStatusCode = parseInt(toks[1]);
}
if (toks.length >= 3) {
closeReason = toks.slice(2).join(' ');
}
if (closeReason.length > 0) {
ws.close(closeStatusCode, closeReason);
} else {
ws.close(closeStatusCode);
}
break;
}
if (closeReason.length > 0) {
ws.close(closeStatusCode, closeReason);
} else {
ws.close(closeStatusCode);
}
break;
}
default:
wsConsole.print(
Console.Types.Error,
Expand All @@ -319,13 +347,11 @@ if (program.listen) {
});

ws.on('close', (code, reason) => {
if (!program.execute) {
wsConsole.print(
Console.Types.Control,
`Disconnected (code: ${code}, reason: "${reason}")`,
Console.Colors.Green
);
}
wsConsole.print(
Console.Types.Control,
`Disconnected (code: ${code}, reason: "${reason}")`,
Console.Colors.Green
);
wsConsole.clear();
process.exit();
});
Expand Down Expand Up @@ -366,8 +392,7 @@ if (program.listen) {
};

if (program.passphrase === true) {
read(
{
read({
prompt: 'Passphrase: ',
silent: true,
replace: '*'
Expand Down