-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (51 loc) · 1.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
import https from 'https'
import core from '@actions/core'
// https://docs.github.com/en/actions/creating-actions/creating-a-javascript-action
// https://github.com/actions/toolkit/tree/main/packages/core
// domains for test:
// - normal: google.com
// - expired: httpforever.com
// - error: anydomaintest.io
// - no certificate: It should never happen
/**
* Get SSL certificate
* @param {string} domain
* @returns {Promise<any>}
*/
const getSSLCertificate = (domain) => {
return new Promise((resolve, reject) => {
const options = { hostname: domain, port: 443, method: 'GET', agent: false }
const request = https.request(options, response => {
resolve(response.socket.getPeerCertificate())
})
request.on('error', reject)
request.end()
})
}
const main = async () => {
try {
const domain = core.getInput('domain', { required: true, trimWhitespace: true })
const certificate = await getSSLCertificate(domain)
if (certificate.valid_to) {
const date = new Date(certificate.valid_to)
const expireDate = date.toString()
const remainingDays = Math.floor((date - new Date()) / (24 * 60 * 60 * 1000))
core.info(`ssl-expire-date: ${expireDate}`)
core.info(`ssl-expire-days-left: ${remainingDays} days`)
core.setOutput("ssl-expire-date", expireDate)
core.setOutput("ssl-expire-days-left", remainingDays)
} else {
core.setFailed(`Unable to get SSL-certificate expiration date for domain ${domain}`)
}
} catch (error) {
if (error?.code === 'CERT_HAS_EXPIRED') {
core.warning(`ssl-expire-date: INVALID`)
core.warning(`ssl-expire-days-left: -1 (expired)`)
core.setOutput("ssl-expire-date", "INVALID")
core.setOutput("ssl-expire-days-left", -1)
} else {
core.setFailed(error)
}
}
}
main()