@@ -25,6 +25,8 @@ import (
2525 "github.com/jtacoma/uritemplates"
2626 "github.com/kelseyhightower/envconfig"
2727 "github.com/rs/cors"
28+ "golang.org/x/crypto/acme"
29+ "golang.org/x/crypto/acme/autocert"
2830)
2931
3032var (
@@ -467,16 +469,19 @@ func (st *StreamTracker) Get(id StreamID) *Stream {
467469}
468470
469471type settings struct {
470- Host string
471- Port uint16 `default:"8080"`
472- HopTimeout time.Duration `default:"1s" split_words:"true"`
473- HopRetries uint `default:"5" split_words:"true"`
474- HopOffset byte `default:"0" split_words:"true"`
475- FilterQueue uint16 `default:"0" split_words:"true"`
476- HTTPSEnabled bool `default:"false" envconfig:"HTTPS_ENABLED"`
477- HTTPSCertFile string `default:"" envconfig:"HTTPS_CERT_FILE"`
478- HTTPSKeyFile string `default:"" envconfig:"HTTPS_KEY_FILE"`
479- InfoUriTemplate string `default:"" split_words:"true"`
472+ Host string
473+ Port uint16 `default:"8080"`
474+ HopTimeout time.Duration `default:"1s" split_words:"true"`
475+ HopRetries uint `default:"5" split_words:"true"`
476+ HopOffset byte `default:"0" split_words:"true"`
477+ FilterQueue uint16 `default:"0" split_words:"true"`
478+ HTTPSEnabled bool `default:"false" envconfig:"HTTPS_ENABLED"`
479+ HTTPSCertFile string `default:"" envconfig:"HTTPS_CERT_FILE"`
480+ HTTPSKeyFile string `default:"" envconfig:"HTTPS_KEY_FILE"`
481+ HTTPSAutocertEnabled bool `default:"" envconfig:"HTTPS_AUTOCERT_ENABLED"`
482+ HTTPSAutocertHosts []string `default:"" envconfig:"HTTPS_AUTOCERT_HOSTS"`
483+ HTTPSAutocertDirCache string `default:"" envconfig:"HTTPS_AUTOCERT_DIR_CACHE"`
484+ InfoUriTemplate string `default:"" split_words:"true"`
480485}
481486
482487func (s settings ) HostPort () string {
@@ -506,11 +511,18 @@ func main() {
506511 if err := envconfig .Process ("" , & s ); err != nil {
507512 log .Fatal (err )
508513 }
509- if s .HTTPSEnabled && (s .HTTPSCertFile == "" || s .HTTPSKeyFile == "" ) {
510- log .Fatal ("HTTPS_ENABLED=true requires HTTPS_CERT_FILE and HTTPS_KEY_FILE" )
514+
515+ httpsEnabled := s .HTTPSEnabled
516+ autocertEnabled := s .HTTPSAutocertEnabled
517+ certsDefined := s .HTTPSCertFile != "" && s .HTTPSKeyFile != ""
518+ if httpsEnabled && ! autocertEnabled && ! certsDefined {
519+ log .Fatal ("HTTPS_ENABLED=true requires either HTTPS_CERT_FILE and HTTPS_KEY_FILE or HTTPS_AUTOCERT_ENABLED=true" )
520+ }
521+ if ! httpsEnabled && (autocertEnabled || certsDefined ) {
522+ log .Fatal ("HTTPS_AUTOCERT_ENABLED=true, HTTPS_CERT_FILE and HTTPS_KEY_FILE require HTTPS_ENABLED=true" )
511523 }
512- if ! s . HTTPSEnabled && ( s . HTTPSCertFile != "" || s . HTTPSKeyFile != "" ) {
513- log .Fatal ("HTTPS_CERT_FILE and HTTPS_KEY_FILE require HTTPS_ENABLED=true " )
524+ if autocertEnabled && certsDefined {
525+ log .Fatal ("HTTPS_AUTOCERT_ENABLED=true can't be defined when HTTPS_CERT_FILE and HTTPS_KEY_FILE are set " )
514526 }
515527
516528 var infoUriTmpl * uritemplates.UriTemplate
@@ -658,10 +670,34 @@ func main() {
658670 TLSNextProto : map [string ]func (* http.Server , * tls.Conn , http.Handler ){},
659671 }
660672 server .SetKeepAlivesEnabled (false )
661- if s .HTTPSEnabled {
673+
674+ if ! httpsEnabled {
675+ log .Fatal (server .ListenAndServe ())
676+ } else if ! autocertEnabled {
662677 log .Fatal (server .ListenAndServeTLS (s .HTTPSCertFile , s .HTTPSKeyFile ))
663678 } else {
664- log .Fatal (server .ListenAndServe ())
679+ var cache autocert.Cache
680+ if s .HTTPSAutocertDirCache != "" {
681+ cache = autocert .DirCache (s .HTTPSAutocertDirCache )
682+ }
683+ var hostPolicy autocert.HostPolicy
684+ if s .HTTPSAutocertHosts != nil {
685+ autocert .HostWhitelist (s .HTTPSAutocertHosts ... )
686+ }
687+ manager := & autocert.Manager {
688+ Cache : cache ,
689+ Prompt : autocert .AcceptTOS ,
690+ HostPolicy : hostPolicy ,
691+ }
692+ server .TLSConfig = & tls.Config {
693+ GetCertificate : manager .GetCertificate ,
694+ NextProtos : []string {
695+ "http/1.1" ,
696+ acme .ALPNProto ,
697+ },
698+ }
699+ fmt .Println ("Autocert enabled" )
700+ log .Fatal (server .ListenAndServeTLS ("" , "" ))
665701 }
666702}
667703
0 commit comments