mcr

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2024 License: MIT Imports: 5 Imported by: 3

README

mcr

Dependency-free Minecraft remote console (RCon) package written in Golang

Pipeline

usage

import (
	"log"
	"os"

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

func main() {
	//create new client with minecraft server address and nonmandatory options if the default values need to be changed
	client := mcr.NewClient(os.Getenv("rcon_address"))

	//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 minecraft server
	if err != nil {
		panic(err)
	}

	log.Println(response)
}

security

  • RCon is an inherently insecure protocol that sends passwords in plaintext. I recommend using a VPN or keeping the connection local when possible.

notes

  • To prevent using connections prematurely the client does not connect to the server on creation, the Connect method must be called
  • To cleanup connections after use call the Close method, it is recommended to defer the Close after the call to Connect
  • The client can be reused after closing by calling the Connect method again

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
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

minecraft remote console client

func NewClient

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

creates a new remote console client using the supplied address and applys the options before returning. The client is not connected to the server and Connect method must be called before the client can be used to send commands

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 minecraft 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. Command examples can be found on the minecraft wiki: https://minecraft.wiki/w/Commands

func (*Client) Connect

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

connects to minecraft 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 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