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.
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 ¶
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 ¶
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 ¶
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) NewContext ¶
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.