-
-
Save yangfch3/cc58ce790ba489e930b49e1b9390ec7c to your computer and use it in GitHub Desktop.
最简单 REPL WebShell Node.js 实现 - 基于 net 与 repl 模块
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
var net = require('net') | |
var sock = net.connect(1337) | |
process.stdin.pipe(sock) | |
sock.pipe(process.stdout) | |
sock.on('connect', function () { | |
process.stdin.resume(); | |
process.stdin.setRawMode(true) | |
}) | |
sock.on('close', function done () { | |
process.stdin.setRawMode(false) | |
process.stdin.pause() | |
sock.removeListener('close', done) | |
}) | |
process.stdin.on('end', function () { | |
sock.destroy() | |
console.log() | |
}) | |
process.stdin.on('data', function (b) { | |
if (b.length === 1 && b[0] === 4) { | |
process.stdin.emit('end') | |
} | |
}) |
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
var repl = require('repl') | |
var net = require('net') | |
net.createServer(function (socket) { | |
var r = repl.start({ | |
prompt: 'socket '+socket.remoteAddress+':'+socket.remotePort+'> ' | |
, input: socket | |
, output: socket | |
, terminal: true | |
, useGlobal: false | |
}) | |
r.on('exit', function () { | |
socket.end() | |
}) | |
r.context.socket = socket | |
}).listen(1337) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment