From 464dc0b7e8bf619979f91a5ce696e9c46526edd0 Mon Sep 17 00:00:00 2001 From: Janos Beaumont Date: Mon, 4 Dec 2017 03:32:40 +0700 Subject: [PATCH 1/6] added execute and wait parameters --- bin/wscat | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) mode change 100755 => 100644 bin/wscat diff --git a/bin/wscat b/bin/wscat old mode 100755 new mode 100644 index 86ef345..70b5e21 --- a/bin/wscat +++ b/bin/wscat @@ -125,6 +125,8 @@ program .option('-c, --connect ', 'connect to a websocket server') .option('-p, --protocol ', 'optional protocol version') .option('-o, --origin ', 'optional origin') + .option('-x, --execute ', 'execute command after connecting') + .option('-w, --wait ', 'wait given seconds after executing command') .option('--host ', 'optional host') .option('-s, --subprotocol ', 'optional subprotocol') .option('-n, --no-check', 'Do not check for unauthorized certificates') @@ -134,7 +136,6 @@ program .option('--cert ', 'Specify a Client SSL Certificate (--connect only)') .option('--key ', 'Specify a Client SSL Certificate\'s key (--connect only)') .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.') - .option('--no-color', 'Run without color') .parse(process.argv); if (program.listen && program.connect) { @@ -214,12 +215,25 @@ if (program.listen && program.connect) { options.headers = headers; var ws = new WebSocket(connectUrl, options); + if (program.wait) { + var wait = program.wait*1000; + }else{ + var wait = 2000; + } + ws.on('open', function open() { wsConsole.print(Console.Types.Control, 'connected (press CTRL+C to quit)', Console.Colors.Green); - wsConsole.on('line', function line(data) { - ws.send(data); - wsConsole.prompt(); - }); + if(program.execute){ + ws.send(program.execute); + setTimeout(function () { + ws.close(); + }, wait) + }else{ + wsConsole.on('line', function line(data) { + ws.send(data); + wsConsole.prompt(); + }); + } }).on('close', function close() { wsConsole.print(Console.Types.Control, 'disconnected', Console.Colors.Green); wsConsole.clear(); From 34154fcae0c99065fd4428d0d96f133588085678 Mon Sep 17 00:00:00 2001 From: Janos Beaumont Date: Mon, 4 Dec 2017 04:27:20 +0700 Subject: [PATCH 2/6] fixed text output and colors --- bin/wscat | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/bin/wscat b/bin/wscat index 70b5e21..958f396 100644 --- a/bin/wscat +++ b/bin/wscat @@ -67,7 +67,7 @@ Console.prototype.print = function print(type, msg, color) { if (tty.isatty(1)) { this.clear(); color = color || Console.Colors.Default; - if (!program.color) color = ''; + if (program.nocolor) color = ''; this.stdout.write(color + type + msg + Console.Colors.Default + '\n'); this.prompt(); } else if (type === Console.Types.Incoming) { @@ -136,6 +136,7 @@ program .option('--cert ', 'Specify a Client SSL Certificate (--connect only)') .option('--key ', 'Specify a Client SSL Certificate\'s key (--connect only)') .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.') + .option('--no-color', 'Run without color') .parse(process.argv); if (program.listen && program.connect) { @@ -179,7 +180,11 @@ if (program.listen && program.connect) { }).on('error', function error(code, description) { wsConsole.print(Console.Types.Error, code + (description ? ' ' + description : ''), Console.Colors.Yellow); }).on('message', function message(data) { - wsConsole.print(Console.Types.Incoming, data, Console.Colors.Blue); + if(program.execute){ + wsConsole.print(Console.Types.Control, data, Console.Colors.Blue); + }else{ + wsConsole.print(Console.Types.Incoming, data, Console.Colors.Blue); + } }); }).on('error', function servererrror(error) { wsConsole.print(Console.Types.Error, error.message, Console.Colors.Yellow); @@ -222,20 +227,22 @@ if (program.listen && program.connect) { } ws.on('open', function open() { - wsConsole.print(Console.Types.Control, 'connected (press CTRL+C to quit)', Console.Colors.Green); if(program.execute){ ws.send(program.execute); setTimeout(function () { ws.close(); }, wait) }else{ + wsConsole.print(Console.Types.Control, 'connected (press CTRL+C to quit)', Console.Colors.Green); wsConsole.on('line', function line(data) { ws.send(data); wsConsole.prompt(); }); } }).on('close', function close() { - wsConsole.print(Console.Types.Control, 'disconnected', Console.Colors.Green); + if(!program.execute){ + wsConsole.print(Console.Types.Control, 'disconnected', Console.Colors.Green); + } wsConsole.clear(); process.exit(); }).on('error', function error(code, description) { From dc8c2062e15e2a8931374185fe3e615d9e63d6e4 Mon Sep 17 00:00:00 2001 From: Janos Beaumont Date: Mon, 4 Dec 2017 04:37:00 +0700 Subject: [PATCH 3/6] fixed color parameter --- bin/wscat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/wscat b/bin/wscat index 958f396..2ceed5c 100644 --- a/bin/wscat +++ b/bin/wscat @@ -136,7 +136,7 @@ program .option('--cert ', 'Specify a Client SSL Certificate (--connect only)') .option('--key ', 'Specify a Client SSL Certificate\'s key (--connect only)') .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.') - .option('--no-color', 'Run without color') + .option('--nocolor', 'Run without color') .parse(process.argv); if (program.listen && program.connect) { From fc6c0471d3bc4d58c3945ed5e5fad6c7ea209690 Mon Sep 17 00:00:00 2001 From: Janos Beaumont Date: Mon, 4 Dec 2017 18:40:37 +0700 Subject: [PATCH 4/6] cleaned up white spaces and indentation --- bin/wscat | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/bin/wscat b/bin/wscat index 2ceed5c..87e940e 100644 --- a/bin/wscat +++ b/bin/wscat @@ -180,9 +180,9 @@ if (program.listen && program.connect) { }).on('error', function error(code, description) { wsConsole.print(Console.Types.Error, code + (description ? ' ' + description : ''), Console.Colors.Yellow); }).on('message', function message(data) { - if(program.execute){ + if (program.execute){ wsConsole.print(Console.Types.Control, data, Console.Colors.Blue); - }else{ + } else { wsConsole.print(Console.Types.Incoming, data, Console.Colors.Blue); } }); @@ -222,17 +222,17 @@ if (program.listen && program.connect) { if (program.wait) { var wait = program.wait*1000; - }else{ + } else { var wait = 2000; } ws.on('open', function open() { - if(program.execute){ + if (program.execute) { ws.send(program.execute); setTimeout(function () { ws.close(); }, wait) - }else{ + } else { wsConsole.print(Console.Types.Control, 'connected (press CTRL+C to quit)', Console.Colors.Green); wsConsole.on('line', function line(data) { ws.send(data); @@ -240,9 +240,9 @@ if (program.listen && program.connect) { }); } }).on('close', function close() { - if(!program.execute){ + if (!program.execute) { wsConsole.print(Console.Types.Control, 'disconnected', Console.Colors.Green); - } + } wsConsole.clear(); process.exit(); }).on('error', function error(code, description) { From a0c5c700709a27a37b292346bd027e2d4c57b649 Mon Sep 17 00:00:00 2001 From: Janos Beaumont Date: Mon, 4 Dec 2017 18:50:19 +0700 Subject: [PATCH 5/6] reverted --no-color changes --- bin/wscat | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/wscat b/bin/wscat index 87e940e..878b89e 100644 --- a/bin/wscat +++ b/bin/wscat @@ -67,7 +67,7 @@ Console.prototype.print = function print(type, msg, color) { if (tty.isatty(1)) { this.clear(); color = color || Console.Colors.Default; - if (program.nocolor) color = ''; + if (!program.color) color = ''; this.stdout.write(color + type + msg + Console.Colors.Default + '\n'); this.prompt(); } else if (type === Console.Types.Incoming) { @@ -136,7 +136,7 @@ program .option('--cert ', 'Specify a Client SSL Certificate (--connect only)') .option('--key ', 'Specify a Client SSL Certificate\'s key (--connect only)') .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.') - .option('--nocolor', 'Run without color') + .option('--no-color', 'Run without color') .parse(process.argv); if (program.listen && program.connect) { From 6c9054a3fd9ad96f4a62f82594c05d5219c0ae98 Mon Sep 17 00:00:00 2001 From: Janos Beaumont Date: Tue, 5 Dec 2017 14:25:09 +0700 Subject: [PATCH 6/6] replaced an if/else with a one-liner --- bin/wscat | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/bin/wscat b/bin/wscat index 878b89e..75cab43 100644 --- a/bin/wscat +++ b/bin/wscat @@ -67,7 +67,8 @@ Console.prototype.print = function print(type, msg, color) { if (tty.isatty(1)) { this.clear(); color = color || Console.Colors.Default; - if (!program.color) color = ''; + if (!program.color || program.execute) color = ''; + if (program.execute) type = ''; this.stdout.write(color + type + msg + Console.Colors.Default + '\n'); this.prompt(); } else if (type === Console.Types.Incoming) { @@ -180,11 +181,7 @@ if (program.listen && program.connect) { }).on('error', function error(code, description) { wsConsole.print(Console.Types.Error, code + (description ? ' ' + description : ''), Console.Colors.Yellow); }).on('message', function message(data) { - if (program.execute){ - wsConsole.print(Console.Types.Control, data, Console.Colors.Blue); - } else { - wsConsole.print(Console.Types.Incoming, data, Console.Colors.Blue); - } + wsConsole.print(Console.Types.Incoming, data, Console.Colors.Blue); }); }).on('error', function servererrror(error) { wsConsole.print(Console.Types.Error, error.message, Console.Colors.Yellow); @@ -275,4 +272,4 @@ if (program.listen && program.connect) { } } else { program.help(); -} +} \ No newline at end of file