2019-10-11 18:25:15 +03:00
|
|
|
// Package certmagic is the ACME provider from github.com/mholt/certmagic
|
|
|
|
package certmagic
|
|
|
|
|
|
|
|
import (
|
2019-10-15 21:32:20 +03:00
|
|
|
"log"
|
2019-10-17 18:31:02 +03:00
|
|
|
"math/rand"
|
2019-10-11 18:25:15 +03:00
|
|
|
"net"
|
2019-10-17 18:31:02 +03:00
|
|
|
"time"
|
2019-10-11 18:25:15 +03:00
|
|
|
|
|
|
|
"github.com/mholt/certmagic"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/api/server/acme"
|
|
|
|
)
|
|
|
|
|
|
|
|
type certmagicProvider struct {
|
2019-10-24 00:15:15 +03:00
|
|
|
opts acme.Options
|
2019-10-11 18:25:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *certmagicProvider) NewListener(ACMEHosts ...string) (net.Listener, error) {
|
2019-10-17 18:31:02 +03:00
|
|
|
certmagic.Default.CA = c.opts.CA
|
2019-10-11 18:25:15 +03:00
|
|
|
if c.opts.ChallengeProvider != nil {
|
|
|
|
// Enabling DNS Challenge disables the other challenges
|
|
|
|
certmagic.Default.DNSProvider = c.opts.ChallengeProvider
|
|
|
|
}
|
|
|
|
if c.opts.OnDemand {
|
|
|
|
certmagic.Default.OnDemand = new(certmagic.OnDemandConfig)
|
|
|
|
}
|
2019-10-17 18:31:02 +03:00
|
|
|
if c.opts.Cache != nil {
|
|
|
|
// already validated by new()
|
|
|
|
certmagic.Default.Storage = c.opts.Cache.(certmagic.Storage)
|
|
|
|
}
|
|
|
|
// If multiple instances of the provider are running, inject some
|
|
|
|
// randomness so they don't collide
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
randomDuration := (7 * 24 * time.Hour) + (time.Duration(rand.Intn(504)) * time.Hour)
|
|
|
|
certmagic.Default.RenewDurationBefore = randomDuration
|
|
|
|
|
2019-10-11 18:25:15 +03:00
|
|
|
return certmagic.Listen(ACMEHosts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a certmagic provider
|
|
|
|
func New(options ...acme.Option) acme.Provider {
|
2019-10-24 00:15:15 +03:00
|
|
|
opts := acme.DefaultOptions()
|
|
|
|
|
|
|
|
for _, o := range options {
|
|
|
|
o(&opts)
|
2019-10-11 18:25:15 +03:00
|
|
|
}
|
2019-10-24 00:15:15 +03:00
|
|
|
|
|
|
|
if opts.Cache != nil {
|
|
|
|
if _, ok := opts.Cache.(certmagic.Storage); !ok {
|
2019-10-15 21:32:20 +03:00
|
|
|
log.Fatal("ACME: cache provided doesn't implement certmagic's Storage interface")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 18:25:15 +03:00
|
|
|
return &certmagicProvider{
|
2019-10-24 00:15:15 +03:00
|
|
|
opts: opts,
|
2019-10-11 18:25:15 +03:00
|
|
|
}
|
|
|
|
}
|