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

Fixes #3253 - make User-Agent configurable #3255

Open
wants to merge 1 commit into
base: develop
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ _This release is scheduled to be released on 2024-01-01._

- Added node 21 to the test matrix
- Added transform object to calendar:customEvents
- Added configuration option for `User-Agent`, used by calendar & news module

### Removed

Expand Down
25 changes: 23 additions & 2 deletions js/server_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async function cors(req, res) {
* @returns {object} An object specifying name and value of the headers.
*/
function getHeadersToSend(url) {
const headersToSend = { "User-Agent": `Mozilla/5.0 MagicMirror/${global.version}` };
const headersToSend = { "User-Agent": getUserAgent() };
const headersToSendMatch = new RegExp("sendheaders=(.+?)(&|$)", "g").exec(url);
if (headersToSendMatch) {
const headers = headersToSendMatch[1].split(",");
Expand Down Expand Up @@ -127,4 +127,25 @@ function getVersion(req, res) {
res.send(global.version);
}

module.exports = { cors, getConfig, getHtml, getVersion, getStartup };
/**
* Gets the preferred `User-Agent`
* @returns {string} `User-Agent` to be used
*/
function getUserAgent() {
const defaultUserAgent = `Mozilla/5.0 (Node.js ${Number(process.version.match(/^v(\d+\.\d+)/)[1])}) MagicMirror/${global.version}`;

if (typeof config === "undefined") {
return defaultUserAgent;
}

switch (typeof config.userAgent) {
case "function":
return config.userAgent();
case "string":
return config.userAgent;
default:
return defaultUserAgent;
}
}

module.exports = { cors, getConfig, getHtml, getVersion, getStartup, getUserAgent };
4 changes: 2 additions & 2 deletions modules/default/calendar/calendarfetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const https = require("https");
const ical = require("node-ical");
const Log = require("logger");
const NodeHelper = require("node_helper");
const { getUserAgent } = require("../../../js/server_functions");
const CalendarFetcherUtils = require("./calendarfetcherutils");

/**
Expand Down Expand Up @@ -36,10 +37,9 @@ const CalendarFetcher = function (url, reloadInterval, excludedEvents, maximumEn
const fetchCalendar = () => {
clearTimeout(reloadTimer);
reloadTimer = null;
const nodeVersion = Number(process.version.match(/^v(\d+\.\d+)/)[1]);
let httpsAgent = null;
let headers = {
"User-Agent": `Mozilla/5.0 (Node.js ${nodeVersion}) MagicMirror/${global.version}`
"User-Agent": getUserAgent()
};

if (selfSignedCert) {
Expand Down
4 changes: 2 additions & 2 deletions modules/default/newsfeed/newsfeedfetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const iconv = require("iconv-lite");
const { htmlToText } = require("html-to-text");
const Log = require("logger");
const NodeHelper = require("node_helper");
const { getUserAgent } = require("../../../js/server_functions");

/**
* Responsible for requesting an update on the set interval and broadcasting the data.
Expand Down Expand Up @@ -100,9 +101,8 @@ const NewsfeedFetcher = function (url, reloadInterval, encoding, logFeedWarnings
}
});

const nodeVersion = Number(process.version.match(/^v(\d+\.\d+)/)[1]);
const headers = {
"User-Agent": `Mozilla/5.0 (Node.js ${nodeVersion}) MagicMirror/${global.version}`,
"User-Agent": getUserAgent(),
"Cache-Control": "max-age=0, no-cache, no-store, must-revalidate",
Pragma: "no-cache"
};
Expand Down
19 changes: 18 additions & 1 deletion tests/unit/functions/server_functions_spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { cors } = require("../../../js/server_functions");
const { expect } = require("playwright/test");
const { cors, getUserAgent } = require("../../../js/server_functions");

describe("server_functions tests", () => {
describe("The cors method", () => {
Expand Down Expand Up @@ -142,5 +143,21 @@ describe("server_functions tests", () => {
expect(corsResponse.set.mock.calls[2][0]).toBe("header2");
expect(corsResponse.set.mock.calls[2][1]).toBe("value2");
});

test("Gets User-Agent from configuration", async () => {
config = {};
let userAgent;

userAgent = getUserAgent();
expect(userAgent).toContain("Mozilla/5.0 (Node.js ");

config.userAgent = "Mozilla/5.0 (Foo)";
userAgent = getUserAgent();
expect(userAgent).toBe("Mozilla/5.0 (Foo)");

config.userAgent = () => "Mozilla/5.0 (Bar)";
userAgent = getUserAgent();
expect(userAgent).toBe("Mozilla/5.0 (Bar)");
});
});
});