Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4d850ef

Browse files
committedJun 16, 2022
Adds custom ACME providers
Ports joohoi#283
1 parent a33c09a commit 4d850ef

File tree

3 files changed

+49
-47
lines changed

3 files changed

+49
-47
lines changed
 

‎config.cfg

+4-2
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@ ip = "0.0.0.0"
3636
disable_registration = false
3737
# listen port, eg. 443 for default HTTPS
3838
port = "443"
39-
# possible values: "letsencrypt", "letsencryptstaging", "cert", "none"
39+
# possible values: "letsencrypt", "letsencryptstaging", "custom", "cert", "none"
4040
tls = "letsencryptstaging"
4141
# only used if tls = "cert"
4242
tls_cert_privkey = "/etc/tls/example.org/privkey.pem"
4343
tls_cert_fullchain = "/etc/tls/example.org/fullchain.pem"
44-
# only used if tls = "letsencrypt"
44+
# only used if tls = "letsencrypt", "letsencryptstaging", or "custom"
4545
acme_cache_dir = "api-certs"
46+
# only used if tls = "custom"
47+
acme_dir = "https://acme-v02.example.com/directory"
4648
# optional e-mail address to which Let's Encrypt will send expiration notices for the API's cert
4749
notification_email = ""
4850
# CORS AllowOrigins, wildcards can be used

‎main.go

+23-32
Original file line numberDiff line numberDiff line change
@@ -114,42 +114,47 @@ func startHTTPAPI(errChan chan error, config DNSConfig, dnsservers []*DNSServer)
114114

