2020-03-19 23:20:59 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
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-19 23:20:59 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type serverData struct {
|
|
|
|
IPAddr string
|
2020-03-20 11:37:17 -05:00
|
|
|
Port int
|
2020-03-19 23:20:59 -05:00
|
|
|
Title string
|
|
|
|
Players string
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
2020-03-19 23:40:23 -05:00
|
|
|
serverAddr := flag.String("serverAddr", "factorio.blackolivepineapple.pizza", "Server to check status of (optional, defaults to factorio.bopp")
|
|
|
|
serverPort := flag.Int("serverport", 34196, "RCON port on the Factorio server")
|
|
|
|
password := flag.String("password", "", "RCON password of the server (required)")
|
2020-03-19 23:20:59 -05:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if *port < 1 || *port > 65535 {
|
|
|
|
fmt.Printf("Invalid port %v\n", *port)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-19 23:40:23 -05:00
|
|
|
if *password == "" {
|
|
|
|
fmt.Printf("Password flag is required")
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-03-19 23:40:23 -05:00
|
|
|
rconConnection, err := rcon.Dial(fmt.Sprintf("%v:%v", *serverAddr, *serverPort), *password)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error making RCON connection: %v", err)
|
2020-03-19 23:20:59 -05:00
|
|
|
}
|
2020-03-19 23:40:23 -05:00
|
|
|
defer 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
|
|
|
|
|
|
|
_, err := rconConnection.Write("/players o")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Print(w, "Error connecting to server")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
playersOnline, _, err := rconConnection.Read()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Print(w, "Error receiving data from server")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data := serverData{
|
|
|
|
*serverAddr,
|
2020-03-20 11:37:17 -05:00
|
|
|
34197,
|
2020-03-19 23:40:23 -05:00
|
|
|
"Server with Bob's Mod, est. Feb 2020",
|
|
|
|
playersOnline,
|
|
|
|
}
|
|
|
|
|
2020-03-19 23:20:59 -05:00
|
|
|
t.Execute(w, data)
|
|
|
|
})
|
|
|
|
|
|
|
|
fmt.Printf("Serving on :%v...\n", *port)
|
|
|
|
http.ListenAndServe(fmt.Sprintf(":%v", *port), nil)
|
|
|
|
|
|
|
|
}
|