1- // Usage:
1+ // Usage
22//
3- // go run certcheck.go https://www.example.com
3+ // go run certcheck.go https://chrisshort.net
44//
5- // Output:
5+ // go run certcheck.go https://chrisshort.net 30
66//
7- // Subject: www.example.com
8- // Issuer: Google Inc
9- // Valid from: 2020-01-01 00:00:00 +0000 UTC
10- // Valid until: 2030-01-01 00:00:00 +0000 UTC
11- // Serial number: 46d1c9e7a9e9f9e4
12- // DNS Names: [www.example.com]
13- // IP Addresses: []
14- // Signature algorithm: sha256WithRSAEncryption
157
168package main
179
@@ -20,15 +12,32 @@ import (
2012 "fmt"
2113 "net/http"
2214 "os"
15+ "strconv"
16+ "time"
17+
18+ "errors"
19+
20+ "github.com/fatih/color"
2321)
2422
2523func main () {
26- if len (os .Args ) != 2 {
27- fmt .Println ("Please provide a URL (include https://)" )
24+ if len (os .Args ) < 2 || len ( os . Args ) > 3 {
25+ fmt .Println ("Please provide a URL (include https://) and an optional number of days " )
2826 os .Exit (1 )
2927 }
3028
3129 url := os .Args [1 ]
30+ var days int = - 1 // Default value if days argument is not provided
31+
32+ if len (os .Args ) == 3 {
33+ daysStr := os .Args [2 ]
34+ var err error
35+ days , err = parseDays (daysStr )
36+ if err != nil {
37+ fmt .Println ("Invalid number of days:" , err )
38+ os .Exit (1 )
39+ }
40+ }
3241
3342 tr := & http.Transport {
3443 TLSClientConfig : & tls.Config {
@@ -58,7 +67,18 @@ func main() {
5867 fmt .Printf ("Subject: %s\n " , cert .Subject .CommonName )
5968 fmt .Printf ("Issuer: %s\n " , cert .Issuer .CommonName )
6069 fmt .Printf ("Valid from: %s\n " , cert .NotBefore )
61- fmt .Printf ("Valid until: %s\n " , cert .NotAfter )
70+ fmt .Printf ("Valid until: %s" , cert .NotAfter )
71+
72+ if days != - 1 {
73+ daysLeft := int (time .Until (cert .NotAfter ).Hours () / 24 )
74+ if daysLeft <= days {
75+ color .Set (color .Bold , color .FgRed )
76+ fmt .Printf (" (%d days left)" , daysLeft )
77+ color .Unset ()
78+ }
79+ }
80+
81+ fmt .Println ()
6282 fmt .Printf ("Serial number: %s\n " , cert .SerialNumber .String ())
6383 fmt .Printf ("DNS Names: %v\n " , cert .DNSNames )
6484 fmt .Printf ("IP Addresses: %v\n " , cert .IPAddresses )
@@ -67,8 +87,21 @@ func main() {
6787 }
6888
6989 if validChain {
90+ color .Set (color .Bold , color .FgGreen )
7091 fmt .Println ("Certificate chain is valid and in the correct order." )
7192 } else {
93+ color .Set (color .Bold , color .FgRed )
7294 fmt .Println ("Certificate chain is invalid or not in the correct order." )
7395 }
7496}
97+
98+ func parseDays (daysStr string ) (int , error ) {
99+ days , err := strconv .Atoi (daysStr )
100+ if err != nil {
101+ return 0 , err
102+ }
103+ if days < 0 {
104+ return 0 , errors .New ("number of days cannot be negative" )
105+ }
106+ return days , nil
107+ }
0 commit comments