Skip to content

☁️ Fix Cloudflare Speed #1083

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

Open
wants to merge 6 commits into
base: development
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
15 changes: 15 additions & 0 deletions server/config/binaries.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,19 @@ module.exports.libreList = [
{os: 'freebsd', arch: 'ia32', suffix: 'freebsd_386.tar.gz'},
{os: 'freebsd', arch: 'arm', suffix: 'freebsd_armv7.tar.gz'},
{os: 'freebsd', arch: 'arm64', suffix: 'freebsd_arm64.tar.gz'}
];

module.exports.cloudflareVersion = "1.3.4";
module.exports.cloudflareList = [
// MacOS
{os: 'darwin', arch: 'x64', suffix: 'cfspeedtest-x86_64-apple-darwin.tar.gz'},
{os: 'darwin', arch: 'arm64', suffix: 'cfspeedtest-aarch64-apple-darwin.tar.gz'},
{os: 'darwin', arch: 'universal', suffix: 'cfspeedtest-universal-apple-darwin.tar.gz'},

// Windows
{os: 'win32', arch: 'x64', suffix: 'cfspeedtest-x86_64-pc-windows-msvc.zip'},

// Linux
{os: 'linux', arch: 'x64', suffix: 'cfspeedtest-x86_64-unknown-linux-gnu.tar.gz'},
{os: 'linux', arch: 'arm64', suffix: 'cfspeedtest-aarch64-unknown-linux-gnu.tar.gz'}
]
234 changes: 0 additions & 234 deletions server/tasks/cloudflare.js

This file was deleted.

10 changes: 1 addition & 9 deletions server/tasks/speedtest.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const controller = require("../controller/recommendations");
const parseData = require('../util/providers/parseData');
let {setState, sendRunning, sendError, sendFinished} = require("./integrations");
const serverController = require("../controller/servers");
const cloudflareTask = require("./cloudflare");

let isRunning = false;

Expand Down Expand Up @@ -48,14 +47,7 @@ module.exports.run = async (retryAuto = false) => {
if (serverId === "none")
serverId = undefined;

let speedtest;
if (mode === "cloudflare") {
const startTime = new Date().getTime();
speedtest = await cloudflareTask();
speedtest = {...speedtest, elapsed: (new Date().getTime() - startTime) / 1000};
} else {
speedtest = await (retryAuto ? speedTest(mode) : speedTest(mode, serverId));
}
let speedtest = await (retryAuto ? speedTest(mode) : speedTest(mode, serverId));

if (mode === "ookla" && speedtest.server) {
if (serverId === undefined) await config.updateValue("ooklaId", speedtest.server?.id);
Expand Down
2 changes: 2 additions & 0 deletions server/util/loadCli.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const libreProvider = require('./providers/loadLibre');
const ooklaProvider = require('./providers/loadOokla');
const cloudflareProvider = require('./providers/loadCloudflare');

module.exports.load = async () => {
await libreProvider.load();
await ooklaProvider.load();
await cloudflareProvider.load();
}
78 changes: 78 additions & 0 deletions server/util/providers/loadCloudflare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const fs = require('fs');
const path = require('path');
const { get } = require('https');
const decompress = require("decompress");
const decompressTarGz = require('decompress-targz');
const decompressUnzip = require('decompress-unzip');
const { file } = require("tmp");
const binaries = require('../../config/binaries');

const binaryName = `cfspeedtest${process.platform === "win32" ? ".exe" : ""}`;
const binaryDirectory = path.join(__dirname, "../../../bin");
const binaryPath = path.join(binaryDirectory, binaryName);
const downloadBaseURL = `https://github.com/code-inflation/cfspeedtest/releases/download/v${binaries.cloudflareVersion}/`;

const binaryRegex = /cfspeedtest(.exe)?$/;

module.exports.fileExists = async () => fs.existsSync(binaryPath);

const downloadToFile = (url, destinationPath) => {
return new Promise((resolve, reject) => {
get(url, (res) => {
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
return resolve(downloadToFile(res.headers.location, destinationPath));
}

const writeStream = fs.createWriteStream(destinationPath);
res.pipe(writeStream);

writeStream.on('finish', () => resolve());
writeStream.on('error', reject);
res.on('error', reject);
}).on('error', reject);
});
}

const decompressBinary = async (archivePath) => {
await decompress(archivePath, binaryDirectory, {
plugins: [decompressTarGz(), decompressUnzip()],
filter: file => binaryRegex.test(file.path),
map: file => {
file.path = binaryName;
return file;
}
});
}

module.exports.downloadFile = async () => {
let binary = binaries.cloudflareList.find(b => b.os === process.platform && b.arch === process.arch);

if (!binary && process.platform === 'darwin') {
binary = binaries.cloudflareList.find(b => b.os === 'darwin' && b.arch === 'universal');
}

if (!binary) {
throw new Error(`Your platform (${process.platform}-${process.arch}) is not supported by the Cloudflare CLI`);
}

return new Promise((resolve, reject) => {
file({ postfix: binary.suffix }, async (err, tempPath) => {
if (err) return reject(err);

try {
const fullUrl = downloadBaseURL + binary.suffix;
await downloadToFile(fullUrl, tempPath);
await decompressBinary(tempPath);
resolve();
} catch (error) {
reject(new Error(`Failed to download and extract binary: ${error.message}`));
}
});
});
};

module.exports.load = async () => {
if (!await module.exports.fileExists()) {
await module.exports.downloadFile();
}
};
Loading