forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sites.go
150 lines (133 loc) · 4.25 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
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 ""
}
// GetSitesAllowedHostnames 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
}