-
Notifications
You must be signed in to change notification settings - Fork 0
/
values_caddy2.go
77 lines (67 loc) · 1.92 KB
/
values_caddy2.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
package form
import (
"strings"
"time"
"github.com/nging-plugins/caddymanager/application/library/htpasswd"
)
// {remote} - {user} [{when}] "{method} {scheme} {host} {uri} {proto}" {status} {size} "{>Referer}" "{>User-Agent}" {latency}
var Caddy2LogFormatReplacer = strings.NewReplacer(
`{method} {uri} {proto}`, `{request>method} {request>uri} {request>proto}`,
`{remote}`, `{request>remote_ip}`,
`{user}`, `{request>user_id}`,
`{when}`, `{ts}`,
`{method}`, `{request>method}`,
//`{scheme}`, `{scheme}`, TODO
//`{host}`, `{host}`, TODO
`{uri}`, `{request>uri}`,
`{proto}`, `{request>proto}`,
//`{status}`, `{status}`, 相同
//`{size}`, `{size}`, 相同
`{>Referer}`, `{request>headers>Referer>[0]}`,
`{>User-Agent}`, `{request>headers>User-Agent>[0]}`,
`{latency}`, `{duration}`, //TODO
)
func AsCaddy2LogFormat(value string) string {
value = ExplodeCombinedLogFormat(value)
return Caddy2LogFormatReplacer.Replace(value)
}
func (v Values) AsDuration(str string) time.Duration {
return ParseDuration(str)
}
func (v Values) AsCaddy2LogFormat() string {
return AsCaddy2LogFormat(v.Get(`log_format`))
}
// 添加路径通配符后缀
func (v Values) AddPathWildcardSuffix(path string) string {
if strings.HasSuffix(path, `*`) {
return path
}
if !strings.HasSuffix(path, `/`) {
return path
}
return path + `*`
}
func (v Values) BCrypt(password string) string {
password, _ = htpasswd.HashBCrypt(password)
return password
}
// 添加扩展名通配符前缀
func (v Values) AddExtWildcardPrefix(path string) string {
if strings.HasPrefix(path, `*`) {
return path
}
if !strings.HasPrefix(path, `.`) {
path = `.` + path
}
return `*` + path
}
func (v Values) AddonAttrFullKey(fullKey string, item string, defaults ...string) string {
val := v.Values.Get(fullKey)
if len(val) == 0 {
if len(defaults) > 0 && len(defaults[0]) > 0 {
return item + ` ` + defaults[0]
}
return ``
}
return item + ` ` + val
}