Skip to content

Commit bb0a472

Browse files
committed
Added 300s limit on connections
1 parent 1b7b791 commit bb0a472

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

server.js

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ app.use(function (req, res, next) {
2525
// initializes as empty client array
2626
const clients = {};
2727

28+
// loops through clients and removes clients exceeding 300 seconds
29+
const trimClients = () => {
30+
for (const client in clients) {
31+
const time = new Date().getTime();
32+
if (Math.abs(clients[client].lastAccessed - time) > 300000) {
33+
// if client is older than 300 seconds
34+
delete clients[client];
35+
}
36+
}
37+
};
38+
39+
// starts client trimmer on an interval of 30 seconds
40+
setInterval(trimClients, 30000);
41+
2842
// basic-ftp
2943

3044
app.post("/navigate", (req, res) => {
@@ -36,33 +50,19 @@ app.post("/navigate", (req, res) => {
3650
const { ftpHost, ftpUser, ftpPassword, ftpSecure, path } = decryptedData;
3751

3852
// if user is found and logged in
39-
if (
40-
clients[ftpUser] !== undefined &&
41-
clients[ftpUser].status === "connected"
42-
) {
43-
let status;
44-
45-
// tries to access connection status
46-
try {
47-
status = await clients[ftpUser].send("STAT", (err) => {
48-
if (err) {
49-
console.log(err);
50-
}
51-
});
52-
} catch (e) {
53-
clients[ftpUser].status = "disconnected";
54-
}
55-
53+
if (clients[ftpHost + ftpUser] !== undefined) {
5654
// if connection status is good: return list
57-
if (clients[ftpUser].status === "connected") {
58-
const list = await clients[ftpUser].list(path);
55+
if (!clients[ftpHost + ftpUser].closed) {
56+
const list = await clients[ftpHost + ftpUser].list(path);
57+
clients[ftpHost + ftpUser].lastAccessed = new Date().getTime();
5958
res.status(200).send(list);
6059
} else {
6160
// if connection status is bad: RETRY
6261
init();
6362
}
6463
} else {
6564
// if no user found OR user found but not connected: reconnects
65+
6666
const client = new ftp.Client();
6767

6868
try {
@@ -73,8 +73,9 @@ app.post("/navigate", (req, res) => {
7373
secure: ftpSecure,
7474
});
7575

76-
clients[ftpUser] = client;
77-
clients[ftpUser].status = "connected";
76+
client.lastAccessed = new Date().getTime();
77+
78+
clients[ftpHost + ftpUser] = client;
7879

7980
const list = await client.list(path);
8081
res.status(200).send(list);
@@ -102,18 +103,18 @@ app.post("/disconnect", (req, res) => {
102103
var bytes = CryptoJS.AES.decrypt(cipherText, process.env.PASSWORD);
103104
var decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
104105

105-
const { ftpUser } = decryptedData;
106+
const { ftpHost, ftpUser } = decryptedData;
106107

107108
if (
108-
clients[ftpUser] !== undefined &&
109-
clients[ftpUser].status === "connected"
109+
clients[ftpHost + ftpUser] !== undefined &&
110+
!clients[ftpHost + ftpUser].closed
110111
) {
111-
clients[ftpUser].close();
112-
delete clients[ftpUser];
113-
res.status(200).send("Successfully logged out");
114-
} else if (clients[ftpUser] === undefined) {
112+
clients[ftpHost + ftpUser].close();
113+
delete clients[ftpHost + ftpUser];
114+
res.status(200).send("Successfully closed connection.");
115+
} else if (clients[ftpHost + ftpUser] === undefined) {
115116
// if user is not at all logged in
116-
res.status(204).send("User already disconnected");
117+
res.status(204).send("User already disconnected.");
117118
}
118119
};
119120

0 commit comments

Comments
 (0)