Skip to content

Instantly share code, notes, and snippets.

@tenox7
Last active March 31, 2024 13:09
Show Gist options
  • Save tenox7/9c27b2cbfb261ae4b6f6a67103921e77 to your computer and use it in GitHub Desktop.
Save tenox7/9c27b2cbfb261ae4b6f6a67103921e77 to your computer and use it in GitHub Desktop.
get cert from acme / letsencrypto auto cert manager
// get cert from acme / letsencrypto auto cert manager
// usage: go run getacme.go hostname
package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"log"
"net/http"
"github.com/grantae/certinfo"
"golang.org/x/crypto/acme/autocert"
)
func main() {
acm := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist("test.domain.com"),
Cache: autocert.DirCache("cache"),
}
go http.ListenAndServe(":8080", acm.HTTPHandler(nil))
https := &http.Server{
Addr: ":8443",
TLSConfig: &tls.Config{GetCertificate: acm.GetCertificate},
}
go https.ListenAndServeTLS("", "")
cert, err := acm.GetCertificate(&tls.ClientHelloInfo{ServerName: "test.domain.com"})
if err != nil {
log.Fatal(err)
}
pub, err := x509.ParseCertificate(cert.Certificate[0])
if err != nil {
log.Fatal(err)
}
txt, err := certinfo.CertificateText(pub)
if err != nil {
log.Fatal(err)
}
fmt.Println(txt)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment