mirror of
https://github.com/Nezumi-2711/cloudflare-gateway-pihole-scripts.git
synced 2026-09-22 13:38:37 +00:00
60 lines
1.6 KiB
JavaScript
60 lines
1.6 KiB
JavaScript
import { existsSync } from "fs";
|
|
import { unlink } from "fs/promises";
|
|
import { resolve } from "path";
|
|
|
|
import {
|
|
LIST_TYPE,
|
|
PROCESSING_FILENAME,
|
|
RECOMMENDED_ALLOWLIST_URLS,
|
|
RECOMMENDED_BLOCKLIST_URLS,
|
|
USER_DEFINED_ALLOWLIST_URLS,
|
|
USER_DEFINED_BLOCKLIST_URLS,
|
|
} from "./lib/constants.js";
|
|
import { downloadFiles } from "./lib/utils.js";
|
|
|
|
const allowlistUrls = USER_DEFINED_ALLOWLIST_URLS || RECOMMENDED_ALLOWLIST_URLS;
|
|
const blocklistUrls = USER_DEFINED_BLOCKLIST_URLS || RECOMMENDED_BLOCKLIST_URLS;
|
|
const listType = process.argv[2];
|
|
|
|
const downloadLists = async (filename, urls) => {
|
|
const filePath = resolve(`./${filename}`);
|
|
|
|
if (existsSync(filePath)) {
|
|
await unlink(filePath);
|
|
}
|
|
|
|
try {
|
|
await downloadFiles(filePath, urls);
|
|
|
|
console.log(
|
|
`Done. The ${filename} file contains merged data from the following list(s):`
|
|
);
|
|
console.log(
|
|
urls.reduce(
|
|
(previous, current, index) => previous + `${index + 1}. ${current}\n`,
|
|
""
|
|
)
|
|
);
|
|
} catch (err) {
|
|
console.error(`An error occurred while processing ${filename}:\n`, err);
|
|
console.error("URLs:\n", urls);
|
|
throw err;
|
|
}
|
|
};
|
|
|
|
switch (listType) {
|
|
case LIST_TYPE.ALLOWLIST: {
|
|
await downloadLists(PROCESSING_FILENAME.ALLOWLIST, allowlistUrls);
|
|
break;
|
|
}
|
|
case LIST_TYPE.BLOCKLIST: {
|
|
await downloadLists(PROCESSING_FILENAME.BLOCKLIST, blocklistUrls);
|
|
break;
|
|
}
|
|
default:
|
|
await Promise.all([
|
|
downloadLists(PROCESSING_FILENAME.ALLOWLIST, allowlistUrls),
|
|
downloadLists(PROCESSING_FILENAME.BLOCKLIST, blocklistUrls),
|
|
]);
|
|
}
|