aboutsummaryrefslogtreecommitdiff
path: root/config.go
blob: 176126d5f549f827c8122728c1f1ee8070e3f98d (plain) (blame)
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 proxy

import (
	"encoding/json"
	"fmt"
	"log"
	"math/rand"
	"os"
	"sync"
	"time"
)

const (
	defaultCmdPrefix    = ">"
	defaultSendInterval = 0.09
	defaultUserLimit    = 10
	defaultAuthBackend  = "files"
	defaultTelnetAddr   = "[::1]:40010"
	defaultBindAddr     = ":40000"
	defaultListInterval = 300
)

var config Config
var configMu sync.RWMutex

var loadConfigOnce sync.Once

type Server struct {
	Addr      string
	MediaPool string
	Groups    []string
	Fallback  string

	dynamic   bool
	poolAdded time.Time
}

// A Config contains information from the configuration file
// that affects the way the proxy works.
type Config struct {
	NoPlugins        bool
	NoAutoPlugins    bool
	CmdPrefix        string
	RequirePasswd    bool
	SendInterval     float32
	UserLimit        int
	AuthBackend      string
	AuthPostgresConn string
	NoTelnet         bool
	TelnetAddr       string
	BindAddr         string
	DefaultSrv       string
	SrvSelector      string
	Servers          map[string]Server
	ForceDefaultSrv  bool
	KickOnNewPool    bool
	CSMRF            struct {
		NoCSMs          bool
		ChatMsgs        bool
		ItemDefs        bool
		NodeDefs        bool
		NoLimitMapRange bool
		PlayerList      bool
	}
	MapRange   uint32
	DropCSMRF  bool
	Groups     map[string][]string
	UserGroups map[string]string
	List       struct {
		Enable   bool
		Addr     string
		Interval int

		Name     string
		Desc     string
		URL      string
		Creative bool
		Dmg      bool
		PvP      bool
		Game     string
		FarNames bool
		Mods     []string
	}
}

// Conf returns a copy of the Config used by the proxy.
// Any modifications will not affect the original Config.
func Conf() Config {
	loadConfigOnce.Do(func() {
		if err := LoadConfig(); err != nil {
			log.Fatal(err)
		}
	})

	configMu.RLock()
	defer configMu.RUnlock()

	return config.clone()
}

// AddServer dynamically configures a new Server at runtime.
// Servers added in this way are ephemeral and will be lost
// when the proxy shuts down.
// The server must be part of a media pool with at least one
// other member. At least one of the other members always
// needs to be reachable.
// WARNING: Reloading the config will not overwrite servers
// added using this function. The server definition from the
// configuration file will silently be ignored.
func AddServer(name string, s Server) bool {
	configMu.Lock()
	defer configMu.Unlock()

	s.dynamic = true
	s.poolAdded = startTime

	if _, ok := config.Servers[name]; ok {
		return false
	}

	var poolMembers bool
	for _, srv := range config.Servers {
		if !srv.dynamic && srv.MediaPool == s.MediaPool {
			poolMembers = true
		}
	}

	if !poolMembers {
		return false
	}

	config.Servers[name] = s
	return true
}

// RmServer deletes a Server from the Config at runtime.
// Only servers added using AddServer can be deleted at runtime.
// Returns true on success or if the server doesn't exist.
func RmServer(name string) bool {
	configMu.Lock()
	defer configMu.Unlock()

	s, ok := config.Servers[name]
	if !ok {
		return true
	}

	if !s.dynamic {
		return false
	}

	// Can't remove server if players are connected to it
	for cc := range Clts() {
		if cc.ServerName() == name {
			return false
		}
	}

	delete(config.Servers, name)
	return true
}

func (cnf Config) clone() Config {
	newConfig := cnf

	newConfig.Servers = copyMap(cnf.Servers)

	newConfig.Groups = copyMapSlice(cnf.Groups)
	newConfig.UserGroups = copyMap(cnf.UserGroups)

	newConfig.List.Mods = make([]string, len(cnf.List.Mods))
	copy(newConfig.List.Mods, cnf.List.Mods)

	return newConfig
}

// WARNING: Doesn't handle nested maps.
func copyMap[K comparable, V any](in map[K]V) map[K]V {
	out := make(map[K]V)
	for k, v := range in {
		out[k] = v
	}

	return out
}

func copyMapSlice[K comparable, V any](in map[K][]V) map[K][]V {
	out := make(map[K][]V)
	for k, v := range in {
		out[k] = make([]V, len(v))
		copy(out[k], v)
	}

	return out
}

// DefaultServerInfo returns both the name of the default server
// and information about it. The return values are uninitialized
// if no servers exist.
func (cnf Config) DefaultServerInfo() (string, Server) {
	name, ok := cnf.RandomGroupServer(cnf.DefaultSrv)
	if !ok {
		return "", Server{}
	}

	srv, ok := cnf.Servers[name]
	if !ok {
		return "", Server{}
	}

	return name, srv
}

