惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
About on SuperTechFans
WordPress大学
WordPress大学
雷峰网
雷峰网
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Latest news
Latest news
Spread Privacy
Spread Privacy
T
Threat Research - Cisco Blogs
T
Tor Project blog
博客园 - Franky
U
Unit 42
K
Kaspersky official blog
博客园_首页
G
GRAHAM CLULEY
美团技术团队
I
Intezer
T
The Exploit Database - CXSecurity.com
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
The Hacker News
The Hacker News
B
Blog
云风的 BLOG
云风的 BLOG
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Securelist
Last Week in AI
Last Week in AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
M
MIT News - Artificial intelligence
Martin Fowler
Martin Fowler
Schneier on Security
Schneier on Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
SegmentFault 最新的问题
W
WeLiveSecurity
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
J
Java Code Geeks
Cyberwarzone
Cyberwarzone
爱范儿
爱范儿
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
The GitHub Blog
The GitHub Blog
S
Security @ Cisco Blogs
GbyAI
GbyAI
P
Proofpoint News Feed
D
Docker
D
DataBreaches.Net
aimingoo的专栏
aimingoo的专栏
博客园 - 司徒正美
T
Tenable Blog
V2EX - 技术
V2EX - 技术
The Register - Security
The Register - Security
V
Vulnerabilities – Threatpost
B
Blog RSS Feed

轶哥博客

blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog
blog
2022-01-28 · via 轶哥博客

部分程序配置了免费 SSL 证书更新程序,但是更新程序更新证书失败并不会通知到管理员,可能导致业务异常。通过针对域名的 SSL 证书过期监测小程序,作为二道防火墙,避免事故发生。

需求

  1. 以 cron 风格定时监测 SSL 证书过期和可用状态。
  2. 提前 2 天发送短信通知管理员(需要支持多手机号)。
  3. 可以在线维护通知列表(域名+手机号)。
  4. 程序要尽量简单,尽量降低后续维护成本。
  5. 开发要尽可能快。

分析

  1. 以简单的 js 脚本在 node.js 平台实现程序,利用现成的node-schedule库实现定时任务。
  2. 通过axios调用第三方接口实现短信发送,参数为手机号数组+域名+即将过期天数/-过期天数
  3. 通过 HTML + JS 实现通知列表的可视化维护,通过 Basic Auth 进行身份认证。需要一个 Web 服务器实现服务器端向客户端输出 HTML 数据和 HTTP API,选择方便快捷的fastify及其插件fastify-basic-auth
  4. 通过 Node.js 的https相关 API 实现对证书信息的获取。

实现

安装可能用到的依赖:

yarn add node-schedule axios fastify fastify-basic-auth

获取某个域名的 SSL 证书信息:

const https = require('https')

const req = https.request(
  {
    host: 'www.wyr.me',
    port: 443,
    method: 'GET',
    rejectUnauthorized: false,
    agent: new https.Agent({
      maxCachedSessions: 0,
    }),
  },
  (res) => {
    console.log(res.connection.getPeerCertificate())
  }
)

req.end()

其中,agent: new https.Agent({ maxCachedSessions: 0 })是必须的,否则第二次请求的时候将无法获得证书信息。

短信模板:

【SSL监控】@var(host)的SSL证书@(day)天过期(负数为已过期)。请及时处理!

其余代码实现参阅: https://github.com/yi-ge/ssl-checker/blob/main/main.js

总共不到两百行,代码的意图很明显了,不再赘述。

可视化配置编辑界面

sslchecker.png

2022年02月08日更新:支持添加注释,优化空格检测,支持不填写手机号。

开源仓库地址

https://github.com/yi-ge/ssl-checker

在线 Demo(运行于腾讯云函数,用户名密码均为admin):https://service-pnlsi3d9-1251007030.cd.apigw.tencentcs.com

其他说明

查询资料、编写此博文及思考耗时约 25 分钟,代码编写及测试约 65 分钟。一共耗时约个半小时。

请注意,不建议直接运行于腾讯云函数,实例配置数据可能因销毁而丢失,除非使用持久化存储配置信息。腾讯云函数不支持非/tmp目录下的写入操作,因此需要先复制文件并对应修改相关文件操作:

if (!fs.existsSync('/tmp/config.txt')) {
  fs.copyFileSync(path.join(__dirname, 'config.txt'), '/tmp/config.txt')
  fs.copyFileSync(path.join(__dirname, 'index.html'), '/tmp/index.html')
}

...

// example
fastify.get('/api/config', (req, reply) => {
  return {
    code: 1,
    result: fs.readFileSync('/tmp/config.txt', 'utf8'),
  }
})

相比之下普通服务器部署会省事一些。