From 50a1fb41d185a7a6826fafe77cb22c9b986f842a Mon Sep 17 00:00:00 2001 From: Captain ALM Date: Mon, 14 Aug 2023 17:36:14 +0100 Subject: [PATCH] Fix internal code. --- LICENSE | 2 +- pageHandler/page-handler.go | 11 ++++++-- pageHandler/pages/index/data.go | 6 +++++ pageHandler/pages/index/index-page.go | 5 ++-- pageHandler/pages/index/mc.go | 11 +++++++- pageHandler/pages/index/template-marshal.go | 30 +++++++++------------ 6 files changed, 41 insertions(+), 24 deletions(-) diff --git a/LICENSE b/LICENSE index 7fbe392..874e6ca 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ BSD 3-Clause License -Copyright (c) 2022, Captain ALM +Copyright (c) 2023, Captain ALM All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/pageHandler/page-handler.go b/pageHandler/page-handler.go index fcb7b6c..e93ad4a 100644 --- a/pageHandler/page-handler.go +++ b/pageHandler/page-handler.go @@ -1,8 +1,6 @@ package pageHandler import ( - "golang.captainalm.com/mc-webserver/conf" - "golang.captainalm.com/mc-webserver/pageHandler/utils" "io" "mime/multipart" "net/http" @@ -11,6 +9,9 @@ import ( "strings" "sync" "time" + + "golang.captainalm.com/mc-webserver/conf" + "golang.captainalm.com/mc-webserver/pageHandler/utils" ) const indexName = "index.go" @@ -286,6 +287,9 @@ func (ph *PageHandler) GetRegisteredPages() []string { } func (ph *PageHandler) GetCachedPages() []string { + if ph.pageContentsCacheRWMutex == nil { + return make([]string, 0) + } ph.pageContentsCacheRWMutex.RLock() defer ph.pageContentsCacheRWMutex.RUnlock() pages := make([]string, len(ph.PageContentsCache)) @@ -298,6 +302,9 @@ func (ph *PageHandler) GetCachedPages() []string { } func (ph *PageHandler) GetNumberOfCachedPages() int { + if ph.pageContentsCacheRWMutex == nil { + return 0 + } ph.pageContentsCacheRWMutex.RLock() defer ph.pageContentsCacheRWMutex.RUnlock() return len(ph.PageContentsCache) diff --git a/pageHandler/pages/index/data.go b/pageHandler/pages/index/data.go index 39222f3..23b36f5 100644 --- a/pageHandler/pages/index/data.go +++ b/pageHandler/pages/index/data.go @@ -2,12 +2,14 @@ package index import ( "html/template" + "strings" "time" ) type DataYaml struct { PageTitle string `yaml:"pageTitle"` ServerDescription template.HTML `yaml:"serverDescription"` + Footer string `yaml:"footer"` MCAddress string `yaml:"mcAddress"` MCPort uint16 `yaml:"mcPort"` MCType string `yaml:"mcType"` @@ -27,3 +29,7 @@ type DataYaml struct { AllowDisplayModded bool `yaml:"allowDisplayModded"` AllowModListing bool `yaml:"allowModListing"` } + +func (dy DataYaml) GetCleanMCType() string { + return strings.Title(dy.MCType) +} diff --git a/pageHandler/pages/index/index-page.go b/pageHandler/pages/index/index-page.go index 1554c51..1c62e22 100644 --- a/pageHandler/pages/index/index-page.go +++ b/pageHandler/pages/index/index-page.go @@ -1,8 +1,6 @@ package index import ( - "golang.captainalm.com/mc-webserver/utils/io" - "gopkg.in/yaml.v3" "html/template" "net/url" "os" @@ -10,6 +8,9 @@ import ( "strings" "sync" "time" + + "golang.captainalm.com/mc-webserver/utils/io" + "gopkg.in/yaml.v3" ) const templateName = "index.go.html" diff --git a/pageHandler/pages/index/mc.go b/pageHandler/pages/index/mc.go index 5aa307c..79353d8 100644 --- a/pageHandler/pages/index/mc.go +++ b/pageHandler/pages/index/mc.go @@ -18,10 +18,19 @@ type MC struct { MOTD string ActualHost *string ActualPort *uint16 - Favicon *template.HTML + Favicon *string Edition *string ModCount int64 Mods []string SecureProfilesEnforced *bool PreviewChatEnforced *bool } + +func (m MC) GetFaviconSRC() template.HTMLAttr { + toReturn := "src=\"" + if m.Favicon != nil { + toReturn += *m.Favicon + } + toReturn += "\"" + return template.HTMLAttr(toReturn) +} diff --git a/pageHandler/pages/index/template-marshal.go b/pageHandler/pages/index/template-marshal.go index 808e6eb..2f3cc9f 100644 --- a/pageHandler/pages/index/template-marshal.go +++ b/pageHandler/pages/index/template-marshal.go @@ -2,12 +2,13 @@ package index import ( "errors" - "github.com/mcstatus-io/mcutil" - "github.com/mcstatus-io/mcutil/options" - "github.com/mcstatus-io/mcutil/response" "html/template" "strings" "time" + + "github.com/mcstatus-io/mcutil" + "github.com/mcstatus-io/mcutil/options" + "github.com/mcstatus-io/mcutil/response" ) type Marshal struct { @@ -24,7 +25,7 @@ func (m Marshal) NewMC() (MC, error) { switch strings.ToLower(m.Data.MCType) { case "java": r, err := mcutil.Status(m.Data.MCAddress, m.Data.MCPort, options.JavaStatus{ - EnableSRV: m.ExtendedShown && m.Data.AllowDisplayActualAddress, + EnableSRV: m.Data.AllowDisplayActualAddress, Timeout: m.Data.MCTimeout, ProtocolVersion: m.Data.MCProtocolVersion, }) @@ -32,6 +33,7 @@ func (m Marshal) NewMC() (MC, error) { return MC{}, err } r2, err := mcutil.StatusRaw(m.Data.MCAddress, m.Data.MCPort, options.JavaStatus{ + EnableSRV: m.Data.AllowDisplayActualAddress, Timeout: m.Data.MCTimeout, ProtocolVersion: m.Data.MCProtocolVersion, }) @@ -51,7 +53,7 @@ func (m Marshal) NewMC() (MC, error) { MOTD: r.MOTD.Clean, ActualHost: CollectSRVHost(r.SRVResult), ActualPort: CollectSRVPort(r.SRVResult), - Favicon: CollectFavicon(r.Favicon), + Favicon: r.Favicon, Edition: CollectModEdition(r.ModInfo), ModCount: CollectModCount(r.ModInfo), Mods: CollectMods(r.ModInfo), @@ -60,7 +62,7 @@ func (m Marshal) NewMC() (MC, error) { }, nil case "legacy", "legacyjava", "javalegacy", "legacy java", "java legacy", "legacy_java", "java_legacy": r, err := mcutil.StatusLegacy(m.Data.MCAddress, m.Data.MCPort, options.JavaStatusLegacy{ - EnableSRV: m.ExtendedShown && m.Data.AllowDisplayActualAddress, + EnableSRV: m.Data.AllowDisplayActualAddress, Timeout: m.Data.MCTimeout, ProtocolVersion: m.Data.MCProtocolVersion, }) @@ -89,7 +91,7 @@ func (m Marshal) NewMC() (MC, error) { }, nil case "bedrock": r, err := mcutil.StatusBedrock(m.Data.MCAddress, m.Data.MCPort, options.BedrockStatus{ - EnableSRV: m.ExtendedShown && m.Data.AllowDisplayActualAddress, + EnableSRV: m.Data.AllowDisplayActualAddress, Timeout: m.Data.MCTimeout, ClientGUID: m.Data.MCClientGUID, }) @@ -146,14 +148,6 @@ func CollectSRVPort(srv *response.SRVRecord) *uint16 { return &srv.Port } -func CollectFavicon(favicon *string) *template.HTML { - if favicon == nil { - return nil - } - toReturn := template.HTML(*favicon) - return &toReturn -} - func CollectModEdition(mod *response.ModInfo) *string { if mod == nil { return nil @@ -204,8 +198,8 @@ func (m Marshal) CollectIPv4Port(port *uint16) uint16 { return *port } -func (m *Marshal) ToggleQuery(option string) string { - toReturn := "" +func (m *Marshal) ToggleQuery(option string) template.HTML { + toReturn := "?" if (!m.Dark && option == "dark") || (m.Dark && option != "dark") { toReturn += "dark&" } @@ -218,5 +212,5 @@ func (m *Marshal) ToggleQuery(option string) string { if (!m.PlayersShown && option == "players") || (m.PlayersShown && option != "players") { toReturn += "players" } - return strings.TrimRight(toReturn, "&") + return template.HTML(strings.TrimRight(toReturn, "&")) }