-
Notifications
You must be signed in to change notification settings - Fork 5
/
config.go
138 lines (119 loc) · 3.17 KB
/
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
package main
import (
"os"
"strings"
"github.com/BurntSushi/toml"
"github.com/kelseyhightower/envconfig"
)
type config struct {
Listen string
IncludeServers []string
AllowOrigins []string
Database string
Name string
Description string
Favicon string
Welcome string
NsfmWords []string
AllowWellKnownPorts bool
ProtocolWhitelist []string
MaxSessionsPerHost int
MaxSessionsPerNamedHost int
TrustedHosts []string
BannedHosts []string
ProxyHeaders bool
WarnIpv6 bool
Public bool
CheckServer bool
SessionTimeout int
ShutdownTimeout int
LogRequests bool
EnableAdminApi bool
IncludeCacheTtl int
IncludeStatusCacheTtl int
IncludeTimeout int
}
func (c *config) IsTrustedHost(host string) bool {
for _, v := range c.TrustedHosts {
if host == v {
return true
}
}
return false
}
func (c *config) ContainsNsfmWords(str string) bool {
str = strings.ToUpper(str)
for _, s := range c.NsfmWords {
if strings.Contains(str, s) {
return true
}
}
return false
}
func defaultConfig() *config {
hostname, err := os.Hostname()
if err != nil {
hostname = "Unconfigured server"
}
return &config{
Listen: "localhost:8080",
IncludeServers: []string{},
AllowOrigins: []string{"*"},
Database: "memory",
Name: hostname,
Description: "A Drawpile listing server",
Favicon: "",
Welcome: "",
NsfmWords: []string{"18+", "NSFW", "NSFM"},
AllowWellKnownPorts: false,
ProtocolWhitelist: []string{},
MaxSessionsPerHost: 3,
MaxSessionsPerNamedHost: 10,
TrustedHosts: []string{},
BannedHosts: []string{},
ProxyHeaders: false,
WarnIpv6: true,
Public: true,
CheckServer: true,
SessionTimeout: 10,
ShutdownTimeout: 1,
LogRequests: false,
EnableAdminApi: false,
IncludeCacheTtl: 0,
IncludeStatusCacheTtl: 0,
IncludeTimeout: 0,
}
}
func readConfigFile(path string) (*config, error) {
cfg := defaultConfig()
if _, err := toml.DecodeFile(path, cfg); err != nil {
return nil, err
}
doNormalizations(cfg)
return cfg, nil
}
func readEnv() (*config, error) {
cfg := defaultConfig()
if err := envconfig.Process("ls", cfg); err != nil {
return nil, err
}
doNormalizations(cfg)
return cfg, nil
}
func doNormalizations(cfg *config) {
for i, s := range cfg.NsfmWords {
cfg.NsfmWords[i] = strings.ToUpper(s)
}
if cfg.MaxSessionsPerNamedHost < cfg.MaxSessionsPerHost {
cfg.MaxSessionsPerNamedHost = cfg.MaxSessionsPerHost
}
for i, h := range cfg.BannedHosts {
cfg.BannedHosts[i] = strings.ToLower(h)
}
for i, h := range cfg.TrustedHosts {
cfg.TrustedHosts[i] = strings.ToLower(h)
}
if cfg.IncludeStatusCacheTtl < cfg.IncludeCacheTtl {
cfg.IncludeStatusCacheTtl = cfg.IncludeCacheTtl
}
}