MCSManager/ftpd/ftpserver.js

64 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-04-16 11:08:31 +08:00
//因为这库在旧版本节点上有问题,所以从 github 获取最新版本
//npm i https://github.com/nodeftpd/nodeftpd.git --save
2020-10-08 11:59:48 +08:00
var ftpdzz = require("ftpd");
2018-04-16 11:08:31 +08:00
2017-11-13 12:26:31 +08:00
var options = null;
var FTPserver = null;
2020-10-08 18:18:07 +08:00
// var rootCwdListener = () => {
// return "/";
// }; //默认
2017-11-13 12:26:31 +08:00
module.exports.initFTPdServerOptions = (_options) => {
options = _options;
2020-10-08 11:59:48 +08:00
};
2017-11-13 12:26:31 +08:00
module.exports.createFTPServer = (createOptions) => {
2018-04-16 11:08:31 +08:00
FTPserver = new ftpdzz.FtpServer(options.host, createOptions);
2020-10-08 11:59:48 +08:00
};
2017-11-13 12:26:31 +08:00
module.exports.initFTPServerListener = (config) => {
2020-10-08 11:59:48 +08:00
FTPserver.on("error", function (error) {
MCSERVER.infoLog("FTP RRROR".red, error, true);
config["errorFunc"] && config["errorFunc"]();
2017-11-13 12:26:31 +08:00
});
2020-10-08 11:59:48 +08:00
FTPserver.on("client:connected", function (connection) {
2017-11-13 12:26:31 +08:00
var username = null;
2020-10-08 11:59:48 +08:00
connection.on("command:user", function (user, success, failure) {
2017-11-13 12:26:31 +08:00
if (user.length >= 6) {
username = user;
success();
} else {
failure();
}
});
2020-10-08 11:59:48 +08:00
connection.on("command:pass", function (pswd, success, failure) {
2018-04-16 11:08:31 +08:00
if (!pswd || !username) {
failure();
return;
}
2017-11-13 12:26:31 +08:00
//通过外接函数
2020-10-08 11:59:48 +08:00
let result = config["loginCheck"](connection, username, pswd);
2017-11-13 12:26:31 +08:00
if (result) {
success(username);
} else {
failure();
}
});
});
2020-10-08 11:59:48 +08:00
};
2017-11-13 12:26:31 +08:00
module.exports.getFTPserver = () => {
return FTPserver;
2020-10-08 11:59:48 +08:00
};
2017-11-13 12:26:31 +08:00
module.exports.runFTPServer = () => {
FTPserver.debugging = 0;
FTPserver.listen(options.port);
2018-04-18 11:37:16 +08:00
2020-10-08 12:13:27 +08:00
MCSERVER.infoLog("FTP".green, [" FTP 被动传输端口范围: [", MCSERVER.localProperty.ftp_start_port, "-", MCSERVER.localProperty.ftp_end_port, "]"].join(" "));
2018-04-18 11:37:16 +08:00
2020-10-08 12:13:27 +08:00
MCSERVER.infoLog("FTP".green, " FTP 模块监听: [ ftp://" + (options.host || "127.0.0.1".yellow) + ":" + options.port + " ]");
2020-10-08 11:59:48 +08:00
};