Allow backwards compatibility of Votifier votes

This commit is contained in:
Jacob Gunther 2024-05-20 18:18:15 -05:00
parent bf3d6ecb83
commit 3e0541ab60
No known key found for this signature in database
GPG Key ID: 9E6F3F4BF45EC433
4 changed files with 20 additions and 58 deletions

2
go.mod
View File

@ -7,7 +7,7 @@ toolchain go1.22.3
require (
github.com/go-redsync/redsync/v4 v4.13.0
github.com/gofiber/fiber/v2 v2.52.4
github.com/mcstatus-io/mcutil/v3 v3.5.0
github.com/mcstatus-io/mcutil/v3 v3.6.0
github.com/redis/go-redis/v9 v9.5.1
gopkg.in/yaml.v3 v3.0.1
)

6
go.sum
View File

@ -36,10 +36,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mcstatus-io/mcutil/v3 v3.4.0 h1:mYELJ+lP6mWqL+bt2W9JIUMmfSZtrfBogBZ2p3HsCPQ=
github.com/mcstatus-io/mcutil/v3 v3.4.0/go.mod h1:f1hgiUD3WoNmeZdN1AXYASSEO7yPxVEsLCGXnPkK6p4=
github.com/mcstatus-io/mcutil/v3 v3.5.0 h1:RoMFR2hZtJL1JA4rrTSTL8Gp11j6mhLR8yOSU0ICCzs=
github.com/mcstatus-io/mcutil/v3 v3.5.0/go.mod h1:f1hgiUD3WoNmeZdN1AXYASSEO7yPxVEsLCGXnPkK6p4=
github.com/mcstatus-io/mcutil/v3 v3.6.0 h1:IJqIXMtVtz1yUcMKxnrUIcAlChm/OTNAPzyb16TTwLk=
github.com/mcstatus-io/mcutil/v3 v3.6.0/go.mod h1:6Rv4qnejaTezu/feuQ2M4131phQxw0Q4C5h3An/iNxI=
github.com/redis/go-redis/v9 v9.5.1 h1:H1X4D3yHPaYrkL5X06Wh6xNVM/pX0Ft4RV0vMGvLBh8=
github.com/redis/go-redis/v9 v9.5.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/redis/rueidis v1.0.19 h1:s65oWtotzlIFN8eMPhyYwxlwLR1lUdhza2KtWprKYSo=

View File

@ -162,15 +162,12 @@ func SendVoteHandler(ctx *fiber.Ctx) error {
return ctx.Status(http.StatusBadRequest).SendString(err.Error())
}
switch opts.Version {
case 1:
{
c, cancel := context.WithTimeout(context.Background(), opts.Timeout)
defer cancel()
if err = mcutil.SendVote(c, opts.Host, opts.Port, options.Vote{
RequireVersion: 1,
Token: opts.Token,
PublicKey: opts.PublicKey,
ServiceName: opts.ServiceName,
Username: opts.Username,
@ -180,26 +177,6 @@ func SendVoteHandler(ctx *fiber.Ctx) error {
}); err != nil {
return ctx.Status(http.StatusBadRequest).SendString(err.Error())
}
}
case 2:
{
c, cancel := context.WithTimeout(context.Background(), opts.Timeout)
defer cancel()
if err = mcutil.SendVote(c, opts.Host, opts.Port, options.Vote{
RequireVersion: 2,
ServiceName: opts.ServiceName,
Username: opts.Username,
Token: opts.Token,
UUID: opts.UUID,
Timestamp: opts.Timestamp,
Timeout: opts.Timeout,
}); err != nil {
return ctx.Status(http.StatusBadRequest).SendString(err.Error())
}
}
}
return ctx.Status(http.StatusOK).SendString("The vote was successfully sent to the server")
}

View File

@ -29,7 +29,6 @@ var (
// VoteOptions is the options provided as query parameters to the vote route.
type VoteOptions struct {
Version int
IPAddress string
Host string
Port uint16
@ -152,15 +151,6 @@ func ParseAddress(address string, defaultPort uint16) (string, uint16, error) {
func GetVoteOptions(ctx *fiber.Ctx) (*VoteOptions, error) {
result := VoteOptions{}
// Version
{
result.Version = ctx.QueryInt("version", 2)
if result.Version < 0 || result.Version > 2 {
return nil, fmt.Errorf("invalid 'version' query parameter: %d", result.Version)
}
}
// Host
{
result.Host = ctx.Query("host")
@ -203,19 +193,11 @@ func GetVoteOptions(ctx *fiber.Ctx) (*VoteOptions, error) {
// Public Key
{
result.PublicKey = ctx.Query("publickey")
if result.Version == 1 && len(result.PublicKey) < 1 {
return nil, fmt.Errorf("invalid 'publickey' query parameter: %s", result.PublicKey)
}
}
// Token
{
result.Token = ctx.Query("token")
if result.Version == 2 && len(result.Token) < 1 {
return nil, fmt.Errorf("invalid 'token' query parameter: %s", result.Token)
}
}
// IP Address
@ -245,6 +227,11 @@ func GetVoteOptions(ctx *fiber.Ctx) (*VoteOptions, error) {
result.Timeout = time.Duration(math.Max(float64(time.Second)*ctx.QueryFloat("timeout", 5.0), float64(time.Millisecond*250)))
}
// Test token and public key parameters
if len(result.Token) < 1 && len(result.PublicKey) < 1 {
return nil, errors.New("query parameter 'token', 'publickey' or both must have a value, but both were empty")
}
return &result, nil
}