2022-10-11 22:44:11 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/acme/autocert"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AutoTLS enables automatic HTTPS via Let's Encrypt.
|
|
|
|
func AutoTLS(conf *config.Config) (*autocert.Manager, error) {
|
|
|
|
var siteDomain, tlsEmail, certDir string
|
|
|
|
|
|
|
|
// Enable automatic HTTPS via Let's Encrypt?
|
|
|
|
if !conf.SiteHttps() {
|
2022-10-26 18:05:21 +02:00
|
|
|
return nil, fmt.Errorf("tls disabled")
|
2022-10-11 22:44:11 +02:00
|
|
|
} else if siteDomain = conf.SiteDomain(); !strings.Contains(siteDomain, ".") {
|
|
|
|
return nil, fmt.Errorf("no fully qualified site domain")
|
2022-10-19 05:09:09 +02:00
|
|
|
} else if tlsEmail = conf.TLSEmail(); tlsEmail == "" {
|
2022-10-11 22:44:11 +02:00
|
|
|
return nil, fmt.Errorf("automatic tls disabled")
|
2022-10-19 05:09:09 +02:00
|
|
|
} else if certDir = conf.CertificatesPath(); certDir == "" {
|
|
|
|
return nil, fmt.Errorf("certificates path not found")
|
2022-10-11 22:44:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create Let's Encrypt cert manager.
|
|
|
|
m := &autocert.Manager{
|
|
|
|
Email: tlsEmail,
|
|
|
|
Prompt: autocert.AcceptTOS,
|
|
|
|
HostPolicy: autocert.HostWhitelist(siteDomain),
|
|
|
|
Cache: autocert.DirCache(certDir),
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|