rcon

package module
v0.0.0-...-63f30b2 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2023 License: MIT Imports: 8 Imported by: 0

README

Rcon

GitHub Build Coverage Go Report Card GoDoc

Source RCON Protocol implementation in Go.

Protocol Specifications

RCON Protocol described in the valve documentation.

Supported Games

Open pull request if you have successfully used a package with another game with rcon support and add it to the list.

Install

go get github.com/gorcon/rcon

See Changelog for release details.

Usage

package main

import (
	"log"
	"fmt"

	"github.com/gorcon/rcon"
)

func main() {
	conn, err := rcon.Dial("127.0.0.1:16260", "password")
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	response, err := conn.Execute("help")
	if err != nil {
		log.Fatal(err)
	}
	
	fmt.Println(response)	
}

Requirements

Go 1.15 or higher

Contribute

Contributions are more than welcome!

If you think that you have found a bug, create an issue and publish the minimum amount of code triggering the bug so it can be reproduced.

If you want to fix the bug then you can create a pull request. If possible, write a test that will cover this bug.

License

MIT License, see LICENSE

Documentation

Overview

Package rcon implements Source RCON Protocol which is described in the documentation: https://developer.valvesoftware.com/wiki/Source_RCON_Protocol.

Index

Constants

View Source
const (
	PacketPaddingSize int32 = 2 // Size of Packet's padding.
	PacketHeaderSize  int32 = 8 // Size of Packet's header.

	MinPacketSize = PacketPaddingSize + PacketHeaderSize
	MaxPacketSize = 4096 + MinPacketSize
)

Packet sizes definitions.

View Source
const (
	// DefaultDialTimeout provides default auth timeout to remote server.
	DefaultDialTimeout = 5 * time.Second

	// DefaultDeadline provides default deadline to tcp read/write operations.
	DefaultDeadline = 5 * time.Second

	// MaxCommandLen is an artificial restriction, but it will help in case of random
	// large queries.
	MaxCommandLen = 1000

	// SERVERDATA_AUTH is the first packet sent by the client,
	// which is used to authenticate the conn with the server.
	SERVERDATA_AUTH int32 = 3

	// SERVERDATA_AUTH_ID is any positive integer, chosen by the client
	// (will be mirrored back in the server's response).
	SERVERDATA_AUTH_ID int32 = 0

	// SERVERDATA_AUTH_RESPONSE packet is a notification of the conn's current auth
	// status. When the server receives an auth request, it will respond with an empty
	// SERVERDATA_RESPONSE_VALUE, followed immediately by a SERVERDATA_AUTH_RESPONSE
	// indicating whether authentication succeeded or failed. Note that the status
	// code is returned in the packet id field, so when pairing the response with
	// the original auth request, you may need to look at the packet id of the
	// preceding SERVERDATA_RESPONSE_VALUE.
	// If authentication was successful, the ID assigned by the request.
	// If auth failed, -1 (0xFF FF FF FF).
	SERVERDATA_AUTH_RESPONSE int32 = 2

	// SERVERDATA_RESPONSE_VALUE packet is the response to a SERVERDATA_EXECCOMMAND
	// request. The ID assigned by the original request.
	SERVERDATA_RESPONSE_VALUE int32 = 0

	// SERVERDATA_EXECCOMMAND packet type represents a command issued to the server
	// by a client. The response will vary depending on the command issued.
	SERVERDATA_EXECCOMMAND int32 = 2

	// SERVERDATA_EXECCOMMAND_ID is any positive integer, chosen by the client
	// (will be mirrored back in the server's response).
	SERVERDATA_EXECCOMMAND_ID int32 = 0
)

Variables

View Source
var (
	// ErrAuthNotRCON is returned when got auth response with negative size.
	ErrAuthNotRCON = errors.New("response from not rcon server")

	// ErrInvalidAuthResponse is returned when we didn't get an auth packet
	// back for second read try after discard empty SERVERDATA_RESPONSE_VALUE
	// from authentication response.
	ErrInvalidAuthResponse = errors.New("invalid authentication packet type response")

	// ErrAuthFailed is returned when the package id from authentication
	// response is -1.
	ErrAuthFailed = errors.New("authentication failed")

	// ErrInvalidPacketID is returned when the package id from server response
	// was not mirrored back from request.
	ErrInvalidPacketID = errors.New("response for another request")

	// ErrInvalidPacketPadding is returned when the bytes after type field from
	// response is not equal to null-terminated ASCII strings.
	ErrInvalidPacketPadding = errors.New("invalid response padding")

	// ErrResponseTooSmall is returned when the server response is smaller
	// than 10 bytes.
	ErrResponseTooSmall = errors.New("response too small")

	// ErrCommandTooLong is returned when executed command length is bigger
	// than MaxCommandLen characters.
	ErrCommandTooLong = errors.New("command too long")

	// ErrCommandEmpty is returned when executed command length equal 0.
	ErrCommandEmpty = errors.New("command too small")

	// ErrMultiErrorOccurred is returned when close connection failed with
	// error after auth failed.
	ErrMultiErrorOccurred = errors.New("an error occurred while handling another error")
	ErrExecuteTimeout     = errors.New("execute timeout")
)
View Source
var DefaultSettings = Settings{
	// contains filtered or unexported fields
}

