minecraft-site/factorio-site.go

132 lines
2.9 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-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 {
Servers []server `json:"servers"`
}
type server struct {
Host string `json:"host"`
Port int `json:"port"`
RCONPort int `json:"rconport"`
RCONPassword string `json:"rconpassword"`
Title string `json:"title"`
Description string `json:"description"`
rconConnection *rcon.RemoteConsole
}
2020-03-19 23:20:59 -05:00
type serverData struct {
IPAddr string
Port int
Title string
Players string
Version string
Description string
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-03-19 23:20:59 -05:00
fmt.Print("Parsing templates...\n")
t, err := template.ParseFiles("templates/index.html")
if err != nil {
fmt.Printf("Error parsing HTML template: %v\n", err)
}
for _, server := range config.Servers {
server.rconConnection, err = rcon.Dial(fmt.Sprintf("%v:%v", server.Host, server.RCONPort), server.RCONPassword)
if err != nil {
log.Fatalf("Error making RCON connection to %v: %v", server.Title, err)
}
defer server.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
data := []serverData{}
2020-03-19 23:40:23 -05:00
for _, server := range config.Servers {
2020-03-19 23:40:23 -05:00
playersOnline, err := server.rconCommand("/players o")
if err != nil {
log.Printf("Error executing players online command: %v\n", err)
}
2020-03-29 21:32:18 -05:00
version, err := server.rconCommand("/version")
if err != nil {
log.Printf("Error executing version command: %v\n", err)
}
2020-03-29 21:32:18 -05:00
data = append(data, serverData{
server.Host,
server.Port,
server.Title,
playersOnline,
version,
server.Description,
})
}
2020-03-19 23:40:23 -05:00
2020-03-19 23:20:59 -05:00
t.Execute(w, data)
})
2020-03-29 21:05:34 -05:00
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist("factorio.blackolivepineapple.pizza"), // TODO: add config
Cache: autocert.DirCache("certs"),
}
server := &http.Server{
Addr: ":https",
TLSConfig: &tls.Config{
GetCertificate: certManager.GetCertificate,
},
}
go http.ListenAndServe(":http", certManager.HTTPHandler(nil)) // Handler for LetsEncrypt
2020-03-29 21:02:32 -05:00
fmt.Println("Serving...")
2020-03-29 21:05:34 -05:00
server.ListenAndServeTLS("", "") // Key/cert come from server.TLSConfig
2020-03-19 23:20:59 -05:00
}