diff --git a/src/data/fileStructure.json b/src/data/fileStructure.json index be2c357e..12ee2b6b 100644 --- a/src/data/fileStructure.json +++ b/src/data/fileStructure.json @@ -578,13 +578,18 @@ { "path": "src/dataManager/dataManager.js", "url": "https://raw.githubusercontent.com/3urobeat/steam-comment-service-bot/beta-testing/src/dataManager/dataManager.js", - "checksum": "5e226fff75fc009bda5121c9ccbe8a5d" + "checksum": "9a9c827bc01c11624c0d7f60a1227c53" }, { "path": "src/dataManager/dataProcessing.js", "url": "https://raw.githubusercontent.com/3urobeat/steam-comment-service-bot/beta-testing/src/dataManager/dataProcessing.js", "checksum": "e3927776806e2df7cda77eace731232d" }, + { + "path": "src/dataManager/helpers/checkProxies.js", + "url": "https://raw.githubusercontent.com/3urobeat/steam-comment-service-bot/beta-testing/src/dataManager/helpers/checkProxies.js", + "checksum": "031337c90a9a313691b5a4e744f1cd31" + }, { "path": "src/dataManager/helpers/getLang.js", "url": "https://raw.githubusercontent.com/3urobeat/steam-comment-service-bot/beta-testing/src/dataManager/helpers/getLang.js", @@ -798,7 +803,7 @@ { "path": "types/types.d.ts", "url": "https://raw.githubusercontent.com/3urobeat/steam-comment-service-bot/beta-testing/types/types.d.ts", - "checksum": "1b0de8d2e346f31a3c94aeaa1d09f683" + "checksum": "7fb30947e8c2489efdf582f30dcebc09" } ] } \ No newline at end of file diff --git a/src/dataManager/dataManager.js b/src/dataManager/dataManager.js index 6a24e5e3..a8c427df 100644 --- a/src/dataManager/dataManager.js +++ b/src/dataManager/dataManager.js @@ -4,7 +4,7 @@ * Created Date: 21.03.2023 22:34:51 * Author: 3urobeat * - * Last Modified: 10.10.2023 16:57:59 + * Last Modified: 10.10.2023 22:16:36 * Modified By: 3urobeat * * Copyright (c) 2023 3urobeat @@ -127,7 +127,7 @@ DataManager.prototype._loadDataManagerFiles = function() { // The files need to be explicitly defined for restoring using checkAndGetFile to work const helperPaths = [ "dataCheck.js", "dataExport.js", "dataImport.js", "dataIntegrity.js", "dataProcessing.js", - "helpers/getLang.js", "helpers/getQuote.js", "helpers/handleCooldowns.js", "helpers/handleExpiringTokens.js", "helpers/misc.js", "helpers/refreshCache.js", "helpers/repairFile.js" + "helpers/checkProxies.js", "helpers/getLang.js", "helpers/getQuote.js", "helpers/handleCooldowns.js", "helpers/handleExpiringTokens.js", "helpers/misc.js", "helpers/refreshCache.js", "helpers/repairFile.js" ]; helperPaths.forEach(async (e, i) => { @@ -206,6 +206,19 @@ DataManager.prototype.verifyIntegrity = function() {}; */ DataManager.prototype.processData = async function() {}; +/** + * Checks if a proxy can reach steamcommunity.com and updates its isOnline and lastOnlineCheck + * @param {number} proxyIndex Index of the proxy to check in the DataManager proxies array + * @returns {boolean} True if the proxy can reach steamcommunity.com, false otherwise. + */ +DataManager.prototype.checkProxy = async function(proxyIndex) {}; // eslint-disable-line + +/** + * Checks all proxies if they can reach steamcommunity.com and updates their entries + * @returns {Promise.} Resolves when all proxies have been checked + */ +DataManager.prototype.checkAllProxies = async function() {}; + /** * Retrieves a language string from one of the available language files and replaces keywords if desired. * If a userID is provided it will lookup which language the user has set. If nothing is set, the default language set in the config will be returned. diff --git a/src/dataManager/helpers/checkProxies.js b/src/dataManager/helpers/checkProxies.js new file mode 100644 index 00000000..bfb5c3cd --- /dev/null +++ b/src/dataManager/helpers/checkProxies.js @@ -0,0 +1,61 @@ +/* + * File: checkProxies.js + * Project: steam-comment-service-bot + * Created Date: 09.10.2023 21:08:13 + * Author: 3urobeat + * + * Last Modified: 10.10.2023 22:12:47 + * Modified By: 3urobeat + * + * Copyright (c) 2023 3urobeat + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * You should have received a copy of the GNU General Public License along with this program. If not, see . + */ + + +const DataManager = require("../dataManager"); + + +/** + * Checks if a proxy can reach steamcommunity.com and updates its isOnline and lastOnlineCheck + * @param {number} proxyIndex Index of the proxy to check in the DataManager proxies array + * @returns {boolean} True if the proxy can reach steamcommunity.com, false otherwise. + */ +DataManager.prototype.checkProxy = async function(proxyIndex) { + let { checkConnection, splitProxyString } = this.controller.misc; + + let thisProxy = this.proxies[proxyIndex]; + + // Check connection using checkConnection helper + await checkConnection("https://steamcommunity.com", true, thisProxy.proxy != null ? splitProxyString(thisProxy.proxy) : null) // Quick ternary to only split non-hosts + .then((res) => { + thisProxy.isOnline = res.statusCode >= 200 && res.statusCode < 300; // Check if response code is in 200 (OK) range + }) + .catch(() => { + thisProxy.isOnline = false; + }); + + thisProxy.lastOnlineCheck = Date.now(); + + // Return result of check above + return thisProxy.isOnline; +}; + + +/** + * Checks all proxies if they can reach steamcommunity.com and updates their entries + * @returns {Promise.} Resolves when all proxies have been checked + */ +DataManager.prototype.checkAllProxies = async function() { + let promiseArr = []; + + // Iterate over all proxies and call this.checkProxies(). We don't need any delay here as all requests go over different IPs + this.proxies.forEach((e) => { + promiseArr.push(this.checkProxy(e.proxyIndex)); + }); + + // Await all promises so this function resolves when all proxies have been checked + await Promise.all(promiseArr); +}; \ No newline at end of file diff --git a/types/types.d.ts b/types/types.d.ts index b04f92a0..082ea12c 100644 --- a/types/types.d.ts +++ b/types/types.d.ts @@ -150,6 +150,11 @@ declare class Bot { * Handles checking for missing game licenses, requests them and then starts playing */ handleMissingGameLicenses(): void; + /** + * Changes the proxy of this bot account and relogs it. + * @param newProxyIndex - Index of the new proxy inside the DataManager.proxies array. + */ + switchProxy(newProxyIndex: number): void; /** * Attempts to get this account, after failing all logOnRetries, back online after some time. Does not apply to initial logins. */ @@ -825,7 +830,7 @@ declare class DataManager { /** * Stores all proxies provided via the `proxies.txt` file. */ - proxies: string[]; + proxies: { proxy: string; proxyIndex: number; isOnline: boolean; lastOnlineCheck: number; }[]; /** * Stores IDs from config files converted at runtime and backups for all config & data files. */ @@ -912,6 +917,17 @@ declare class DataManager { * Converts owners and groups imported from config.json to steam ids and updates cachefile. (Call this after dataImport and before dataCheck) */ processData(): void; + /** + * Checks if a proxy can reach steamcommunity.com and updates its isOnline and lastOnlineCheck + * @param proxyIndex - Index of the proxy to check in the DataManager proxies array + * @returns True if the proxy can reach steamcommunity.com, false otherwise. + */ + checkProxy(proxyIndex: number): boolean; + /** + * Checks all proxies if they can reach steamcommunity.com and updates their entries + * @returns Resolves when all proxies have been checked + */ + checkAllProxies(): Promise; /** * Retrieves a language string from one of the available language files and replaces keywords if desired. * If a userID is provided it will lookup which language the user has set. If nothing is set, the default language set in the config will be returned. @@ -984,6 +1000,17 @@ declare class DataManager { * Converts owners and groups imported from config.json to steam ids and updates cachefile. (Call this after dataImport and before dataCheck) */ processData(): void; + /** + * Checks if a proxy can reach steamcommunity.com and updates its isOnline and lastOnlineCheck + * @param proxyIndex - Index of the proxy to check in the DataManager proxies array + * @returns True if the proxy can reach steamcommunity.com, false otherwise. + */ + checkProxy(proxyIndex: number): boolean; + /** + * Checks all proxies if they can reach steamcommunity.com and updates their entries + * @returns Resolves when all proxies have been checked + */ + checkAllProxies(): Promise; /** * Retrieves a language string from one of the available language files and replaces keywords if desired. * If a userID is provided it will lookup which language the user has set. If nothing is set, the default language set in the config will be returned.