mcr

package module
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2026 License: MIT Imports: 6 Imported by: 3

README

mcr

tests
mcr is a pure go remote console (RCon) package following the Source protocol.

Usage

import (
	"log"
	"os"

	"github.com/jake-young-dev/mcr"
)

func main() {
	//create new client to server address on port 9876
	client := mcr.NewClient(os.Getenv("rcon_address"), mcr.WithPort(9876))

	//connect to server and authenticate with password
	err := client.Connect(os.Getenv("rcon_password"))
	if err != nil {
		panic(err)
	}
	defer client.Close() //always call close to clean up your connections

	response, err := client.Command("list") //run "list" command on server
	if err != nil {
		panic(err)
	}

	log.Println(response)
}

Default Options

  • Timeout is defaulted 10 seconds
  • Port is defaulted to 61695

Security

  • RCon is an inherently insecure protocol that sends passwords in plaintext. I recommend using a VPN or keeping the connection local when possible.
  • All code is checked with gosec as an added security measure

Documentation

Overview

mcr is an RCon client that provides useful methods for connecting to and managing game servers that support the source protocol

Index

Constants

View Source
const (

	//the server encountered an error while handling the last request
	FailurePacket = int32(-1)
	//represents any command structs as well as there responses
	CommandPacket = int32(2)
	//used for any packets in the authentication handshake
	AuthPacket = int32(3)

	//tcp constants
	Protocol          = "tcp"
	PacketRequestSize = 10 //size of headers plus padding bytes, not including Size header per RCon standard
	PacketHeaderSize  = 8  //size of headers not including Size header per RCon standard
	PacketPaddingSize = 2  //size of padding required after body

	//default client configuration values
	ResetID        = 1
	DefaultCap     = 100
	DefaultTimeout = time.Second * 10
	DefaultPort    = 61695
)

Variables

View Source
var (
	ErrClientNotConnected = errors.New("client not connected. The Connect method must be called before commands can be run")
	ErrIntOverflow        = errors.New("integer overflowed 32 bits")
)

Functions

This section is empty.

Types

type Client

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

Client contains the configuration and methods for the RCon connection

func NewClient

func NewClient(addr string, opts ...Option) *Client

NewClient creates a new remote console client configured with the supplied options. The Connect method must be called before the server can be interacted with.

func (*Client) Address added in v1.4.0

func (c *Client) Address() string

Address returns the current server address, this value cannot be updated after the connection is made. Instead, a new client should be created

func (*Client) Close

func (c *Client) Close() error

Close disconnects from the server and resets the clients request id.

func (*Client) Command

func (c *Client) Command(cmd string) (string, error)

Command sends the payload to the server and waits for the server response. The response packet is parsed and the body data is returned, an error is returned if the client has not connected before sending the packet.

func (*Client) CommandNoResponse added in v1.3.2

func (c *Client) CommandNoResponse(cmd string) error

CommandNoResponse sends a payload to the server without waiting for a response, the client must be connected to the server before any commands can be sent.

func (*Client) Connect

func (c *Client) Connect(password string) error

Connect sends the connection request to the server and authenticates the client. Ensure to call, or defer the call to, the Close method to clean up the connection after use

func (*Client) Connection added in v1.4.0

func (c *Client) Connection() net.Conn

Connection returns the underlying client connection, this value cannot be updated after the server has been connected to. Instead, a new client must be created.

func (*Client) Port added in v1.4.0

func (c *Client) Port() int

Port returns the server port, this value cannot be updated after the connection is made. Instead, a new client should be created

func (*Client) RequestID added in v0.1.7

func (c *Client) RequestID() int32

RequestID returns the current packets request ID.

func (*Client) SetRequestID added in v1.4.0

func (c *Client) SetRequestID(id int32)

SetRequestID of the current packet.

func (*Client) Timeout added in v1.4.0

func (c *Client) Timeout() time.Duration

Timeout returns the current clients connection timeout.

type Option added in v0.2.0

type Option func(cn *Client)

func WithCap added in v0.2.1

func WithCap(c int32) Option

set custom request ID cap, the request ID is the identifier used per-packet a max value is set to prevent any overflow issues

func WithConnection added in v1.1.1

func WithConnection(c net.Conn) Option

enables use of a custom connection instead of the default one

func WithID added in v1.4.0

func WithID(i int32) Option

updates the current request id

func WithPort added in v1.0.1

func WithPort(port int) Option

apply a custom port number to the connection

func WithTimeout added in v0.2.0

func WithTimeout(timeout time.Duration) Option

set a specific timeout for the underlying connection, this timeout applies to the resolution of addresses and establishing the initial connection

Jump to

Keyboard shortcuts

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