github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/internal/acme/autocert/autocert.go (about)

     1  // Package autocert is the ACME provider from golang.org/x/crypto/acme/autocert
     2  // This provider does not take any config.
     3  package autocert
     4  
     5  import (
     6  	"crypto/tls"
     7  	"net"
     8  	"os"
     9  
    10  	"github.com/volts-dev/volts/internal/acme"
    11  	"github.com/volts-dev/volts/logger"
    12  	"golang.org/x/crypto/acme/autocert"
    13  )
    14  
    15  var log = logger.New("acme")
    16  
    17  // autoCertACME is the ACME provider from golang.org/x/crypto/acme/autocert.
    18  type autocertProvider struct {
    19  	logger logger.ILogger
    20  }
    21  
    22  // Listen implements acme.Provider.
    23  func (a *autocertProvider) Listen(hosts ...string) (net.Listener, error) {
    24  	return autocert.NewListener(hosts...), nil
    25  }
    26  
    27  // TLSConfig returns a new tls config.
    28  func (a *autocertProvider) TLSConfig(hosts ...string) (*tls.Config, error) {
    29  	// create a new manager
    30  	m := &autocert.Manager{
    31  		Prompt: autocert.AcceptTOS,
    32  	}
    33  	if len(hosts) > 0 {
    34  		m.HostPolicy = autocert.HostWhitelist(hosts...)
    35  	}
    36  	dir := cacheDir()
    37  	if err := os.MkdirAll(dir, 0700); err != nil {
    38  		log.Warnf("autocert not using a cache: %v", err)
    39  	} else {
    40  		m.Cache = autocert.DirCache(dir)
    41  	}
    42  	return m.TLSConfig(), nil
    43  }
    44  
    45  // New returns an autocert acme.Provider.
    46  func NewProvider() acme.Provider {
    47  	return &autocertProvider{}
    48  }