Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: load counter info #17

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 76 additions & 19 deletions src/system.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,95 @@

/** @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.
* @param {Object} options - FritzBox.js options object.
* @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.
* @param {Object} options server protocol
* @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 } };
}
};
19 changes: 19 additions & 0 deletions test/onlinecounter.js
Original file line number Diff line number Diff line change
@@ -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()