diff --git a/go.mod b/go.mod index 4c55ed1..dbc1e0d 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,12 @@ 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.43.0 - github.com/mcstatus-io/mcutil v0.0.0-20230331002438-59f81e936131 + github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 + github.com/mcstatus-io/mcutil v0.0.0-20230331012121-a2cb94e8ebfe + golang.org/x/image v0.6.0 gopkg.in/yaml.v3 v3.0.1 ) diff --git a/go.sum b/go.sum index 53546dd..cbd0c26 100644 --- a/go.sum +++ b/go.sum @@ -4,11 +4,15 @@ 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.43.0 h1:yit3E4kHf178B60p5CQBa/3v+WVuziWMa/G2ZNyLJB0= github.com/gofiber/fiber/v2 v2.43.0/go.mod h1:mpS1ZNE5jU+u+BA4FbM+KKnUzJ4wzTK+FT2tG3tU+6I= +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.3 h1:XuJt9zzcnaz6a16/OU53ZjWp/v7/42WcR5t2a0PcNQY= @@ -20,10 +24,8 @@ github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp9 github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= -github.com/mcstatus-io/mcutil v0.0.0-20230326225412-9e5254880e3c h1:/8+KhqSkpQR26FLuuxlV2Yw17T+9w9VlnrwuOgolPVo= -github.com/mcstatus-io/mcutil v0.0.0-20230326225412-9e5254880e3c/go.mod h1:VUB87/x9EYITmQVXZO4eS+egaZOdvxId4IdpU4L5LoA= -github.com/mcstatus-io/mcutil v0.0.0-20230331002438-59f81e936131 h1:h5aBODlRBQTHRAvjbdNdYWt98BlP5e2GijGJnSP5b7g= -github.com/mcstatus-io/mcutil v0.0.0-20230331002438-59f81e936131/go.mod h1:VUB87/x9EYITmQVXZO4eS+egaZOdvxId4IdpU4L5LoA= +github.com/mcstatus-io/mcutil v0.0.0-20230331012121-a2cb94e8ebfe h1:hbmOZmXWQgx+R7RTL4V0ORTEn4GNp2/+cKcqyz4k9yU= +github.com/mcstatus-io/mcutil v0.0.0-20230331012121-a2cb94e8ebfe/go.mod h1:VUB87/x9EYITmQVXZO4eS+egaZOdvxId4IdpU4L5LoA= github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE= @@ -53,15 +55,19 @@ 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.6.0 h1:bR8b5okrPI3g/gyZakLZHeWxAR8Dn5CyxXv1hLH5g/4= +golang.org/x/image v0.6.0/go.mod h1:MXLdDR43H7cDJq5GEGXEVeeNhPgi+YYEQ2pC1byI1x0= 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= @@ -76,21 +82,26 @@ 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 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= golang.org/x/sys v0.6.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.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68= +golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= 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 new file mode 100644 index 0000000..b173da2 Binary files /dev/null and b/src/Ubuntu-B.ttf differ diff --git a/src/Ubuntu-R.ttf b/src/Ubuntu-R.ttf new file mode 100644 index 0000000..d748728 Binary files /dev/null and b/src/Ubuntu-R.ttf differ diff --git a/src/UbuntuMono-R.ttf b/src/UbuntuMono-R.ttf new file mode 100644 index 0000000..fdd309d Binary files /dev/null and b/src/UbuntuMono-R.ttf differ diff --git a/src/routes.go b/src/routes.go index 2ff7cf0..eb0f10d 100644 --- a/src/routes.go +++ b/src/routes.go @@ -11,6 +11,7 @@ import ( func init() { app.Get("/ping", PingHandler) app.Get("/status/java/:address", JavaStatusHandler) + app.Get("/widget/java/:address", JavaWidgetHandler) app.Get("/status/bedrock/:address", BedrockStatusHandler) app.Get("/icon", DefaultIconHandler) app.Get("/icon/:address", IconHandler) @@ -49,6 +50,33 @@ 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) + + 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) @@ -101,7 +129,7 @@ func IconHandler(ctx *fiber.Ctx) error { // DefaultIconHandler returns the default server icon. func DefaultIconHandler(ctx *fiber.Ctx) error { - return ctx.Type("png").Send(defaultIcon) + return ctx.Type("png").Send(defaultIconBytes) } // NotFoundHandler handles requests to routes that do not exist and returns a 404 Not Found status. diff --git a/src/status.go b/src/status.go index 07da24e..8e0470c 100644 --- a/src/status.go +++ b/src/status.go @@ -187,7 +187,7 @@ func GetServerIcon(host string, port uint16) ([]byte, time.Duration, error) { return cache, ttl, err } - icon := defaultIcon + icon := defaultIconBytes status, err := mcutil.Status(host, port) diff --git a/src/util.go b/src/util.go index c5c8297..9030dd0 100644 --- a/src/util.go +++ b/src/util.go @@ -1,25 +1,86 @@ 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 - defaultIcon []byte - blockedServers *MutexArray = nil - ipAddressRegex *regexp.Regexp = regexp.MustCompile(`^\d{1,3}(\.\d{1,3}){3}$`) + 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}$`) ) +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{} @@ -123,3 +184,49 @@ 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 +} diff --git a/src/widget.go b/src/widget.go new file mode 100644 index 0000000..829babb --- /dev/null +++ b/src/widget.go @@ -0,0 +1,95 @@ +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()) +}