-
Notifications
You must be signed in to change notification settings - Fork 180
/
util.go
147 lines (137 loc) · 3.98 KB
/
util.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
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright (c) 2019-2022. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package caddy
import (
"net/url"
"path/filepath"
"strings"
"google.golang.org/protobuf/proto"
"github.com/pydio/cells/v4/common"
"github.com/pydio/cells/v4/common/crypto/providers"
"github.com/pydio/cells/v4/common/proto/install"
"github.com/pydio/cells/v4/common/runtime"
"github.com/pydio/cells/v4/common/utils/uuid"
)
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
// LogFile for this site
Log string
// LogLevel for this site
LogLevel string
}
// Redirects compute required redirects if SSLRedirect is set
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 == "" {
host = "0.0.0.0"
}
} else {
host = bind
}
targetHost := host
if host == "0.0.0.0" {
targetHost = "{host}"
host = ":80"
}
if port == "" || port == "443" {
rr["http://"+host] = "https://" + targetHost + "{uri} permanent"
} else if port == "80" {
continue
} else {
rr["http://"+host] = "https://" + targetHost + ":" + port + "{uri} permanent"
}
}
return rr
}
// SitesToCaddyConfigs computes all SiteConf from all *install.ProxyConfig by analyzing
// TLSConfig, ReverseProxyURL and Maintenance fields values
func SitesToCaddyConfigs(sites []*install.ProxyConfig) (caddySites []SiteConf, er error) {
for _, proxyConfig := range sites {
if bc, er := computeSiteConf(proxyConfig); er == nil {
caddySites = append(caddySites, bc)
} else {
return caddySites, er
}
}
return caddySites, nil
}
func computeSiteConf(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://" + strings.Replace(b, "0.0.0.0", "", 1)
}
} else {
for i, b := range bc.Binds {
bc.Binds[i] = strings.Replace(b, "0.0.0.0", "", 1)
}
switch v := bc.TLSConfig.(type) {
case *install.ProxyConfig_Certificate, *install.ProxyConfig_SelfSigned:
certFile, keyFile, err := providers.LoadCertificates(pc, runtime.CertsStoreURL())
if err != nil {
return bc, err
}
bc.TLSCert = certFile
bc.TLSKey = keyFile
case *install.ProxyConfig_LetsEncrypt:
caUrl := common.DefaultCaUrl
if v.LetsEncrypt.StagingCA {
caUrl = common.DefaultCaStagingUrl
}
bc.TLS = v.LetsEncrypt.Email + ` {
ca ` + caUrl + `
}`
}
}
bc.WebRoot = uuid.New()
// Translating log level to caddy
logLevel := runtime.LogLevel()
if logLevel != "warn" {
if logLevel == "debug" {
bc.Log = filepath.Join(runtime.ApplicationWorkingDir(runtime.ApplicationDirLogs), "caddy_access.log")
bc.LogLevel = "INFO"
} else {
bc.Log = filepath.Join(runtime.ApplicationWorkingDir(runtime.ApplicationDirLogs), "caddy_errors.log")
bc.LogLevel = "ERROR"
}
}
return bc, nil
}