mcr

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2025 License: MIT Imports: 6 Imported by: 3

README

mcr

mcr is a dependency-free remote console (RCon) package written in Golang following the Source protocol.

Pipeline

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.

Documentation

Index

Constants

View Source
const (
	//rcon packet type values
	FailurePacket = int32(-1)
	CommandPacket = int32(2)
	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 values
	ResetID        = 1
	DefaultCap     = 100
	DefaultTimeout = time.Second * 10
	DefaultPort    = 61695
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

remote console client

func NewClient

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

creates a new remote console client configured with the supplied options. The client does not connect to the server until the Connect method is called to authenticate the client. Check the README for information on default values

func (*Client) Close

func (c *Client) Close() error

closes remote console connection, nil's out the connection value in client struct, and resets the request id

func (*Client) Command

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

sends a command to the server and returns the server response, an error is returned if the client has not connected to the server before attempting to send a command

func (*Client) Connect

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

connects to server and authenticates the client. Ensure to call or defer the call to the Close method to clean up the connection

type IClient

type IClient interface {
	//main rcon methods
	Connect(password string) error
	Command(cmd string) (string, error)
	Close() error
	// contains filtered or unexported methods
}

type Option added in v0.2.0

type Option func(cn *Client)

request option func skeleton

func WithCap added in v0.2.1

func WithCap(c int32) Option

option to allow for custom request id cap

func WithConnection added in v1.1.1

func WithConnection(c net.Conn) Option

option to allow for use of custom connections

func WithPort added in v1.0.1

func WithPort(port int) Option

option to allow for custom port values

func WithTimeout added in v0.2.0

func WithTimeout(timeout time.Duration) Option

option to allow for custom timeout values

Jump to

Keyboard shortcuts

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