Skip to content

Commit

Permalink
Add version checking for Votifier methods
Browse files Browse the repository at this point in the history
  • Loading branch information
PassTheMayo committed May 19, 2024
1 parent 1e77ed4 commit f7fba5c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
17 changes: 9 additions & 8 deletions options/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import "time"

// Vote is the options used by the SendVote() function
type Vote struct {
PublicKey string
ServiceName string
Username string
Token string
UUID string
IPAddress string
Timestamp time.Time
Timeout time.Duration
RequireVersion int
PublicKey string
ServiceName string
Username string
Token string
UUID string
IPAddress string
Timestamp time.Time
Timeout time.Duration
}
4 changes: 2 additions & 2 deletions status.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func getStatus(host string, port uint16, options ...options.JavaStatus) (*respon
latency = time.Since(pingStart)
}

return formatJavaStatusResponse(serverResponse, srvRecord, latency, opts)
return formatJavaStatusResponse(serverResponse, srvRecord, latency)
}

func parseJavaStatusOptions(opts ...options.JavaStatus) options.JavaStatus {
Expand Down Expand Up @@ -303,7 +303,7 @@ func readJavaStatusPongPacket(r io.Reader, payload int64) error {
return nil
}

func formatJavaStatusResponse(serverResponse rawJavaStatus, srvRecord *response.SRVRecord, latency time.Duration, opts options.JavaStatus) (*response.JavaStatus, error) {
func formatJavaStatusResponse(serverResponse rawJavaStatus, srvRecord *response.SRVRecord, latency time.Duration) (*response.JavaStatus, error) {
motd, err := formatting.Parse(serverResponse.Description)

if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func sendVote(host string, port uint16, opts options.Vote) error {
switch strings.Split(version, ".")[0] {
case "1":
{
if opts.RequireVersion != 0 && opts.RequireVersion != 1 {
return fmt.Errorf("vote: version mismatch (server is running Votifier 1, expected to send version %d vote)", opts.RequireVersion)
}

if len(opts.PublicKey) < 1 {
return ErrPublicKeyRequired
}
Expand Down Expand Up @@ -163,6 +167,10 @@ func sendVote(host string, port uint16, opts options.Vote) error {
}
case "2":
{
if opts.RequireVersion != 0 && opts.RequireVersion != 2 {
return fmt.Errorf("vote: version mismatch (server is running Votifier 2, expected to send version %d vote)", opts.RequireVersion)
}

if len(opts.Token) < 1 {
return ErrTokenRequired
}
Expand Down

0 comments on commit f7fba5c

Please sign in to comment.