Skip to content

Instantly share code, notes, and snippets.

@yangfch3
Forked from TooTallNate/repl-client.js
Last active August 23, 2018 01:43
Show Gist options
  • Save yangfch3/cc58ce790ba489e930b49e1b9390ec7c to your computer and use it in GitHub Desktop.
Save yangfch3/cc58ce790ba489e930b49e1b9390ec7c to your computer and use it in GitHub Desktop.
最简单 REPL WebShell Node.js 实现 - 基于 net 与 repl 模块
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')
}
})
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