UNPKG

iocat

Version:

WebSocket netcat with Socket.io support

227 lines (205 loc) 7.33 kB
// Generated by CoffeeScript 1.10.0 (function() { var Program, Url, fs, program, bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, hasProp = {}.hasOwnProperty; fs = require('fs'); program = require('commander'); Url = require('./Url').Url; Program = (function() { function Program(options1) { var base; this.options = options1 != null ? options1 : {}; this.run = bind(this.run, this); this.runServer = bind(this.runServer, this); this.runClient = bind(this.runClient, this); this.initSIOServer = bind(this.initSIOServer, this); this.initWSServer = bind(this.initWSServer, this); this.initSIOClient = bind(this.initSIOClient, this); this.initWSClient = bind(this.initWSClient, this); this.initShell = bind(this.initShell, this); this.parseOptions = bind(this.parseOptions, this); if ((base = this.options).name == null) { base.name = 'iocat'; } this.initCommander(); return this; } Program.prototype.initCommander = function() { program.name = this.options.name; return program.version(Program.getVersion()).usage('[options] URL').option('-v, --verbose', 'verbose').option('-l, --listen', 'Start in listen mode, creating a server').option('-p, --local-port <port>', 'Specify local port for remote connections', parseInt).option('--socketio', 'Use socket.io').option('-k, --keep-listen', 'Keep inbound sockets open for multiple connects').option('-e, --emit-key <key>', 'Emit-key, default is "message"', "message"); }; Program.getVersion = function() { return JSON.parse(fs.readFileSync(__dirname + "/../package.json", 'utf8')).version; }; Program.prototype.parseOptions = function() { program.parse(process.argv); return extend(this.options, program); }; Program.prototype.initShell = function() { var Shell; Shell = require('./Shell').Shell; this.shell = new Shell; return this.shell.start(); }; Program.prototype.initWSClient = function(destString) { var WSClient, dest; WSClient = require('./WSClient').WSClient; dest = new Url(destString); this.client = new WSClient(dest, this.options); return this.client.start(); }; Program.prototype.initSIOClient = function(destString) { var SIOClient, dest; SIOClient = require('./SIOClient').SIOClient; dest = new Url(destString); this.client = new SIOClient(dest, this.options); return this.client.start(); }; Program.prototype.initWSServer = function() { var WSServer; WSServer = require('./WSServer').WSServer; this.server = new WSServer(this.options); return this.server.start(); }; Program.prototype.initSIOServer = function() { var SIOServer; SIOServer = require('./SIOServer').SIOServer; this.server = new SIOServer(this.options); return this.server.start(); }; Program.prototype.runClient = function(destString) { this.initShell(); if (this.options.socketio) { this.initSIOClient(destString); } else { this.initWSClient(destString); } this.shell.on('line', (function(_this) { return function(d) { return _this.client.send(d); }; })(this)); this.client.on('error', (function(_this) { return function(err) { console.log('client.on error'); return _this.shell.exit(0); }; })(this)); this.client.on('data', (function(_this) { return function(d) { _this.shell.stdin.pause(); _this.shell.send(d); return _this.shell.stdin.resume(); }; })(this)); this.client.on(this.options.emitKey, (function(_this) { return function(d) { return _this.shell.send(d); }; })(this)); this.client.on('close', (function(_this) { return function() { console.log('client.on close'); _this.shell.stdin.pause(); _this.shell.send("\nconnection closed by foreign host."); _this.shell.close(); return _this.shell.exit(0); }; })(this)); this.client.on('open', (function(_this) { return function() { return _this.shell.send("Connection to " + destString + " succeeded!"); }; })(this)); return this.shell.on('SIGINT', (function(_this) { return function() { console.log('shell.on SIGINT'); _this.shell.stdin.pause(); _this.shell.send("\nending session"); _this.shell.close(); _this.client.end(); return _this.shell.exit(0); }; })(this)); }; Program.prototype.runServer = function() { this.initShell(); if (this.options.socketio) { this.initSIOServer(); } else { this.initWSServer(); } this.shell.on('line', (function(_this) { return function(d) { return _this.server.send(d); }; })(this)); this.server.on('error', (function(_this) { return function(err) { console.log('server.on error'); return _this.shell.exit(0); }; })(this)); this.server.on(this.options.emitKey, (function(_this) { return function(d) { return _this.shell.send(d); }; })(this)); this.server.on('connection', (function(_this) { return function() { return console.log('New connection'); }; })(this)); this.server.on('close', (function(_this) { return function() { _this.shell.stdin.pause(); _this.shell.send("\nconnection closed by foreign host."); if (!_this.options.keepListen) { _this.shell.close(); return _this.shell.exit(0); } }; })(this)); return this.shell.on('SIGINT', (function(_this) { return function() { console.log('shell.on SIGINT'); _this.shell.stdin.pause(); _this.shell.send("\nending session"); _this.shell.close(); _this.server.end(); return _this.shell.exit(0); }; })(this)); }; Program.prototype.run = function() { var ref; this.parseOptions(); if (((ref = this.options.args) != null ? ref.length : void 0) === 1) { return this.runClient(this.options.args[0]); } else if (this.options.listen) { return this.runServer(); } else { return program.help(); } }; Program.create = function(options) { if (options == null) { options = {}; } return new Program(options); }; Program.run = function() { var prog; prog = Program.create(); return prog.run(); }; return Program; })(); module.exports = { Program: Program, create: Program.create, run: Program.run }; }).call(this);