2020-03-19 23:20:59 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-03-29 21:05:34 -05:00
|
|
|
"crypto/tls"
|
2020-03-29 22:02:18 -05:00
|
|
|
"encoding/json"
|
2020-03-19 23:20:59 -05:00
|
|
|
"fmt"
|
|
|
|
"html/template"
|
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
|
|
|
|
2023-09-29 19:59:46 -05:00
|
|
|
"github.com/dreamscached/minequery/v2"
|
2020-03-29 21:05:34 -05:00
|
|
|
"golang.org/x/crypto/acme/autocert"
|
2020-03-19 23:20:59 -05:00
|
|
|
)
|
|
|
|
|
2020-03-29 22:02:18 -05:00
|
|
|
type config struct {
|
2023-09-29 19:59:46 -05:00
|
|
|
Title string `json:"title"`
|
|
|
|
Content template.HTML `json:"content"`
|
|
|
|
Servers []server `json:"servers"`
|
|
|
|
UseTLS bool `json:"useTLS"`
|
|
|
|
TLSHostname string `json:"tlshostname"`
|
|
|
|
ServerPort int `json:"serverport"`
|
|
|
|
BackupDir string `json:"backupDir"`
|
2020-03-29 22:02:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type server struct {
|
2023-09-29 19:59:46 -05:00
|
|
|
Host string `json:"host"`
|
|
|
|
Port int `json:"port"`
|
|
|
|
Status *minequery.Status17
|
2020-03-29 22:02:18 -05:00
|
|
|
}
|
|
|
|
|
2021-02-13 21:18:51 -06:00
|
|
|
type pageData struct {
|
|
|
|
Title string
|
2023-09-29 19:59:46 -05:00
|
|
|
Content template.HTML
|
2021-02-13 21:18:51 -06:00
|
|
|
Servers []server
|
2020-03-19 23:20:59 -05:00
|
|
|
}
|
|
|
|
|
2020-03-29 22:02:18 -05:00
|
|
|
func main() {
|
2020-03-19 23:20:59 -05:00
|
|
|
|
2020-03-29 22:02:18 -05:00
|
|
|
// Parse config file
|
2023-09-29 19:59:46 -05:00
|
|
|
configData, err := os.ReadFile("./config.json")
|
2020-03-29 22:02:18 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error reading config file: %v\n", err)
|
2020-03-19 23:20:59 -05:00
|
|
|
}
|
|
|
|
|
2020-03-29 22:02:18 -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
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-09-29 19:59:46 -05:00
|
|
|
// TODO: Query instead (opportunistically?) to get mod lists, etc
|
|
|
|
config.Servers[i].Status, err = minequery.Ping17(s.Host, s.Port)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error querying server: %v", err)
|
2020-03-29 22:02:18 -05:00
|
|
|
}
|
|
|
|
}
|
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
|
2023-09-29 19:59:46 -05:00
|
|
|
if serveBackups { // TODO: add HTML
|
2020-04-17 19:53:02 -05:00
|
|
|
http.Handle("/backups/", http.StripPrefix("/backups/", http.FileServer(http.Dir(config.BackupDir))))
|
|
|
|
}
|
|
|
|
|
2020-03-29 22:23:27 -05:00
|
|
|
if config.UseTLS {
|
2020-03-29 21:05:34 -05:00
|
|
|
|
2020-03-29 22:23:27 -05:00
|
|
|
certManager := autocert.Manager{
|
|
|
|
Prompt: autocert.AcceptTOS,
|
2020-03-30 20:00:48 -05:00
|
|
|
HostPolicy: autocert.HostWhitelist(config.TLSHostname),
|
2020-03-29 22:23:27 -05:00
|
|
|
Cache: autocert.DirCache("certs"),
|
|
|
|
}
|
2020-03-29 21:05:34 -05:00
|
|
|
|
2020-03-29 22:23:27 -05:00
|
|
|
srv := &http.Server{
|
|
|
|
Addr: ":https",
|
|
|
|
TLSConfig: &tls.Config{
|
|
|
|
GetCertificate: certManager.GetCertificate,
|
|
|
|
},
|
|
|
|
}
|
2020-03-29 21:05:34 -05:00
|
|
|
|
2020-03-29 22:23:27 -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)
|
2020-03-29 22:23:27 -05:00
|
|
|
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
|
2020-03-30 20:00:48 -05:00
|
|
|
}
|
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-29 22:23:27 -05:00
|
|
|
}
|
2020-03-19 23:20:59 -05:00
|
|
|
|
|
|
|
}
|