我正在尝试使用nwjs来建立客户端和服务器的FTP,并使用ftpd作为服务器,并使用basic-ftp作为客户端来建立彼此的连接。问题是:建立服务器后(使用ftpd提供的test.js示例),我无法使用basic-ftp进行连接(我正确配置了主机,用户,密码和端口,并在另一软件上进行了测试,并且可以正常工作)。
每次尝试时,node都会返回以下内容:
{"code":502,"message":"502 Command not implemented"}
客户代码:
const ftp = require("basic-ftp")
const fs = require("fs")
example()
async function example() {
const client = new ftp.Client()
try {
await client.access({
host: '192.168.0.7',
port: 7002,
user: "oi",
password: "oi",
secure: true
})
console.log(await client.list())
}
catch(err) {
console.log(err)
}
client.close()
}
服务器代码:
var ftpd = require('./example/ftpserver/ftpd.js');
var fs = require('fs');
var path = require('path');
var keyFile;
var certFile;
var server;
var options = {
host: process.env.IP || '127.0.0.1',
port: process.env.PORT || 7002,
tls: null,
};
if (process.env.KEY_FILE && process.env.CERT_FILE) {
console.log('Running as FTPS server');
if (process.env.KEY_FILE.charAt(0) !== '/') {
keyFile = path.join(__dirname, process.env.KEY_FILE);
}
if (process.env.CERT_FILE.charAt(0) !== '/') {
certFile = path.join(__dirname, process.env.CERT_FILE);
}
options.tls = {
key: fs.readFileSync(keyFile),
cert: fs.readFileSync(certFile),
ca: !process.env.CA_FILES ? null : process.env.CA_FILES
.split(':')
.map(function(f) {
return fs.readFileSync(f);
}),
};
} else {
console.log('*** To run as FTPS server, ***');
console.log('*** set "KEY_FILE", "CERT_FILE" ***');
console.log('*** and (optionally) "CA_FILES" env vars. ***');
}
server = new ftpd.FtpServer(options.host, {
getInitialCwd: function() {
return '/';
},
getRoot: function() {
return process.cwd();
},
pasvPortRangeStart: 1025,
pasvPortRangeEnd: 1050,
tlsOptions: options.tls,
allowUnauthorizedTls: true,
useWriteFile: false,
useReadFile: false,
uploadMaxSlurpSize: 7000, // N/A unless 'useWriteFile' is true.
});
server.on('error', function(error) {
console.log('FTP Server error:', error);
});
server.on('client:connected', function(connection) {
var username = 'oi';
console.log('client connected: ' + connection.remoteAddress);
connection.on('command:user', function(user, success, failure) {
if (user) {
username = user;
success();
} else {
failure();
}
});
connection.on('command:pass', function(pass, success, failure) {
if (pass) {
success(username);
} else {
failure();
}
});
});
server.debugging = 4;
server.listen(options.port);
console.log('Listening on port ' + options.port);