Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for FTP on admin-lists.js #331

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ The following section of the configuration contains information about your Squad
{
"type": "remote",
"source": "http://yourWebsite.com/Server1/Admins.cfg",
},
{
"type": "ftp",
"ftp": {
"host": "XXX.XX.XXX.XX",
"port": 12345,
"user": "FTP Username",
"password": "FTP Password"
},
"source": "/SquadGame/ServerConfig/Admins.cfg"
}
]
},
Expand Down
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();
}
}