rcontest

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package rcontest contains RCON server for RCON client testing.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuthHandler

func AuthHandler(c *Context)

AuthHandler checks authorisation data and responses with SERVERDATA_AUTH_RESPONSE packet.

func EmptyHandler

func EmptyHandler(c *Context)

EmptyHandler responses with empty body. Is used when start RCON Server with nil commandHandler.

Types

type Context

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

Context represents the context of the current RCON request. It holds request and conn objects.

func (*Context) Conn

func (c *Context) Conn() net.Conn

Conn returns current RCON connection.

func (*Context) Request

func (c *Context) Request() *rcon.Packet

Request returns received *rcon.Packet.

func (*Context) Server

func (c *Context) Server() *Server

Server returns the Server instance.

type HandlerFunc

type HandlerFunc func(c *Context)

HandlerFunc defines a function to serve RCON requests.

type Option

type Option func(s *Server)

Option allows to inject Settings to Server.

func SetAuthHandler

func SetAuthHandler(handler HandlerFunc) Option

SetAuthHandler injects HandlerFunc with authorisation data checking.

func SetCommandHandler

func SetCommandHandler(handler HandlerFunc) Option

SetCommandHandler injects HandlerFunc with commands processing.

func SetSettings

func SetSettings(settings Settings) Option

SetSettings injects configuration for RCON Server.

type Server

type Server struct {
	Settings Settings
	Listener net.Listener
	// contains filtered or unexported fields
}

Server is an RCON server listening on a system-chosen port on the local loopback interface, for use in end-to-end RCON tests.

Example
package main

import (
	"fmt"
	"log"

	"github.com/gorcon/rcon"
	"github.com/gorcon/rcon/rcontest"
)

func main() {
	server := rcontest.NewServer(
		rcontest.SetSettings(rcontest.Settings{Password: "password"}),
		rcontest.SetCommandHandler(func(c *rcontest.Context) {
			switch c.Request().Body() {
			case "Hello, server":
				rcon.NewPacket(rcon.SERVERDATA_RESPONSE_VALUE, c.Request().ID, "Hello, client").WriteTo(c.Conn())
			default:
				rcon.NewPacket(rcon.SERVERDATA_RESPONSE_VALUE, c.Request().ID, "unknown command").WriteTo(c.Conn())
			}
		}),
	)
	defer server.Close()

	client, err := rcon.Dial(server.Addr(), "password")
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	response, err := client.Execute("Hello, server")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(response)

	response, err = client.Execute("Hi!")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(response)

}
Output:
Hello, client
unknown command

func NewServer

func NewServer(options ...Option) *Server

NewServer returns a running RCON Server or nil if an error occurred. The caller should call Close when finished, to shut it down.

func NewUnstartedServer

func NewUnstartedServer(options ...Option) *Server

NewUnstartedServer returns a new Server but doesn't start it. After changing its configuration, the caller should call Start. The caller should call Close when finished, to shut it down.

func (*Server) Addr

func (s *Server) Addr() string

Addr returns IPv4 string Server address.

func (*Server) Close

func (s *Server) Close()

Close shuts down the Server.

func (*Server) NewContext

func (s *Server) NewContext(conn net.Conn) (*Context, error)

NewContext returns a Context instance.

func (*Server) SetAuthHandler

func (s *Server) SetAuthHandler(handler HandlerFunc)

SetAuthHandler injects HandlerFunc with authorisation data checking.

func (*Server) SetCommandHandler

func (s *Server) SetCommandHandler(handler HandlerFunc)

SetCommandHandler injects HandlerFunc with commands processing.

func (*Server) Start

func (s *Server) Start()

Start starts a server from NewUnstartedServer.

type Settings

type Settings struct {
	Password             string
	AuthResponseDelay    time.Duration
	CommandResponseDelay time.Duration
}

Settings contains configuration for RCON Server.

Jump to

Keyboard shortcuts

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