-
-
Save kubicek/718494 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .DS_Store | |
| *.log | |
| tmp/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [submodule "lib/socket.io"] | |
| path = lib/socket.io | |
| url = http://github.com/LearnBoost/Socket.IO.git | |
| [submodule "lib/socket.io-node"] | |
| path = lib/socket.io-node | |
| url = http://github.com/LearnBoost/Socket.IO-node.git |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>ssh.js</title> | |
| <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
| <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> | |
| <script src="/socket.io/socket.io.js"></script> | |
| <style> | |
| body | |
| { color: #1a2c37; | |
| font-family: 'Helvetica', sans-serif; font-size: 86%; | |
| padding: 2em; } | |
| #info | |
| { font-size: 120%; | |
| font-weight: bold; } | |
| #ssh | |
| { border: 1px solid #ccc; | |
| height: 300px; | |
| padding: 0.5em; | |
| overflow: hidden; | |
| position: relative; | |
| overflow-y: scroll; } | |
| #form { width: 782px; background: #333; padding: 5px 10px; display: block; } | |
| #form input[type=text] { width: 700px; padding: 5px; background: #fff; border: 1px solid #fff; } | |
| #form input[type=submit] { cursor: pointer; background: #999; border: none; padding: 6px 8px; -moz-border-radius: 8px; -webkit-border-radius: 8px; margin-left: 5px; text-shadow: 0 1px 0 #fff; } | |
| #form input[type=submit]:hover { background: #A2A2A2; } | |
| #form input[type=submit]:active { position: relative; top: 2px; } | |
| </style> | |
| </head> | |
| <body> | |
| <pre id="info"></pre> | |
| <pre id="announcement"></pre> | |
| <pre id="ssh"></pre> | |
| <form id="form" onsubmit="send(); return false"> | |
| <input type="text" autocomplete="off" id="text"><input type="submit" value="Send"> | |
| </form> | |
| <script> | |
| var Application = function() { | |
| var socket = new io.Socket(null, {port: 8000}); | |
| socket.connect(); | |
| var lines = 0; | |
| socket.on('connect', function() { | |
| console.log('Connected to:', socket.host); | |
| }); | |
| socket.on('message', function(message) { | |
| console.log('Received message:', message); | |
| if (message.servername) { | |
| $('#info').html( '$ server: ' + message.servername ); | |
| }; | |
| if (message.error) { | |
| $('#ssh').html( $('#ssh').html() +'<p><strong>ERR:</strong> ' + message.error + '</p>'); | |
| }; | |
| if (message.announcement) { | |
| $('#ssh').html( $('#ssh').html() +'<p><strong>' + message.announcement + '</strong></p>'); | |
| }; | |
| if (message.ssh) { | |
| $('#ssh').html( $('#ssh').html() + message.ssh ); | |
| lines++ | |
| $('#ssh').scrollTop(lines*1000) | |
| } | |
| }); | |
| return { | |
| socket : socket | |
| }; | |
| }; | |
| $(function() { var app = Application(); window.app = app;}); | |
| function send(){ | |
| var val = $('#text').val() | |
| window.app.socket.send(val); | |
| $('#text').val( '' ) | |
| } | |
| </script> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if(!this.JSON){JSON=function(){function f(n){return n<10?'0'+n:n;} | |
| Date.prototype.toJSON=function(){return this.getUTCFullYear()+'-'+ | |
| f(this.getUTCMonth()+1)+'-'+ | |
| f(this.getUTCDate())+'T'+ | |
| f(this.getUTCHours())+':'+ | |
| f(this.getUTCMinutes())+':'+ | |
| f(this.getUTCSeconds())+'Z';};var m={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'};function stringify(value,whitelist){var a,i,k,l,r=/["\\\x00-\x1f\x7f-\x9f]/g,v;switch(typeof value){case'string':return r.test(value)?'"'+value.replace(r,function(a){var c=m[a];if(c){return c;} | |
| c=a.charCodeAt();return'\\u00'+Math.floor(c/16).toString(16)+ | |
| (c%16).toString(16);})+'"':'"'+value+'"';case'number':return isFinite(value)?String(value):'null';case'boolean':case'null':return String(value);case'object':if(!value){return'null';} | |
| if(typeof value.toJSON==='function'){return stringify(value.toJSON());} | |
| a=[];if(typeof value.length==='number'&&!(value.propertyIsEnumerable('length'))){l=value.length;for(i=0;i<l;i+=1){a.push(stringify(value[i],whitelist)||'null');} | |
| return'['+a.join(',')+']';} | |
| if(whitelist){l=whitelist.length;for(i=0;i<l;i+=1){k=whitelist[i];if(typeof k==='string'){v=stringify(value[k],whitelist);if(v){a.push(stringify(k)+':'+v);}}}}else{for(k in value){if(typeof k==='string'){v=stringify(value[k],whitelist);if(v){a.push(stringify(k)+':'+v);}}}} | |
| return'{'+a.join(',')+'}';}} | |
| return{stringify:stringify,parse:function(text,filter){var j;function walk(k,v){var i,n;if(v&&typeof v==='object'){for(i in v){if(Object.prototype.hasOwnProperty.apply(v,[i])){n=walk(i,v[i]);if(n!==undefined){v[i]=n;}}}} | |
| return filter(k,v);} | |
| if(/^[\],:{}\s]*$/.test(text.replace(/\\./g,'@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']').replace(/(?:^|:|,)(?:\s*\[)+/g,''))){j=eval('('+text+')');return typeof filter==='function'?walk('',j):j;} | |
| throw new SyntaxError('parseJSON');}};}();} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // =================================== | |
| // `ssh` in Node.js and WebSockets | |
| // =================================== | |
| // | |
| // Usage: | |
| // | |
| // git clone git://gist.github.com/718494.git ssh.js | |
| // cd ssh.js | |
| // git submodule update --init | |
| // | |
| // node server.js username servername | |
| // | |
| // Connect with browser and work in ssh console | |
| // | |
| // | |
| require.paths.unshift('./lib/socket-io/lib/', | |
| './lib/socket.io-node/lib/'); | |
| var http = require('http'), | |
| io = require('socket.io'), | |
| fs = require('fs'), | |
| url = require('url'), | |
| util = require('util'); | |
| var spawn = require('child_process').spawn; | |
| var username = process.ARGV[2]; | |
| var servername = process.ARGV[3]; | |
| if (!servername) return util.puts("Usage: node <server.js> <username> <servername>"); | |
| // -- Node.js Server ---------------------------------------------------------- | |
| server = http.createServer(function(req, res){ | |
| // your normal server code | |
| var path = url.parse(req.url).pathname; | |
| switch (path){ | |
| case '/': | |
| res.writeHead(200, {'Content-Type': 'text/html'}); | |
| res.write('<h1>Welcome. Try the <a href="/index.html">SSH</a> console.</h1>'); | |
| res.end(); | |
| break; | |
| case '/json.js': | |
| case '/index.html': | |
| fs.readFile(__dirname + path, function(err, data){ | |
| if (err) return send404(res); | |
| res.writeHead(200, {'Content-Type': path == 'json.js' ? 'text/javascript' : 'text/html'}) | |
| res.write(data, 'utf8'); | |
| res.end(); | |
| }); | |
| break; | |
| default: send404(res); | |
| } | |
| }), | |
| send404 = function(res){ | |
| res.writeHead(404); | |
| res.write('404'); | |
| res.end(); | |
| }; | |
| server.listen(8000, '0.0.0.0'); | |
| // -- Setup Socket.IO --------------------------------------------------------- | |
| var io = io.listen(server); | |
| io.on('connection', function(client){ | |
| console.log('Client connected'); | |
| var ssh = connect() | |
| ssh.stdin.write("cd app/current\n") | |
| client.send( { servername : servername } ); | |
| client.send( { announcement : 'CONNECT 2400' }) | |
| ssh.stdin.write("echo \"Welcome to $HOSTNAME\"\n"); | |
| function connect() { | |
| return spawn('ssh', ['-T' , '-l' + username, servername]); | |
| } | |
| ssh.stdout.on("data", function (data) { | |
| console.log(data.toString('utf-8')) | |
| client.send( { ssh : data.toString('utf-8') } ) | |
| client.send( { ssh : "$ " } ) | |
| }); | |
| ssh.on('exit', function (code, signal) { | |
| client.send({ announcement : 'NO CARRIER' }) | |
| }); | |
| ssh.stderr.on('data', function (err) { | |
| client.send({ error : err + '' }) | |
| }); | |
| client.on('message', function(message){ | |
| console.log("command: "+message) | |
| client.send( { ssh : message+"\n" } ) | |
| ssh.stdin.write(message+"\n"); | |
| }); | |
| }); | |
| console.log('Server running at http://0.0.0.0:8000/, connect with a browser to see ssh console'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment