diff --git a/go.mod b/go.mod index c1b15b5..83b563b 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,9 @@ module main go 1.19 require ( - github.com/fogleman/gg v1.3.0 github.com/go-redis/redis/v8 v8.11.5 github.com/gofiber/fiber/v2 v2.45.0 - github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 github.com/mcstatus-io/mcutil v1.1.0 - golang.org/x/image v0.7.0 gopkg.in/yaml.v3 v3.0.1 ) @@ -30,4 +27,5 @@ require ( github.com/valyala/fasthttp v1.47.0 // indirect github.com/valyala/tcplisten v1.0.0 // indirect golang.org/x/sys v0.8.0 // indirect + golang.org/x/text v0.9.0 // indirect ) diff --git a/go.sum b/go.sum index 21b500d..61abcb2 100644 --- a/go.sum +++ b/go.sum @@ -4,15 +4,11 @@ github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= -github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8= -github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI= github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo= github.com/gofiber/fiber/v2 v2.45.0 h1:p4RpkJT9GAW6parBSbcNFH2ApnAuW3OzaQzbOCoDu+s= github.com/gofiber/fiber/v2 v2.45.0/go.mod h1:DNl0/c37WLe0g92U6lx1VMQuxGUQY5V7EIaVoEsUffc= -github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= -github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/klauspost/compress v1.16.5 h1:IFV2oUNUzZaz+XyusxpLzpzS8Pt5rh0Z16For/djlyI= @@ -55,19 +51,15 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/image v0.7.0 h1:gzS29xtG1J5ybQlv0PuyfE3nmc6R4qB73m6LUUmvFuw= -golang.org/x/image v0.7.0/go.mod h1:nd/q4ef1AKKYl/4kft7g+6UyGbdiqWqTP1ZAbRoV7Rg= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE= -golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -82,19 +74,16 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.3.0/go.mod h1:q750SLmJuPmVoN1blW3UFBPREJfb1KmY3vwxfr+nFDA= -golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= @@ -102,7 +91,6 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.0.0-20201022035929-9cf592e881e9/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ= -golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/src/Ubuntu-B.ttf b/src/Ubuntu-B.ttf deleted file mode 100644 index b173da2..0000000 Binary files a/src/Ubuntu-B.ttf and /dev/null differ diff --git a/src/Ubuntu-R.ttf b/src/Ubuntu-R.ttf deleted file mode 100644 index d748728..0000000 Binary files a/src/Ubuntu-R.ttf and /dev/null differ diff --git a/src/UbuntuMono-R.ttf b/src/UbuntuMono-R.ttf deleted file mode 100644 index fdd309d..0000000 Binary files a/src/UbuntuMono-R.ttf and /dev/null differ diff --git a/src/routes.go b/src/routes.go index 84214dd..0c3e8e8 100644 --- a/src/routes.go +++ b/src/routes.go @@ -12,7 +12,6 @@ func init() { app.Get("/ping", PingHandler) app.Get("/status/java/:address", JavaStatusHandler) app.Get("/status/bedrock/:address", BedrockStatusHandler) - app.Get("/widget/java/:address", JavaWidgetHandler) app.Get("/icon", DefaultIconHandler) app.Get("/icon/:address", IconHandler) app.Use(NotFoundHandler) @@ -52,33 +51,6 @@ func JavaStatusHandler(ctx *fiber.Ctx) error { return ctx.JSON(response) } -// JavaWidgetHandler returns the widget of the Java Edition server. -func JavaWidgetHandler(ctx *fiber.Ctx) error { - host, port, err := ParseAddress(ctx.Params("address"), 25565) - - if err != nil { - return ctx.Status(http.StatusBadRequest).SendString("Invalid address value") - } - - if err = r.Increment(fmt.Sprintf("java-hits:%s-%d", host, port)); err != nil { - return err - } - - response, _, err := GetJavaStatus(host, port, false) - - if err != nil { - return err - } - - widget, err := GenerateJavaWidget(response, ctx.QueryBool("dark", true)) - - if err != nil { - return err - } - - return ctx.Type("png").Send(widget) -} - // BedrockStatusHandler returns the status of the Bedrock edition Minecraft server specified in the address parameter. func BedrockStatusHandler(ctx *fiber.Ctx) error { host, port, err := ParseAddress(ctx.Params("address"), 19132) diff --git a/src/util.go b/src/util.go index 8c724ff..c74ac43 100644 --- a/src/util.go +++ b/src/util.go @@ -1,86 +1,25 @@ package main import ( - "bytes" "crypto/sha1" _ "embed" - "encoding/base64" "encoding/hex" "fmt" - "image" - "image/png" "io" - "log" "net/http" "regexp" "strconv" "strings" "sync" - - "github.com/golang/freetype/truetype" - "golang.org/x/image/font" ) var ( //go:embed icon.png defaultIconBytes []byte - defaultIcon image.Image - //go:embed Ubuntu-B.ttf - ubuntuBoldFontBytes []byte - ubuntuBoldFont font.Face - //go:embed Ubuntu-R.ttf - ubuntuRegularFontBytes []byte - ubuntuRegularFont font.Face - //go:embed UbuntuMono-R.ttf - ubuntuMonoFontBytes []byte - ubuntuMonoFont font.Face - ubuntuMonoSmallFont font.Face - blockedServers *MutexArray = nil - ipAddressRegex *regexp.Regexp = regexp.MustCompile(`^\d{1,3}(\.\d{1,3}){3}$`) + blockedServers *MutexArray = nil + ipAddressRegex *regexp.Regexp = regexp.MustCompile(`^\d{1,3}(\.\d{1,3}){3}$`) ) -func init() { - var err error - - if defaultIcon, err = png.Decode(bytes.NewReader(defaultIconBytes)); err != nil { - log.Fatalf("Failed to parse default icon: %v", err) - } - - ubuntuBold, err := truetype.Parse(ubuntuBoldFontBytes) - - if err != nil { - log.Fatalf("Failed to parse Ubuntu Bold font: %v", err) - } - - ubuntuBoldFont = truetype.NewFace(ubuntuBold, &truetype.Options{ - Size: 36, - }) - - ubuntuRegular, err := truetype.Parse(ubuntuRegularFontBytes) - - if err != nil { - log.Fatalf("Failed to parse Ubuntu Regular font: %v", err) - } - - ubuntuRegularFont = truetype.NewFace(ubuntuRegular, &truetype.Options{ - Size: 36, - }) - - ubuntuMono, err := truetype.Parse(ubuntuMonoFontBytes) - - if err != nil { - log.Fatalf("Failed to parse Ubuntu Mono Regular font: %v", err) - } - - ubuntuMonoFont = truetype.NewFace(ubuntuMono, &truetype.Options{ - Size: 36, - }) - - ubuntuMonoSmallFont = truetype.NewFace(ubuntuMono, &truetype.Options{ - Size: 20, - }) -} - // MutexArray is a thread-safe array for storing and checking values. type MutexArray struct { List []interface{} @@ -185,52 +124,6 @@ func ParseAddress(address string, defaultPort uint16) (string, uint16, error) { return result[0], uint16(port), nil } -// EncodePNG encodes an image.Image into a byte array. -func EncodePNG(img image.Image) ([]byte, error) { - buf := &bytes.Buffer{} - - err := png.Encode(buf, img) - - return buf.Bytes(), err -} - -// GetStatusIcon returns the icon of the server if it exists, or returns the default icon. -func GetStatusIcon(response *JavaStatusResponse) (image.Image, error) { - if response == nil || !response.Online || response.Icon == nil { - return defaultIcon, nil - } - - data, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(*response.Icon, "data:image/png;base64,")) - - if err != nil { - return nil, err - } - - return png.Decode(bytes.NewReader(data)) -} - -// ScaleImageNearestNeighbor scales an image using the nearest neighbor method. -// This method is extremely inefficient, but at the small scaling values we are using the performance hit is almost immeasurable. -func ScaleImageNearestNeighbor(img image.Image, sx, sy int) image.Image { - s := img.Bounds().Size() - - out := image.NewRGBA(image.Rect(0, 0, s.X*sx, s.Y*sy)) - - for ix := 0; ix < s.X; ix++ { - for iy := 0; iy < s.Y; iy++ { - c := img.At(ix, iy) - - for ox := 0; ox < sx; ox++ { - for oy := 0; oy < sy; oy++ { - out.Set(ix*sx+ox, iy*sy+oy, c) - } - } - } - } - - return out -} - // PointerOf returns a pointer of the argument passed. func PointerOf[T any](v T) *T { return &v diff --git a/src/widget.go b/src/widget.go deleted file mode 100644 index 829babb..0000000 --- a/src/widget.go +++ /dev/null @@ -1,95 +0,0 @@ -package main - -import ( - "fmt" - "image/color" - - "github.com/fogleman/gg" -) - -const ( - // WidgetWidth is the width of the image of the widget. - WidgetWidth = 860 - // WidgetHeight is the height of the image of the widget. - WidgetHeight = 240 -) - -// GenerateJavaWidget generates a widget image from the Java Edition status response. -func GenerateJavaWidget(status *JavaStatusResponse, isDark bool) ([]byte, error) { - var statusColor color.Color = color.RGBA{230, 57, 70, 255} - var statusText string = "Offline" - var players string = "Unknown players" - var address string = status.Host - - if status.Online { - statusColor = color.RGBA{46, 204, 113, 255} - statusText = "Online" - - if status.Players.Online != nil && status.Players.Max != nil { - players = fmt.Sprintf("%d/%d players", *status.Players.Online, *status.Players.Max) - } else if status.Players.Online != nil { - players = fmt.Sprintf("%d players", *status.Players.Online) - } else { - players = "Unknown players" - } - } - - if status.Port != 25565 { - address += fmt.Sprintf(":%d", status.Port) - } - - icon, err := GetStatusIcon(status) - - if err != nil { - return nil, err - } - - ctx := gg.NewContext(WidgetWidth, WidgetHeight) - - // Background color - if isDark { - ctx.SetColor(color.Gray{32}) - } else { - ctx.SetColor(color.White) - } - ctx.DrawRoundedRectangle(0, 0, WidgetWidth, WidgetHeight, 16) - ctx.Fill() - - // Draw server icon - ctx.DrawImage(ScaleImageNearestNeighbor(icon, 3, 3), (WidgetHeight-64*3)/2, (WidgetHeight-64*3)/2) - - // Draw status bubble - ctx.SetColor(statusColor) - ctx.DrawCircle(WidgetHeight+8, WidgetHeight/2-55, 8) - ctx.Fill() - - // Draw status text - ctx.SetFontFace(ubuntuBoldFont) - ctx.DrawString(statusText, WidgetHeight+28, WidgetHeight/2-44) - ctx.Stroke() - - // Draw address text - if isDark { - ctx.SetColor(color.White) - } else { - ctx.SetColor(color.Black) - } - - ctx.SetFontFace(ubuntuMonoFont) - ctx.DrawString(address, WidgetHeight, WidgetHeight/2+4) - ctx.Stroke() - - // Draw players text - ctx.SetColor(color.Gray{127}) - ctx.SetFontFace(ubuntuRegularFont) - ctx.DrawString(players, WidgetHeight, WidgetHeight/2+58) - ctx.Stroke() - - // Draw mcstatus.io branding - ctx.SetColor(color.Gray{152}) - ctx.SetFontFace(ubuntuMonoSmallFont) - ctx.DrawStringAnchored("© mcstatus.io", WidgetWidth-16, WidgetHeight-38, 1.0, 1.0) - ctx.Stroke() - - return EncodePNG(ctx.Image()) -}