github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/util/acme/autocert/autocert.go (about) 1 // Copyright 2020 Asim Aslam 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // Original source: github.com/micro/go-micro/v3/api/server/acme/autocert/autocert.go 16 17 // Package autocert is the ACME provider from golang.org/x/crypto/acme/autocert 18 // This provider does not take any config. 19 package autocert 20 21 import ( 22 "crypto/tls" 23 "net" 24 "os" 25 26 "github.com/tickoalcantara12/micro/v3/service/logger" 27 "github.com/tickoalcantara12/micro/v3/util/acme" 28 "golang.org/x/crypto/acme/autocert" 29 ) 30 31 // autoCertACME is the ACME provider from golang.org/x/crypto/acme/autocert 32 type autocertProvider struct{} 33 34 // Listen implements acme.Provider 35 func (a *autocertProvider) Listen(hosts ...string) (net.Listener, error) { 36 return autocert.NewListener(hosts...), nil 37 } 38 39 // TLSConfig returns a new tls config 40 func (a *autocertProvider) TLSConfig(hosts ...string) (*tls.Config, error) { 41 // create a new manager 42 m := &autocert.Manager{ 43 Prompt: autocert.AcceptTOS, 44 } 45 if len(hosts) > 0 { 46 m.HostPolicy = autocert.HostWhitelist(hosts...) 47 } 48 dir := cacheDir() 49 if err := os.MkdirAll(dir, 0700); err != nil { 50 if logger.V(logger.InfoLevel, logger.DefaultLogger) { 51 logger.Infof("warning: autocert not using a cache: %v", err) 52 } 53 } else { 54 m.Cache = autocert.DirCache(dir) 55 } 56 return m.TLSConfig(), nil 57 } 58 59 // New returns an autocert acme.Provider 60 func NewProvider() acme.Provider { 61 return &autocertProvider{} 62 }