远程 Shell 应用程序 Node.js
Remote Shell Application Node.js
我使用 .net 和 .readline 模块在 NodeJS 中编写了一个远程 Shell 应用程序(TCP 服务器) .客户端可以使用 Telnet 命令控制台连接到服务器。但是我在实现 rl.log 方法时遇到了问题,该方法不会在客户端 shell 中写入任何内容。其他文件(cmds_user.js、cmds_quiz、js、cmds_favs.js)不相关,它们在 运行 本地程序(没有 TCP 服务器实现)时完美运行。
const net = require('net');
const user = require("./cmds_user.js");
const quiz = require("./cmds_quiz.js");
const favs = require("./cmds_favs.js");
const readline = require('readline');
let port = 8080;
let server = net.createServer((socket) =>{
let rl = readline.createInterface({
input:socket,
output:socket,
prompt: ">"
});
socket.on('end',()=>{rl.close();});
socket.on('error',()=>{rl.close();});
rl.log = async (msg) => {
await rl.write(msg);
};
rl.prompt();
rl.questionP = async function (string) { // Add questionP to rl interface
return new Promise ( (resolve) => {
this.question(` ${string}: `, (answer) => resolve(answer.trim()))
})
};
rl.on('line', async (line) => {
try{
let cmd = line.trim()
if ('' ===cmd) {}
else if ('h' ===cmd) { user.help(rl);}
else if (['lu', 'ul', 'u'].includes(cmd)) { await user.list(rl);}
else if (['cu', 'uc'].includes(cmd)) { await user.create(rl);}
else if (['ru', 'ur', 'r'].includes(cmd)) { await user.read(rl);}
else if (['uu'].includes(cmd)) { await user.update(rl);}
else if (['du', 'ud'].includes(cmd)) { await user.delete(rl);}
else if (['lq', 'ql', 'q'].includes(cmd)) { await quiz.list(rl);}
else if (['cq', 'qc'].includes(cmd)) { await quiz.create(rl);}
else if (['tq', 'qt', 't'].includes(cmd)) { await quiz.test(rl);}
else if (['uq', 'qu'].includes(cmd)) { await quiz.update(rl);}
else if (['dq', 'qd'].includes(cmd)) { await quiz.delete(rl);}
else if (['lf', 'fl', 'f'].includes(cmd)) { await favs.list(rl);}
else if (['cf', 'fc'].includes(cmd)) { await favs.create(rl);}
else if (['df', 'fd'].includes(cmd)) { await favs.delete(rl);}
else if ('e'===cmd) { rl.log('Bye!'); socket.destroy()}
else { rl.log('UNSUPPORTED COMMAND!'); user.help(rl);}
} catch (err) {
rl.log(` ${err}`);
} finally {
rl.prompt();
}
});
});
server.listen(port);
解决方案:
rl.log = (msg) => {socket.write(`${msg}\n`);};
解决方案
rl.log = (msg) => {socket.write(`${msg}\n`);};
我使用 .net 和 .readline 模块在 NodeJS 中编写了一个远程 Shell 应用程序(TCP 服务器) .客户端可以使用 Telnet 命令控制台连接到服务器。但是我在实现 rl.log 方法时遇到了问题,该方法不会在客户端 shell 中写入任何内容。其他文件(cmds_user.js、cmds_quiz、js、cmds_favs.js)不相关,它们在 运行 本地程序(没有 TCP 服务器实现)时完美运行。
const net = require('net');
const user = require("./cmds_user.js");
const quiz = require("./cmds_quiz.js");
const favs = require("./cmds_favs.js");
const readline = require('readline');
let port = 8080;
let server = net.createServer((socket) =>{
let rl = readline.createInterface({
input:socket,
output:socket,
prompt: ">"
});
socket.on('end',()=>{rl.close();});
socket.on('error',()=>{rl.close();});
rl.log = async (msg) => {
await rl.write(msg);
};
rl.prompt();
rl.questionP = async function (string) { // Add questionP to rl interface
return new Promise ( (resolve) => {
this.question(` ${string}: `, (answer) => resolve(answer.trim()))
})
};
rl.on('line', async (line) => {
try{
let cmd = line.trim()
if ('' ===cmd) {}
else if ('h' ===cmd) { user.help(rl);}
else if (['lu', 'ul', 'u'].includes(cmd)) { await user.list(rl);}
else if (['cu', 'uc'].includes(cmd)) { await user.create(rl);}
else if (['ru', 'ur', 'r'].includes(cmd)) { await user.read(rl);}
else if (['uu'].includes(cmd)) { await user.update(rl);}
else if (['du', 'ud'].includes(cmd)) { await user.delete(rl);}
else if (['lq', 'ql', 'q'].includes(cmd)) { await quiz.list(rl);}
else if (['cq', 'qc'].includes(cmd)) { await quiz.create(rl);}
else if (['tq', 'qt', 't'].includes(cmd)) { await quiz.test(rl);}
else if (['uq', 'qu'].includes(cmd)) { await quiz.update(rl);}
else if (['dq', 'qd'].includes(cmd)) { await quiz.delete(rl);}
else if (['lf', 'fl', 'f'].includes(cmd)) { await favs.list(rl);}
else if (['cf', 'fc'].includes(cmd)) { await favs.create(rl);}
else if (['df', 'fd'].includes(cmd)) { await favs.delete(rl);}
else if ('e'===cmd) { rl.log('Bye!'); socket.destroy()}
else { rl.log('UNSUPPORTED COMMAND!'); user.help(rl);}
} catch (err) {
rl.log(` ${err}`);
} finally {
rl.prompt();
}
});
});
server.listen(port);
解决方案:
rl.log = (msg) => {socket.write(`${msg}\n`);};
解决方案
rl.log = (msg) => {socket.write(`${msg}\n`);};