browser

package
v1.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 22, 2024 License: MIT Imports: 12 Imported by: 4

Documentation

Index

Constants

View Source
const (
	// Used for master server
	// RequestServerList is a header used in order to fetch the server list from the master server
	RequestServerList = "\xff\xff\xff\xffreq2"
	// RequestServerCount is a header used in order to fetch the number of servers from the master server
	RequestServerCount = "\xff\xff\xff\xffcou2"

	// SendServerList is used by the master server when sending the server list
	SendServerList = "\xff\xff\xff\xfflis2"
	// SendServerCount is used by the master server when sending the server count
	SendServerCount = "\xff\xff\xff\xffsiz2"

	// Used for the gameserver
	// RequestInfo is used in order to request the server info of a game server
	RequestInfo = "\xff\xff\xff\xffgie3\x00" // need explicitly the trailing \x00
	// SendInfo is used
	SendInfo = "\xff\xff\xff\xffinf3\x00"
)

Variables

View Source
var (
	// Logging can be set to "true" in order to see more logging output from the package.
	Logging = false

	// TimeoutMasterServers is used by ServerInfos as a value that drops few packets
	TimeoutMasterServers = 5 * time.Second

	// TimeoutServers is also used by ServerInfos as a alue that drops few packets
	TimeoutServers = TokenExpirationDuration

	// ErrInvalidIP is returned if the passed IP to some function is not a valid IP address
	ErrInvalidAddress = errors.New("invalid address passed")

	// ErrInvalidIP is returned if the passed IP to some function is not a valid IP address
	ErrInvalidIP = errors.New("invalid IP passed")

	// ErrInvalidPort is returned if the passed port is either negative or an invalid value above 65536.
	ErrInvalidPort = errors.New("invalid port passed")

	// ErrTokenExpired is returned when a request packet is being constructed with an expired token
	ErrTokenExpired = errors.New("token expired")

	// ErrInvalidResponseMessage is returned when a passed response message does not contain the expected data.
	ErrInvalidResponseMessage = errors.New("invalid response message")

	// ErrInvalidHeaderLength is returned, if a too short byte slice is passed to some of the parsing methods
	ErrInvalidHeaderLength = errors.New("invalid header length")

	// ErrInvalidHeaderFlags is returned, if the first byte of a response message does not corespond to the expected flags.
	ErrInvalidHeaderFlags = errors.New("invalid header flags")

	// ErrUnexpectedResponseHeader is returned, if a message is passed to a parsing function, that expects a different response
	ErrUnexpectedResponseHeader = errors.New("unexpected response header")

	// ErrMalformedResponseData is reurned is a server sends broken or malformed response data
	// that cannot be properly parsed.
	ErrMalformedResponseData = errors.New("malformed response data")

	// ErrTimeout is used in Retry functions that support a timeout parameter
	ErrTimeout = errors.New("timeout")

	// ErrInvalidWrite is returned if writing to an io.Writer failed
	ErrInvalidWrite = errors.New("invalid write")

	// ErrRequestResponseMismatch is returned by functions that request and receive data, but the received data does not match the requested data.
	ErrRequestResponseMismatch = errors.New("request response mismatch")

	// TokenExpirationDuration sets the protocol expiration time of a token
	// This variable can be changed
	TokenExpirationDuration = time.Second * 16

	// MasterServerAddresses contains the resolved addresses as ip:port
	// initialized on startup with master servers that can be reached
	MasterServerAddresses = []string{
		"master1.teeworlds.com:8283",
		"master2.teeworlds.com:8283",
		"master3.teeworlds.com:8283",
		"master4.teeworlds.com:8283",
	}

	// ResponsePacketList is a list of known headers that we can expect from either master or game servers
	ResponsePacketList = [][]byte{
		[]byte(SendServerCount),
		[]byte(SendServerList),
		[]byte(SendInfo),
	}
)

Functions

func GetServerAddresses added in v1.3.0

func GetServerAddresses() ([]netip.AddrPort, error)

func NewRequestPacket added in v1.3.0

func NewRequestPacket(t Token, requestHeader string) ([]byte, error)

NewRequestPacket creates a new request payload from a token and a request header

func NewTokenRequestPacket

func NewTokenRequestPacket() []byte

NewTokenRequestPacket generates a new token request packet that can be used to request for a new server token

Types

type Client added in v1.3.0

type Client struct {
	// contains filtered or unexported fields
}

Client is a browser client tha can fetch master server infos and server infos of game servers

func NewClient added in v1.3.0

func NewClient(address string) (*Client, error)

NewClient creates a new browser client that can fetch the number of registered servers, from a single master server address which is the parameter that is passed to this constructor. Currently it is also possible to pass a game server address to this constructor in order to fetch that specific server's server info.

func (*Client) Close added in v1.3.0

func (c *Client) Close() error

func (*Client) GetServerAddresses added in v1.3.0

func (c *Client) GetServerAddresses() ([]netip.AddrPort, error)

GetServerAddresses returns a list of server addresses from the underlying master server

func (*Client) GetServerCount added in v1.3.0

func (c *Client) GetServerCount() (int, error)

GetServerCount returns the number of registered servers for the current master server

func (*Client) GetServerInfo added in v1.3.0

func (c *Client) GetServerInfo() (ServerInfo, error)

GetServerInfo returns the server info of a game server. This function requires the target to be set to a game server address

func (*Client) GetToken added in v1.3.0

func (c *Client) GetToken() (*Token, error)

