forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sites.go
170 lines (152 loc) · 5.06 KB
/
sites.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* Copyright (c) 2019-2021. 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 config
import (
"net/url"
"github.com/spf13/viper"
"github.com/pkg/errors"
"github.com/pydio/cells/common/proto/install"
"github.com/pydio/cells/x/configx"
)
var (
defaultAlwaysOverride = false
DefaultBindingSite = &install.ProxyConfig{
Binds: []string{"0.0.0.0:8080"},
TLSConfig: &install.ProxyConfig_SelfSigned{SelfSigned: &install.TLSSelfSigned{}},
SSLRedirect: false,
}
)
// GetDefaultSiteURL returns the first available bindURL of all available sites
func GetDefaultSiteURL(sites ...*install.ProxyConfig) string {
if len(sites) == 0 {
sites, _ = LoadSites()
}
// Try first to find a declared external URL
for _, s := range sites {
if s.ReverseProxyURL != "" {
return s.ReverseProxyURL
}
}
// Else return default Bind URL
for _, s := range sites {
return s.GetDefaultBindURL()
}
return ""
}
// GetSitesAllowedURLs returns a map of hostname => url for all sites.
// TODO : this function could switch to a list of specific authorized hostnames
func GetSitesAllowedURLs() map[string]*url.URL {
ss, _ := LoadSites()
hh := make(map[string]*url.URL)
for _, site := range ss {
for k, v := range site.GetExternalUrls() {
hh[k] = v
}
}
return hh
}
// LoadSites returns all sites defined by order of preference :
// - ENV VARS
// - YAML CONFIG
// - INTERNAL CONFIG
// - If none is found, returns a default value
// If configOnly is set to true, will only return the ones saved in configs
func LoadSites(configOnly ...bool) ([]*install.ProxyConfig, error) {
var sites []*install.ProxyConfig
if e := Get(configx.FormatPath("defaults", "sites")).Scan(&sites); e != nil {
return nil, errors.WithMessage(e, "error while parsing sites from config ")
}
if len(configOnly) > 0 && configOnly[0] {
return sites, nil
} else if defaultAlwaysOverride {
// If we did not require only the configs (for management),
// defaultAlwaysOverride skips all configs and just returns DefaultBindingSite
return []*install.ProxyConfig{DefaultBindingSite}, nil
}
if len(sites) == 0 {
sites = append(sites, DefaultBindingSite)
}
return sites, nil
}
// SaveSites saves a list of sites inside configuration
func SaveSites(sites []*install.ProxyConfig, user, msg string) error {
Set(sites, configx.FormatPath("defaults", "sites"))
e := Save(user, msg)
if e != nil {
return e
}
//ResetTlsConfigs()
return nil
}
// GetPublicBaseUri returns the default public uri
func GetPublicBaseUri() string {
return "/public"
}
func EnvOverrideDefaultBind() bool {
bind := viper.GetString("bind")
if bind == "" {
return false
}
defaultAlwaysOverride = true
DefaultBindingSite.Binds = []string{bind}
if ext := viper.GetString("external"); ext != "" {
DefaultBindingSite.ReverseProxyURL = ext
}
if noTls := viper.GetBool("no_tls"); noTls {
DefaultBindingSite.TLSConfig = nil
} else if tlsCert, tlsKey := viper.GetString("tls_cert_file"), viper.GetString("tls_cert_key"); tlsCert != "" && tlsKey != "" {
DefaultBindingSite.TLSConfig = &install.ProxyConfig_Certificate{Certificate: &install.TLSCertificate{
CertFile: tlsCert,
KeyFile: tlsKey,
}}
} else if leEmail, leAgree := viper.GetString("le_email"), viper.GetBool("le_agree"); leEmail != "" && leAgree {
le := &install.TLSLetsEncrypt{
Email: leEmail,
AcceptEULA: leAgree,
}
if viper.GetBool("le_staging") {
le.StagingCA = true
}
DefaultBindingSite.TLSConfig = &install.ProxyConfig_LetsEncrypt{LetsEncrypt: le}
}
return true
}
func DefaultBindOverrideToFlags() (flags []string) {
if !defaultAlwaysOverride {
return
}
flags = append(flags, "--bind", viper.GetString("bind"))
if ext := viper.GetString("external"); ext != "" {
flags = append(flags, "--external", ext)
}
if noTls := viper.GetBool("no_tls"); noTls {
flags = append(flags, "--no_tls")
} else if tlsCert, tlsKey := viper.GetString("tls_cert_file"), viper.GetString("tls_cert_key"); tlsCert != "" && tlsKey != "" {
flags = append(flags, "--tls_cert_file", tlsCert)
flags = append(flags, "--tls_key_file", tlsKey)
} else if leEmail, leAgree := viper.GetString("le_email"), viper.GetBool("le_agree"); leEmail != "" && leAgree {
flags = append(flags, "--le_email", leEmail)
flags = append(flags, "--le_agree")
if viper.GetBool("le_staging") {
flags = append(flags, "--le_staging")
}
}
return
}