diff --git a/js/server.js b/js/server.js index b4d71ca5..fcd096ad 100644 --- a/js/server.js +++ b/js/server.js @@ -78,23 +78,31 @@ function Server(config, callback) { app.use(directory, express.static(path.resolve(global.root_path + directory))); } - app.get("/cors", function (req, res) { - // http://localhost:8080/cors?url=https://google.de - console.dir(req.query.url); + app.get("/cors", async function (req, res) { + // example: http://localhost:8080/cors?url=https://google.de - fetch(req.query.url, { headers: { "User-Agent": "Mozilla/5.0 MagicMirror/" + global.version } }) - .then((response) => { - global.mmCorsHeader = response.headers.get("Content-Type"); - if (! global.mmCorsHeader) {global.mmCorsHeader = "application/text"} - return response.text(); - }) - .then((responseData) => { - res.set("Content-Type", global.mmCorsHeader); - res.send(responseData); - }) - .catch((error) => { - Log.error(error); - }); + try { + const reg = "^/cors.+url=(.*)"; + let url = ""; + + let match = new RegExp(reg, "g").exec(req.url); + if (!match) { + url = "invalid url: " + req.url; + Log.error(url); + res.send(url); + } else { + url = match[1]; + Log.log("cors url: " + url); + const response = await fetch(url, { headers: { "User-Agent": "Mozilla/5.0 MagicMirror/" + global.version } }); + const header = response.headers.get("Content-Type"); + const data = await response.text(); + if (header) res.set("Content-Type", header); + res.send(data); + } + } catch (error) { + Log.error(error); + res.send(error); + } }); app.get("/version", function (req, res) { diff --git a/modules/default/weather/providers/darksky.js b/modules/default/weather/providers/darksky.js index 451ac98b..01bd79cd 100755 --- a/modules/default/weather/providers/darksky.js +++ b/modules/default/weather/providers/darksky.js @@ -18,7 +18,7 @@ WeatherProvider.register("darksky", { // Set the default config properties that is specific to this provider defaults: { - apiBase: "https://cors-anywhere.herokuapp.com/https://api.darksky.net", + apiBase: "https://api.darksky.net", weatherEndpoint: "/forecast", apiKey: "", lat: 0, @@ -67,7 +67,7 @@ WeatherProvider.register("darksky", { // Create a URL from the config and base URL. getUrl() { const units = this.units[this.config.units] || "auto"; - return `${this.config.apiBase}${this.config.weatherEndpoint}/${this.config.apiKey}/${this.config.lat},${this.config.lon}?units=${units}&lang=${this.config.lang}`; + return this.getCorsUrl() + `${this.config.apiBase}${this.config.weatherEndpoint}/${this.config.apiKey}/${this.config.lat},${this.config.lon}?units=${units}&lang=${this.config.lang}`; }, // Implement WeatherDay generator. diff --git a/modules/default/weather/providers/envcanada.js b/modules/default/weather/providers/envcanada.js index 9ea65e2f..8cbf313c 100644 --- a/modules/default/weather/providers/envcanada.js +++ b/modules/default/weather/providers/envcanada.js @@ -164,7 +164,7 @@ WeatherProvider.register("envcanada", { // CORS errors when accessing EC // getUrl() { - return "http://localhost:8080/cors?url=https://dd.weather.gc.ca/citypage_weather/xml/" + this.config.provCode + "/" + this.config.siteCode + "_e.xml"; + return this.getCorsUrl() + "https://dd.weather.gc.ca/citypage_weather/xml/" + this.config.provCode + "/" + this.config.siteCode + "_e.xml"; }, // diff --git a/modules/default/weather/providers/openweathermap.js b/modules/default/weather/providers/openweathermap.js index c1258d3c..4d3238f6 100755 --- a/modules/default/weather/providers/openweathermap.js +++ b/modules/default/weather/providers/openweathermap.js @@ -116,7 +116,7 @@ WeatherProvider.register("openweathermap", { * Gets the complete url for the request */ getUrl() { - return this.config.apiBase + this.config.apiVersion + this.config.weatherEndpoint + this.getParams(); + return this.getCorsUrl() + this.config.apiBase + this.config.apiVersion + this.config.weatherEndpoint + this.getParams(); }, /* diff --git a/modules/default/weather/providers/smhi.js b/modules/default/weather/providers/smhi.js index 603aa814..adc5f07a 100644 --- a/modules/default/weather/providers/smhi.js +++ b/modules/default/weather/providers/smhi.js @@ -91,7 +91,7 @@ WeatherProvider.register("smhi", { getURL() { let lon = this.config.lon; let lat = this.config.lat; - return `https://opendata-download-metfcst.smhi.se/api/category/pmp3g/version/2/geotype/point/lon/${lon}/lat/${lat}/data.json`; + return this.getCorsUrl() + `https://opendata-download-metfcst.smhi.se/api/category/pmp3g/version/2/geotype/point/lon/${lon}/lat/${lat}/data.json`; }, /** diff --git a/modules/default/weather/providers/ukmetoffice.js b/modules/default/weather/providers/ukmetoffice.js index 158592bd..195e9d2a 100755 --- a/modules/default/weather/providers/ukmetoffice.js +++ b/modules/default/weather/providers/ukmetoffice.js @@ -73,7 +73,7 @@ WeatherProvider.register("ukmetoffice", { * Gets the complete url for the request */ getUrl(forecastType) { - return this.config.apiBase + this.config.locationID + this.getParams(forecastType); + return this.getCorsUrl() + this.config.apiBase + this.config.locationID + this.getParams(forecastType); }, /* diff --git a/modules/default/weather/providers/ukmetofficedatahub.js b/modules/default/weather/providers/ukmetofficedatahub.js index bab1220d..86ede528 100644 --- a/modules/default/weather/providers/ukmetofficedatahub.js +++ b/modules/default/weather/providers/ukmetofficedatahub.js @@ -63,7 +63,7 @@ WeatherProvider.register("ukmetofficedatahub", { queryStrings += "&includeLocationName=" + true; // Return URL, making sure there is a trailing "/" in the base URL. - return this.config.apiBase + (this.config.apiBase.endsWith("/") ? "" : "/") + forecastType + queryStrings; + return this.getCorsUrl() + this.config.apiBase + (this.config.apiBase.endsWith("/") ? "" : "/") + forecastType + queryStrings; }, // Build the list of headers for the request diff --git a/modules/default/weather/providers/weatherbit.js b/modules/default/weather/providers/weatherbit.js index 17f27812..9023f742 100644 --- a/modules/default/weather/providers/weatherbit.js +++ b/modules/default/weather/providers/weatherbit.js @@ -72,7 +72,7 @@ WeatherProvider.register("weatherbit", { // Create a URL from the config and base URL. getUrl() { const units = this.units[this.config.units] || "auto"; - return `${this.config.apiBase}${this.config.weatherEndpoint}?lat=${this.config.lat}&lon=${this.config.lon}&units=${units}&key=${this.config.apiKey}`; + return this.getCorsUrl() + `${this.config.apiBase}${this.config.weatherEndpoint}?lat=${this.config.lat}&lon=${this.config.lon}&units=${units}&key=${this.config.apiKey}`; }, // Implement WeatherDay generator. diff --git a/modules/default/weather/providers/weathergov.js b/modules/default/weather/providers/weathergov.js index 31934784..cf7912c3 100755 --- a/modules/default/weather/providers/weathergov.js +++ b/modules/default/weather/providers/weathergov.js @@ -40,7 +40,7 @@ WeatherProvider.register("weathergov", { // Called to set the config, this config is the same as the weather module's config. setConfig: function (config) { this.config = config; - (this.config.apiBase = "https://api.weather.gov"), this.fetchWxGovURLs(this.config); + (this.config.apiBase = this.getCorsUrl() + "https://api.weather.gov"), this.fetchWxGovURLs(this.config); }, // Called when the weather provider is about to start. diff --git a/modules/default/weather/weatherprovider.js b/modules/default/weather/weatherprovider.js index 5721dad6..e2a9d32f 100644 --- a/modules/default/weather/weatherprovider.js +++ b/modules/default/weather/weatherprovider.js @@ -111,6 +111,15 @@ const WeatherProvider = Class.extend({ this.delegate.updateAvailable(this); }, + getCorsUrl: function () { + const url = window.config.address + ":" + window.config.port + "/cors?url="; + if (window.config.useHttps) { + return "https://" + url; + } else { + return "http://" + url; + } + }, + // A convenience function to make requests. It returns a promise. fetchData: function (url, method = "GET", data = null) { const getData = function (mockData) {