-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
186 lines (150 loc) · 4.29 KB
/
index.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
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
183
184
185
186
'use strict';
const path = require('path');
const { app, BrowserWindow, ipcMain, Notification } = require('electron');
/// const {autoUpdater} = require('electron-updater');
const { is } = require('electron-util');
const getCertificate = require('./GetCertificate');
const DataStore = require('./DataStore');
try {
require('electron-reloader')(module);
} catch (_) { }
const monitorData = new DataStore({ name: 'Monitors' })
// Uncomment this before publishing your first version.
// It's commented out as it throws an error if there are no published versions.
// if (!is.development) {
// const FOUR_HOURS = 1000 * 60 * 60 * 4;
// setInterval(() => {
// autoUpdater.checkForUpdates();
// }, FOUR_HOURS);
//
// autoUpdater.checkForUpdates();
// }
// Prevent window from being garbage collected
let mainWindow, addWebsiteWin;
const createMainWindow = async () => {
const win = new BrowserWindow({
title: app.name,
show: false,
width: 900,
height: 700,
resizable: false,
webPreferences: {
//devTools: true,
nodeIntegration: true
}
});
win.on('ready-to-show', () => {
win.show();
setTimeout(() => {
win.webContents.send('websites', monitorData.websites)
}, 1);
});
win.on('closed', () => {
// Dereference the window
// For multiple windows store them in an array
mainWindow = undefined;
});
win.removeMenu();
//win.setAlwaysOnTop(true);
await win.loadFile(path.join(__dirname, 'renderer/index.html'));
return win;
};
// Prevent multiple instances of the app
if (!app.requestSingleInstanceLock()) {
app.quit();
}
if (process.platform === 'win32')
{
app.setAppUserModelId(app.name);
}
app.on('second-instance', () => {
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.show();
}
});
app.on('window-all-closed', () => {
if (!is.macos) {
app.quit();
}
});
app.on('activate', async () => {
if (!mainWindow) {
mainWindow = await createMainWindow();
}
});
function get_days_left(date_string) {
var now = Date.now();
var then = new Date(Date.parse(date_string));
var days_left = Math.round((then - now) / 86400000);
return days_left;
};
(async () => {
await app.whenReady();
mainWindow = await createMainWindow();
if(monitorData.websites.length > 0) {
let notificationOptions = {
title: "Monitors",
body: "All monitors look good.",
icon: 'icons/nic-success.png'
}
let expiringWebsites = monitorData.websites.filter(website => {return get_days_left(website.info.valid_to) < 21}).length
if (expiringWebsites > 0) {
notificationOptions.body = `${expiringWebsites} monitor${expiringWebsites == 1 ? "" : "s"} needs your attention.`
notificationOptions.icon = 'icons/nic-error.png';
}
new Notification(notificationOptions).show();
}
ipcMain.on("get-websites", (event) => {
mainWindow.send('websites', monitorData.websites);
})
// add-website
ipcMain.on('add-website', (event, website) => {
const websiteExists = monitorData.websites.filter(item => {
return item.server == website.host
});
if(websiteExists.length > 0) {
mainWindow.send("website-exists");
} else {
getCertificate(website.host)
.then(data => {
if(data.errorCode) {
mainWindow.send('website-error', data);
} else {
data.name = website.name;
const updatedWebsites = monitorData.addWebsite(data).websites
mainWindow.send('websites', updatedWebsites);
}
// })
// .catch(err => {
// console.log(err);
// mainWindow.send('websites', err);
});
}
})
// delete-website
ipcMain.on('delete-website', (event, hostname) => {
const website = monitorData.websites.filter(item => {
return item.server == hostname
});
if(website.length > 0) {
const updatedWebsites = monitorData.deleteWebsite(website[0]).websites
mainWindow.send('websites', updatedWebsites)
}
})
// update-website
ipcMain.on('refresh-website', (event, hostname) => {
const website = monitorData.getWebsite(hostname);
console.log(website);
getCertificate(website.server)
.then(data => {
if(!data.errorCode) {
data.name = website.name;
const updatedWebsites = monitorData.updateWebsite(data).websites
mainWindow.send('websites', updatedWebsites);
}
});
})
})();