Skip to content

Commit 6eec69e

Browse files
authored
Merge pull request #3 from chris-short/days
Adding Days Left Functionality
2 parents 435cb12 + 9a84d13 commit 6eec69e

File tree

4 files changed

+100
-15
lines changed

4 files changed

+100
-15
lines changed

README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ A Go program to display certificate chains and validate their order in the same
66

77
certcheck https://chrisshort.net
88

9-
## Example Output
9+
certcheck https://chrisshort.net 90
10+
11+
### Example Output (no days)
1012

1113
Subject: *.chrisshort.net
1214
Issuer: R3
@@ -35,6 +37,38 @@ A Go program to display certificate chains and validate their order in the same
3537
IP Addresses: []
3638
Signature algorithm: SHA256-RSA
3739
-----
40+
Certificate chain is valid and in the correct order.
41+
42+
### Example Output with Days
43+
44+
Subject: *.chrisshort.net
45+
Issuer: R3
46+
Valid from: 2023-04-25 02:30:44 +0000 UTC
47+
Valid until: 2023-07-24 02:30:43 +0000 UTC (66 days left)
48+
Serial number: 403588798235445259445834570997555816122123
49+
DNS Names: [*.chrisshort.net chrisshort.net]
50+
IP Addresses: []
51+
Signature algorithm: SHA256-RSA
52+
-----
53+
Subject: R3
54+
Issuer: ISRG Root X1
55+
Valid from: 2020-09-04 00:00:00 +0000 UTC
56+
Valid until: 2025-09-15 16:00:00 +0000 UTC
57+
Serial number: 192961496339968674994309121183282847578
58+
DNS Names: []
59+
IP Addresses: []
60+
Signature algorithm: SHA256-RSA
61+
-----
62+
Subject: ISRG Root X1
63+
Issuer: DST Root CA X3
64+
Valid from: 2021-01-20 19:14:03 +0000 UTC
65+
Valid until: 2024-09-30 18:14:03 +0000 UTC
66+
Serial number: 85078200265644417569109389142156118711
67+
DNS Names: []
68+
IP Addresses: []
69+
Signature algorithm: SHA256-RSA
70+
-----
71+
Certificate chain is valid and in the correct order.
3872

3973
## About
4074

certcheck.go

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
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

168
package 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

2523
func 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+
}

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
module certcheck
22

33
go 1.18
4+
5+
require github.com/fatih/color v1.15.0
6+
7+
require (
8+
github.com/mattn/go-colorable v0.1.13 // indirect
9+
github.com/mattn/go-isatty v0.0.17 // indirect
10+
golang.org/x/sys v0.6.0 // indirect
11+
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
2+
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
3+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
4+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
5+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
6+
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
7+
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
8+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
9+
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
10+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

0 commit comments

Comments
 (0)