-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
214 lines (198 loc) · 5.79 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
'use strict';
require('dotenv').config({silent: true});
var ssllabs = require('node-ssllabs');
var nodemailer = require('nodemailer');
var mg = require('nodemailer-mailgun-transport');
var moment = require('moment');
var fs = require('fs');
var app = require('./src/app.js');
var whois = require('whois-json');
var minimumGrade = process.env.MINIMUM_GRADE || 'C';
var laterAlert = parseInt(process.env.ALERT_MONTHLY) || 30;
var soonAlert = parseInt(process.env.ALERT_DAILY) || 7;
var options = {
host: process.env.DOMAIN_NAME,
publish: false,
startNew: true,
};
var mailOptions = {
from: process.env.MAIL_FROM || '',
to: process.env.MAIL_TO || '',
'h:Reply-To': process.env.MAIL_FROM || '',
};
var nodemailerMailgun = nodemailer.createTransport(mg({
auth: {
api_key: process.env.MAIL_API,
domain: process.env.MAIL_URI,
}
}));
// Scan for certificate
ssllabs.scan(options, function(err, host) {
var expiry = new Date(host.endpoints[0].details.cert.notAfter);
var grade = host.endpoints[0].grade;
var result = {
type: 'SSL',
grade: grade,
expiry: expiry.toString(),
};
// Output results
if (process.env.VERBOSE) {
console.log(JSON.stringify(result, null, 2));
}
// Certificate has expired
if (app.isExpired(expiry)) {
console.error('Certificate expired ' + expiry.toString());
if (process.env.EXIT_ON_EXPIRED_DATE) {
process.exit(1);
}
}
// Certificate is going to expire soon
if (app.isSoonExpiring(expiry, soonAlert)) {
mailOptions.subject = 'SSL Certificate verification - ' +
process.env.DOMAIN_NAME +
' - SSL certificate is going to expires in less than ' +
soonAlert + ' days';
if (process.env.VERBOSE) {
console.log(mailOptions.subject);
}
if (process.env.MAIL_URI) {
app.loadFile(
'src/mail.html',
mailOptions,
process.env.DOMAIN_NAME,
'less than ' + soonAlert + ' days',
function(err, options) {
if (err) {
console.error('Couldn\'t load file for sending email.');
process.exit(1);
}
nodemailerMailgun.sendMail(options, function(err, info) {
if (err) {
console.error('Couldn\'t send email.');
process.exit(1);
}
console.log('Email has been sent.');
});
});
}
}
// Certificate is going to expire later
if (app.isLaterExpiring(expiry, laterAlert)) {
mailOptions.subject = 'SSL Certificate verification - ' +
process.env.DOMAIN_NAME +
' - SSL certificate is going to expires in ' + laterAlert + ' days';
if (process.env.VERBOSE) {
console.log(mailOptions.subject);
}
if (process.env.MAIL_URI) {
app.loadFile(
'src/mail.html',
mailOptions,
process.env.DOMAIN_NAME,
laterAlert + ' days',
function(err, options) {
if (err) {
console.error('Couldn\'t load file for sending email.');
process.exit(1);
}
nodemailerMailgun.sendMail(options, function(err, info) {
if (err) {
console.error('Couldn\'t send email.');
process.exit(1);
}
console.log('Email has been sent.');
});
});
}
}
// Grade verification
if (!app.isGradeSufficient(minimumGrade, grade)) {
console.log('Certificate has not met minimum grade requirement (' +
minimumGrade + ') with grade: ' + grade);
if (process.env.EXIT_ON_INSUFFICIENT_GRADE) {
process.exit(1);
}
}
});
// Scan for DNS validity
whois(process.env.DOMAIN_NAME, function(err, data) {
if (err) {
console.log(err);
process.exit(1);
}
var result = {
type: 'DNS',
expiry: 'No Data',
};
if (data.registryExpiryDate) {
console.log('foo');
result.expiry = new Date(Date.parse(data.registryExpiryDate));
result.expiry = result.expiry.toString();
}
// Output results
if (process.env.VERBOSE) {
console.log(JSON.stringify(result, null, 2));
if (result.expiry === 'No Data') {
return;
}
}
// DNS is going to expire soon
if (app.isSoonExpiring(result.expiry, soonAlert)) {
mailOptions.subject = 'DNS verification - ' +
process.env.DOMAIN_NAME +
' - DNS is going to expires in less than ' +
soonAlert + ' days';
if (process.env.VERBOSE) {
console.log(mailOptions.subject);
}
if (process.env.MAIL_URI) {
app.loadFile(
'src/mail-dns.html',
mailOptions,
process.env.DOMAIN_NAME,
'less than ' + laterAlert + ' days',
function(err, options) {
if (err) {
console.error('Couldn\'t load file for sending email.');
process.exit(1);
}
nodemailerMailgun.sendMail(options, function(err, info) {
if (err) {
console.error('Couldn\'t send email.');
process.exit(1);
}
console.log('Email has been sent.');
});
});
}
}
// Certificate is going to expire later
if (app.isLaterExpiring(result.expiry, laterAlert)) {
mailOptions.subject = 'DNS verification - ' +
process.env.DOMAIN_NAME +
' - DNS is going to expires in ' + laterAlert + ' days';
if (process.env.VERBOSE) {
console.log(mailOptions.subject);
}
if (process.env.MAIL_URI) {
app.loadFile(
'src/mail-dns.html',
mailOptions,
process.env.DOMAIN_NAME,
laterAlert + ' days',
function(err, options) {
if (err) {
console.error('Couldn\'t load file for sending email.');
process.exit(1);
}
nodemailerMailgun.sendMail(options, function(err, info) {
if (err) {
console.error('Couldn\'t send email.');
process.exit(1);
}
console.log('Email has been sent.');
});
});
}
}
});