-
Notifications
You must be signed in to change notification settings - Fork 180
/
conf.go
135 lines (124 loc) · 3.15 KB
/
conf.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package caddy
import (
"net/url"
"path/filepath"
"strings"
"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
"github.com/pydio/cells/common/caddy/maintenance"
"github.com/pydio/cells/common/config"
"github.com/pydio/cells/discovery/install/assets"
"github.com/mholt/caddy/caddytls"
"github.com/pydio/cells/common/crypto/providers"
"github.com/pydio/cells/common/proto/install"
)
var (
DefaultCaUrl = "https://acme-v02.api.letsencrypt.org/directory"
DefaultCaStagingUrl = "https://acme-staging-v02.api.letsencrypt.org/directory"
)
type SiteConf struct {
*install.ProxyConfig
// Parsed values from proto oneOf
TLS string
TLSCert string
TLSKey string
// Parsed External host if any
ExternalHost string
// Custom Root for this site
WebRoot string
}
func (s SiteConf) Redirects() map[string]string {
rr := make(map[string]string)
for _, bind := range s.GetBinds() {
parts := strings.Split(bind, ":")
var host, port string
if len(parts) == 2 {
host = parts[0]
port = parts[1]
if host == "" {
continue
}
} else {
host = bind
}
if port == "" {
rr["http://"+host] = "https://" + host
} else if port == "80" {
continue
} else {
rr["http://"+host] = "https://" + host + ":" + port
}
}
return rr
}
func SiteConfFromProxyConfig(pc *install.ProxyConfig) (SiteConf, error) {
bc := SiteConf{
ProxyConfig: proto.Clone(pc).(*install.ProxyConfig),
}
if pc.ReverseProxyURL != "" {
if u, e := url.Parse(pc.ReverseProxyURL); e == nil {
bc.ExternalHost = u.Host
}
}
if bc.TLSConfig == nil {
for i, b := range bc.Binds {
bc.Binds[i] = "http://" + b
}
} else {
switch v := bc.TLSConfig.(type) {
case *install.ProxyConfig_Certificate, *install.ProxyConfig_SelfSigned:
certFile, keyFile, err := providers.LoadCertificates(pc)
if err != nil {
return bc, err
}
bc.TLSCert = certFile
bc.TLSKey = keyFile
case *install.ProxyConfig_LetsEncrypt:
bc.TLS = v.LetsEncrypt.Email
}
}
if bc.Maintenance {
mDir, e := GetMaintenanceRoot()
if e != nil {
return bc, e
}
bc.WebRoot = mDir
}
return bc, nil
}
func SitesToCaddyConfigs(sites []*install.ProxyConfig) (caddySites []SiteConf, er error) {
for _, proxyConfig := range sites {
if bc, er := SiteConfFromProxyConfig(proxyConfig); er == nil {
caddySites = append(caddySites, bc)
if proxyConfig.HasTLS() && proxyConfig.GetLetsEncrypt() != nil {
le := proxyConfig.GetLetsEncrypt()
if le.AcceptEULA {
caddytls.Agreed = true
}
if le.StagingCA {
caddytls.DefaultCAUrl = DefaultCaStagingUrl
} else {
caddytls.DefaultCAUrl = DefaultCaUrl
}
}
} else {
return caddySites, er
}
}
return caddySites, nil
}
var maintenanceDir string
func GetMaintenanceRoot() (string, error) {
if maintenanceDir != "" {
return maintenanceDir, nil
}
dir, err := assets.GetAssets("./maintenance/src")
if err != nil {
dir = filepath.Join(config.ApplicationWorkingDir(), "static", "maintenance")
if err, _, _ := assets.RestoreAssets(dir, maintenance.PydioMaintenanceBox, nil); err != nil {
return "", errors.Wrap(err, "could not restore maintenance package")
}
}
maintenanceDir = dir
return dir, nil
}