From db7f3eb2797f1a656e2c09d57603869ba1463f6c Mon Sep 17 00:00:00 2001 From: phenpessoa Date: Tue, 26 Sep 2023 12:08:45 -0300 Subject: [PATCH 1/2] webserver: return on error and check if location is not nil --- src/webserver.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/webserver.go b/src/webserver.go index 6533ce3a..3da94e8c 100644 --- a/src/webserver.go +++ b/src/webserver.go @@ -20,10 +20,8 @@ import ( "github.com/go-resty/resty/v2" ) -var ( - // TibiaData app resty vars - TibiaDataUserAgent, TibiaDataProxyDomain string -) +// TibiaData app resty vars +var TibiaDataUserAgent, TibiaDataProxyDomain string // Information - child of JSONData type Information struct { @@ -101,8 +99,8 @@ func runWebServer() { // Tibia guilds v3.GET("/guild/:name", tibiaGuildsGuildV3) - //v3.GET("/guild/:name/events",TibiaGuildsGuildEventsV3) - //v3.GET("/guild/:name/wars",TibiaGuildsGuildWarsV3) + // v3.GET("/guild/:name/events",TibiaGuildsGuildEventsV3) + // v3.GET("/guild/:name/wars",TibiaGuildsGuildWarsV3) v3.GET("/guilds/:world", tibiaGuildsOverviewV3) // Tibia highscores @@ -757,10 +755,10 @@ func tibiaWorldsWorldV3(c *gin.Context) { func tibiaDataRequestHandler(c *gin.Context, tibiaDataRequest TibiaDataRequestStruct, requestHandler func(string) (interface{}, int), handlerName string) { BoxContentHTML, err := TibiaDataHTMLDataCollectorV3(tibiaDataRequest) - // return error (e.g. for maintenance mode) if err != nil { TibiaDataAPIHandleResponse(c, http.StatusBadGateway, handlerName, gin.H{"error": err.Error()}) + return } jsonData, httpStatusCode := requestHandler(BoxContentHTML) @@ -870,7 +868,7 @@ func TibiaDataHTMLDataCollectorV3(TibiaDataRequest TibiaDataRequestStruct) (stri case http.StatusFound: // Check if page is in maintenance mode location, _ := res.RawResponse.Location() - if location.Host == "maintenance.tibia.com" { + if location != nil && location.Host == "maintenance.tibia.com" { LogMessage := "maintenance mode detected on tibia.com" log.Printf("[info] TibiaDataHTMLDataCollectorV3: %s!", LogMessage) return "", errors.New(LogMessage) @@ -894,6 +892,7 @@ func TibiaDataHTMLDataCollectorV3(TibiaDataRequest TibiaDataRequestStruct) (stri doc, err := goquery.NewDocumentFromReader(resIo2) if err != nil { log.Printf("[error] TibiaDataHTMLDataCollectorV3 (URL: %s) error: %s", TibiaDataRequest.URL, err) + return "", err } // Find of this to get div with class BoxContent @@ -936,6 +935,6 @@ func readyz(c *gin.Context) { c.JSON(http.StatusServiceUnavailable, gin.H{"error": http.StatusText(http.StatusServiceUnavailable)}) return } - //c.JSON(http.StatusOK, gin.H{"status": http.StatusText(http.StatusOK)}) + // c.JSON(http.StatusOK, gin.H{"status": http.StatusText(http.StatusOK)}) TibiaDataAPIHandleResponse(c, http.StatusOK, "readyz", gin.H{"status": http.StatusText(http.StatusOK)}) } From 7b26796c6c8168f1dd6c8dc175cffcdf7a1de0ae Mon Sep 17 00:00:00 2001 From: phenpessoa Date: Tue, 26 Sep 2023 12:36:49 -0300 Subject: [PATCH 2/2] webserver: return on error and check if location is not nil, and fix sending invalid json payloads --- src/webserver.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/webserver.go b/src/webserver.go index 3da94e8c..8a93b5ac 100644 --- a/src/webserver.go +++ b/src/webserver.go @@ -8,6 +8,7 @@ import ( "net/http" "os" "os/signal" + "reflect" "strconv" "strings" "time" @@ -464,6 +465,10 @@ func tibiaHousesOverviewV3(c *gin.Context) { town = TibiaDataStringWorldFormatToTitleV3(town) jsonData := TibiaHousesOverviewV3Impl(c, world, town, TibiaDataHTMLDataCollectorV3) + // An error occured, prevent sending an invalid json + if reflect.DeepEqual(jsonData, HousesOverviewResponse{}) { + return + } // return jsonData TibiaDataAPIHandleResponse(c, http.StatusOK, "TibiaHousesOverviewV3", jsonData) @@ -868,7 +873,7 @@ func TibiaDataHTMLDataCollectorV3(TibiaDataRequest TibiaDataRequestStruct) (stri case http.StatusFound: // Check if page is in maintenance mode location, _ := res.RawResponse.Location() - if location != nil && location.Host == "maintenance.tibia.com" { + if location.Host == "maintenance.tibia.com" { LogMessage := "maintenance mode detected on tibia.com" log.Printf("[info] TibiaDataHTMLDataCollectorV3: %s!", LogMessage) return "", errors.New(LogMessage) @@ -892,7 +897,6 @@ func TibiaDataHTMLDataCollectorV3(TibiaDataRequest TibiaDataRequestStruct) (stri doc, err := goquery.NewDocumentFromReader(resIo2) if err != nil { log.Printf("[error] TibiaDataHTMLDataCollectorV3 (URL: %s) error: %s", TibiaDataRequest.URL, err) - return "", err } // Find of this to get div with class BoxContent