2021-09-06 02:03:27 -07:00
|
|
|
package proxy
|
2021-08-27 09:59:27 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
|
|
|
"os"
|
2021-08-28 04:23:07 -07:00
|
|
|
"path/filepath"
|
2021-09-05 03:18:22 -07:00
|
|
|
"sync"
|
2021-08-27 09:59:27 -07:00
|
|
|
)
|
|
|
|
|
2021-09-13 03:14:11 -07:00
|
|
|
const (
|
|
|
|
defaultCmdPrefix = ">"
|
|
|
|
defaultSendInterval = 0.09
|
|
|
|
defaultUserLimit = 10
|
|
|
|
defaultAuthBackend = "sqlite3"
|
|
|
|
defaultTelnetAddr = "[::1]:40010"
|
|
|
|
defaultBindAddr = ":40000"
|
|
|
|
defaultListInterval = 300
|
|
|
|
)
|
2021-08-27 09:59:27 -07:00
|
|
|
|
2021-09-05 03:18:22 -07:00
|
|
|
var config Config
|
|
|
|
var configMu sync.RWMutex
|
2021-08-27 09:59:27 -07:00
|
|
|
|
2021-09-10 03:47:19 -07:00
|
|
|
// A Config contains information from the configuration file
|
|
|
|
// that affects the way the proxy works.
|
2021-08-27 09:59:27 -07:00
|
|
|
type Config struct {
|
2021-09-05 10:19:27 -07:00
|
|
|
NoPlugins bool
|
|
|
|
CmdPrefix string
|
2021-08-27 09:59:27 -07:00
|
|
|
RequirePasswd bool
|
|
|
|
SendInterval float32
|
|
|
|
UserLimit int
|
|
|
|
AuthBackend string
|
2021-09-12 03:03:20 -07:00
|
|
|
TelnetAddr string
|
2021-08-27 09:59:27 -07:00
|
|
|
BindAddr string
|
|
|
|
Servers []struct {
|
|
|
|
Name string
|
|
|
|
Addr string
|
|
|
|
}
|
2021-08-27 11:40:07 -07:00
|
|
|
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
|
2021-09-10 07:12:15 -07:00
|
|
|
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-08-27 09:59:27 -07:00
|
|
|
}
|
|
|
|
|
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 {
|
2021-09-05 03:18:22 -07:00
|
|
|
configMu.RLock()
|
|
|
|
defer configMu.RUnlock()
|
|
|
|
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
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-08-27 09:59:27 -07:00
|
|
|
|
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
|
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
|
2021-08-27 09:59:27 -07:00
|
|
|
|
2021-08-28 04:23:07 -07:00
|
|
|
executable, err := os.Executable()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
path := filepath.Dir(executable) + "/config.json"
|
|
|
|
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0666)
|
2021-08-27 09:59:27 -07:00
|
|
|
if err != nil {
|
2021-09-05 03:18:22 -07:00
|
|
|
config = oldConf
|
2021-08-27 09:59:27 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if fi, _ := f.Stat(); fi.Size() == 0 {
|
2021-08-29 10:45:20 -07:00
|
|
|
f.WriteString("{\n\t\n}\n")
|
2021-08-27 09:59:27 -07:00
|
|
|
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
|
2021-08-27 09:59:27 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-13 03:14:11 -07:00
|
|
|
log.Print("load config")
|
2021-08-27 09:59:27 -07:00
|
|
|
return nil
|
|
|
|
}
|