Skip to content

Commit

Permalink
Add support for FTP on admin-lists.js
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeRedDev committed Dec 2, 2023
1 parent ae2441d commit 729a55b
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions squad-server/utils/admin-lists.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import crypto from 'crypto';
import { Client } from 'basic-ftp';

import axios from 'axios';
import Logger from 'core/logger';
Expand Down Expand Up @@ -31,6 +33,15 @@ export default async function fetchAdminLists(adminLists) {
data = fs.readFileSync(listPath, 'utf8');
break;
}
case 'ftp': {
const provider = new FTPAdminListProvider({
...list.ftp,
source: list.source
});
data = await provider.fetch();
provider.close();
break;
}
default:
throw new Error(`Unsupported AdminList type:${list.type}`);
}
Expand Down Expand Up @@ -84,3 +95,59 @@ export default async function fetchAdminLists(adminLists) {
Logger.verbose('SquadServer', 1, `${Object.keys(admins).length} admins loaded...`);
return admins;
}

class FTPAdminListProvider {
constructor(options) {
this.options = {
host: options.host,
port: options.port || 21,
user: options.user,
password: options.password,
secure: options.secure || false,
timeout: options.timeout || 2000,
downloadPath: options.source,
encoding: options.encoding || 'utf8',
verbose: options.verbose || false
}

this.client = new Client(this.options.timeout);
this.client.ftp.verbose = this.options.verbose;
this.client.encoding = this.options.encoding;

this.tempFilePath = path.join(
process.cwd(),
crypto
.createHash('md5')
.update(`${this.options.host}:${this.options.port}:${this.options.path}:Admins.cfg`)
.digest('hex') + '.tmp'
);
}

async fetch() {
if (!this.client.closed) throw new Error("Tried to fetch admin list on closed FTP client!");
try {
await this.client.access({
host: this.options.host,
port: this.options.port,
user: this.options.user,
password: this.options.password,
secure: this.options.secure
});

await this.client.downloadTo(this.tempFilePath, this.options.downloadPath);

if (!fs.existsSync(this.tempFilePath)) throw new Error(`No admin file ${this.tempFilePath} found!`);

return fs.readFileSync(this.tempFilePath, 'utf8');
} catch (err) {
console.log(err);
throw new Error("Failed to fetch admin list from FTP!");
}
}

close() {
if (this.client.closed) return;

this.client.close();
}
}

0 comments on commit 729a55b

Please sign in to comment.