DefaultSettings provides default deadline settings to Conn.

Functions

This section is empty.

Types

type Conn

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

Conn is source RCON generic stream-oriented network connection.

func Dial

func Dial(address string, password string, options ...Option) (*Conn, error)

Dial creates a new authorized Conn tcp dialer connection.

func (*Conn) Close

func (c *Conn) Close() error

Close closes the connection.

func (*Conn) Execute

func (c *Conn) Execute(command string, timeout ...int) (string, error)

Execute sends command type and it string to execute to the remote server, creating a packet with a SERVERDATA_EXECCOMMAND_ID for the server to mirror, and compiling its payload bytes in the appropriate order. The response body is decompiled from bytes into a string for return.

func (*Conn) LocalAddr

func (c *Conn) LocalAddr() net.Addr

LocalAddr returns the local network address.

func (*Conn) RemoteAddr

func (c *Conn) RemoteAddr() net.Addr

RemoteAddr returns the remote network address.

type Option

type Option func(s *Settings)

Option allows to inject settings to Settings.

func SetDeadline

func SetDeadline(timeout time.Duration) Option

SetDeadline injects read/write Timeout to Settings.

func SetDialTimeout

func SetDialTimeout(timeout time.Duration) Option

SetDialTimeout injects dial Timeout to Settings.

type Packet

type Packet struct {
	// The packet size field is a 32-bit little endian integer, representing
	// the length of the request in bytes. Note that the packet size field
	// itself is not included when determining the size of the packet,
	// so the value of this field is always 4 less than the packet's actual
	// length. The minimum possible value for packet size is 10.
	// The maximum possible value of packet size is 4096.
	// If the response is too large to fit into one packet, it will be split
	// and sent as multiple packets.
	Size int32

	// The packet id field is a 32-bit little endian integer chosen by the
	// client for each request. It may be set to any positive integer.
	// When the RemoteServer responds to the request, the response packet
	// will have the same packet id as the original request (unless it is
	// a failed SERVERDATA_AUTH_RESPONSE packet).
	// It need not be unique, but if a unique packet id is assigned,
	// it can be used to match incoming responses to their corresponding requests.
	ID int32

	// The packet type field is a 32-bit little endian integer, which indicates
	// the purpose of the packet. Its value will always be either 0, 2, or 3,
	// depending on which of the following request/response types the packet
	// represents:
	// SERVERDATA_AUTH = 3,
	// SERVERDATA_AUTH_RESPONSE = 2,
	// SERVERDATA_EXECCOMMAND = 2,
	// SERVERDATA_RESPONSE_VALUE = 0.
	Type int32
	// contains filtered or unexported fields
}

Packet is a rcon packet. Both requests and responses are sent as TCP packets. Their payload follows the following basic structure.

func NewPacket

func NewPacket(packetType int32, packetID int32, body string) *Packet

NewPacket creates and initializes a new Packet using packetType, packetID and body as its initial contents. NewPacket is intended to calculate packet size from body length and 10 bytes for rcon headers and termination strings.

func (*Packet) Body

func (packet *Packet) Body() string

Body returns packet bytes body as a string.

func (*Packet) ReadFrom

func (packet *Packet) ReadFrom(r io.Reader) (int64, error)

ReadFrom implements io.ReaderFrom for read a packet from r.

func (*Packet) WriteTo

func (packet *Packet) WriteTo(w io.Writer) (int64, error)

WriteTo implements io.WriterTo for write a packet to w.

type Settings

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

Settings contains option to Conn.

Directories

Path Synopsis
Package rcontest contains RCON server for RCON client testing.
Package rcontest contains RCON server for RCON client testing.

Jump to

Keyboard shortcuts

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