五分钟教你SSL证书自动巡检与过期提醒

点击在线阅读,体验更好 链接
现代JavaScript高级小册 链接
深入浅出Dart 链接
现代TypeScript高级小册 链接
linwu的算法笔记📒 链接

引言

最近我的主站编程时光证书过期了,导致用户访问网站时候出错,后面续上免费的证书,由于我是使用的免费的证书,所以证书的有效期只有一年。为了避免证书过期导致网站无法访问,我决定写一个小程序来自动检查证书的过期时间,并在证书快过期时通知我及时更换证书。

自动巡检SSL证书过期时间

我们首先定义了一个checkCertificates函数,该函数的主要任务是读取一个名为domain.txt的文件,该文件中列出了需要检查的域名和对应的IP地址。

go 复制代码
func checkCertificates() {
 file, err := os.Open("domain.txt")
 if err != nil {
  panic(err)
 }
 defer file.Close()

 scanner := bufio.NewScanner(file)
 for scanner.Scan() {
  line := scanner.Text()
  parts := strings.Split(line, ":")
  domain := parts[0]
  ipPool := strings.Split(strings.ReplaceAll(parts[1], ",", " "), " ")
  // ipPool [43.138.235.240]
  fmt.Println("ipPool", ipPool)
  for _, ip := range ipPool {
   conn, err := tls.Dial("tcp", ip+":443", &tls.Config{
    ServerName:         domain,
    InsecureSkipVerify: true, // We're only checking expiration, not validity
   })
   if err != nil {
    fmt.Printf("Error! %s\n", ip)
    fmt.Println(domain)
    continue
   }
   defer conn.Close()

   cert := conn.ConnectionState().PeerCertificates[0]
   endDate := cert.NotAfter
   currentTime := time.Now()
   remainingDays := endDate.Sub(currentTime).Hours() / 24

   fmt.Printf("ip: %s\ndomain: %s\n", ip, domain)

   sendEmail("SSL证书过期提醒", fmt.Sprintf("The certificate for domain %s (IP: %s) will expire in less than %f days!", domain, ip, remainingDays))
   if remainingDays < 7 && remainingDays >= 0 {
    fmt.Println("剩余时间小于七天!请及时更换证书!")
    fmt.Printf("ip: %s, %s\n", ip, domain)
   } else if remainingDays < 0 {
    fmt.Println("证书已过期!请及时更换证书!")
   } else {
    fmt.Printf("剩余天数为:%f\n", remainingDays)
   }
  }
 }

 if err := scanner.Err(); err != nil {
  panic(err)
 }
}

邮件通知

当我们知道了证书的剩余有效期后,下一步是通知相关人员。在sendEmail函数中,我们使用了gomail库来发送邮件。

go 复制代码
func sendEmail(subject, body string) {
    from := "linwu.hi@gmail.com"
    pass := "xxx"
    to := "linwu.hi@gmail.com"

    m := gomail.NewMessage()
    m.SetHeader("From", from)
    m.SetHeader("To", to)
    m.SetHeader("Subject", subject)
    m.SetBody("text/plain", body)

    d := gomail.NewDialer("smtp.gmail.com", 587, from, pass)

    if err := d.DialAndSend(m); err != nil {
        log.Fatal(err)
    }
}

定时任务

为了确保我们能够及时地检测到证书的状态,我们需要定期执行上述的检查任务。在main函数中,我们使用了cron库来创建一个定时任务。

go 复制代码
func main() {
    c := cron.New(cron.WithSeconds())
    c.AddFunc("0 0 0 * * ?", checkCertificates) // 每日凌晨0点执行, 检查证书过期时间,通知到老板
    c.Start()
    select {} // 阻止主goroutine退出
}

完整代码

go 复制代码
package main

import (
 "bufio"
 "crypto/tls"
 "fmt"
 gomail "gopkg.in/gomail.v2"
 "log"
 "os"
 "strings"
 "time"
)

