summaryrefslogtreecommitdiff
path: root/deprecated-webircgateway/pkg/webircgateway/letsencrypt.go
blob: ffa6afe81bf2a4ece96582785429a1d7cd93a143 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package webircgateway

import (
	"context"
	"strings"
	"sync"

	"golang.org/x/crypto/acme/autocert"
)

type LEManager struct {
	// ensure only one instance of the manager and handler is running
	// while allowing multiple listeners to use it
	Mutex   sync.Mutex
	Manager *autocert.Manager
	gateway *Gateway
}

func NewLetsEncryptManager(gateway *Gateway) *LEManager {
	return &LEManager{gateway: gateway}
}

func (le *LEManager) Get(certCacheDir string) *autocert.Manager {
	le.Mutex.Lock()
	defer le.Mutex.Unlock()

	// Create it if it doesn't already exist
	if le.Manager == nil {
		le.Manager = &autocert.Manager{
			Prompt: autocert.AcceptTOS,
			Cache:  autocert.DirCache(strings.TrimRight(certCacheDir, "/")),
			HostPolicy: func(ctx context.Context, host string) error {
				le.gateway.Log(2, "Automatically requesting a HTTPS certificate for %s", host)
				return nil
			},
		}
		le.gateway.HttpRouter.Handle("/.well-known/", le.Manager.HTTPHandler(nil))
	}

	return le.Manager
}