2019-10-11 18:25:15 +03:00
|
|
|
// Package autocert is the ACME provider from golang.org/x/crypto/acme/autocert
|
|
|
|
// This provider does not take any config.
|
2019-10-09 18:42:05 +03:00
|
|
|
package autocert
|
|
|
|
|
|
|
|
import (
|
2020-02-15 18:10:26 +03:00
|
|
|
"crypto/tls"
|
2019-10-09 18:42:05 +03:00
|
|
|
"net"
|
2020-02-15 18:10:26 +03:00
|
|
|
"os"
|
2019-10-09 18:42:05 +03:00
|
|
|
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/api/server/acme"
|
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
2019-10-09 18:42:05 +03:00
|
|
|
"golang.org/x/crypto/acme/autocert"
|
|
|
|
)
|
|
|
|
|
|
|
|
// autoCertACME is the ACME provider from golang.org/x/crypto/acme/autocert
|
|
|
|
type autocertProvider struct{}
|
|
|
|
|
2020-09-05 02:43:16 +03:00
|
|
|
func (a *autocertProvider) Init(opts ...acme.Option) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-15 18:10:26 +03:00
|
|
|
// Listen implements acme.Provider
|
|
|
|
func (a *autocertProvider) Listen(hosts ...string) (net.Listener, error) {
|
|
|
|
return autocert.NewListener(hosts...), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TLSConfig returns a new tls config
|
|
|
|
func (a *autocertProvider) TLSConfig(hosts ...string) (*tls.Config, error) {
|
|
|
|
// create a new manager
|
|
|
|
m := &autocert.Manager{
|
|
|
|
Prompt: autocert.AcceptTOS,
|
|
|
|
}
|
|
|
|
if len(hosts) > 0 {
|
|
|
|
m.HostPolicy = autocert.HostWhitelist(hosts...)
|
|
|
|
}
|
|
|
|
dir := cacheDir()
|
|
|
|
if err := os.MkdirAll(dir, 0700); err != nil {
|
2020-09-05 02:11:29 +03:00
|
|
|
if logger.V(logger.InfoLevel) {
|
2020-03-24 23:45:11 +03:00
|
|
|
logger.Infof("warning: autocert not using a cache: %v", err)
|
|
|
|
}
|
2020-02-15 18:10:26 +03:00
|
|
|
} else {
|
|
|
|
m.Cache = autocert.DirCache(dir)
|
|
|
|
}
|
|
|
|
return m.TLSConfig(), nil
|
2019-10-09 18:42:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns an autocert acme.Provider
|
2020-02-15 18:10:26 +03:00
|
|
|
func NewProvider() acme.Provider {
|
2019-10-09 18:42:05 +03:00
|
|
|
return &autocertProvider{}
|
|
|
|
}
|