diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-09-01 00:33:47 -0600 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-09-01 01:45:17 -0600 |
commit | d26030ecbc2d0baa614ff31e8c0c28e98e1d70ab (patch) | |
tree | 295604dd1baf4cb3231628b392b530aad16d58cc /bin-src/tls-getcerts.go | |
parent | f27db0f7772b3d160e0afececc5b30425ebb2d9b (diff) |
Use golangci-lint to modernize my Go
Diffstat (limited to 'bin-src/tls-getcerts.go')
-rw-r--r-- | bin-src/tls-getcerts.go | 41 |
1 files changed, 18 insertions, 23 deletions
diff --git a/bin-src/tls-getcerts.go b/bin-src/tls-getcerts.go index 0c181d3..d8c69ba 100644 --- a/bin-src/tls-getcerts.go +++ b/bin-src/tls-getcerts.go @@ -13,7 +13,6 @@ import ( "net/textproto" "net/url" "os" - "strings" "time" ) @@ -67,8 +66,8 @@ func xmppStartTLS(connRaw net.Conn, host string) error { } // smtpCmd is a convenience function that sends a command, and reads -// (but discards) the response -func smtpCmd(tp *textproto.Conn, expectCode int, format string, args ...interface{}) error { +// (but discards) the response. +func smtpCmd(tp *textproto.Conn, expectCode int, format string, args ...any) error { id, err := tp.Cmd(format, args...) if err != nil { return err @@ -79,7 +78,7 @@ func smtpCmd(tp *textproto.Conn, expectCode int, format string, args ...interfac return err } -func smtpStartTLS(connRaw net.Conn, host string) error { +func smtpStartTLS(connRaw net.Conn) error { tp := textproto.NewConn(connRaw) // let the server introduce itself @@ -107,7 +106,12 @@ func smtpStartTLS(connRaw net.Conn, host string) error { return nil } -func getcert(socket string) (*x509.Certificate, error) { +func getcert(socket string) (cert *x509.Certificate, err error) { + maybeSetErr := func(_err error) { + if _err != nil && err == nil { + err = _err + } + } u, err := url.Parse(socket) if err != nil { return nil, err @@ -130,21 +134,20 @@ func getcert(socket string) (*x509.Certificate, error) { case "", "/": // do nothing case "/xmpp": - err = xmppStartTLS(connRaw, host) - if err != nil { + if err := xmppStartTLS(connRaw, host); err != nil { return nil, err } case "/smtp": - err = smtpStartTLS(connRaw, host) - if err != nil { + if err := smtpStartTLS(connRaw); err != nil { return nil, err } default: - return nil, fmt.Errorf("Unknown negotiation path: %q", u.Path) + return nil, fmt.Errorf("unknown negotiation path: %q", u.Path) } + //nolint:gosec // that's the point connTLS := tls.Client(connRaw, &tls.Config{ServerName: host, InsecureSkipVerify: true}) - defer connTLS.Close() + defer func() { maybeSetErr(connTLS.Close()) }() err = connTLS.Handshake() if err != nil { return nil, err @@ -160,22 +163,14 @@ func getcert(socket string) (*x509.Certificate, error) { opts.Intermediates.AddCert(cert) } - cert := cstate.PeerCertificates[0] + cert = cstate.PeerCertificates[0] _, err = cert.Verify(opts) return cert, err } -func split(socket string) (net, addr string) { - ary := strings.SplitN(socket, ":", 2) - if len(ary) == 1 { - return "tcp", ary[0] - } - return ary[0], ary[1] -} - func main() { for _, socket := range os.Args[1:] { - fmt.Fprintf(os.Stderr, "Getting %q... ", socket) + _, _ = fmt.Fprintf(os.Stderr, "Getting %q... ", socket) block := pem.Block{ Type: "CERTIFICATE", Headers: map[string]string{"X-Socket": socket}, @@ -188,7 +183,7 @@ func main() { if err != nil { block.Headers["X-Error"] = err.Error() } - pem.Encode(os.Stdout, &block) - fmt.Fprintln(os.Stderr, "[done]") + _ = pem.Encode(os.Stdout, &block) + _, _ = fmt.Fprintln(os.Stderr, "[done]") } } |