115115
api := httprouter.New()
116116
c := cors.New(cors.Options{
117-
AllowedOrigins: Config.API.CorsOrigins,
117+
AllowedOrigins: config.API.CorsOrigins,
118118
AllowedMethods: []string{"GET", "POST"},
119119
OptionsPassthrough: false,
120-
Debug: Config.General.Debug,
120+
Debug: config.General.Debug,
121121
})
122-
if Config.General.Debug {
122+
if config.General.Debug {
123123
// Logwriter for saner log output
124124
c.Log = stdlog.New(logwriter, "", 0)
125125
}
126-
if !Config.API.DisableRegistration {
126+
if !config.API.DisableRegistration {
127127
api.POST("/register", webRegisterPost)
128128
}
129129
api.POST("/update", Auth(webUpdatePost))
130130
api.GET("/health", healthCheck)
131131

132-
host := Config.API.IP + ":" + Config.API.Port
132+
host := config.API.IP + ":" + config.API.Port
133133

134134
// TLS specific general settings
135135
cfg := &tls.Config{
136136
MinVersion: tls.VersionTLS12,
137137
}
138138
provider := NewChallengeProvider(dnsservers)
139-
storage := certmagic.FileStorage{Path: Config.API.ACMECacheDir}
139+
storage := certmagic.FileStorage{Path: config.API.ACMECacheDir}
140140

141141
// Set up certmagic for getting certificate for acme-dns api
142142
certmagic.DefaultACME.DNS01Solver = &provider
143143
certmagic.DefaultACME.Agreed = true
144-
if Config.API.TLS == "letsencrypt" {
144+
switch config.API.TLS {
145+
case TlsTypeLetsEncrypt:
145146
certmagic.DefaultACME.CA = certmagic.LetsEncryptProductionCA
146-
} else {
147+
case TlsTypeAcmeCustom:
148+
certmagic.DefaultACME.CA = config.API.ACMEDir
149+
case TlsTypeLetsEncryptStaging:
150+
default:
147151
certmagic.DefaultACME.CA = certmagic.LetsEncryptStagingCA
152+
148153
}
149-
certmagic.DefaultACME.Email = Config.API.NotificationEmail
154+
certmagic.DefaultACME.Email = config.API.ACMENotificationEmail
150155
magicConf := certmagic.NewDefault()
151156
magicConf.Storage = &storage
152-
magicConf.DefaultServerName = Config.General.Domain
157+
magicConf.DefaultServerName = config.General.Domain
153158

154159
magicCache := certmagic.NewCache(certmagic.CacheOptions{
155160
GetConfigForCert: func(cert certmagic.Certificate) (*certmagic.Config, error) {
@@ -159,25 +164,11 @@ func startHTTPAPI(errChan chan error, config DNSConfig, dnsservers []*DNSServer)
159164

160165
magic := certmagic.New(magicCache, *magicConf)
161166
var err error
162-
switch Config.API.TLS {
163-
case "letsencryptstaging":
164-
err = magic.ManageAsync(context.Background(), []string{Config.General.Domain})
165-
if err != nil {
166-
errChan <- err
167-
return
168-
}
169-
cfg.GetCertificate = magic.GetCertificate
170-
171-
srv := &http.Server{
172-
Addr: host,
173-
Handler: c.Handler(api),
174-
TLSConfig: cfg,
175-
ErrorLog: stdlog.New(logwriter, "", 0),
176-
}
177-
log.WithFields(log.Fields{"host": host, "domain": Config.General.Domain}).Info("Listening HTTPS")
178-
err = srv.ListenAndServeTLS("", "")
179-
case "letsencrypt":
180-
err = magic.ManageAsync(context.Background(), []string{Config.General.Domain})
167+
switch config.API.TLS {
168+
case TlsTypeLetsEncrypt:
169+
case TlsTypeLetsEncryptStaging:
170+
case TlsTypeAcmeCustom:
171+
err = magic.ManageAsync(context.Background(), []string{config.General.Domain})
181172
if err != nil {
182173
errChan <- err
183174
return
@@ -189,17 +180,17 @@ func startHTTPAPI(errChan chan error, config DNSConfig, dnsservers []*DNSServer)
189180
TLSConfig: cfg,
190181
ErrorLog: stdlog.New(logwriter, "", 0),
191182
}
192-
log.WithFields(log.Fields{"host": host, "domain": Config.General.Domain}).Info("Listening HTTPS")
183+
log.WithFields(log.Fields{"host": host, "domain": config.General.Domain}).Info("Listening HTTPS")
193184
err = srv.ListenAndServeTLS("", "")
194-
case "cert":
185+
case TlsTypeCert:
195186
srv := &http.Server{
196187
Addr: host,
197188
Handler: c.Handler(api),
198189
TLSConfig: cfg,
199190
ErrorLog: stdlog.New(logwriter, "", 0),
200191
}
201192
log.WithFields(log.Fields{"host": host}).Info("Listening HTTPS")
202-
err = srv.ListenAndServeTLS(Config.API.TLSCertFullchain, Config.API.TLSCertPrivkey)
193+
err = srv.ListenAndServeTLS(config.API.TLSCertFullchain, config.API.TLSCertPrivkey)
203194
default:
204195
log.WithFields(log.Fields{"host": host}).Info("Listening HTTP")
205196
err = http.ListenAndServe(host, c.Handler(api))

‎types.go

+22-13
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,30 @@ type dbsettings struct {
3737
Connection string
3838
}
3939

40+
const (
41+
TlsTypeLetsEncrypt = "letsencrypt"
42+
TlsTypeLetsEncryptStaging = "letsencryptstaging"
43+
TlsTypeAcmeCustom = "custom"
44+
TlsTypeCert = "cert"
45+
TlsTypeNone = "none"
46+
)
47+
4048
// API config
4149
type httpapi struct {
42-
Domain string `toml:"api_domain"`
43-
IP string
44-
DisableRegistration bool `toml:"disable_registration"`
45-
AutocertPort string `toml:"autocert_port"`
46-
Port string `toml:"port"`
47-
TLS string
48-
TLSCertPrivkey string `toml:"tls_cert_privkey"`
49-
TLSCertFullchain string `toml:"tls_cert_fullchain"`
50-
ACMECacheDir string `toml:"acme_cache_dir"`
51-
NotificationEmail string `toml:"notification_email"`
52-
CorsOrigins []string
53-
UseHeader bool `toml:"use_header"`
54-
HeaderName string `toml:"header_name"`
50+
Domain string `toml:"api_domain"`
51+
IP string
52+
DisableRegistration bool `toml:"disable_registration"`
53+
AutocertPort string `toml:"autocert_port"`
54+
Port string `toml:"port"`
55+
TLS string
56+
TLSCertPrivkey string `toml:"tls_cert_privkey"`
57+
TLSCertFullchain string `toml:"tls_cert_fullchain"`
58+
ACMECacheDir string `toml:"acme_cache_dir"`
59+
ACMEDir string `toml:"acme_dir"`
60+
ACMENotificationEmail string `toml:"notification_email"`
61+
CorsOrigins []string
62+
UseHeader bool `toml:"use_header"`
63+
HeaderName string `toml:"header_name"`
5564
}
5665

5766
// Logging config

0 commit comments

Comments
 (0)
Please sign in to comment.