forked from unmojang/drasl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
306 lines (270 loc) · 8.85 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
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
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"errors"
"fmt"
"github.com/BurntSushi/toml"
"github.com/dgraph-io/ristretto"
"log"
"net/url"
"os"
"path"
"path/filepath"
"strings"
)
type rateLimitConfig struct {
Enable bool
RequestsPerSecond float64
}
type bodyLimitConfig struct {
Enable bool
SizeLimitKiB int
}
type FallbackAPIServer struct {
Nickname string
SessionURL string
AccountURL string
ServicesURL string
SkinDomains []string
CacheTTLSeconds int
DenyUnknownUsers bool
}
type transientUsersConfig struct {
Allow bool
UsernameRegex string
Password string
}
type registrationNewPlayerConfig struct {
Allow bool
AllowChoosingUUID bool
RequireInvite bool
}
type registrationExistingPlayerConfig struct {
Allow bool
Nickname string
SessionURL string
AccountURL string
SetSkinURL string
RequireSkinVerification bool
RequireInvite bool
}
type Config struct {
AllowCapes bool
AllowChangingPlayerName bool
AllowMultipleAccessTokens bool
AllowSkins bool
ApplicationOwner string
ApplicationName string
BaseURL string
BodyLimit bodyLimitConfig
DataDirectory string
DefaultAdmins []string
DefaultPreferredLanguage string
Domain string
EnableBackgroundEffect bool
EnableFooter bool
FallbackAPIServers []FallbackAPIServer
ForwardSkins bool
InstanceName string
ListenAddress string
LogRequests bool
MinPasswordLength int
RateLimit rateLimitConfig
RegistrationExistingPlayer registrationExistingPlayerConfig
RegistrationNewPlayer registrationNewPlayerConfig
RequestCache ristretto.Config
SignPublicKeys bool
SkinSizeLimit int
OfflineSkins bool
StateDirectory string
TestMode bool
TokenExpireSec int
TokenStaleSec int
TransientUsers transientUsersConfig
ValidPlayerNameRegex string
}
var defaultRateLimitConfig = rateLimitConfig{
Enable: true,
RequestsPerSecond: 5,
}
var defaultBodyLimitConfig = bodyLimitConfig{
Enable: true,
SizeLimitKiB: 8192,
}
func DefaultConfig() Config {
return Config{
AllowCapes: true,
AllowChangingPlayerName: true,
AllowSkins: true,
ApplicationName: "Drasl",
ApplicationOwner: "Anonymous",
BaseURL: "",
BodyLimit: defaultBodyLimitConfig,
DataDirectory: DEFAULT_DATA_DIRECTORY,
DefaultAdmins: []string{},
DefaultPreferredLanguage: "en",
Domain: "",
EnableBackgroundEffect: true,
EnableFooter: true,
ForwardSkins: true,
InstanceName: "Drasl",
ListenAddress: "0.0.0.0:25585",
LogRequests: true,
MinPasswordLength: 8,
OfflineSkins: true,
RateLimit: defaultRateLimitConfig,
RegistrationExistingPlayer: registrationExistingPlayerConfig{
Allow: false,
},
RegistrationNewPlayer: registrationNewPlayerConfig{
Allow: true,
AllowChoosingUUID: false,
RequireInvite: false,
},
RequestCache: ristretto.Config{
// Defaults from https://pkg.go.dev/github.com/dgraph-io/ristretto#readme-config
NumCounters: 1e7,
MaxCost: 1 << 30, // 1 GiB
BufferItems: 64,
},
SignPublicKeys: true,
SkinSizeLimit: 128,
StateDirectory: DEFAULT_STATE_DIRECTORY,
TestMode: false,
TokenExpireSec: 0,
TokenStaleSec: 0,
TransientUsers: transientUsersConfig{
Allow: false,
},
ValidPlayerNameRegex: "^[a-zA-Z0-9_]+$",
}
}
func CleanConfig(config *Config) error {
if config.BaseURL == "" {
return errors.New("BaseURL must be set. Example: https://drasl.example.com")
}
if _, err := url.Parse(config.BaseURL); err != nil {
return fmt.Errorf("Invalid BaseURL: %s", err)
}
config.BaseURL = strings.TrimRight(config.BaseURL, "/")
if !IsValidPreferredLanguage(config.DefaultPreferredLanguage) {
return fmt.Errorf("Invalid DefaultPreferredLanguage %s", config.DefaultPreferredLanguage)
}
if config.Domain == "" {
return errors.New("Domain must be set to a valid fully qualified domain name")
}
if config.InstanceName == "" {
return errors.New("InstanceName must be set")
}
if config.ListenAddress == "" {
return errors.New("ListenAddress must be set. Example: 0.0.0.0:25585")
}
if _, err := os.Open(config.DataDirectory); err != nil {
return fmt.Errorf("Couldn't open DataDirectory: %s", err)
}
if config.RegistrationExistingPlayer.Allow {
if config.RegistrationExistingPlayer.Nickname == "" {
return errors.New("RegistrationExistingPlayer.Nickname must be set")
}
if config.RegistrationExistingPlayer.SessionURL == "" {
return errors.New("RegistrationExistingPlayer.SessionURL must be set. Example: https://sessionserver.mojang.com")
}
if _, err := url.Parse(config.RegistrationExistingPlayer.SessionURL); err != nil {
return fmt.Errorf("Invalid RegistrationExistingPlayer.SessionURL: %s", err)
}
config.RegistrationExistingPlayer.SessionURL = strings.TrimRight(config.RegistrationExistingPlayer.SessionURL, "/")
if config.RegistrationExistingPlayer.AccountURL == "" {
return errors.New("RegistrationExistingPlayer.AccountURL must be set. Example: https://api.mojang.com")
}
if _, err := url.Parse(config.RegistrationExistingPlayer.AccountURL); err != nil {
return fmt.Errorf("Invalid RegistrationExistingPlayer.AccountURL: %s", err)
}
config.RegistrationExistingPlayer.AccountURL = strings.TrimRight(config.RegistrationExistingPlayer.AccountURL, "/")
}
for _, fallbackAPIServer := range PtrSlice(config.FallbackAPIServers) {
if fallbackAPIServer.Nickname == "" {
return errors.New("FallbackAPIServer Nickname must be set")
}
if fallbackAPIServer.AccountURL == "" {
return errors.New("FallbackAPIServer AccountURL must be set")
}
if _, err := url.Parse(fallbackAPIServer.AccountURL); err != nil {
return fmt.Errorf("Invalid FallbackAPIServer AccountURL %s: %s", fallbackAPIServer.AccountURL, err)
}
fallbackAPIServer.AccountURL = strings.TrimRight(fallbackAPIServer.AccountURL, "/")
if fallbackAPIServer.SessionURL == "" {
return errors.New("FallbackAPIServer SessionURL must be set")
}
if _, err := url.Parse(fallbackAPIServer.SessionURL); err != nil {
return fmt.Errorf("Invalid FallbackAPIServer SessionURL %s: %s", fallbackAPIServer.ServicesURL, err)
}
fallbackAPIServer.SessionURL = strings.TrimRight(fallbackAPIServer.SessionURL, "/")
if fallbackAPIServer.ServicesURL == "" {
return errors.New("FallbackAPIServer ServicesURL must be set")
}
if _, err := url.Parse(fallbackAPIServer.ServicesURL); err != nil {
return fmt.Errorf("Invalid FallbackAPIServer ServicesURL %s: %s", fallbackAPIServer.ServicesURL, err)
}
fallbackAPIServer.ServicesURL = strings.TrimRight(fallbackAPIServer.ServicesURL, "/")
for _, skinDomain := range fallbackAPIServer.SkinDomains {
if skinDomain == "" {
return fmt.Errorf("SkinDomain can't be blank for FallbackAPIServer \"%s\"", fallbackAPIServer.Nickname)
}
}
}
return nil
}
const TEMPLATE_CONFIG_FILE = `# Drasl default config file
# Example: drasl.example.com
Domain = ""
# Example: https://drasl.example.com
BaseURL = ""
# List of usernames who automatically become admins of the Drasl instance
DefaultAdmins = [""]
[RegistrationNewPlayer]
Allow = true
AllowChoosingUUID = true
RequireInvite = true
`
func ReadOrCreateConfig(path string) *Config {
config := DefaultConfig()
_, err := os.Stat(path)
if err != nil {
// File doesn't exist? Try to create it
log.Println("Config file at", path, "doesn't exist, creating it with template values.")
dir := filepath.Dir(path)
err := os.MkdirAll(dir, 0755)
Check(err)
f := Unwrap(os.Create(path))
defer f.Close()
_, err = f.Write([]byte(TEMPLATE_CONFIG_FILE))
Check(err)
}
metadata, err := toml.DecodeFile(path, &config)
Check(err)
for _, key := range metadata.Undecoded() {
log.Println("Warning: unknown config option", strings.Join(key, "."))
}
log.Println("Loading config from", path)
err = CleanConfig(&config)
if err != nil {
log.Fatal(fmt.Errorf("Error in config: %s", err))
}
return &config
}
func ReadOrCreateKey(config *Config) *rsa.PrivateKey {
path := path.Join(config.StateDirectory, "key.pkcs8")
der, err := os.ReadFile(path)
if err == nil {
key := Unwrap(x509.ParsePKCS8PrivateKey(der))
return key.(*rsa.PrivateKey)
} else {
key := Unwrap(rsa.GenerateKey(rand.Reader, 4096))
der := Unwrap(x509.MarshalPKCS8PrivateKey(key))
err = os.WriteFile(path, der, 0600)
Check(err)
return key
}
}