Skip to content

Commit

Permalink
feat - Handle introduced A2S_INFO challenge (#5)
Browse files Browse the repository at this point in the history
* update A2S_INFO to send challenge if necessary

* remove extra print statements
  • Loading branch information
mpawlowski authored May 20, 2021
1 parent 4c67caf commit ee5866f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
9 changes: 2 additions & 7 deletions challenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@ var (
ErrBadChallengeResponse = errors.New("Bad challenge response")
)

func (c *Client) getChallenge(header byte, fullResult byte) ([]byte, bool, error) {
if err := c.send([]byte{
0xff, 0xff, 0xff, 0xff,
header,
0xff, 0xff, 0xff, 0xff,
}); err != nil {
func (c *Client) getChallenge(header []byte, fullResult byte) ([]byte, bool, error) {
if err := c.send(header); err != nil {
return nil, false, err
}

Expand All @@ -32,7 +28,6 @@ func (c *Client) getChallenge(header byte, fullResult byte) ([]byte, bool, error
switch int32(reader.ReadUint32()) {
case -2:
// We received an unexpected full reply

return data, true, nil
case -1:
// Continue
Expand Down
29 changes: 21 additions & 8 deletions info.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package a2s

import "errors"
import (
"errors"
)

const (
A2S_INFO_HEADER = 0x49 // Source & up
A2S_INFO_REQUEST = 0x54
A2S_INFO_RESPONSE = 0x49 // Source & up
)

var (
Expand Down Expand Up @@ -103,19 +106,28 @@ func (c *Client) QueryInfo() (*ServerInfo, error) {
*/
builder.WriteBytes([]byte{
0xff, 0xff, 0xff, 0xff, 0x54,
0xFF, 0xFF, 0xFF, 0xFF, A2S_INFO_REQUEST,
})

builder.WriteCString("Source Engine Query")

if err := c.send(builder.Bytes()); err != nil {
data, immediate, err := c.getChallenge(builder.Bytes(), A2S_INFO_RESPONSE)

if err != nil {
return nil, err
}

data, err := c.receive()
if !immediate {
builder.WriteBytes(data)
if err := c.send(builder.Bytes()); err != nil {
return nil, err
}

if err != nil {
return nil, err
data, err = c.receive()

if err != nil {
return nil, err
}
}

/*
Expand All @@ -131,7 +143,8 @@ func (c *Client) QueryInfo() (*ServerInfo, error) {

info := &ServerInfo{}

if reader.ReadUint8() != A2S_INFO_HEADER {
header := reader.ReadUint8()
if header != A2S_INFO_RESPONSE {
return nil, ErrUnsupportedHeader
}

Expand Down
3 changes: 2 additions & 1 deletion player.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ func (c *Client) QueryPlayer() (*PlayerInfo, error) {
FF FF FF FF 55 4B A1 D5 22 ÿÿÿÿUÿÿÿÿ"
*/

data, immediate, err := c.getChallenge(A2S_PLAYER_REQUEST, A2S_PLAYER_RESPONSE)
playerRequest := []byte{0xFF, 0xFF, 0xFF, 0xFF, A2S_PLAYER_REQUEST, 0xFF, 0xFF, 0xFF, 0xFF}
data, immediate, err := c.getChallenge(playerRequest, A2S_PLAYER_RESPONSE)

if err != nil {
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ func (c *Client) QueryRules() (*RulesInfo, error) {
FF FF FF FF 56 4B A1 D5 22 ÿÿÿÿVK¡Õ"
*/

data, immediate, err := c.getChallenge(A2S_RULES_REQUEST, A2S_RULES_RESPONSE)
ruleRequest := []byte{0xFF, 0xFF, 0xFF, 0xFF, A2S_RULES_REQUEST, 0xFF, 0xFF, 0xFF, 0xFF}
data, immediate, err := c.getChallenge(ruleRequest, A2S_RULES_RESPONSE)

if err != nil {
return nil, err
Expand Down

0 comments on commit ee5866f

Please sign in to comment.