minecraft-site/factorio-site.go

166 lines
4.3 KiB
Go
Raw Normal View History

2020-03-19 23:20:59 -05:00
package main
import (
2020-03-29 21:05:34 -05:00
"crypto/tls"
"encoding/json"
2020-03-19 23:20:59 -05:00
"fmt"
"html/template"
"io/ioutil"
2020-03-19 23:40:23 -05:00
"log"
2020-03-19 23:20:59 -05:00
"net/http"
2020-04-17 19:53:02 -05:00
"os"
2020-03-19 23:40:23 -05:00
"github.com/james4k/rcon"
2020-03-29 21:05:34 -05:00
"golang.org/x/crypto/acme/autocert"
2020-03-19 23:20:59 -05:00
)
type config struct {
2021-02-13 21:18:51 -06:00
Title string `json:"title"`
Content string `json:"content"`
Servers []server `json:"servers"`
UseTLS bool `json:"useTLS"`
TLSHostname string `json:"tlshostname"`
ServerPort int `json:"serverport"`
BackupDir string `json:"backupDir"`
}
type server struct {
2021-02-13 21:18:51 -06:00
Host string `json:"host"` // for display only
Port int `json:"port"`
2021-02-14 00:11:52 -06:00
RCONHost string `json:"rconhost"` // not displayed, but used to connect; leave blank for no RCON connection
RCONPort int `json:"rconport"`
RCONPassword string `json:"rconpassword"`
2021-02-13 21:18:51 -06:00
Title string `json:"title"` // TODO: get this from RCON?
Description string `json:"description"` // TODO: get this from RCON?
Version string // Populated by RCON
Players string // Populated by RCON
rconConnection *rcon.RemoteConsole
}
2021-02-13 21:18:51 -06:00
type pageData struct {
Title string
Content string
Servers []server
2020-03-19 23:20:59 -05:00
}
// rconCommand executes a command on the server, and returns the server's
// response as a string.
func (s server) rconCommand(command string) (response string, err error) {
_, err = s.rconConnection.Write(command)
if err != nil {
return "", err
}
2020-03-19 23:20:59 -05:00
response, _, err = s.rconConnection.Read()
if err != nil {
return "", err
}
return
}
func main() {
2020-03-19 23:20:59 -05:00
// Parse config file
configData, err := ioutil.ReadFile("./config.json")
if err != nil {
log.Fatalf("Error reading config file: %v\n", err)
2020-03-19 23:20:59 -05:00
}
var config config
err = json.Unmarshal(configData, &config)
if err != nil {
log.Fatalf("Error parsing config file: %v\n", err)
2020-03-19 23:40:23 -05:00
}
2020-04-17 19:53:02 -05:00
serveBackups := true
if stat, err := os.Stat(config.BackupDir); os.IsNotExist(err) || !stat.IsDir() {
log.Printf("Backup directory %v does not exist; not serving backups.", config.BackupDir)
serveBackups = false
}
2021-02-13 21:18:51 -06:00
data := pageData{}
data.Title = config.Title
data.Content = config.Content
data.Servers = config.Servers
2020-04-17 19:53:02 -05:00
// Set up templates
2020-03-19 23:20:59 -05:00
fmt.Print("Parsing templates...\n")
t, err := template.ParseFiles("templates/index.html")
if err != nil {
2021-02-13 21:18:51 -06:00
log.Fatalf("Error parsing HTML template: %v\n", err)
2020-03-19 23:20:59 -05:00
}
2020-04-17 19:53:02 -05:00
// Connect to RCON servers
for i := range config.Servers {
2020-03-29 22:23:04 -05:00
s := config.Servers[i]
2021-02-14 00:11:52 -06:00
if s.RCONHost != "" {
config.Servers[i].rconConnection, err = rcon.Dial(fmt.Sprintf("%v:%v", s.RCONHost, s.RCONPort), s.RCONPassword)
if err != nil {
log.Fatalf("Error making RCON connection to %v: %v", s.Title, err)
}
defer config.Servers[i].rconConnection.Close()
}
2020-03-19 23:20:59 -05:00
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
2020-03-19 23:40:23 -05:00
2021-02-13 21:18:51 -06:00
// Update servers with current data
for i, s := range config.Servers {
2021-02-14 00:11:52 -06:00
if s.rconConnection != nil {
config.Servers[i].Players, err = s.rconCommand("/players o")
if err != nil {
log.Printf("Error executing players online command: %v\n", err)
}
config.Servers[i].Version, err = s.rconCommand("/version")
if err != nil {
log.Printf("Error executing version command: %v\n", err)
}
}
}
2020-03-19 23:40:23 -05:00
2021-02-13 21:18:51 -06:00
err = t.Execute(w, data)
if err != nil {
log.Printf("Error executing template: %v\n", err)
}
2020-03-19 23:20:59 -05:00
})
2020-04-17 19:53:02 -05:00
// Serve backup directory
2021-02-13 21:18:51 -06:00
if serveBackups { // TODO: also remove HTML if disabled
2020-04-17 19:53:02 -05:00
http.Handle("/backups/", http.StripPrefix("/backups/", http.FileServer(http.Dir(config.BackupDir))))
}
if config.UseTLS {
2020-03-29 21:05:34 -05:00
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(config.TLSHostname),
Cache: autocert.DirCache("certs"),
}
2020-03-29 21:05:34 -05:00
srv := &http.Server{
Addr: ":https",
TLSConfig: &tls.Config{
GetCertificate: certManager.GetCertificate,
},
}
2020-03-29 21:05:34 -05:00
go http.ListenAndServe(":http", certManager.HTTPHandler(nil)) // Handler for LetsEncrypt
2021-02-13 21:18:51 -06:00
if config.ServerPort == 0 { // Value not set in JSON
config.ServerPort = 443
}
fmt.Printf("Serving HTTPS on port %v...\n", config.ServerPort)
srv.ListenAndServeTLS("", "") // Key/cert come from srv.TLSConfig
2021-02-13 21:18:51 -06:00
} else {
if config.ServerPort == 0 { // Value not set in JSON
config.ServerPort = 80
}
2021-02-13 21:18:51 -06:00
fmt.Printf("Serving HTTP on port %v...\n", config.ServerPort)
http.ListenAndServe(fmt.Sprintf(":%v", config.ServerPort), nil)
}
2020-03-19 23:20:59 -05:00
}