// DefaultServerName returns the name of the default server.
// If no servers exist it returns an empty string.
func (cnf Config) DefaultServerName() string {
	name, _ := cnf.DefaultServerInfo()
	return name
}

// DefaultServer returns information about the default server.
// If no servers exist the returned struct will be uninitialized.
// This is a faster shortcut for Config.Servers[Config.DefaultServerName()].
// You should thus only use this method or the DefaultServerInfo method.
func (cnf Config) DefaultServer() Server {
	_, srv := cnf.DefaultServerInfo()
	return srv
}

// Pools returns all media pools and their member servers.
func (cnf Config) Pools() map[string]map[string]Server {
	pools := make(map[string]map[string]Server)
	for name, srv := range cnf.Servers {
		if pools[srv.MediaPool] == nil {
			pools[srv.MediaPool] = make(map[string]Server)
		}

		pools[srv.MediaPool][name] = srv
	}

	return pools
}

// Groups returns all server groups and their member servers.
func (cnf Config) ServerGroups() map[string]map[string]Server {
	groups := make(map[string]map[string]Server)
	for name, srv := range cnf.Servers {
		for _, group := range srv.Groups {
			if groups[group] == nil {
				groups[group] = make(map[string]Server)
			}

			groups[group][name] = srv
		}
	}

	return groups
}

// RandomGroupServer returns the name of a random member of a server group
// or the input string if it is a valid, existent server name.
// It also returns a boolean indicating success.
// The returned string is blank if there is a failure,
// i.e. if the input string is neither a server nor a group.
func (cnf Config) RandomGroupServer(search string) (string, bool) {
	candidates := make([]string, 0)
	for name, srv := range cnf.Servers {
		if name == search {
			return name, true
		}

		for _, grp := range srv.Groups {
			if grp == search {
				candidates = append(candidates, name)
			}
		}
	}

	if len(candidates) == 0 {
		return "", false
	}

	return candidates[rand.Intn(len(candidates))], true
}

// LoadConfig attempts to parse the configuration file.
// It leaves the config unchanged if there is an error
// and returns the error.
func LoadConfig() error {
	configMu.Lock()
	defer configMu.Unlock()

	oldConf := config.clone()

	config.CmdPrefix = defaultCmdPrefix
	config.SendInterval = defaultSendInterval
	config.UserLimit = defaultUserLimit
	config.AuthBackend = defaultAuthBackend
	config.TelnetAddr = defaultTelnetAddr
	config.BindAddr = defaultBindAddr
	config.Servers = make(map[string]Server)
	config.Groups = make(map[string][]string)
	config.UserGroups = make(map[string]string)
	config.List.Interval = defaultListInterval
	config.List.Mods = make([]string, 0)

	f, err := os.OpenFile(Path("config.json"), os.O_RDWR|os.O_CREATE, 0666)
	if err != nil {
		config = oldConf.clone()
		return err
	}
	defer f.Close()

	if fi, _ := f.Stat(); fi.Size() == 0 {
		f.WriteString("{\n\t\n}\n")
		f.Seek(0, os.SEEK_SET)
	}

	decoder := json.NewDecoder(f)
	if err := decoder.Decode(&config); err != nil {
		config = oldConf.clone()
		return err
	}

	// Dynamic servers shouldn't be deleted silently.
	for name, srv := range oldConf.Servers {
		if srv.dynamic {
			if _, ok := config.Servers[name]; ok {
				config = oldConf.clone()
				return fmt.Errorf("duplicate server %s", name)
			}

			config.Servers[name] = srv
		} else {
			if _, ok := config.Servers[name]; ok {
				continue
			}

			for cc := range Clts() {
				if cc.ServerName() == name {
					config = oldConf.clone()
					return fmt.Errorf("can't delete server %s with players", name)
				}
			}
		}
	}

	for name, srv := range config.Servers {
		if srv.MediaPool == "" {
			srv.MediaPool = name
			config.Servers[name] = srv
		}
	}

	poolKickOnce := sync.OnceFunc(func() {
		for cc := range Clts() {
			cc.Kick("A server with new media has been added to the network. Please reconnect to access it.")
		}
	})

	// Set creation timestamp on new non-dynamic media pools.
	for name, srv := range config.Servers {
		if _, ok := oldConf.Servers[name]; !ok && !srv.dynamic {
			if poolServers, ok := oldConf.Pools()[srv.MediaPool]; ok {
				for _, s2 := range poolServers {
					srv.poolAdded = s2.poolAdded
				}
			} else { // New media pool.
				srv.poolAdded = time.Now()

				if config.KickOnNewPool {
					poolKickOnce()
				}
			}

			config.Servers[name] = srv
		}
	}

	log.Print("load config")
	return nil
}