From 781dffefe0352076f796b0623dff1f048d944f8a Mon Sep 17 00:00:00 2001 From: guessi Date: Sun, 15 Mar 2020 22:07:50 +0800 Subject: [PATCH] Support for non-443 port --- README.md | 58 +++++++++++++++++++++++++++---------------------------- config.go | 1 + utils.go | 30 ++++++++++++++++++++++------ 3 files changed, 54 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 1fe5d1f..a56ef88 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ go get -u github.com/guessi/ssl-certs-checker ## Examples - + install binary to your ${GOPATH} go install github.com/ssl-certs-checker @@ -15,39 +15,39 @@ check single target host certificates infomation ${GOPATH}/bin/ssl-certs-checker --hosts "www.google.com" - +----------------+----------------+----------------+-------------------------------+-------------------------------+------------+ - | Host | Common Name | DNS Names | Not Before | Not After | Issuer | - +----------------+----------------+----------------+-------------------------------+-------------------------------+------------+ - | www.google.com | www.google.com | www.google.com | 2020-02-12 11:47:41 +0000 UTC | 2020-05-06 11:47:41 +0000 UTC | GTS CA 1O1 | - +----------------+----------------+----------------+-------------------------------+-------------------------------+------------+ + +--------------------+----------------+----------------+-------------------------------+-------------------------------+------------+ + | Host | Common Name | DNS Names | Not Before | Not After | Issuer | + +--------------------+----------------+----------------+-------------------------------+-------------------------------+------------+ + | www.google.com:443 | www.google.com | www.google.com | 2020-02-12 11:47:41 +0000 UTC | 2020-05-06 11:47:41 +0000 UTC | GTS CA 1O1 | + +--------------------+----------------+----------------+-------------------------------+-------------------------------+------------+ check multiple target hosts' certificates at once ${GOPATH}/bin/ssl-certs-checker --hosts "www.google.com,www.azure.com,www.amazon.com" - +----------------+----------------+---------------------------------------+-------------------------------+-------------------------------+-----------------------+ - | Host | Common Name | DNS Names | Not Before | Not After | Issuer | - +----------------+----------------+---------------------------------------+-------------------------------+-------------------------------+-----------------------+ - | www.google.com | www.google.com | www.google.com | 2020-02-12 11:47:41 +0000 UTC | 2020-05-06 11:47:41 +0000 UTC | GTS CA 1O1 | - | www.azure.com | *.azure.com | *.azure.com | 2019-12-17 19:51:44 +0000 UTC | 2020-12-17 19:51:44 +0000 UTC | Microsoft IT TLS CA 4 | - | www.amazon.com | www.amazon.com | amazon.com | 2019-09-18 00:00:00 +0000 UTC | 2020-08-23 12:00:00 +0000 UTC | DigiCert Global CA G2 | - | | | amzn.com | | | | - | | | uedata.amazon.com | | | | - | | | us.amazon.com | | | | - | | | www.amazon.com | | | | - | | | www.amzn.com | | | | - | | | corporate.amazon.com | | | | - | | | buybox.amazon.com | | | | - | | | iphone.amazon.com | | | | - | | | yp.amazon.com | | | | - | | | home.amazon.com | | | | - | | | origin-www.amazon.com | | | | - | | | buckeye-retail-website.amazon.com | | | | - | | | huddles.amazon.com | | | | - | | | p-nt-www-amazon-com-kalias.amazon.com | | | | - | | | p-yo-www-amazon-com-kalias.amazon.com | | | | - | | | p-y3-www-amazon-com-kalias.amazon.com | | | | - +----------------+----------------+---------------------------------------+-------------------------------+-------------------------------+-----------------------+ + +--------------------+----------------+---------------------------------------+-------------------------------+-------------------------------+-----------------------+ + | Host | Common Name | DNS Names | Not Before | Not After | Issuer | + +--------------------+----------------+---------------------------------------+-------------------------------+-------------------------------+-----------------------+ + | www.google.com:443 | www.google.com | www.google.com | 2020-02-12 11:47:41 +0000 UTC | 2020-05-06 11:47:41 +0000 UTC | GTS CA 1O1 | + | www.azure.com:443 | *.azure.com | *.azure.com | 2019-12-17 19:51:44 +0000 UTC | 2020-12-17 19:51:44 +0000 UTC | Microsoft IT TLS CA 4 | + | www.amazon.com:443 | www.amazon.com | amazon.com | 2019-09-18 00:00:00 +0000 UTC | 2020-08-23 12:00:00 +0000 UTC | DigiCert Global CA G2 | + | | | amzn.com | | | | + | | | uedata.amazon.com | | | | + | | | us.amazon.com | | | | + | | | www.amazon.com | | | | + | | | www.amzn.com | | | | + | | | corporate.amazon.com | | | | + | | | buybox.amazon.com | | | | + | | | iphone.amazon.com | | | | + | | | yp.amazon.com | | | | + | | | home.amazon.com | | | | + | | | origin-www.amazon.com | | | | + | | | buckeye-retail-website.amazon.com | | | | + | | | huddles.amazon.com | | | | + | | | p-nt-www-amazon-com-kalias.amazon.com | | | | + | | | p-yo-www-amazon-com-kalias.amazon.com | | | | + | | | p-y3-www-amazon-com-kalias.amazon.com | | | | + +--------------------+----------------+---------------------------------------+-------------------------------+-------------------------------+-----------------------+ run with docker diff --git a/config.go b/config.go index 86a3667..c29e196 100644 --- a/config.go +++ b/config.go @@ -4,5 +4,6 @@ import "time" const ( dialerTimeout = 10 * time.Second + defaultPort = 443 protocol = "tcp" ) diff --git a/utils.go b/utils.go index 6eb953d..521c059 100644 --- a/utils.go +++ b/utils.go @@ -3,6 +3,7 @@ package main import ( "crypto/tls" "crypto/x509" + "fmt" "net" "os" "strconv" @@ -33,9 +34,10 @@ func getPeerCertificates(h string, port int) ([]*x509.Certificate, error) { return conn.ConnectionState().PeerCertificates, nil } -func getCells(t table.Writer, h string) { - certs, err := getPeerCertificates(h, 443) +func getCells(t table.Writer, host string, port int) { + certs, err := getPeerCertificates(host, port) if err != nil { + fmt.Printf("err: %s\n", err) return // skip if target host invalid } @@ -43,9 +45,14 @@ func getCells(t table.Writer, h string) { if c.IsCA { continue } - t.AppendRows([]table.Row{ - {h, (*c).Subject.CommonName, strings.Join((*c).DNSNames, "\n"), (*c).NotBefore, (*c).NotAfter, (*c).Issuer.CommonName}, - }) + t.AppendRows([]table.Row{{ + host + ":" + strconv.Itoa(port), + (*c).Subject.CommonName, + strings.Join((*c).DNSNames, "\n"), + (*c).NotBefore, + (*c).NotAfter, + (*c).Issuer.CommonName, + }}) } } @@ -64,7 +71,18 @@ func prettyPrintCertsInfo(h string) { }) for _, target := range targets { - getCells(t, target) + p := defaultPort + ts := strings.Split(target, ":") + if len(ts) == 2 { + tp, err := strconv.Atoi(ts[1]) + if err != nil { + fmt.Printf("err: invalid port [%s], assume target port is 443\n", target) + } else { + p = tp + } + } + + getCells(t, ts[0], p) } t.Style().Format.Header = text.FormatDefault