五分钟教你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获取

相关推荐
用户6000718191012 小时前
【翻译】简化 TSRX
前端
luckdewei12 小时前
那个用 passlib 做认证的新同事,上线第一天就把用户密码写进了日志
后端
IT乐手12 小时前
佛德角逼平西班牙,国足还有啥借口?
前端
ping某13 小时前
为什么 Nginx 明明监听了 80,转发后端时却用了 4xxxx 端口?
后端·nginx
JustHappy13 小时前
我汇总了身边朋友的经历才发现,其实第一份实习是最难找的......
前端·后端·面试
uhakadotcom13 小时前
在python 的 工程化架构中 ,什么是 薄包装器层?
后端·面试·github
星栈13 小时前
Dioxus 的响应式系统:`Signal`、`Memo`、`Effect` 和异步状态到底该怎么分工
前端·前端框架
yingyima13 小时前
Java 正则表达式:比你想象的更强大
前端
yuanyxh16 小时前
macOS 应用 - 纯对话生成
前端·macos·ai编程
大家的林语冰16 小时前
ES5 凉凉,Babel 8 正式发布,默认不再编译为 ES5 和 CJS......
前端·javascript·前端工程化