const checkHttp = async url => { let response; try { response = await got(url, {rejectUnauthorized: false}); } catch (_) { return false; } if (response.headers && response.headers.location) { const url = new URL(response.headers.location); const hostname = url.hostname.replace(/^\[/, '').replace(/\]$/, ''); // Strip [] from IPv6 return !routerIps.has(hostname); } return true; }
/** * Returns filename, encoded in url * @param {string} url - url of file * @return {string} filename, saved at Firebase */ decodeUrl(url) { const decoded = urlParse(querystring.unescape(url)); if (decoded.hostname !== hostName) { throw new APIError('Provided url is not an url of Firebase resource', httpStatus.BAD_REQUEST, true); } else if (regexForAppId.exec(decoded.pathname)[1] !== this.bucketName) { throw new APIError('Provided url is not an url of this Firebase project', httpStatus.BAD_REQUEST, true); } else if (!regexForFileId.test(decoded.pathname)) { throw new APIError('Provided url does not contain file ID', httpStatus.BAD_REQUEST, true); } return regexForFileId.exec(decoded.pathname)[1]; }
const isTargetReachable = async target => { const url = new URL(prependHttp(target)); if (!url.port) { url.port = url.protocol === 'http:' ? 80 : 443; } let address; try { address = await getAddress(url.hostname); } catch (_) { return false; } if (!address || routerIps.has(address)) { return false; } if ([80, 443].includes(url.port)) { return checkHttp(url.toString()); } return isPortReachable(url.port, {host: address}); }