-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
check.js
executable file
·165 lines (152 loc) · 6.65 KB
/
check.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
#!/usr/bin/env node
import sslChecker from 'ssl-checker-node-api';
import axios from 'axios';
import { program } from 'commander';
import Conf from 'conf';
import moment from 'moment';
import * as emoji from 'node-emoji'
import ora from 'ora';
import updateNotifier from 'update-notifier';
import { readFileSync } from "fs";
const packageJSON = JSON.parse(readFileSync(new URL("./package.json", import.meta.url)));
updateNotifier({pkg: packageJSON}).notify();
const config = new Conf({
projectName: 'ssl-expiry-reminder'
});
program.version(packageJSON.version)
.option('-d, --domain <domain name>', 'Add domain without http/https (This Command line Argument for Automation Task)')
.option('-s, --status <domain name>', 'Add domain without http/https')
.option('-g, --gotify <GOTIFY API URL>', 'Gotify URL with Application Key')
.option('-t, --telegram <Telegram API URL>', 'Telegram API URL with your Bot Key')
.option('-c, --chatid <Telegram Chat or Channel ID>', 'Telegram Channel ID or Chat ID')
.option('-r, --remainder <Enter the Day Remaining 1 to 10>', 'Enter the Remaining Day to Get SSL Expiry Remainder Alert')
program.parse(process.argv);
function sendMessage(message) {
var TELEGRAM_URL = config.get('telegramkey');
var TELEGRAM_CHATID = config.get('telegramchatid');
if (TELEGRAM_CHATID && TELEGRAM_URL) {
var url = TELEGRAM_URL
var bodyFormData = {
chat_id: TELEGRAM_CHATID,
parse_mode: 'html',
text: message,
}
axios({
method: 'post',
headers: {
'Content-Type': 'application/json'
},
url: url,
data: bodyFormData,
})
.then(function(response) {
console.log(response.data);
})
.catch(function(error) {
if (!error.response) {
console.log('Telegram API URL is Missing');
} else {
console.log('Something Went Wrong - Enter the Correct Telegram API URL or Chat ID');
}
});
} else {
console.log('Config Error: Telegram BOT API Key or Chat ID is Missing');
}
}
function gotifyMessage(hello) {
var GOTIFY_API = config.get('key');
if (GOTIFY_API) {
var url = GOTIFY_API
var bodyFormData = {
title: 'SSL Expiry reminder',
message: hello,
priority: 5
};
axios({
method: 'post',
headers: {
'Content-Type': 'application/json'
},
url: url,
data: bodyFormData,
})
.then(function(response) {
console.log(response.data);
})
.catch(function(error) {
if (!error.response) {
console.log('Gotify API URL is Missing');
} else {
console.log('Something Went Wrong - Enter the Correct API URL');
}
});
} else {
console.log('Config Error: Gotify API URL and Key is Missing');
}
}
const options = program.opts();
if (options.gotify) {
config.set('key', options.gotify);
var GOTIFY_URL = config.get('key');
console.log('your Gotify API Key is Stored\t' + GOTIFY_URL);
} else if (options.telegram) {
config.set('telegramkey', options.telegram);
var TELEGRAM_URL = config.get('telegramkey');
console.log('your Telegram API URL is Stored\t' + TELEGRAM_URL);
} else if (options.chatid) {
config.set('telegramchatid', options.chatid);
var TELEGRAM_CHATID = config.get('telegramchatid');
console.log('your Telegram Chat ID is Stored\t' + TELEGRAM_CHATID);
} else if (options.remainder) {
config.set('sslremainder', options.remainder);
var SSL_REMAINDER = config.get('sslremainder');
console.log('your SSL Expiry Remainder day saved\t' + SSL_REMAINDER);
} else if (options.domain) {
const lval = options.domain;
sslChecker(lval).then((certdata) => {
console.log(certdata);
var startdate = new Date(certdata.validFrom);
var enddate = new Date(certdata.validTo);
var certstart = moment(startdate);
var certend = moment(enddate);
sendMessage(lval + '\n' + 'Certificate Valid from \t' + emoji.get("raised_hand_with_fingers_splayed") + '\n' + certstart.format('LLLL') + '\n' + 'Certificate Expiry date \t' + emoji.get("point_down") + '\n' + certend.format('LLLL') + '\n' + 'Days Remaining \t' + emoji.get("clock8") + '\t' + certdata.daysRemaining);
gotifyMessage(lval + '\n' + 'Certificate Valid from \t' + emoji.get("raised_hand_with_fingers_splayed") + '\n' + certstart.format('LLLL') + '\n' + 'Certificate Expiry date \t' + emoji.get("point_down") + '\n' + certend.format('LLLL') + '\n' + 'Days Remaining \t' + emoji.get("clock8") + '\t' + certdata.daysRemaining);
var SSL_REMAINDER = config.get('sslremainder');
if (certdata.daysRemaining == 5 || certdata.daysRemaining == SSL_REMAINDER) {
sendMessage('Status: Oops time to Renew SSL for \t' + lval + '\t' + emoji.get("rotating_light"));
gotifyMessage('Status: Oops time to Renew SSL \t' + lval + '\t' + emoji.get("rotating_light"));
}
}).catch((err) => {
if (err.code === 'ENOTFOUND') {
console.log('Fix Hostname or Provide Correct Domain Name');
} else if (err.code === 'ECONNREFUSED') {
console.log('Fix Hostname or Provide Correct Domain Name');
}
});
} else if (options.status) {
const userdomain = options.status;
const spinner = new ora({
text: 'Fetching SSL Data...',
spinner: 'hamburger'
});
spinner.start();
sslChecker(userdomain).then((certdata) => {
var startdate = new Date(certdata.validFrom);
var enddate = new Date(certdata.validTo);
var certstart = moment(startdate);
var certend = moment(enddate);
var provider = certdata.issuer
spinner.stop();
console.log(userdomain + '\n' + 'Certificate Valid from' + '\n' + certstart.format('LLLL') + '\n' + 'Certificate Expiry date' + '\n' + certend.format('LLLL') + '\n' + 'Days Remaining:' + '\t' + certdata.daysRemaining + '\n' + 'Provider: ' + provider);
}).catch((err) => {
if (err.code === 'ENOTFOUND') {
spinner.stop();
console.log('Fix Hostname or Provide Correct Domain Name');
} else if (err.code === 'ECONNREFUSED') {
spinner.stop();
console.log('Fix Hostname or Provide Correct Domain Name');
}
});
} else {
console.log('Please Enter a Valid Option For More Info Run: checkssl -h');
}