From 12305b6fe62dd029165494f982ee3df7f33a9b9c Mon Sep 17 00:00:00 2001 From: guessi Date: Fri, 3 Apr 2020 02:41:41 +0800 Subject: [PATCH] Support for read config from YAML --- README.md | 4 ++-- go.mod | 6 +++--- go.sum | 14 +++++++------- hosts.yaml | 7 +++++++ main.go | 8 ++++---- utils.go | 35 +++++++++++++++++++++++++++++++---- 6 files changed, 54 insertions(+), 20 deletions(-) create mode 100644 hosts.yaml diff --git a/README.md b/README.md index e0c5c69..5fd87f9 100644 --- a/README.md +++ b/README.md @@ -14,13 +14,13 @@ run with docker docker build -t guessi/ssl-certs-checker . - docker run --rm -it guessi/ssl-certs-checker --hosts "www.google.com,www.azure.com,www.amazon.com" + docker run --rm -v $(pwd)/hosts.yaml:/opt/hosts.yaml:ro -it guessi/ssl-certs-checker --config hosts.yaml install binary to your ${GOPATH} and run locally go install github.com/ssl-certs-checker - ${GOPATH}/bin/ssl-certs-checker --hosts "www.google.com,www.azure.com,www.amazon.com" + ${GOPATH}/bin/ssl-certs-checker --config hosts.yaml +--------------------+----------------+---------------------------------------+-------------------------------+-------------------------------+-----------------------+ | Host | Common Name | DNS Names | Not Before | Not After | Issuer | diff --git a/go.mod b/go.mod index 11fafa5..3c32df8 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,8 @@ go 1.14 require ( github.com/go-openapi/strfmt v0.19.5 // indirect github.com/jedib0t/go-pretty v4.3.0+incompatible - github.com/mattn/go-runewidth v0.0.8 // indirect - github.com/stretchr/testify v1.5.1 // indirect + github.com/mattn/go-runewidth v0.0.9 // indirect github.com/urfave/cli/v2 v2.2.0 - golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 // indirect + golang.org/x/sys v0.0.0-20200331124033-c3d80250170d // indirect + gopkg.in/yaml.v2 v2.2.8 ) diff --git a/go.sum b/go.sum index 9a6da66..6240bca 100644 --- a/go.sum +++ b/go.sum @@ -18,8 +18,8 @@ github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/jedib0t/go-pretty v4.3.0+incompatible h1:CGs8AVhEKg/n9YbUenWmNStRW2PHJzaeDodcfvRAbIo= github.com/jedib0t/go-pretty v4.3.0+incompatible/go.mod h1:XemHduiw8R651AF9Pt4FwCTKeG3oo7hrHJAoznj9nag= -github.com/mattn/go-runewidth v0.0.8 h1:3tS41NlGYSmhhe/8fhGRzc+z3AYCw1Fe1WAyLuujKs0= -github.com/mattn/go-runewidth v0.0.8/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= +github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= +github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -30,18 +30,18 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5I github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= -github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= github.com/urfave/cli/v2 v2.2.0 h1:JTTnM6wKzdA0Jqodd966MVj4vWbbquZykeX1sKbe2C4= github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= go.mongodb.org/mongo-driver v1.0.3 h1:GKoji1ld3tw2aC+GX1wbr/J2fX13yNacEYoJ8Nhr0yU= go.mongodb.org/mongo-driver v1.0.3/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qLUO4lqsUM= -golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So= -golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200331124033-c3d80250170d h1:nc5K6ox/4lTFbMVSL9WRR81ixkcwXThoiF6yf+R9scA= +golang.org/x/sys v0.0.0-20200331124033-c3d80250170d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/hosts.yaml b/hosts.yaml new file mode 100644 index 0000000..841c4b2 --- /dev/null +++ b/hosts.yaml @@ -0,0 +1,7 @@ +hosts: +- www.google.com +- www.azure.com +- www.amazon.com +# - www.google.com:443 +# - www.azure.com:443 +# - www.amazon.com:443 diff --git a/main.go b/main.go index e9354a6..ad07d2c 100644 --- a/main.go +++ b/main.go @@ -12,15 +12,15 @@ func main() { Usage: "check SSL certificates at once", Flags: []cli.Flag{ &cli.StringFlag{ - Name: "hosts", - Aliases: []string{"H"}, + Name: "config", + Aliases: []string{"C"}, Value: "", - Usage: "target hosts, splits by comma", + Usage: "config file", Required: true, }, }, Action: func(c *cli.Context) error { - prettyPrintCertsInfo(c.String("hosts")) + prettyPrintCertsInfo(c.String("config")) return nil }, } diff --git a/utils.go b/utils.go index 521c059..f80d645 100644 --- a/utils.go +++ b/utils.go @@ -4,6 +4,7 @@ import ( "crypto/tls" "crypto/x509" "fmt" + "io/ioutil" "net" "os" "strconv" @@ -11,8 +12,30 @@ import ( "github.com/jedib0t/go-pretty/table" "github.com/jedib0t/go-pretty/text" + "gopkg.in/yaml.v2" ) +type Config struct { + Hosts []string `yaml:"hosts"` +} + +func readConfig(config string) Config { + c := Config{} + + y, err := ioutil.ReadFile(config) + if err != nil { + fmt.Printf("fatal: %s\n", err) + os.Exit(1) + } + + err = yaml.Unmarshal(y, &c) + if err != nil { + fmt.Printf("fatal: %s\n", err) + os.Exit(1) + } + return c +} + func getPeerCertificates(h string, port int) ([]*x509.Certificate, error) { conn, err := tls.DialWithDialer( &net.Dialer{ @@ -56,8 +79,12 @@ func getCells(t table.Writer, host string, port int) { } } -func prettyPrintCertsInfo(h string) { - targets := strings.Split(h, ",") +func prettyPrintCertsInfo(config string) { + rc := readConfig(config) + if len(rc.Hosts) <= 0 { + fmt.Printf("key not found, or empty input\n") + return + } t := table.NewWriter() t.SetOutputMirror(os.Stdout) @@ -70,13 +97,13 @@ func prettyPrintCertsInfo(h string) { "Issuer", }) - for _, target := range targets { + for _, target := range rc.Hosts { 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) + fmt.Errorf("err: invalid port [%s], assume target port is 443\n", target) } else { p = tp }