mirror of
https://github.com/Nezumi-2711/cloudflare-gateway-pihole-scripts.git
synced 2026-09-22 20:01:04 +00:00
reorganized code
This commit is contained in:
+10
-8
@@ -1,6 +1,7 @@
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { DRY_RUN, LIST_ITEM_LIMIT } from './lib/constants.js';
|
import { DRY_RUN, FAST_MODE, LIST_ITEM_LIMIT } from './lib/constants.js';
|
||||||
import { createZeroTrustLists } from './lib/api.js';
|
import { createZeroTrustListsAtOnce, createZeroTrustListsOneByOne } from './lib/api.js';
|
||||||
|
import { truncateArray } from './lib/utils.js';
|
||||||
|
|
||||||
if (!process.env.CI) console.log(`List item limit set to ${LIST_ITEM_LIMIT}`);
|
if (!process.env.CI) console.log(`List item limit set to ${LIST_ITEM_LIMIT}`);
|
||||||
|
|
||||||
@@ -99,7 +100,7 @@ fs.readFile('input.csv', 'utf8', async (err, data) => {
|
|||||||
// Trim array to 300,000 domains if it's longer than that
|
// Trim array to 300,000 domains if it's longer than that
|
||||||
if (domains.length > LIST_ITEM_LIMIT) {
|
if (domains.length > LIST_ITEM_LIMIT) {
|
||||||
console.warn(`${domains.length} domains found in input.csv - input has to be trimmed to ${LIST_ITEM_LIMIT} domains`);
|
console.warn(`${domains.length} domains found in input.csv - input has to be trimmed to ${LIST_ITEM_LIMIT} domains`);
|
||||||
domains = trimArray(domains, LIST_ITEM_LIMIT);
|
domains = truncateArray(domains, LIST_ITEM_LIMIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
const listsToCreate = Math.ceil(domains.length / 1000);
|
const listsToCreate = Math.ceil(domains.length / 1000);
|
||||||
@@ -110,9 +111,10 @@ fs.readFile('input.csv', 'utf8', async (err, data) => {
|
|||||||
// TODO: we should probably continue, just without making any real requests to Cloudflare
|
// TODO: we should probably continue, just without making any real requests to Cloudflare
|
||||||
if (DRY_RUN) return console.log('Dry run complete - no lists were created. If this was not intended, please remove the DRY_RUN environment variable and try again.');
|
if (DRY_RUN) return console.log('Dry run complete - no lists were created. If this was not intended, please remove the DRY_RUN environment variable and try again.');
|
||||||
|
|
||||||
await createZeroTrustLists(domains)
|
if (FAST_MODE) {
|
||||||
});
|
createZeroTrustListsAtOnce(items);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
function trimArray(arr, size) {
|
createZeroTrustListsOneByOne(items);
|
||||||
return arr.slice(0, size);
|
});
|
||||||
}
|
|
||||||
|
|||||||
+3
-16
@@ -1,4 +1,4 @@
|
|||||||
import { FAST_MODE, LIST_ITEM_SIZE } from "./constants.js";
|
import { LIST_ITEM_SIZE } from "./constants.js";
|
||||||
import { requestGateway } from "./helpers.js";
|
import { requestGateway } from "./helpers.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -24,7 +24,7 @@ const createZeroTrustList = (name, items) =>
|
|||||||
* Creates Zero Trust lists sequentially.
|
* Creates Zero Trust lists sequentially.
|
||||||
* @param {string[]} items The domains.
|
* @param {string[]} items The domains.
|
||||||
*/
|
*/
|
||||||
const createZeroTrustListsOneByOne = async (items) => {
|
export const createZeroTrustListsOneByOne = async (items) => {
|
||||||
let totalListNumber = Math.ceil(items.length / LIST_ITEM_SIZE);
|
let totalListNumber = Math.ceil(items.length / LIST_ITEM_SIZE);
|
||||||
|
|
||||||
for (let i = 0, listNumber = 1; i < items.length; i += LIST_ITEM_SIZE) {
|
for (let i = 0, listNumber = 1; i < items.length; i += LIST_ITEM_SIZE) {
|
||||||
@@ -49,7 +49,7 @@ const createZeroTrustListsOneByOne = async (items) => {
|
|||||||
* Creates all Zero Trust lists at once.
|
* Creates all Zero Trust lists at once.
|
||||||
* @param {string[]} items The domains.
|
* @param {string[]} items The domains.
|
||||||
*/
|
*/
|
||||||
const createZeroTrustListsAtOnce = async (items) => {
|
export const createZeroTrustListsAtOnce = async (items) => {
|
||||||
const totalListNumber = Math.ceil(items.length / LIST_ITEM_SIZE);
|
const totalListNumber = Math.ceil(items.length / LIST_ITEM_SIZE);
|
||||||
const requests = [];
|
const requests = [];
|
||||||
|
|
||||||
@@ -70,16 +70,3 @@ const createZeroTrustListsAtOnce = async (items) => {
|
|||||||
console.error(`Error occurred while creating lists - ${err.toString()}`);
|
console.error(`Error occurred while creating lists - ${err.toString()}`);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Create Zero Trust lists.
|
|
||||||
* @param {string[]} items The domains.
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
export const createZeroTrustLists = (items) => {
|
|
||||||
if (FAST_MODE) {
|
|
||||||
return createZeroTrustListsAtOnce(items);
|
|
||||||
}
|
|
||||||
|
|
||||||
return createZeroTrustListsOneByOne(items);
|
|
||||||
};
|
|
||||||
|
|||||||
@@ -4,3 +4,11 @@
|
|||||||
*/
|
*/
|
||||||
export const sleep = (ms = 350) =>
|
export const sleep = (ms = 350) =>
|
||||||
new Promise((resolve) => setTimeout(resolve, ms));
|
new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Truncates an array to the specified size.
|
||||||
|
* @param {any[]} arr The array to be truncated.
|
||||||
|
* @param {number} size The size to which the array will be truncated.
|
||||||
|
* @returns {any[]}
|
||||||
|
*/
|
||||||
|
export const truncateArray = (arr, size) => arr.slice(0, size);
|
||||||
|
|||||||
Reference in New Issue
Block a user