This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ftp.js
69 lines (57 loc) · 2 KB
/
ftp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const FtpSrv = require("ftp-srv");
const { Netmask } = require("netmask");
const { networkInterfaces } = require('os');
const nets = networkInterfaces();
const user = "user";
const pw = "pw";
const host = process.argv[2] || "localhost";
const port = process.argv[3] || 21;
const getNetworks = () => {
let networks = {};
for (const name of Object.keys(nets)) {
for (const net of nets[name]) {
if (net.family === 'IPv4' && !net.internal) {
networks[net.address + "/24"] = net.address
}
}
}
return networks;
};
const resolverFunction = (ip) => {
const networks = getNetworks();
for (const network in networks) {
if (new Netmask(network).contains(ip)) {
return networks[network];
}
}
return "0.0.0.0";
};
const ftpServer = new FtpSrv({
url: `ftp://${host}:${port}`,
pasv_url: resolverFunction,
pasv_min: 60000,
pasv_max: 60009,
anonymous: true
});
ftpServer.on("login", ({ connection, username, password }, resolve, reject) => {
connection.on("RETR", (error, filePath) => {
if (error) console.log(`File download error: ${error}`);
else console.log(`File downloaded: ${filePath}`);
});
connection.on("STOR", (error, filePath) => {
if (error) console.log(`File upload error: ${error}`);
else console.log(`File uploaded: ${filePath}`);
});
if (username === user && password === pw) {
return resolve({ root: "./files" });
}
console.log("Invalid username or password");
return reject(new errors.GeneralError("Invalid username or password", 401));
});
ftpServer.on("client-error", ({ connection, context, error }) => {
console.log(`Client error at ${context}: ${error}`);
});
ftpServer.listen().then(() => {
console.log(`Ftp server started at ftp://${host}:${port}...`);
console.log(`Test file url: ftp://${user}:${pw}@${host}:${port}/[filename]`);
});