2023-05-25 16:08:56 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "io"
|
|
|
|
import "os"
|
|
|
|
import "log"
|
2023-05-26 11:05:22 -06:00
|
|
|
import "time"
|
2023-05-25 16:08:56 -06:00
|
|
|
import "hnakra/rotate"
|
|
|
|
import "hnakra/router"
|
2023-05-26 11:05:22 -06:00
|
|
|
import "hnakra/routines"
|
|
|
|
import "hnakra/router/rcon"
|
|
|
|
import "hnakra/router/config"
|
|
|
|
import "hnakra/cmd/router/srvhttps"
|
|
|
|
import "hnakra/cmd/router/srvhnakra"
|
2023-05-25 16:08:56 -06:00
|
|
|
|
|
|
|
const banner = "\n" +
|
|
|
|
" -=\\\n" +
|
|
|
|
"__. __|.\\___\n" +
|
|
|
|
" - ``.__.-` ```'-. | | |\\ | |\\ | / |''| |\\\n" +
|
|
|
|
" `-, u `. |--| | \\| |-\\ |< |-< |-\\\n" +
|
|
|
|
" ,-' ,```` VVv | | | | | \\ | \\ | \\ | \\\n" +
|
|
|
|
"/___-' ````--//----'''``\n" +
|
|
|
|
" |/ DISTRIBUTED HOLANET SERVER ROUTER\n" +
|
|
|
|
" '/"
|
|
|
|
|
|
|
|
func main () {
|
2023-05-26 11:07:05 -06:00
|
|
|
// set up logging
|
2023-05-25 16:08:56 -06:00
|
|
|
logDir := os.Getenv("HNAKRA_LOG_DIR")
|
|
|
|
if logDir != "" {
|
|
|
|
logger, err := rotate.New(logDir)
|
|
|
|
if err != nil { log.Fatal("cannot access log dir:", err) }
|
|
|
|
log.SetOutput(logger)
|
|
|
|
}
|
|
|
|
rcon := rcon.New("/debug/rcon")
|
|
|
|
originalWriter := log.Writer()
|
|
|
|
log.SetOutput(io.MultiWriter(originalWriter, rcon))
|
|
|
|
log.Println(banner)
|
|
|
|
|
|
|
|
// load config
|
|
|
|
log.Println("... loading config")
|
|
|
|
conf, err := config.File("/etc/hnakra/hnakra.conf")
|
|
|
|
if err != nil { log.Fatal(err) }
|
|
|
|
log.Println("(i) users:")
|
|
|
|
conf.OverUsers(func (name string, user config.User) bool {
|
|
|
|
log.Println(" -", name)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
log.Println("(i) aliases:")
|
|
|
|
if conf.AliasFallback() != "" {
|
|
|
|
log.Println(" - (fallback) ->", conf.AliasFallback())
|
|
|
|
}
|
|
|
|
conf.OverAliases(func (alias, target string) bool{
|
|
|
|
log.Println(" -", alias, "->", target)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
rcon.SetConfig(conf)
|
|
|
|
|
2023-05-26 11:07:05 -06:00
|
|
|
// set up servers
|
2023-05-25 16:08:56 -06:00
|
|
|
log.Println("... starting up")
|
2023-05-26 11:07:05 -06:00
|
|
|
manager := routines.Manager { RestartDeadline: time.Second * 8 }
|
2023-05-25 16:08:56 -06:00
|
|
|
rout := router.New(conf)
|
|
|
|
srvhnakra := &srvhnakra.Server { Config: conf, Router: rout }
|
2023-05-26 11:07:05 -06:00
|
|
|
manager.Append(srvhnakra.ListenAndServe)
|
|
|
|
if conf.HTTPSEnable() {
|
|
|
|
srvhttps := &srvhttps.Server { Config: conf, Handler: rout }
|
|
|
|
manager.Append(srvhttps.ListenAndServe)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up rcon
|
2023-05-25 16:08:56 -06:00
|
|
|
rcon.SetRouter(rout)
|
|
|
|
if conf.RconEnable() {
|
|
|
|
err = rout.HTTPMux().Handle("@/debug/rcon/", rcon)
|
|
|
|
if err != nil { log.Fatalln("XXX", err) }
|
|
|
|
} else {
|
|
|
|
log.SetOutput(originalWriter)
|
|
|
|
}
|
2023-05-26 11:07:05 -06:00
|
|
|
|
|
|
|
// run servers
|
|
|
|
err = manager.Run()
|
2023-05-25 16:08:56 -06:00
|
|
|
if err != nil { log.Println("XXX", err) }
|
|
|
|
}
|
|
|
|
|
|
|
|
func httpsRoutine (server *srvhttps.Server) {
|
|
|
|
err := server.ListenAndServe()
|
|
|
|
if err != nil { log.Println("XXX", err) }
|
|
|
|
}
|