pax_global_header00006660000000000000000000000064147012247010014510gustar00rootroot0000000000000052 comment=6b195a77838ec98664c36c9f7522c1fb97a1a7fa wscat-6.0.1/000077500000000000000000000000001470122470100126355ustar00rootroot00000000000000wscat-6.0.1/.gitignore000066400000000000000000000000341470122470100146220ustar00rootroot00000000000000node_modules/ npm-debug.log wscat-6.0.1/.npmignore000066400000000000000000000000031470122470100146250ustar00rootroot00000000000000.* wscat-6.0.1/.npmrc000066400000000000000000000000231470122470100137500ustar00rootroot00000000000000package-lock=false wscat-6.0.1/LICENSE000066400000000000000000000022371470122470100136460ustar00rootroot00000000000000Copyright (c) 2011 Einar Otto Stangvik Copyright (c) 2014 Arnout Kazemier and contributors Copyright (c) 2016 Luigi Pinca and contributors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. wscat-6.0.1/README.md000066400000000000000000000050241470122470100141150ustar00rootroot00000000000000# wscat WebSocket cat. ## Installation This module needs to be installed globally so use the `-g` flag when installing: ``` npm install -g wscat ``` ## Usage ``` Usage: wscat [options] (--listen | --connect ) Options: -V, --version output the version number --auth add basic HTTP authentication header --ca specify a Certificate Authority (--connect only) --cert specify a Client SSL Certificate (--connect only) --host optional host --key specify a Client SSL Certificate's key (--connect only) --max-redirects [num] maximum number of redirects allowed (default: 10) --no-color run without color --passphrase [passphrase] specify a Client SSL Certificate Key's passphrase (--connect only). If you don't provide a value, it will be prompted for --proxy <[protocol://]host[:port]> connect via a proxy. Proxy must support CONNECT method --slash enable slash commands for control frames (/ping [data], /pong [data], /close [code [, reason]]) (--connect only) -c, --connect connect to a WebSocket server -H, --header set an HTTP header. Repeat to set multiple (--connect only) (default: []) -l, --listen listen on port -L, --location follow redirects -n, --no-check do not check for unauthorized certificates (--connect only) -o, --origin optional origin -p, --protocol optional protocol version -P, --show-ping-pong print a notification when a ping or pong is received (--connect only) -s, --subprotocol optional subprotocol. Repeat to specify more than one (default: []) -w, --wait wait given seconds after executing command -x, --execute execute command after connecting (--connect only) -h, --help display help for command ``` ## Example ``` $ wscat -c ws://websocket-echo.com Connected (press CTRL+C to quit) > hi there < hi there > are you a happy parrot? < are you a happy parrot? ``` ## License [MIT](LICENSE) wscat-6.0.1/bin/000077500000000000000000000000001470122470100134055ustar00rootroot00000000000000wscat-6.0.1/bin/wscat000077500000000000000000000251731470122470100144640ustar00rootroot00000000000000#!/usr/bin/env node 'use strict'; const EventEmitter = require('events'); const fs = require('fs'); const readline = require('readline'); const tty = require('tty'); const WebSocket = require('ws'); const { HttpsProxyAgent } = require('https-proxy-agent'); const { program } = require('commander'); const { read } = require('read'); /** * InputReader - processes console input. * * @extends EventEmitter */ class Console extends EventEmitter { constructor() { super(); this.stdin = process.stdin; this.stdout = process.stdout; this.stderr = process.stderr; this.readlineInterface = readline.createInterface(this.stdin, this.stdout); this.readlineInterface .on('line', (data) => { this.emit('line', data); }) .on('close', () => { this.emit('close'); }); this._resetInput = () => { this.clear(); }; } static get Colors() { return { Red: '\u001b[31m', Green: '\u001b[32m', Yellow: '\u001b[33m', Blue: '\u001b[34m', Default: '\u001b[39m' }; } static get Types() { return { Incoming: '< ', Control: '', Error: 'error: ' }; } prompt() { this.readlineInterface.prompt(true); } print(type, msg, color) { if (tty.isatty(1)) { this.clear(); if (programOptions.execute) color = type = ''; else if (!programOptions.color) color = ''; this.stdout.write(color + type + msg + Console.Colors.Default + '\n'); this.prompt(); } else if (type === Console.Types.Incoming) { this.stdout.write(msg + '\n'); } else if (type === Console.Types.Error) { this.stderr.write(type + msg + '\n'); } else { // is a control message and we're not in a tty... drop it. } } clear() { if (tty.isatty(1)) { this.stdout.write('\r\u001b[2K\u001b[3D'); } } pause() { this.stdin.on('keypress', this._resetInput); } resume() { this.stdin.removeListener('keypress', this._resetInput); } } function collect(val, memo) { memo.push(val); return memo; } function noop() {} /** * The actual application */ const version = require('../package.json').version; program .version(version) .usage('[options] (--listen | --connect )') .option('--auth ', 'add basic HTTP authentication header') .option('--ca ', 'specify a Certificate Authority (--connect only)') .option('--cert ', 'specify a Client SSL Certificate (--connect only)') .option('--host ', 'optional host') .option( '--key ', "specify a Client SSL Certificate's key (--connect only)" ) .option( '--max-redirects [num]', 'maximum number of redirects allowed', parseInt, 10 ) .option('--no-color', 'run without color') .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( '--proxy <[protocol://]host[:port]>', 'connect via a proxy. Proxy must support CONNECT method' ) .option( '--slash', 'enable slash commands for control frames (/ping [data], /pong [data], ' + '/close [code [, reason]]) (--connect only)' ) .option('-c, --connect ', 'connect to a WebSocket server') .option( '-H, --header ', 'set an HTTP header. Repeat to set multiple (--connect only)', collect, [] ) .option('-l, --listen ', 'listen on port') .option('-L, --location', 'follow redirects') .option( '-n, --no-check', 'do not check for unauthorized certificates (--connect only)' ) .option('-o, --origin ', 'optional origin') .option('-p, --protocol ', 'optional protocol version') .option( '-P, --show-ping-pong', 'print a notification when a ping or pong is received (--connect only)' ) .option( '-s, --subprotocol ', 'optional subprotocol. Repeat to specify more than one', collect, [] ) .option('-w, --wait ', 'wait given seconds after executing command') .option( '-x, --execute ', 'execute command after connecting (--connect only)' ) .parse(process.argv); const programOptions = program.opts(); if (programOptions.listen && programOptions.connect) { console.error('\u001b[33merror: Use either --listen or --connect\u001b[39m'); process.exit(-1); } if (programOptions.listen) { const wsConsole = new Console(); wsConsole.pause(); let ws = null; const wss = new WebSocket.Server({ port: programOptions.listen }, () => { wsConsole.print( Console.Types.Control, `Listening on port ${programOptions.listen} (press CTRL+C to quit)`, Console.Colors.Green ); wsConsole.clear(); }); wsConsole.on('close', () => { if (ws) ws.close(); process.exit(0); }); wsConsole.on('line', (data) => { if (ws) { ws.send(data); wsConsole.prompt(); } }); wss.on('connection', (newClient) => { if (ws) return newClient.terminate(); ws = newClient; wsConsole.resume(); wsConsole.prompt(); wsConsole.print( Console.Types.Control, 'Client connected', Console.Colors.Green ); ws.on('close', (code, reason) => { wsConsole.print( Console.Types.Control, `Disconnected (code: ${code}, reason: "${reason}")`, Console.Colors.Green ); wsConsole.clear(); wsConsole.pause(); ws = null; }); ws.on('error', (err) => { wsConsole.print(Console.Types.Error, err.message, Console.Colors.Yellow); }); ws.on('message', (data) => { wsConsole.print(Console.Types.Incoming, data, Console.Colors.Blue); }); }); wss.on('error', (err) => { wsConsole.print(Console.Types.Error, err.message, Console.Colors.Yellow); process.exit(-1); }); } else if (programOptions.connect) { const options = {}; const cont = () => { const wsConsole = new Console(); const headers = programOptions.header.reduce((acc, cur) => { const i = cur.indexOf(':'); const key = cur.slice(0, i); const value = cur.slice(i + 1); acc[key] = value; return acc; }, {}); if (programOptions.auth) { headers.Authorization = 'Basic ' + Buffer.from(programOptions.auth).toString('base64'); } if (programOptions.host) headers.Host = programOptions.host; if (programOptions.protocol) options.protocolVersion = +programOptions.protocol; if (programOptions.origin) options.origin = programOptions.origin; if (!programOptions.check) options.rejectUnauthorized = programOptions.check; if (programOptions.ca) options.ca = fs.readFileSync(programOptions.ca); if (programOptions.cert) options.cert = fs.readFileSync(programOptions.cert); if (programOptions.key) options.key = fs.readFileSync(programOptions.key); if (programOptions.proxy) options.agent = new HttpsProxyAgent(programOptions.proxy); if (programOptions.location) options.followRedirects = true; options.maxRedirects = programOptions.maxRedirects; options.headers = headers; let connectUrl = programOptions.connect; if (!/^[a-z][a-z0-9.+-]*:/i.test(connectUrl)) { connectUrl = `ws://${connectUrl}`; } const ws = new WebSocket(connectUrl, programOptions.subprotocol, options); ws.on('open', () => { if (programOptions.execute) { ws.send(programOptions.execute); setTimeout( () => { ws.close(); }, programOptions.wait ? programOptions.wait * 1000 : 2000 ); } else { wsConsole.print( Console.Types.Control, 'Connected (press CTRL+C to quit)', Console.Colors.Green ); wsConsole.on('line', (data) => { if (programOptions.slash && data[0] === '/') { const toks = data.split(/\s+/); switch (toks[0].substr(1)) { case 'ping': if (toks.length >= 2) { ws.ping(toks[1]); } else { ws.ping(noop); } break; case 'pong': if (toks.length >= 2) { ws.pong(toks[1]); } else { 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(' '); } if (closeReason.length > 0) { ws.close(closeStatusCode, closeReason); } else { ws.close(closeStatusCode); } break; } default: wsConsole.print( Console.Types.Error, 'Unrecognized slash command.', Console.Colors.Yellow ); } } else { ws.send(data); } wsConsole.prompt(); }); } }); ws.on('close', (code, reason) => { if (!programOptions.execute) { wsConsole.print( Console.Types.Control, `Disconnected (code: ${code}, reason: "${reason}")`, Console.Colors.Green ); } wsConsole.clear(); process.exit(); }); ws.on('error', (err) => { wsConsole.print(Console.Types.Error, err.message, Console.Colors.Yellow); process.exit(-1); }); ws.on('message', (data) => { wsConsole.print(Console.Types.Incoming, data, Console.Colors.Blue); }); ws.on('ping', (data) => { if (programOptions.showPingPong) { wsConsole.print( Console.Types.Incoming, `Received ping (data: "${data}")`, Console.Colors.Blue ); } }); ws.on('pong', (data) => { if (programOptions.showPingPong) { wsConsole.print( Console.Types.Incoming, `Received pong (data: "${data}")`, Console.Colors.Blue ); } }); wsConsole.on('close', () => { ws.close(); process.exit(); }); }; if (programOptions.passphrase === true) { read({ prompt: 'Passphrase: ', silent: true, replace: '*' }).then((passphrase) => { options.passphrase = passphrase; cont(); }); } else if (typeof programOptions.passphrase === 'string') { options.passphrase = programOptions.passphrase; cont(); } else { cont(); } } else { program.help(); } wscat-6.0.1/package.json000066400000000000000000000011321470122470100151200ustar00rootroot00000000000000{ "name": "wscat", "version": "6.0.1", "description": "WebSocket cat", "main": "index.js", "scripts": { "test": "echo \"No test specified\"" }, "repository": { "type": "git", "url": "git+https://github.com/websockets/wscat.git" }, "keywords": [ "wscat", "websocket", "cat" ], "author": "Arnout Kazemier, Einar Otto Stangvik", "license": "MIT", "engines": { "node": ">=18" }, "dependencies": { "commander": "^12.1.0", "https-proxy-agent": "^7.0.5", "read": "^4.0.0", "ws": "^8.0.0" }, "bin": { "wscat": "bin/wscat" } }