This repository was archived by the owner on Jan 14, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathftp.js
More file actions
143 lines (115 loc) · 4.12 KB
/
Copy pathftp.js
File metadata and controls
143 lines (115 loc) · 4.12 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const fs = require("fs");
const FtpSrv = require("ftp-srv");
const port = 21;
require('dotenv').config();
const { MongoClient } = require('mongodb');
const mongoose = require('mongoose');
const userSchema = require('./data'); // Import your Mongoose schema definition
// bcrypt
const bcrypt = require('bcrypt');
const uri = process.env.MONGODB_URI || 'mongodb://localhost:27017/';
const dbName = process.env.DATABASE_NAME || 'your_database_name';
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
mongoose.connect(uri + "hosting-config", { useNewUrlParser: true, useUnifiedTopology: true });
const ftpServer = new FtpSrv({
url: "ftp://0.0.0.0:" + port,
anonymous: false,
pasv_url: '217.174.245.249',
pasv_min: '15000',
pasv_max: '15100',
});
const { generateConfig } = require("./functions.js");
// Function to calculate the size of a file (you need to implement this)
function calculateFileSize(filename) {
return '22'
// Implement logic to calculate the size of the file
// For example, you can use fs.statSync(filename).size
}
ftpServer.on("login", async ({ connection, username, password }, resolve, reject) => {
try {
if (
username.includes("..") ||
username.includes("/") ||
username.includes("\\") ||
username.includes(" ")
)
return reject("Invalid username");
const User = mongoose.model("hostingdata"); // Replace with your Mongoose model name
const user = await User.findOne({ domain: username }).exec();
if (!user) {
return reject("User does not exist");
}
if (!user.FTP) {
return reject("FTP is disabled for this user");
}
if (user.HashedPassword && !bcrypt.compareSync(password, user.HashedPassword)) {
return reject("Invalid password");
}
return resolve({ root: `content/${username}` });
} catch (err) {
console.log(err);
return reject("Internal server error");
}
});
ftpServer.on("stor", async ({ connection, username, filename }, accept, reject) => {
try {
// Calculate the size of the file being uploaded
const fileSize = calculateFileSize(filename);
// Retrieve the user's storage usage from MongoDB
const User = mongoose.model("hostingdata");
const user = await User.findOne({ domain: username }).exec();
if (!user) {
return reject("User does not exist");
}
if (!user.FTP) {
return reject("FTP is disabled for this user");
}
// Calculate the user's new storage usage after the upload
const newUserStorageUsed = user.storageUsed + fileSize;
// Check if the user's storage will exceed the limit
if (newUserStorageUsed > 100 * 1024 * 1024) { // 100MB in bytes
return reject("Storage quota exceeded. Please delete files to free up space.");
}
// Update the user's storage usage in the database
user.storageUsed = newUserStorageUsed;
await user.save();
// If all checks pass, accept the file upload
accept();
} catch (err) {
console.error(err);
reject("Internal server error");
}
});
ftpServer.on("dele", async ({ connection, username, filename }, resolve, reject) => {
try {
// Calculate the size of the file being deleted
const fileSize = calculateFileSize(filename);
// Retrieve the user's storage usage from MongoDB
const User = mongoose.model("hostingdata");
const user = await User.findOne({ domain: username }).exec();
if (!user) {
return reject("User does not exist");
}
if (!user.FTP) {
return reject("FTP is disabled for this user");
}
// Subtract the file size from the user's storage usage
const newUserStorageUsed = user.storageUsed - fileSize;
// Ensure that the storage usage doesn't go below 0
if (newUserStorageUsed < 0) {
user.storageUsed = 0;
} else {
user.storageUsed = newUserStorageUsed;
}
// Update the user's storage usage in the database
await user.save();
// Resolve the file deletion request
resolve();
} catch (err) {
console.error(err);
reject("Internal server error");
}
});
ftpServer.listen().then(() => {
console.log("FTP Server listening on port " + port);
});