func sendEmail(subject, body string) {
 from := "linwu.hi@gmail.com"
 pass := "lbqhjuvomebvwtox"
 to := "linwu.hi@gmail.com"

 m := gomail.NewMessage()
 m.SetHeader("From", from)
 m.SetHeader("To", to)
 m.SetHeader("Subject", subject)
 m.SetBody("text/plain", body)

 d := gomail.NewDialer("smtp.gmail.com", 587, from, pass)

 // Send the email
 if err := d.DialAndSend(m); err != nil {
  log.Fatal(err)
 }
}

func checkCertificates() {
 file, err := os.Open("domain.txt")
 if err != nil {
  panic(err)
 }
 defer file.Close()

 scanner := bufio.NewScanner(file)
 for scanner.Scan() {
  line := scanner.Text()
  parts := strings.Split(line, ":")
  domain := parts[0]
  ipPool := strings.Split(strings.ReplaceAll(parts[1], ",", " "), " ")
  // ipPool [43.138.235.240]
  fmt.Println("ipPool", ipPool)
  for _, ip := range ipPool {
   conn, err := tls.Dial("tcp", ip+":443", &tls.Config{
    ServerName:         domain,
    InsecureSkipVerify: true, // We're only checking expiration, not validity
   })
   if err != nil {
    fmt.Printf("Error! %s\n", ip)
    fmt.Println(domain)
    continue
   }
   defer conn.Close()

   cert := conn.ConnectionState().PeerCertificates[0]
   endDate := cert.NotAfter
   currentTime := time.Now()
   remainingDays := endDate.Sub(currentTime).Hours() / 24

   fmt.Printf("ip: %s\ndomain: %s\n", ip, domain)

   sendEmail("SSL证书过期提醒", fmt.Sprintf("The certificate for domain %s (IP: %s) will expire in less than %f days!", domain, ip, remainingDays))
   if remainingDays < 7 && remainingDays >= 0 {
    fmt.Println("剩余时间小于七天!请及时更换证书!")
    fmt.Printf("ip: %s, %s\n", ip, domain)
   } else if remainingDays < 0 {
    fmt.Println("证书已过期!请及时更换证书!")
   } else {
    fmt.Printf("剩余天数为:%f\n", remainingDays)
   }
  }
 }

 if err := scanner.Err(); err != nil {
  panic(err)
 }
}

func main() {

 c := cron.New(cron.WithSeconds())           // 创建一个新的cron实例
 c.AddFunc("0 0 0 * * ?", checkCertificates) // 每日凌晨0点执行
 c.Start()                                   // 开始执行定时任务
 select {}                                   // 阻止主goroutine退出
}

小册

整理两本小册,一本是前端面试小册,一本是图解算法,阅读体验都很好,欢迎添加我微信linwu-hi获取

相关推荐
糕冷小美n7 小时前
elementuivue2表格不覆盖整个表格添加固定属性
前端·javascript·elementui
小哥不太逍遥8 小时前
Technical Report 2024
java·服务器·前端
沐墨染8 小时前
黑词分析与可疑对话挖掘组件的设计与实现
前端·elementui·数据挖掘·数据分析·vue·visual studio code
anOnion8 小时前
构建无障碍组件之Disclosure Pattern
前端·html·交互设计
threerocks8 小时前
前端将死,Agent 永生
前端·人工智能·ai编程
苍何8 小时前
即梦Seedance2.0海外火爆出圈,AI 视频的 DeepSeek 时刻来了!(附实测教程)
后端
苍何8 小时前
阿里卷麻了,千问 Qwen-Image-2.0 发布,超强文字渲染、信息图、PPT 轻松做(附实测提示词)
后端
苍何8 小时前
被马斯克疯狂点赞的国产 AI,很可能是 AI 时代的抖音!
后端
苍何8 小时前
国产Windows 版 Claude Cowork 来了,内置海量 Skills,绝了。
后端
问道飞鱼9 小时前
【前端知识】Vite用法从入门到实战
前端·vite·项目构建