function extractURLPath(fullURL) { const url = new URL(fullURL); let urlPath = url.pathname; if (urlPath.length <= 0) { urlPath = "/"; } return normalisePath(urlPath); }
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 getData = (config, type, device) => { const url = getUrl(config, type, device); const arrUrl = urlParse(url); let filename; if (device.vendor === 'panasonic') { filename = 'Config{mac}.xml'; } else if (device.vendor === 'fanvil') { filename = '{mac}.cfg'; } return { url, protocol: arrUrl['protocol'], server: arrUrl['hostname'], qs: arrUrl['pathname'], filename, }; }
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}); }
const replacePhonebooksVars = (config, phonebooks) => { phonebooks.forEach((element, id) => { const maskUrl = '{{' + ( ['phonebook', id + 1, 'url'].join('_') ) + '}}'; // console.log('mask url', maskUrl); const pbUrl = url(element.url); config = config.replace(maskUrl, pbUrl['host'] + pbUrl['pathname'] + pbUrl['query'] + pbUrl['hash']); const maskProtocol = '{{' + ( ['phonebook', id + 1, 'protocol'].join('_') ) + '}}'; const protocol = url(element.url).protocol === 'https:' ? '3' : '1'; config = config.replace(maskProtocol, protocol); const maskFullUrl = '{{' + ( ['phonebook', id + 1, 'fullurl'].join('_') ) + '}}'; config = config.replace(maskFullUrl, element.url); const maskName = '{{' + ( ['phonebook', id + 1, 'name'].join('_') ) + '}}'; config = config.replace(maskName, element.name); }); return config; }