Skip to content

Commit

Permalink
feat: crypto/tlsutil: add SupportsTLSVersion()
Browse files Browse the repository at this point in the history
  • Loading branch information
grokify committed Nov 4, 2024
1 parent f7ed9ad commit 14072e3
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions crypto/tlsutil/tlsutil.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package tlsutil

import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"net/http"
"os"
"strings"

"github.com/grokify/mogo/errors/errorsutil"
"golang.org/x/net/context/ctxhttp"
)

type TLSConfig struct {
Expand Down Expand Up @@ -95,3 +98,21 @@ func (tc *TLSConfig) LoadRootCACert(caCertFilepath string) error {
return nil
}
}

// SupportsTLSVersion returns an error if a connection cannot be made and a nil
// if the connection is successful.
func SupportsTLSVersion(ctx context.Context, tlsVersion TLSVersion, url string) error {
client := &http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{
MinVersion: uint16(tlsVersion),
MaxVersion: uint16(tlsVersion),
},
}}

if resp, err := ctxhttp.Head(ctx, client, url); err != nil {
return errorsutil.Wrapf(err, "tls version not supported (%s)", tlsVersion.String())
} else {
defer resp.Body.Close()
return nil
}
}

0 comments on commit 14072e3

Please sign in to comment.