diff --git a/src/system.js b/src/system.js index f8608ab..f699f89 100644 --- a/src/system.js +++ b/src/system.js @@ -1,11 +1,12 @@ - /** @module fritzSystem */ -let fritzSystem = {} -module.exports = fritzSystem +let fritzSystem = {}; +module.exports = fritzSystem; + +const fritzRequest = require("./request.js"); +const fritzFormat = require("./format.js"); -const fritzRequest = require('./request.js') -const fritzFormat = require('./format.js') +const counterInfoRegex = new RegExp(/\{(.*?)\}}/); /** * Get the version of a Fritz!Box without authentication. @@ -13,17 +14,21 @@ const fritzFormat = require('./format.js') * @return {string} The Fritz!OS version as a string (e.g. `'06.83'`) */ fritzSystem.getVersion = async (options) => { - options.noAuth = true - const rawXml = await fritzRequest.request('/jason_boxinfo.xml', 'GET', options) - if (rawXml.error) return rawXml + options.noAuth = true; + const rawXml = await fritzRequest.request( + "/jason_boxinfo.xml", + "GET", + options + ); + if (rawXml.error) return rawXml; - const object = await fritzFormat.xmlToObject(rawXml.body) - const fullVersion = object['j:BoxInfo']['j:Version'][0] + const object = await fritzFormat.xmlToObject(rawXml.body); + const fullVersion = object["j:BoxInfo"]["j:Version"][0]; - let parts = fullVersion.split('.') - const OSVersion = parts[1] + '.' + parts[2] - return OSVersion -} + let parts = fullVersion.split("."); + const OSVersion = parts[1] + "." + parts[2]; + return OSVersion; +}; /** * Get the version of a Fritz!Box without authentication. @@ -31,8 +36,60 @@ fritzSystem.getVersion = async (options) => { * @return {number} The Fritz!OS version cast as an integer (e.g. `683`) */ fritzSystem.getVersionNumber = async (options) => { - const version = await fritzSystem.getVersion(options) - if (version.error) return version - const versionNumber = parseInt(version.replace('.', '')) - return versionNumber -} + const version = await fritzSystem.getVersion(options); + if (version.error) return version; + const versionNumber = parseInt(version.replace(".", "")); + return versionNumber; +}; + +/** + * Get the online counter information + * @param {Object} options counter options + * @return {number} The parsed object with counter information + */ +fritzSystem.getCounter = async (options) => { + if (options.sid) { + options.noAuth = true; + } + + // Check the version first, because the structure is parsable from v7.25 and up + const version = await fritzSystem.getVersionNumber(options); + if (version.error) { + return version; + } + + if (version < 725) { + return { + error: { message: "This version is not supported", raw: version }, + }; + } + + const response = await fritzRequest.request( + "/data.lua", + "POST", + options, + false, + false, + { + xhr: 1, + page: "netCnt", + sid: options.sid, + } + ); + if (response.error) return response; + + const counterInfoRegexResult = counterInfoRegex.exec(response.body); + let counterInfo = counterInfoRegexResult[0]; + + if (!counterInfo) { + return { + error: { message: "Could not find counter information", raw: response }, + }; + } + + try { + return JSON.parse(counterInfo); + } catch (e) { + return { error: { message: "Could not parse information", raw: response } }; + } +}; diff --git a/test/onlinecounter.js b/test/onlinecounter.js new file mode 100644 index 0000000..95fdcf3 --- /dev/null +++ b/test/onlinecounter.js @@ -0,0 +1,19 @@ +const fritz = require('../index.js') +const options = require('../package.json').options +const fs = require('fs') + +async function getCounterInfo () { + const sessionId = await fritz.getSessionId(options) + options.sid = sessionId + + const counterInfo = await fritz.getCounter(options) + + if (counterInfo.error) { + console.log('Error:', counterInfo.error.message) + process.exit(1) + } + + fs.writeFileSync('counterInfo.json', JSON.stringify(counterInfo, null, 2)) +} + +getCounterInfo()