|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "encoding/json" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "log" |
| 9 | + "net" |
| 10 | + "net/http" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +// init 函数,初始化参数 |
| 15 | +func init() { |
| 16 | + // 传递参数 -l 指定域名 |
| 17 | + flag.StringVar(&Domain, "l", "", "Please enter a valid HTTPS link, multiple domains are separated by commas") |
| 18 | + // 支持传递参数 -t 指定超时时间,单位为秒 |
| 19 | + flag.IntVar(&TimeOut, "t", 5, "Please enter a valid timeout, the unit is seconds, the default is 5 seconds") |
| 20 | + // 支持传递参数 -w 指定是否开启 HTTP 服务 |
| 21 | + flag.BoolVar(&Weh, "w", false, "This parameter is used to enable web,default is false,default port is 8080") |
| 22 | + // 支持传递参数 -a 指定 HTTP 服务的地址 |
| 23 | + flag.StringVar(&Address, "a", "0.0.0.0", "This parameter is used to specify the address of the HTTP service") |
| 24 | + // 支持传递参数 -p 指定 HTTP 服务的端口 |
| 25 | + flag.StringVar(&Port, "p", "8080", "This parameter is used to specify the port of the HTTP service") |
| 26 | + // 解析传递的参数到 Domain 这个变量 |
| 27 | + flag.Parse() |
| 28 | +} |
| 29 | + |
| 30 | +// checkCertExpiration 函数,传递一个域名和超时时间,返回一个切片和错误信息 |
| 31 | +func checkCertExpiration(d string, t time.Duration) ([]byte, error) { |
| 32 | + // 创建 TCP 连接 |
| 33 | + conn, err := net.DialTimeout("tcp", d+":443", t*time.Second) |
| 34 | + if err != nil { |
| 35 | + return nil, err |
| 36 | + } |
| 37 | + // 函数执行完后关闭连接 |
| 38 | + defer conn.Close() |
| 39 | + |
| 40 | + // 配置 TLS 的参数,ServerName 为域名,也就是我们调用函数时传递的参数 |
| 41 | + config := &tls.Config{ |
| 42 | + ServerName: d, |
| 43 | + } |
| 44 | + |
| 45 | + // 创建一个 TLS 的连接 |
| 46 | + tlsConn := tls.Client(conn, config) |
| 47 | + // 函数执行完后关闭连接 |
| 48 | + defer tlsConn.Close() |
| 49 | + |
| 50 | + // 创建一个 TLS 的握手 |
| 51 | + err = tlsConn.Handshake() |
| 52 | + if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + |
| 56 | + // 获取证书信息,返回的是一个切片 |
| 57 | + certs := tlsConn.ConnectionState().PeerCertificates |
| 58 | + for _, cert := range certs { |
| 59 | + info := CertInfo{ |
| 60 | + Domain: d, |
| 61 | + Subject: cert.Subject.CommonName, |
| 62 | + ExpiresOn: cert.NotAfter.Format("2006-01-02"), |
| 63 | + DaysLeft: int(cert.NotAfter.Sub(time.Now()).Hours() / 24), |
| 64 | + } |
| 65 | + // 将结构体转换为 JSON 格式 |
| 66 | + return json.Marshal(info) |
| 67 | + } |
| 68 | + |
| 69 | + return nil, nil |
| 70 | +} |
| 71 | + |
| 72 | +// handleCheckCertExpiration 函数,处理 HTTP 请求 |
| 73 | +func handleCheckCertExpiration(w http.ResponseWriter, r *http.Request) { |
| 74 | + // 获取 Query 的 domain 参数 |
| 75 | + domain := r.URL.Query().Get("domain") |
| 76 | + if domain == "" { |
| 77 | + http.Error(w, "Please provide a domain", http.StatusBadRequest) |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + // 执行函数并获取返回值 |
| 82 | + data, err := checkCertExpiration(domain, time.Duration(TimeOut)) |
| 83 | + if err != nil { |
| 84 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + // 设置响应头 |
| 89 | + w.Header().Set("Content-Type", "application/json") |
| 90 | + // 写入数据 |
| 91 | + w.Write(data) |
| 92 | +} |
| 93 | + |
| 94 | +// Run 函数,执行函数 |
| 95 | +func Run() { |
| 96 | + // 判断是否开启 HTTP 服务 |
| 97 | + if Weh { |
| 98 | + http.HandleFunc("/check", handleCheckCertExpiration) |
| 99 | + err := http.ListenAndServe(Address+":"+Port, nil) |
| 100 | + if err != nil { |
| 101 | + return |
| 102 | + } |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + // 如果传递的参数为空,打印提示信息 |
| 107 | + if Domain == "" { |
| 108 | + fmt.Println("Please enter a valid HTTPS link") |
| 109 | + return |
| 110 | + } |
| 111 | + |
| 112 | + // 执行函数并获取返回值 |
| 113 | + data, err := checkCertExpiration(Domain, time.Duration(TimeOut)) |
| 114 | + if err != nil { |
| 115 | + log.Fatal(err) |
| 116 | + } else { |
| 117 | + fmt.Println(string(data)) |
| 118 | + } |
| 119 | +} |
0 commit comments