mt-multiserver-proxy/config.go

290 lines
5.9 KiB
Go
Raw Normal View History

2021-09-06 02:03:27 -07:00
package proxy
import (
"encoding/json"
"fmt"
"log"
"os"
2021-09-05 03:18:22 -07:00
"sync"
)
2021-09-13 03:14:11 -07:00
const (
defaultCmdPrefix = ">"
defaultSendInterval = 0.09
defaultUserLimit = 10
defaultAuthBackend = "files"
2021-09-13 03:14:11 -07:00
defaultTelnetAddr = "[::1]:40010"
defaultBindAddr = ":40000"
defaultListInterval = 300
)
2021-09-05 03:18:22 -07:00
var config Config
var configMu sync.RWMutex
2022-04-21 05:35:58 -07:00
var loadConfigOnce sync.Once
type Server struct {
Name string
Addr string
MediaPool string
Fallbacks []string
dynamic bool
}
2021-09-10 03:47:19 -07:00
// A Config contains information from the configuration file
// that affects the way the proxy works.
type Config struct {
NoPlugins bool
CmdPrefix string
RequirePasswd bool
SendInterval float32
UserLimit int
AuthBackend string
NoTelnet bool
TelnetAddr string
BindAddr string
Servers []Server
ForceDefaultSrv bool
2022-04-20 13:48:54 -07:00
FallbackServers []string
CSMRF struct {
2021-08-28 04:05:09 -07:00
NoCSMs bool
ChatMsgs bool
ItemDefs bool
NodeDefs bool
2021-08-28 04:02:27 -07:00
NoLimitMapRange bool
2021-08-28 04:05:09 -07:00
PlayerList bool
2021-08-27 11:40:07 -07:00
}
2021-09-07 10:13:12 -07:00
MapRange uint32
DropCSMRF bool
2021-09-07 10:13:12 -07:00
Groups map[string][]string
UserGroups map[string]string
2021-09-11 06:38:45 -07:00
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
}
}
2021-09-10 03:47:19 -07:00
// Conf returns a copy of the Config used by the proxy.
// Any modifications will not affect the original Config.
2021-09-06 02:03:27 -07:00
func Conf() Config {
2022-04-21 05:46:13 -07:00
loadConfigOnce.Do(func() {
2022-04-21 05:35:58 -07:00
if err := LoadConfig(); err != nil {
log.Fatal(err)
}
})
2021-09-05 03:18:22 -07:00
configMu.RLock()
defer configMu.RUnlock()
return config
}
// UniquePoolServers returns a [][]server where each Pool is represented by a []Server
// of all servers that use one pool
func UniquePoolServers() [][]Server {
var srvs = make(map[string][]Server)
2022-05-01 01:14:27 -07:00
conf := Conf()
// every server needs a texturePool property
2022-05-01 01:14:27 -07:00
for _, srv := range conf.Servers {
2022-05-01 12:06:12 -07:00
if len(srv.MediaPool) == 0 {
srv.MediaPool = srv.Name
2022-05-01 01:14:27 -07:00
}
}
// map all to.. map of slices
2022-05-01 01:14:27 -07:00
for _, srv := range conf.Servers {
2022-05-01 12:06:12 -07:00
if srvs[srv.MediaPool] != nil {
srvs[srv.MediaPool] = append(srvs[srv.MediaPool], srv)
} else {
2022-05-01 12:06:12 -07:00
srvs[srv.MediaPool] = []Server{srv}
2022-05-01 01:14:27 -07:00
}
}
2022-05-01 01:14:27 -07:00
var res [][]Server
for _, srvsPool := range srvs {
res = append(res, srvsPool)
2022-05-01 01:14:27 -07:00
}
return res
2022-05-01 01:14:27 -07:00
}
// 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.
func AddServer(s Server) bool {
configMu.Lock()
defer configMu.Unlock()
s.dynamic = true
for _, srv := range config.Servers {
if srv.Name == s.Name {
return false
}
}
var poolMembers bool
for _, srv := range config.Servers {
if srv.MediaPool == s.MediaPool {
poolMembers = true
}
}
if !poolMembers {
return false
}
config.Servers = append(config.Servers, 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()
for i, srv := range config.Servers {
if srv.Name == name {
if srv.dynamic {
return false
}
// Can't remove server if players are connected to it
for cc := range Clts() {
if cc.ServerName() == name {
return false
}
}
config.Servers = append(config.Servers[:i], config.Servers[1+i:]...)
return true
}
}
return true
}
2022-04-21 03:31:05 -07:00
// FallbackServers returns a slice of server names that
2022-04-20 13:48:54 -07:00
// a server can fall back to.
func FallbackServers(server string) []string {
configMu.RLock()
defer configMu.RUnlock()
fallbacks := make([]string, 0)
2022-04-21 03:35:35 -07:00
conf := Conf()
2022-04-21 04:05:01 -07:00
2022-04-20 13:48:54 -07:00
// find server
2022-04-21 03:35:35 -07:00
for _, srv := range conf.Servers {
2022-04-20 13:48:54 -07:00
if srv.Name == server {
fallbacks = append(fallbacks, srv.Fallbacks...)
break
}
}
// global fallbacks
2022-04-21 03:35:35 -07:00
if len(conf.FallbackServers) == 0 {
if len(conf.Servers) == 0 {
return fallbacks
}
2022-04-21 04:05:01 -07:00
2022-04-21 03:35:35 -07:00
return append(fallbacks, conf.Servers[0].Name)
2022-04-20 13:48:54 -07:00
} else {
2022-04-21 03:35:35 -07:00
return append(fallbacks, conf.FallbackServers...)
2022-04-20 13:48:54 -07:00
}
}
2021-09-10 03:47:19 -07:00
// LoadConfig attempts to parse the configuration file.
// It leaves the config unchanged if there is an error
// and returns the error.
2021-09-06 02:03:27 -07:00
func LoadConfig() error {
2021-09-05 03:18:22 -07:00
configMu.Lock()
defer configMu.Unlock()
oldConf := config
2021-09-05 10:19:27 -07:00
config.CmdPrefix = defaultCmdPrefix
2021-09-05 03:18:22 -07:00
config.SendInterval = defaultSendInterval
config.UserLimit = defaultUserLimit
config.AuthBackend = defaultAuthBackend
2021-09-12 03:03:20 -07:00
config.TelnetAddr = defaultTelnetAddr
2021-09-05 03:18:22 -07:00
config.BindAddr = defaultBindAddr
2022-04-20 13:48:54 -07:00
config.FallbackServers = make([]string, 0)
2021-09-07 10:13:12 -07:00
config.Groups = make(map[string][]string)
config.UserGroups = make(map[string]string)
2021-09-11 06:55:10 -07:00
config.List.Interval = defaultListInterval
f, err := os.OpenFile(Path("config.json"), os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
2021-09-05 03:18:22 -07:00
config = oldConf
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)
2021-09-05 03:18:22 -07:00
if err := decoder.Decode(&config); err != nil {
config = oldConf
return err
}
// Dynamic servers shouldn't be deleted silently.
DynLoop:
for _, srv := range oldConf.Servers {
if srv.dynamic {
config.Servers = append(config.Servers, srv)
} else {
for _, s := range config.Servers {
if srv.Name == s.Name {
continue DynLoop
}
}
for cc := range Clts() {
if cc.ServerName() == srv.Name {
config = oldConf
return fmt.Errorf("can't delete server %s with players", srv.Name)
}
}
}
}
for i, srv := range config.Servers {
for _, s := range config.Servers {
if srv.Name == s.Name {
config = oldConf
return fmt.Errorf("duplicate server %s", s.Name)
}
}
if srv.MediaPool == "" {
config.Servers[i].MediaPool = srv.Name
}
}
2021-09-13 03:14:11 -07:00
log.Print("load config")
return nil
}