forked from sheyabernstein/MMM-pihole-stats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
79 lines (66 loc) · 2.46 KB
/
node_helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const Log = require("logger");
const NodeHelper = require("node_helper");
module.exports = NodeHelper.create({
start () {
Log.info(`Starting node_helper for module [${this.name}]`);
},
socketNotificationReceived (notification, payload) {
if (notification === "GET_PIHOLE") {
const config = payload.config;
if (!this.isValidURL(config.apiURL)) {
Log.error(`${this.name}: The apiURL is not a valid URL`);
return;
}
Log.debug(`Notification: ${notification} Payload: ${payload}`);
this.getPiholeData(config, { summary: 1 }, "PIHOLE_DATA");
if (config.showSources && config.sourcesCount > 0) {
if (config.showSources && !config.apiToken) {
Log.error(`${this.name}: Can't load sources because the apiKey is not set.`);
} else {
this.getPiholeData(
config,
{ getQuerySources: config.sourcesCount },
"PIHOLE_SOURCES"
);
}
}
}
},
isValidURL (url) {
try {
new URL(url);
return true;
} catch (_) {
return false;
}
},
buildURL (config, params) {
params = params || {};
if (config.apiToken && !params.hasOwnProperty("auth")) {
params.auth = config.apiToken;
}
const url = new URL(config.apiURL);
url.search = new URLSearchParams(params).toString();
return url.toString();
},
async getPiholeData (config, params, notification) {
const self = this,
url = self.buildURL(config, params),
headers = { Referer: url };
this.sendSocketNotification("LOADING_PIHOLE_URL", url);
try {
const response = await fetch(url, { headers });
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
if (response.headers.get('content-type').includes('application/json')) {
const data = await response.json();
self.sendSocketNotification(notification, data);
} else {
throw new Error(`Expected JSON but received ${response.headers.get('content-type')}`);
}
} catch (error) {
Log.error(self.name + " ERROR:", error);
}
},
});