GetToken returns a client/server token that secures the connection against IP spoofing

func (*Client) SetReadBuffer added in v1.3.0

func (c *Client) SetReadBuffer(bytes int) error

func (*Client) SetReadTimeout added in v1.3.0

func (c *Client) SetReadTimeout(d time.Duration)

func (*Client) SetTarget added in v1.3.0

func (c *Client) SetTarget(address string) error

func (*Client) SetWriteBuffer added in v1.3.0

func (c *Client) SetWriteBuffer(bytes int) error

func (*Client) SetWriteTimeout added in v1.3.0

func (c *Client) SetWriteTimeout(d time.Duration)

type PlayerInfo

type PlayerInfo struct {
	Name    string `json:"name"`
	Clan    string `json:"clan"`
	Type    int    `json:"type"`
	Country int    `json:"country"`
	Score   int    `json:"score"`
}

PlayerInfo contains a players externally visible information

func (*PlayerInfo) MarshalBinary added in v1.3.0

func (p *PlayerInfo) MarshalBinary() ([]byte, error)

marshalBinary returns a binary representation of the PlayerInfo no delimiter is appended at the end of the byte slice

func (*PlayerInfo) String

func (p *PlayerInfo) String() string

func (*PlayerInfo) UnmarshalBinary added in v1.3.0

func (p *PlayerInfo) UnmarshalBinary(data []byte) error

type PlayerInfos added in v1.3.0

type PlayerInfos []PlayerInfo

PlayerInfos must be pre allocated in order for the unmarshaler to know the number of playerinfos

func (PlayerInfos) UnmarshalBinary added in v1.3.0

func (pi PlayerInfos) UnmarshalBinary(data []byte) error

type RequestPacket added in v1.3.0

type RequestPacket struct {
	Token Token
	// Header is the request header defining the type of the request
	Header string
}

RequestPacket is a packet that can be sent either to the master servers or to a game server in order to fetch data

func (*RequestPacket) MarshalBinary added in v1.3.0

func (rp *RequestPacket) MarshalBinary() ([]byte, error)

MarshalBinary packages the request into its binary representation

type ResponsePacket added in v1.3.0

type ResponsePacket struct {
	Addr           *net.UDPAddr
	Token          Token
	ResponseHeader string
	Payload        []byte
}

ResponsePacket is a response that contains a token, a matched response header in order to identify that type and the ramaining payload that contains the data

func NewResponsePacket added in v1.3.0

func NewResponsePacket(data []byte) (*ResponsePacket, error)

func (*ResponsePacket) UnmarshalBinary added in v1.3.0

func (rp *ResponsePacket) UnmarshalBinary(data []byte) error

type ServerInfo

type ServerInfo struct {
	Address     string      `json:"address"`
	Version     string      `json:"version"`
	Name        string      `json:"name"`
	Hostname    string      `json:"hostname,omitempty"`
	Map         string      `json:"map"`
	GameType    string      `json:"gametype"`
	ServerFlags byte        `json:"server_flags"`
	SkillLevel  byte        `json:"skill_level"`
	NumPlayers  int         `json:"num_players"`
	MaxPlayers  int         `json:"max_players"`
	NumClients  int         `json:"num_clients"`
	MaxClients  int         `json:"max_clients"`
	Players     PlayerInfos `json:"players"`
}

ServerInfo contains the server's general information

func GetServerInfos added in v1.3.0

func GetServerInfos() ([]ServerInfo, error)

func GetServerInfosOf added in v1.3.0

func GetServerInfosOf(addresses ...string) ([]ServerInfo, error)

GetServerInfos allows you to fetch a list of server infos directly from Teeworlds servers that you provide here. You can pass domain.name.com:8303 or actual ips in here like ipv4:port or [ipv6]:port

func (*ServerInfo) Empty

func (s *ServerInfo) Empty() bool

Empty returns true if the whole struct does not contain any data at all

func (*ServerInfo) Equal

func (s *ServerInfo) Equal(other ServerInfo) bool

Equal compares two instances of ServerInfo and returns true if they are equal

func (*ServerInfo) MarshalBinary

func (s *ServerInfo) MarshalBinary() ([]byte, error)

MarshalBinary returns a binary representation of the ServerInfo

func (*ServerInfo) String

func (s *ServerInfo) String() string

func (*ServerInfo) UnmarshalBinary

func (s *ServerInfo) UnmarshalBinary(data []byte) error

UnmarshalBinary creates a serverinfo from binary data

type Token

type Token struct {
	//Payload   []byte //len should be 12 at most
	ExpiresAt   time.Time
	ClientToken int
	ServerToken int
}

Token is used to request information from either master of game servers. The token needs to be renewed via NewTokenRequestPacket() followed by parsing the server's response with NewToken(responseMessage []byte) (Token, error)

func (*Token) Equal

func (ts *Token) Equal(t Token) bool

Equal tests if two token contain the same payload

func (*Token) Expired

func (ts *Token) Expired() bool

Expired returns true if the token already expired and needs to be renewed

func (*Token) MarshalBinary added in v1.3.0

func (ts *Token) MarshalBinary() ([]byte, error)

MarshalBinary does satisfy the token prefix for secondary requests, but not for the initial token request

func (*Token) String

func (ts *Token) String() string

String implements the Stringer interface and returns a stringrepresentation of the token

func (*Token) UnmarshalBinary added in v1.3.0

func (ts *Token) UnmarshalBinary(data []byte) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL