From 492c5f7178783ffa969a1539a4082e85bcab51a8 Mon Sep 17 00:00:00 2001 From: mariusbackes Date: Wed, 29 Dec 2021 21:53:45 +0100 Subject: [PATCH 1/4] added method to retrieve online counter information --- src/system.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/system.js b/src/system.js index f8608ab..fb0ad31 100644 --- a/src/system.js +++ b/src/system.js @@ -7,6 +7,8 @@ module.exports = fritzSystem const fritzRequest = require('./request.js') const fritzFormat = require('./format.js') +const counterInfoRegex = new RegExp(/\{(.*?)\}}/) + /** * Get the version of a Fritz!Box without authentication. * @param {Object} options - FritzBox.js options object. @@ -36,3 +38,41 @@ fritzSystem.getVersionNumber = async (options) => { 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 + } + + 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 } } + } +} From 5f6ca7ae434283f0bc53381c66caca5284cf2854 Mon Sep 17 00:00:00 2001 From: mariusbackes Date: Sun, 9 Jan 2022 15:20:03 +0100 Subject: [PATCH 2/4] check fritzbox version before loading counter info --- src/system.js | 83 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 50 insertions(+), 33 deletions(-) diff --git a/src/system.js b/src/system.js index fb0ad31..b7c56fd 100644 --- a/src/system.js +++ b/src/system.js @@ -1,13 +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(/\{(.*?)\}}/) +const counterInfoRegex = new RegExp(/\{(.*?)\}}/); /** * Get the version of a Fritz!Box without authentication. @@ -15,17 +14,21 @@ const counterInfoRegex = new RegExp(/\{(.*?)\}}/) * @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. @@ -33,11 +36,11 @@ 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 @@ -46,33 +49,47 @@ fritzSystem.getVersionNumber = async (options) => { */ fritzSystem.getCounter = async (options) => { if (options.sid) { - options.noAuth = true + options.noAuth = true; + } + + // Check the version first, because the structure is parsable from v7.25 and up + const version = await fritzSystem.getVersionNumber(); + 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', + "/data.lua", + "POST", options, false, false, { xhr: 1, - page: 'netCnt', - sid: options.sid + page: "netCnt", + sid: options.sid, } - ) - if (response.error) return response + ); + if (response.error) return response; - const counterInfoRegexResult = counterInfoRegex.exec(response.body) - let counterInfo = counterInfoRegexResult[0] + const counterInfoRegexResult = counterInfoRegex.exec(response.body); + let counterInfo = counterInfoRegexResult[0]; if (!counterInfo) { - return { error: { message: 'Could not find counter information', raw: response } } + return { + error: { message: "Could not find counter information", raw: response }, + }; } try { - return JSON.parse(counterInfo) + return JSON.parse(counterInfo); } catch (e) { - return { error: { message: 'Could not parse information', raw: response } } + return { error: { message: "Could not parse information", raw: response } }; } -} +}; From b533509548ea921d648f58f4fd01b35ae289c4e4 Mon Sep 17 00:00:00 2001 From: mariusbackes Date: Sun, 9 Jan 2022 15:29:14 +0100 Subject: [PATCH 3/4] fixed issue on retreiving version number for counter info --- src/system.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/system.js b/src/system.js index b7c56fd..f699f89 100644 --- a/src/system.js +++ b/src/system.js @@ -53,7 +53,7 @@ fritzSystem.getCounter = async (options) => { } // Check the version first, because the structure is parsable from v7.25 and up - const version = await fritzSystem.getVersionNumber(); + const version = await fritzSystem.getVersionNumber(options); if (version.error) { return version; } From 37fb71984e6cb7ff4671cbc9c39e7ca97f6a0aca Mon Sep 17 00:00:00 2001 From: mariusbackes Date: Sun, 9 Jan 2022 15:31:17 +0100 Subject: [PATCH 4/4] added test for loading counter info --- test/onlinecounter.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 test/onlinecounter.js 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()