Allow backwards compatibility of Votifier votes

This commit is contained in:
Jacob Gunther
2024-05-20 18:15:00 -05:00
parent c1c77e7f64
commit 24d0102648
4 changed files with 68 additions and 91 deletions

View File

@@ -167,41 +167,20 @@ 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)
c, cancel := context.WithTimeout(context.Background(), opts.Timeout)
defer cancel()
defer cancel()
if err = mcutil.SendVote(c, opts.Host, opts.Port, options.Vote{
PublicKey: opts.PublicKey,
ServiceName: opts.ServiceName,
Username: opts.Username,
IPAddress: opts.IPAddress,
Timestamp: opts.Timestamp,
Timeout: opts.Timeout,
}); 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{
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())
}
}
if err = mcutil.SendVote(c, opts.Host, opts.Port, options.Vote{
PublicKey: opts.PublicKey,
Token: opts.Token,
ServiceName: opts.ServiceName,
Username: opts.Username,
IPAddress: opts.IPAddress,
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

@@ -154,15 +154,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")
@@ -206,18 +197,12 @@ func GetVoteOptions(ctx *fiber.Ctx) (*VoteOptions, error) {
{
result.PublicKey = ctx.Query("publickey")
if result.Version == 1 && len(result.PublicKey) < 1 {
return nil, fmt.Errorf("invalid 'publickey' query parameter: %s", result.PublicKey)
}
log.Println(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
@@ -247,6 +232,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
}