forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy-config.go
236 lines (218 loc) · 5.82 KB
/
proxy-config.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package install
import (
"fmt"
"net/url"
"strings"
json "github.com/pydio/cells/x/jsonx"
"github.com/pydio/cells/common/utils/net"
)
func (m *ProxyConfig) GetDefaultBindURL() string {
scheme := "http"
if m.TLSConfig != nil {
scheme = "https"
}
return fmt.Sprintf("%s://%s", scheme, m.Binds[0])
}
func (m *ProxyConfig) GetBindURLs() (addresses []string) {
scheme := "http"
if m.HasTLS() {
scheme = "https"
}
for _, b := range m.Binds {
host := b
if strings.HasPrefix(b, ":") {
host = "localhost" + b
}
addresses = append(addresses, fmt.Sprintf("%s://%s", scheme, host))
}
return
}
func (m *ProxyConfig) canonicalUrl(s string) (*url.URL, error) {
u, e := url.Parse(s)
if e != nil {
return nil, e
}
if (u.Port() == "80" && u.Scheme == "http") || (u.Port() == "443" && u.Scheme == "https") {
u.Host = u.Hostname() // Replace host with version without port
}
return u, nil
}
func (m *ProxyConfig) expandBindAll(u *url.URL) []*url.URL {
var out []*url.URL
if ii, e := net.GetAvailableIPs(); e == nil {
for _, i := range ii {
exp, _ := url.Parse(strings.ReplaceAll(u.String(), "0.0.0.0", i.String()))
out = append(out, exp)
}
}
if other, e := net.HostsFileLookup(); e == nil {
for _, o := range other {
exp, _ := url.Parse(strings.ReplaceAll(u.String(), "0.0.0.0", o))
out = append(out, exp)
}
}
return out
}
func (m *ProxyConfig) GetExternalUrls() map[string]*url.URL {
uniques := make(map[string]*url.URL)
if ext := m.GetReverseProxyURL(); ext != "" {
if u, e := m.canonicalUrl(ext); e == nil {
uniques[u.Host] = u
}
}
for _, b := range m.GetBindURLs() {
u, e := m.canonicalUrl(b)
if e != nil {
continue
}
uniques[u.Host] = u
if u.Hostname() == "0.0.0.0" {
for _, exp := range m.expandBindAll(u) {
uniques[exp.Host] = exp
}
}
}
return uniques
}
func (m *ProxyConfig) HasTLS() bool {
return m.TLSConfig != nil
}
func (m *ProxyConfig) GetTLSCertificate() *TLSCertificate {
if cert, ok := m.TLSConfig.(*ProxyConfig_Certificate); ok {
return cert.Certificate
} else {
return nil
}
}
func (m *ProxyConfig) GetTLSSelfSigned() *TLSSelfSigned {
if cert, ok := m.TLSConfig.(*ProxyConfig_SelfSigned); ok {
return cert.SelfSigned
} else {
return nil
}
}
func (m *ProxyConfig) GetTLSLetsEncrypt() *TLSLetsEncrypt {
if cert, ok := m.TLSConfig.(*ProxyConfig_LetsEncrypt); ok {
return cert.LetsEncrypt
} else {
return nil
}
}
func (m *ProxyConfig) UnmarshalFromMap(data map[string]interface{}, getKey func(string) string) error {
if u, o := data[getKey("Binds")]; o {
if s, o := u.([]interface{}); o {
for _, v := range s {
if d, o := v.(string); o {
m.Binds = append(m.Binds, d)
} else {
return fmt.Errorf("unexpected type for Binds item (expected string)")
}
}
} else {
return fmt.Errorf("unexpected type for Binds (expected array)")
}
}
if u, o := data[getKey("SSLRedirect")]; o {
if b, o := u.(bool); o {
m.SSLRedirect = b
} else {
return fmt.Errorf("unexpected type for SSLRedirect (expected bool)")
}
}
if u, o := data[getKey("ReverseProxyURL")]; o {
if b, o := u.(string); o {
m.ReverseProxyURL = b
} else {
return fmt.Errorf("unexpected type for ReverseProxyURL (expected string)")
}
}
if t, o := data[getKey("TLSConfig")]; o {
tls := mapInterface2mapString(t)
if tls != nil {
if u, o := tls[getKey("SelfSigned")]; o {
uu := mapInterface2mapString(u)
r, e := json.Marshal(uu)
if e != nil {
return fmt.Errorf("cannot remarsh data")
}
selfSigned := TLSSelfSigned{}
if e := json.Unmarshal(r, &selfSigned); e == nil {
m.TLSConfig = &ProxyConfig_SelfSigned{SelfSigned: &selfSigned}
} else {
return fmt.Errorf("unexpected type for SelfSigned (expected TLSSelfSigned)")
}
} else if u, o := tls[getKey("LetsEncrypt")]; o {
uu := mapInterface2mapString(u)
r, _ := json.Marshal(uu)
le := &TLSLetsEncrypt{}
if e := json.Unmarshal(r, &le); e == nil {
m.TLSConfig = &ProxyConfig_LetsEncrypt{LetsEncrypt: le}
} else {
return fmt.Errorf("unexpected type for SelfSigned (expected TLSSelfSigned)")
}
} else if u, o := tls[getKey("Certificate")]; o {
uu := mapInterface2mapString(u)
r, _ := json.Marshal(uu)
cert := &TLSCertificate{}
if e := json.Unmarshal(r, &cert); e == nil {
m.TLSConfig = &ProxyConfig_Certificate{Certificate: cert}
} else {
return fmt.Errorf("unexpected type for SelfSigned (expected TLSSelfSigned)")
}
}
}
}
if u, o := data[getKey("Maintenance")]; o {
if b, o := u.(bool); o {
m.Maintenance = b
} else {
return fmt.Errorf("unexpected type for Maintenance (expected bool)")
}
}
if u, o := data[getKey("MaintenanceConditions")]; o {
if s, o := u.([]interface{}); o {
for _, v := range s {
if d, o := v.(string); o {
m.MaintenanceConditions = append(m.MaintenanceConditions, d)
} else {
return fmt.Errorf("unexpected type for MaintenanceConditions item (expected string)")
}
}
} else {
return fmt.Errorf("unexpected type for MaintenanceConditions (expected array)")
}
}
return nil
}
func (m *ProxyConfig) UnmarshalJSON(bb []byte) error {
data := make(map[string]interface{})
e := json.Unmarshal(bb, &data)
if e != nil {
return e
}
return m.UnmarshalFromMap(data, func(s string) string {
return s
})
}
func (m *ProxyConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
data := make(map[string]interface{})
if err := unmarshal(&data); err != nil {
return err
}
return m.UnmarshalFromMap(data, func(s string) string {
return strings.ToLower(s)
})
}
func mapInterface2mapString(in interface{}) map[string]interface{} {
if t, o := in.(map[string]interface{}); o {
return t
}
if t, o := in.(map[interface{}]interface{}); o {
out := make(map[string]interface{})
for k, v := range t {
out[k.(string)] = v
}
return out
}
return nil
}