-
-
Save rushi216/d6886f7f54bbd02ae74c438a9b5badb1 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env node | |
/** | |
* term.js | |
* Copyright (c) 2012-2013, Christopher Jeffrey (MIT License) | |
*/ | |
var http = require('http') | |
, express = require('express') | |
, io = require('socket.io') | |
, pty = require('pty.js') | |
, terminal = require('term.js'); | |
/** | |
* term.js | |
*/ | |
process.title = 'JC'; | |
/** | |
* Dump | |
*/ | |
var stream; | |
if (process.argv[2] === '--dump') { | |
stream = require('fs').createWriteStream(__dirname + '/dump.log'); | |
} | |
/** | |
* Open Terminal | |
*/ | |
/** | |
* App & Server | |
*/ | |
var app = express() | |
, server = http.createServer(app); | |
app.use(function(req, res, next) { | |
var setHeader = res.setHeader; | |
res.setHeader = function(name) { | |
switch (name) { | |
case 'Cache-Control': | |
case 'Last-Modified': | |
case 'ETag': | |
return; | |
} | |
return setHeader.apply(res, arguments); | |
}; | |
next(); | |
}); | |
app.use(express.static(__dirname)); | |
app.use(terminal.middleware()); | |
if (!~process.argv.indexOf('-n')) { | |
server.on('connection', function(socket) { | |
var address = socket.remoteAddress; | |
if (false) { | |
try { | |
socket.destroy(); | |
} catch (e) { | |
; | |
} | |
console.log('Attempted connection from %s. Refused.', address); | |
} | |
}); | |
} | |
server.listen(8080); | |
/** | |
* Sockets | |
*/ | |
io = io.listen(server, { | |
log: false | |
}); | |
io.sockets.on('connection', function(socket) { | |
var buff = []; | |
var term = pty.fork(process.env.SHELL || 'sh', [], { | |
name: require('fs').existsSync('/usr/share/terminfo/x/xterm-256color') | |
? 'xterm-256color' | |
: 'xterm', | |
cols: 80, | |
rows: 24, | |
cwd: "/var/www/www/html/ProjectDir" | |
}); | |
term.on('data', function(data) { | |
if (stream) stream.write('OUT: ' + data + '\n-\n'); | |
return !socket | |
? buff.push(data) | |
: socket.emit('data', data); | |
}); | |
console.log('' | |
+ 'Created shell with pty master/slave' | |
+ ' pair (master: %d, pid: %d)', | |
term.fd, term.pid); | |
socket.on('data', function(data) { | |
if (stream) stream.write('IN: ' + data + '\n-\n'); | |
//console.log(JSON.stringify(data)); | |
term.write(data); | |
}); | |
socket.on('disconnect', function() { | |
socket = null; | |
}); | |
while (buff.length) { | |
socket.emit('data', buff.shift()); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment