-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI_manager.js
More file actions
182 lines (163 loc) · 7.28 KB
/
GUI_manager.js
File metadata and controls
182 lines (163 loc) · 7.28 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const $ = require('jquery');
const sqlite3 = require('sqlite3').verbose();
const { getUserDataDir } = require('./userpath');
const path = require('path');
const fs = require('fs');
const {addIp, getAllIps, updateIp, deleteIp, addWebHook, getAllWebhooks, updateWebhook, deleteWebhook, initialiseDatabase, insertDefaultWebhookData, incrementElapsedTimes} = require('./database');
const Logger = require('./logger');
const dbPath = path.join(getUserDataDir(), 'webhooks.db');
const db = new sqlite3.Database(dbPath);
class GUIManager {
static async attachWebhookToggleHandlers() {
const names = ['zero', 'one', 'two', 'three', 'four'];
names.forEach((name, index) => {
const buttonId = `#webhook${name}-btn`;
$(document).off('click', buttonId);
$(document).on('click', buttonId, async () => {
try {
const data = await getAllWebhooks();
const webhook = data[index];
if (!webhook) {
Logger.error(`Webhook at index ${index} not found`);
return;
}
const newStatus = !webhook.is_active;
await updateWebhook(webhook.id, { ...webhook, is_active: newStatus });
await GUIManager.loadWebhookData();
} catch (err) {
Logger.error(`Error toggling webhook ${name}:`, err);
}
});
});
}
static async load_log()
{
$('#log-btn').on('click', () => {
if($('#log-btn').text() == 'Show Log')
{
const logPath = path.join(getUserDataDir(), 'app.log');
fs.readFile(logPath, 'utf8', (err, data) => {
if (err) {
Logger.error('Failed to read log file:', err);
$('#logtext').val('Could not load log file.');
return;
}
$('#logtext').val(data);
$('#log-btn').text('Hide Log')
$('#logtext').toggle();
});
}
if($('#log-btn').text() == 'Hide Log')
{
$('#logtext').val('');
$('#log-btn').text('Show Log')
$('#logtext').toggle();
}
});
}
static formatDuration(seconds) {
if (!seconds) return '0 seconds';
const units = [
{ name: 'day', seconds: 86400 },
{ name: 'hour', seconds: 3600 },
{ name: 'minute', seconds: 60 },
{ name: 'second', seconds: 1 }
];
return units.reduce((acc, unit) => {
if (seconds >= unit.seconds) {
const count = Math.floor(seconds / unit.seconds);
seconds %= unit.seconds;
acc.push(`${count} ${unit.name}${count !== 1 ? 's' : ''}`);
}
return acc;
}, []).join(' ');
}
static async loadWebhookData() {
try {
const data = await getAllWebhooks();
if (!Array.isArray(data)) {
Logger.error('Invalid data format:', data);
return;
}
const names = ['zero', 'one', 'two', 'three', 'four'];
data.forEach((webhook, index) => {
const name = names[index];
if (!name) return;
const webhookNumber = names.indexOf(name);
const activeStatus = webhook.is_active ? 'Active' : 'Inactive';
const webhookURL = webhook.url || `Error loading URL`;
const upAlerts = webhook.up_alerts || 0;
const downAlerts = webhook.down_alerts || 0;
const timeSinceLastUpAlert = webhook.time_since_last_up_alert ?? 0;
const timeSinceLastDownAlert = webhook.time_since_last_down_alert ?? 0;
const successName = webhook.success_name || `success${webhookNumber}.wav`;
const errorName = webhook.error_name || `error${webhookNumber}.wav`;
const buttonText = webhook.is_active ? 'DISABLE' : 'ENABLE';
$(`#webhook${name}`).text(`Webhook${webhookNumber}`);
$(`#webhook${name}-url strong`).text(activeStatus);
$(`#webhook${name}-url .summary`).text(webhookURL);
$(`#webhook${name}-upcnt strong`).text(`✅Up alerts (${upAlerts})`);
$(`#webhook${name}-downcnt strong`).text(`❌Down alerts (${downAlerts})`);
$(`#wb${webhookNumber}upalt`).text(`${GUIManager.formatDuration(timeSinceLastUpAlert)}`);
$(`#wb${webhookNumber}dwnalt`).text(`${GUIManager.formatDuration(timeSinceLastDownAlert)}`);
$(`#webhook${name}-success strong`).text(`✅${successName}`);
$(`#webhook${name}-error strong`).text(`❌${errorName}`);
$(`.webhook.${name} button`).text(buttonText);
});
} catch (error) {
Logger.error('Error loading webhooks:', error);
}
}
static async loadIpWhitelist() {
try {
const ips = await new Promise((resolve, reject) => {
getAllIps((err, rows) => {
if (err) reject(err);
else resolve(rows.map(row => row.allowed_ip).join(', '));
});
});
$('#whitelist').val(ips);
} catch (error) {
Logger.error('Failed to load IP whitelist:', error);
}
}
static startIpMonitoring() {
setInterval(async () => {
try {
const currentIps = await new Promise((resolve, reject) => {
getAllIps((err, rows) => {
if (err) reject(err);
else resolve(rows.map(row => row.allowed_ip));
});
});
const textareaContent = $('#whitelist').val().trim();
const enteredIps = textareaContent.split(',')
.map(ip => ip.trim().replace(/^::ffff:/, ''))
.filter(ip => ip.length > 0);
if (JSON.stringify(currentIps) !== JSON.stringify(enteredIps)) {
await new Promise(resolve => {
db.run(`DELETE FROM ip_whitelist`, (err) => {
if (err) Logger.error(err);
resolve();
});
});
const stmt = db.prepare(`INSERT INTO ip_whitelist (allowed_ip) VALUES (?)`);
for (const ip of enteredIps) {
await new Promise(resolve => {
stmt.run(ip, (err) => {
if (err) Logger.error(err);
resolve();
});
});
}
stmt.finalize();
$('#whitelist').val(enteredIps.join(', '));
}
} catch (error) {
Logger.error(`IP sync error: ${error?.message || error}`);
console.error(error);
}
}, 5000);
}
}
module.exports = { GUIManager };