-
Notifications
You must be signed in to change notification settings - Fork 0
/
values_nginx.go
382 lines (355 loc) · 8.37 KB
/
values_nginx.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package form
import (
"fmt"
"html/template"
"math"
"mime"
"path"
"sort"
"strconv"
"strings"
"github.com/webx-top/com"
"github.com/webx-top/echo/param"
)
type NginxDomainInfo struct {
Port int
Args []string
Domains []string
CertsPath []*CertPath
}
var allPathFieldsForNginx = []string{
`header_path`,
//`fastcgi_path`,
`proxy_from`,
`browse_path`,
`expires_match_k[]`,
`basicauth_resources[]`,
`ratelimit_resources[]`,
`cors_path`,
}
type LocationDef struct {
PathKey string
Module string
Location string
IsRegexp bool
Items []*Item
}
type Item struct {
Key interface{}
Val interface{}
Args []interface{}
}
type Locations struct {
SortedStaticPath []string
SortedRegexpPath []string
GroupByPath map[string][]*LocationDef
}
type SortByLen []string
func (s SortByLen) Len() int { return len(s) }
func (s SortByLen) Less(i, j int) bool {
return len(s[i]) < len(s[j])
}
func (s SortByLen) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (v Values) NginxLocations() Locations {
return v.GroupByLocations(allPathFieldsForNginx)
}
// {remote} - {user} [{when}] "{method} {scheme} {host} {uri} {proto}" {status} {size} "{>Referer}" "{>User-Agent}" {latency}
var NginxLogFormatReplacer = strings.NewReplacer(
//`{method} {scheme} {host} {uri} {proto}`, `$request`,
`{method} {uri} {proto}`, `$request`,
`{remote}`, `$remote_addr`,
`{user}`, `$remote_user`,
`{when}`, `$time_local`,
`{method}`, `$request_method`,
`{scheme}`, `$scheme`,
`{host}`, `$http_host`,
`{uri}`, `$request_uri`,
`{proto}`, `$server_protocol`,
`{status}`, `$status`,
`{size}`, `$body_bytes_sent`,
`{>Referer}`, `$http_referer`,
`{>User-Agent}`, `$http_user_agent`,
`{latency}`, `$request_time`,
)
func AsNginxLogFormat(value string) string {
value = ExplodeCombinedLogFormat(value)
return NginxLogFormatReplacer.Replace(value)
}
func (v Values) AsNginxLogFormat() string {
return AsNginxLogFormat(v.Get(`log_format`))
}
func (v Values) NginxLimitRateWithUnit() string {
rate := param.AsInt64(v.Get(`ratelimit_rate`))
switch v.Get(`ratelimit_unit`) {
case `second`:
return fmt.Sprintf(`%dr/s`, rate)
case `minute`:
return fmt.Sprintf(`%dr/m`, rate)
case `hour`:
return fmt.Sprintf(`%vr/m`, math.Floor(float64(rate)/60))
case `day`:
return fmt.Sprintf(`%vr/m`, math.Floor((float64(rate)/24)/60))
case `week`:
return fmt.Sprintf(`%vr/m`, math.Floor(((float64(rate)/7)/24)/60))
default:
return fmt.Sprintf(`%dr/s`, rate)
}
}
func (v Values) ExtensionsToMime(value string) []string {
extensions := SplitBySpace(value)
mimes := make([]string, 0, len(extensions))
for _, ext := range extensions {
// text/css; charset=utf-8
mimeType := strings.SplitN(mime.TypeByExtension(ext), `;`, 2)[0]
if len(mimeType) > 0 {
mimes = append(mimes, mimeType)
}
}
return mimes
}
type CertPath struct {
Cert string
Key string
Trust string
Domain string
}
func (v Values) GetCerts(domains []string) []*CertPath {
res := make([]*CertPath, 0, len(domains))
for _, domain := range domains {
cert := v.Get(`tls/` + domain + `/cert`)
certKey := v.Get(`tls/` + domain + `/cert_key`)
certTrust := v.Get(`tls/` + domain + `/cert_trust`)
if len(cert) > 0 && len(certKey) > 0 {
res = append(res, &CertPath{
Cert: cert,
Key: certKey,
Trust: certTrust,
Domain: domain,
})
}
}
return res
}
func (v Values) GetNginxDomainList() []NginxDomainInfo {
domainList := v.GetDomainList()
var list []NginxDomainInfo
portsDomains := map[int][]string{}
var ports []int
for _, domain := range domainList {
domain = com.ParseEnvVar(domain)
parts := strings.SplitN(domain, `://`, 2)
var scheme, host, port string
if len(parts) == 2 {
scheme = parts[1]
host, port = com.SplitHostPort(parts[1])
} else {
host, port = com.SplitHostPort(domain)
}
portNumber, _ := strconv.ParseUint(port, 10, 16)
portN := int(portNumber)
if portN == 0 {
switch scheme {
case `http`:
portN = 80
case `https`:
portN = 443
default:
portN = 80
}
}
if len(host) == 0 {
host = `127.0.0.1`
}
if _, ok := portsDomains[portN]; !ok {
portsDomains[portN] = []string{}
ports = append(ports, portN)
}
portsDomains[portN] = append(portsDomains[portN], host)
}
sort.Sort(sort.IntSlice(ports))
isTLS := v.Values.Get(`tls`) == `1`
for _, portN := range ports {
info := NginxDomainInfo{
Port: portN,
Domains: portsDomains[portN],
}
if isTLS {
info.CertsPath = v.GetCerts(info.Domains)
if len(info.CertsPath) > 0 {
info.Args = append(info.Args, `ssl`)
}
}
list = append(list, info)
}
return list
}
type UpstreamInfo struct {
Scheme string
Host string
Path string
UpstreamName string
Rewrite string // rewrite ^/user/(.*)$ /$1 break;
withQuote bool
}
func (u UpstreamInfo) String() string {
host := u.Host
if len(u.UpstreamName) > 0 {
host = u.UpstreamName
}
value := u.Scheme
if u.Scheme == `unix` {
value += `:/`
} else {
value += `://`
}
value += host + u.Path
if u.withQuote {
value = `"` + com.AddCSlashes(value, '"') + `"`
}
return value
}
func (v Values) ServerGroup(key string, customHost string, withQuotes ...bool) interface{} {
var withQuote bool
if len(withQuotes) > 0 {
withQuote = withQuotes[0]
}
val := v.Get(key)
sh := strings.SplitN(val, `://`, 2)
var scheme string
var host string
var ppath string
if len(sh) == 2 {
scheme = sh[0]
host = sh[1]
} else {
parts := strings.SplitN(val, `:`, 2)
if len(parts) == 2 {
scheme = parts[0]
host = strings.TrimLeft(parts[1], "/")
} else {
scheme = `http`
host = val
}
}
hp := strings.SplitN(host, `/`, 2)
if len(hp) == 2 {
host = hp[0]
ppath = `/` + hp[1]
}
var rewrite string
if scheme == `http` || scheme == `https` {
stripPrefix := v.Get(`proxy_without`)
if len(stripPrefix) > 0 {
proxyPath := v.Get(`proxy_from`)
if stripPrefix == proxyPath {
ppath = `/`
} else {
if !strings.HasPrefix(stripPrefix, `/`) {
stripPrefix = `/` + stripPrefix
}
var targetPrefix string
if len(ppath) > 0 && path.Join(proxyPath, ppath) != strings.TrimSuffix(stripPrefix, `/`) {
targetPrefix = strings.TrimSuffix(ppath, `/`)
}
rewrite = fmt.Sprintf(
`rewrite %q %s/$1 break;`,
`^`+v.AddSlashes(stripPrefix)+`(.*)`,
targetPrefix,
)
}
}
}
return UpstreamInfo{
Scheme: scheme,
Host: host,
Path: ppath,
UpstreamName: customHost,
Rewrite: rewrite,
withQuote: withQuote,
}
}
func (v Values) IteratorHeaderKV(addon string, item string, plusPrefix string, minusPrefix string, withValueAndQuotes ...bool) interface{} {
if len(addon) > 0 && len(item) > 0 {
addon += `_`
}
k := addon + item + `_k`
keys, _ := v.Values[k]
k = addon + item + `_v`
values, _ := v.Values[k]
var r, t string
var withValueAndQuote bool
if len(withValueAndQuotes) > 0 {
withValueAndQuote = withValueAndQuotes[0]
}
l := len(values)
var suffix string
if v.Config.GetEngine() == `nginx` {
suffix = `;`
}
for i, k := range keys {
if i < l {
prefix := plusPrefix
if strings.HasPrefix(k, `-`) {
if len(minusPrefix) == 0 {
continue
}
k = strings.TrimPrefix(k, `-`)
prefix = minusPrefix
} else {
if len(plusPrefix) == 0 {
continue
}
k = strings.TrimPrefix(k, `+`)
}
if withValueAndQuote {
v := values[i]
v = `"` + com.AddCSlashes(v, '"') + `"`
r += t + prefix + k + ` ` + v + suffix
} else {
r += t + prefix + k + suffix
}
t = "\n"
}
}
if withValueAndQuote {
return template.HTML(r)
}
return r
}
func (v Values) IteratorNginxProxyHeaderKV() interface{} {
addon := `proxy`
item := `header_downstream`
if len(addon) > 0 && len(item) > 0 {
addon += `_`
}
k := addon + item + `_k`
keys, _ := v.Values[k]
k = addon + item + `_v`
values, _ := v.Values[k]
var r, t string
l := len(values)
suffix := `;`
for i, k := range keys {
if i < l {
var prefix string
if strings.HasPrefix(k, `-`) {
k = strings.TrimPrefix(k, `-`)
prefix = `proxy_hide_header`
r += t + prefix + ` ` + k + suffix
} else {
k = strings.TrimPrefix(k, `+`)
prefix = `proxy_pass_header`
v := values[i]
if len(v) > 0 {
prefix = `add_header`
v = `"` + com.AddCSlashes(v, '"') + `"`
r += t + prefix + ` ` + k + ` ` + v + suffix
} else {
r += t + prefix + ` ` + k + suffix
}
}
t = "\n"
}
}
return template.HTML(r)
}