gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/api/server/acme/certmagic/certmagic.go (about) 1 // Package certmagic is the ACME provider from github.com/mholt/certmagic 2 package certmagic 3 4 import ( 5 "log" 6 "math/rand" 7 "net" 8 "time" 9 10 "github.com/mholt/certmagic" 11 12 "gitee.com/liuxuezhan/go-micro-v1.18.0/api/server/acme" 13 ) 14 15 type certmagicProvider struct { 16 opts acme.Options 17 } 18 19 func (c *certmagicProvider) NewListener(ACMEHosts ...string) (net.Listener, error) { 20 certmagic.Default.CA = c.opts.CA 21 if c.opts.ChallengeProvider != nil { 22 // Enabling DNS Challenge disables the other challenges 23 certmagic.Default.DNSProvider = c.opts.ChallengeProvider 24 } 25 if c.opts.OnDemand { 26 certmagic.Default.OnDemand = new(certmagic.OnDemandConfig) 27 } 28 if c.opts.Cache != nil { 29 // already validated by new() 30 certmagic.Default.Storage = c.opts.Cache.(certmagic.Storage) 31 } 32 // If multiple instances of the provider are running, inject some 33 // randomness so they don't collide 34 rand.Seed(time.Now().UnixNano()) 35 randomDuration := (7 * 24 * time.Hour) + (time.Duration(rand.Intn(504)) * time.Hour) 36 certmagic.Default.RenewDurationBefore = randomDuration 37 38 return certmagic.Listen(ACMEHosts) 39 } 40 41 // New returns a certmagic provider 42 func New(options ...acme.Option) acme.Provider { 43 opts := acme.DefaultOptions() 44 45 for _, o := range options { 46 o(&opts) 47 } 48 49 if opts.Cache != nil { 50 if _, ok := opts.Cache.(certmagic.Storage); !ok { 51 log.Fatal("ACME: cache provided doesn't implement certmagic's Storage interface") 52 } 53 } 54 55 return &certmagicProvider{ 56 opts: opts, 57 } 58 }