diff --git a/packages/dashmate/src/status/providers.js b/packages/dashmate/src/status/providers.js index a3a08b94b69..2eee8a72f51 100644 --- a/packages/dashmate/src/status/providers.js +++ b/packages/dashmate/src/status/providers.js @@ -1,6 +1,8 @@ +import https from 'https'; import { PortStateEnum } from './enums/portState.js'; const MAX_REQUEST_TIMEOUT = 5000; +const MAX_RESPONSE_SIZE = 1 * 1024 * 1024; // 1 MB const request = async (url) => { try { @@ -29,12 +31,6 @@ const requestJSON = async (url) => { return response; }; -const requestText = async (url) => { - const response = await request(url); - - return response.text(); -}; - const insightURLs = { testnet: 'https://testnet-insight.dashevo.org/insight-api', mainnet: 'https://insight.dash.org/insight-api', @@ -68,15 +64,72 @@ export default { }, mnowatch: { checkPortStatus: async (port) => { - try { - return requestText(`https://mnowatch.org/${port}/`); - } catch (e) { - if (process.env.DEBUG) { - // eslint-disable-next-line no-console - console.warn(e); - } - return PortStateEnum.ERROR; - } + // We use http request instead fetch function to force + // using IPv4 otherwise mnwatch could try to connect to IPv6 node address + // and fail (Core listens for IPv4 only) + // https://github.com/dashpay/platform/issues/2100 + + const options = { + hostname: 'mnowatch.org', + port: 443, + path: `/${port}/`, + method: 'GET', + family: 4, // Force IPv4 + timeout: MAX_REQUEST_TIMEOUT, + }; + + return new Promise((resolve) => { + const req = https.request(options, (res) => { + let data = ''; + + // Check if the status code is 200 + if (res.statusCode !== 200) { + if (process.env.DEBUG) { + // eslint-disable-next-line no-console + console.warn(`Port check request failed with status code ${res.statusCode}`); + } + // Consume response data to free up memory + res.resume(); + resolve(PortStateEnum.ERROR); + return; + } + + // Optionally set the encoding to receive strings directly + res.setEncoding('utf8'); + + // Collect data chunks + res.on('data', (chunk) => { + data += chunk; + + if (data.length > MAX_RESPONSE_SIZE) { + resolve(PortStateEnum.ERROR); + + if (process.env.DEBUG) { + // eslint-disable-next-line no-console + console.warn('Port check response size exceeded'); + } + + req.destroy(); + } + }); + + // Handle the end of the response + res.on('end', () => { + resolve(data); + }); + }); + + req.on('error', (e) => { + if (process.env.DEBUG) { + // eslint-disable-next-line no-console + console.warn(`Port check request failed: ${e}`); + } + + resolve(PortStateEnum.ERROR); + }); + + req.end(); + }); }, }, };