-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacme.go
65 lines (59 loc) · 1.33 KB
/
acme.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package easiest
import (
"crypto/tls"
"os"
"path/filepath"
"runtime"
"golang.org/x/crypto/acme/autocert"
)
func newAcme(domains []string, dir string) *tls.Config {
m := &autocert.Manager{
Prompt: autocert.AcceptTOS,
}
if len(domains) > 0 {
m.HostPolicy = autocert.HostWhitelist(domains...)
}
if dir == "" {
dir = cacheDir()
}
m.Cache = autocert.DirCache(dir)
tlsConfig := m.TLSConfig()
tlsConfig.NextProtos = removeOneFromSet(tlsConfig.NextProtos, "h2")
return tlsConfig
}
func removeOneFromSet(set []string, o string) []string {
for i, s := range set {
if s == o {
return append(set[:i], set[i+1:]...)
}
}
return set
}
func homeDir() string {
if runtime.GOOS == "windows" {
return os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
}
if h := os.Getenv("HOME"); h != "" {
return h
}
return "/"
}
func cacheDir() string {
const base = "autocert"
switch runtime.GOOS {
case "darwin":
return filepath.Join(homeDir(), "Library", "Caches", base)
case "windows":
for _, ev := range []string{"APPDATA", "CSIDL_APPDATA", "TEMP", "TMP"} {
if v := os.Getenv(ev); v != "" {
return filepath.Join(v, base)
}
}
// Worst case:
return filepath.Join(homeDir(), base)
}
if xdg := os.Getenv("XDG_CACHE_HOME"); xdg != "" {
return filepath.Join(xdg, base)
}
return filepath.Join(homeDir(), ".cache", base)
}