rcon/rcon.go

304 lines
8.4 KiB
Go
Raw Permalink Normal View History

2012-08-31 09:42:43 +02:00
package rcon
import (
"bytes"
"encoding/binary"
"errors"
2012-09-05 05:42:40 +02:00
"io"
2012-08-31 09:42:43 +02:00
"net"
"sync"
"time"
)
// Information to the protocol can be found under: https://developer.valvesoftware.com/wiki/Source_RCON_Protocol
2012-08-31 09:42:43 +02:00
const (
2021-03-03 21:08:37 +01:00
typeAuth = 3
typeExecCommand = 2
typeResponseValue = 0
typeAuthResponse = 2
2021-03-03 13:45:03 +01:00
fieldPackageSize = 4
fieldIDSize = 4
fieldTypeSize = 4
fieldMinBodySize = 1
fieldEndSize = 1
2012-08-31 09:42:43 +02:00
)
// The minimum package size contains:
// 4 bytes for the ID field
// 4 bytes for the Type field
// 1 byte minimum for an empty body string
// 1 byte for the empty string at the end
//
// https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Packet_Size
2021-03-03 13:45:03 +01:00
// The 4 bytes representing the size of the package are not included.
const minPackageSize = fieldIDSize + fieldTypeSize + fieldMinBodySize + fieldEndSize
// maxPackageSize of a request/response package.
// This size does not include the size field.
// https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Packet_Size
const maxPackageSize = 4096
// RemoteConsole holds the information to communicate withe remote console.
2012-08-31 09:42:43 +02:00
type RemoteConsole struct {
2021-03-03 21:08:37 +01:00
conn net.Conn
readBuff []byte
readMutex sync.Mutex
queuedBuff []byte
2012-08-31 09:42:43 +02:00
}
var (
// ErrAuthFailed the authentication against the server failed.
2021-03-03 21:08:37 +01:00
// This happens if the request id doesn't match the response id.
ErrAuthFailed = errors.New("rcon: authentication failed")
// ErrInvalidAuthResponse the response of an authentication request doesn't match the correct type.
2012-09-05 05:42:40 +02:00
ErrInvalidAuthResponse = errors.New("rcon: invalid response type during auth")
// ErrUnexpectedFormat the response package is not correctly formatted.
ErrUnexpectedFormat = errors.New("rcon: unexpected response format")
// ErrCommandTooLong the command is bigger than the bodyBufferSize.
ErrCommandTooLong = errors.New("rcon: command too long")
// ErrResponseTooLong the response package is bigger than the maxPackageSize.
ErrResponseTooLong = errors.New("rcon: response too long")
2012-08-31 09:42:43 +02:00
)
// Dial establishes a connection with the remote server.
// It can return multiple errors:
// - ErrInvalidAuthResponse
// - ErrAuthFailed
// - and other types of connection errors that are not specified in this package.
func Dial(host, password string) (*RemoteConsole, error) {
2012-08-31 09:42:43 +02:00
const timeout = 10 * time.Second
conn, err := net.DialTimeout("tcp", host, timeout)
if err != nil {
return nil, err
}
r := &RemoteConsole{conn: conn, readBuff: make([]byte, maxPackageSize+fieldPackageSize)}
r.auth(password, timeout)
2012-08-31 09:42:43 +02:00
if err != nil {
return nil, err
}
return r, nil
}
// LocalAddr returns the local network address.
func (r *RemoteConsole) LocalAddr() net.Addr {
return r.conn.LocalAddr()
}
// RemoteAddr returns the remote network address.
func (r *RemoteConsole) RemoteAddr() net.Addr {
return r.conn.RemoteAddr()
}
// Write a command to the server.
//
// It can return ErrCommandTooLong if the given cmd str is too long.
// Additionally it can return any other connection related errors.
func (r *RemoteConsole) Write(cmd string) (requestID int, err error) {
requestID = int(newRequestID())
err = r.writeCmd(int32(requestID), typeExecCommand, cmd)
return
2012-08-31 09:42:43 +02:00
}
// Read a incoming response from the server.
// If the response doesn't contain the correct ResponseValue it will return a response with a empty string an the request id = 0.
// This is also the case if an error happens even though the error will be returned.
//
// It can return following errors:
// - ErrResponseTooLong
// - ErrUnexpectedFormat
// - or a connection error that isn't typed in this package
func (r *RemoteConsole) Read() (response string, requestID int, err error) {
2012-08-31 09:42:43 +02:00
var respType int
2012-09-05 05:42:40 +02:00
var respBytes []byte
respType, requestID, respBytes, err = r.readResponse(2 * time.Minute)
2021-03-03 21:08:37 +01:00
if err != nil || respType != typeResponseValue {
2012-08-31 09:42:43 +02:00
response = ""
requestID = 0
2012-09-05 05:42:40 +02:00
} else {
response = string(respBytes)
2012-08-31 09:42:43 +02:00
}
return
}
// Close the connection to the server.
2012-08-31 09:48:25 +02:00
func (r *RemoteConsole) Close() error {
return r.conn.Close()
2012-08-31 09:48:25 +02:00
}
func newRequestID() int32 {
return int32((time.Now().UnixNano() / 100000) % 100000)
2012-08-31 09:42:43 +02:00
}
func (r *RemoteConsole) auth(password string, timeout time.Duration) error {
reqID := newRequestID()
err := r.writeCmd(reqID, typeAuth, password)
if err != nil {
return err
}
respType, responseID, _, err := r.readResponse(timeout)
if err != nil {
return err
}
// if we didn't get an auth response back, try again. it is often a bug
// with RCON servers that you get an empty response before receiving the
// auth response.
if respType != typeAuthResponse {
respType, responseID, _, err = r.readResponse(timeout)
}
if err != nil {
return err
}
if respType != typeAuthResponse {
return ErrInvalidAuthResponse
}
if responseID != int(reqID) {
return ErrAuthFailed
}
return nil
}
func (r *RemoteConsole) writeCmd(reqID, pkgType int32, cmd string) error {
if len(cmd) > maxPackageSize-minPackageSize {
return ErrCommandTooLong
2012-09-05 05:42:40 +02:00
}
buffer := bytes.NewBuffer(make([]byte, 0, minPackageSize+fieldPackageSize+len(cmd)))
2012-08-31 09:42:43 +02:00
// packet size
binary.Write(buffer, binary.LittleEndian, int32(minPackageSize+len(cmd)))
2012-08-31 09:42:43 +02:00
// request id
2021-03-03 21:08:37 +01:00
binary.Write(buffer, binary.LittleEndian, int32(reqID))
2012-08-31 09:42:43 +02:00
// auth cmd
2021-03-03 13:45:03 +01:00
binary.Write(buffer, binary.LittleEndian, int32(pkgType))
2012-08-31 09:42:43 +02:00
// string (null terminated)
buffer.WriteString(cmd)
binary.Write(buffer, binary.LittleEndian, byte(0))
2012-08-31 09:42:43 +02:00
// string 2 (null terminated)
// we don't have a use for string 2
binary.Write(buffer, binary.LittleEndian, byte(0))
2012-08-31 09:42:43 +02:00
r.conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
_, err := r.conn.Write(buffer.Bytes())
return err
2012-08-31 09:42:43 +02:00
}
2012-09-05 05:42:40 +02:00
func (r *RemoteConsole) readResponse(timeout time.Duration) (int, int, []byte, error) {
2021-03-03 21:08:37 +01:00
r.readMutex.Lock()
defer r.readMutex.Unlock()
2012-08-31 09:42:43 +02:00
r.conn.SetReadDeadline(time.Now().Add(timeout))
var readBytes int
2012-09-05 05:42:40 +02:00
var err error
2021-03-03 21:08:37 +01:00
if r.queuedBuff != nil {
copy(r.readBuff, r.queuedBuff)
readBytes = len(r.queuedBuff)
2021-03-03 21:08:37 +01:00
r.queuedBuff = nil
2012-09-05 05:42:40 +02:00
} else {
readBytes, err = r.conn.Read(r.readBuff)
2012-09-05 05:42:40 +02:00
if err != nil {
return 0, 0, nil, err
}
}
dataSize, readBytes, err := r.readResponsePackageSize(readBytes)
if err != nil {
return 0, 0, nil, err
2012-09-05 05:42:40 +02:00
}
if dataSize > maxPackageSize {
2012-09-05 05:42:40 +02:00
return 0, 0, nil, ErrResponseTooLong
}
2021-03-03 21:08:37 +01:00
totalPackageSize := dataSize + fieldPackageSize
readBytes, err = r.readResponsePackage(totalPackageSize, readBytes)
if err != nil {
return 0, 0, nil, err
2012-09-05 05:42:40 +02:00
}
// The data has to be explicitly selected to prevent copying empty bytes.
2021-03-03 21:08:37 +01:00
data := r.readBuff[fieldPackageSize:totalPackageSize]
// Save not package related bytes for the next read.
if readBytes > totalPackageSize {
2012-09-05 05:42:40 +02:00
// start of the next buffer was at the end of this packet.
// save it for the next read.
// The data has to be explicitly selected to prevent copying empty bytes.
r.queuedBuff = r.readBuff[totalPackageSize:readBytes]
2012-08-31 09:42:43 +02:00
}
2012-09-05 05:42:40 +02:00
return r.readResponseData(data)
}
// readResponsePackageSize wait until first 4 bytes are read to get the package size.
// Takes as param how many bytes are already read. The returned size does not include the size field.
func (r *RemoteConsole) readResponsePackageSize(readBytes int) (int, int, error) {
for readBytes < fieldPackageSize {
// need the 4 byte packet size...
b, err := r.conn.Read(r.readBuff[readBytes:])
if err != nil {
return 0, 0, err
}
readBytes += b
}
var size int32
b := bytes.NewBuffer(r.readBuff[:fieldPackageSize])
err := binary.Read(b, binary.LittleEndian, &size)
if err != nil {
return 0, 0, err
}
if size < minPackageSize {
return 0, 0, ErrUnexpectedFormat
}
return int(size), readBytes, nil
}
// readResponsePackage waits until the whole package is read including the size field.
func (r *RemoteConsole) readResponsePackage(totalPackageSize, readBytes int) (int, error) {
for totalPackageSize > readBytes {
b, err := r.conn.Read(r.readBuff[readBytes:])
if err != nil {
return readBytes, err
}
readBytes += b
}
return readBytes, nil
}
2012-09-05 05:42:40 +02:00
func (r *RemoteConsole) readResponseData(data []byte) (int, int, []byte, error) {
var requestID, responseType int32
var response []byte
2021-03-03 13:45:03 +01:00
buffer := bytes.NewBuffer(data)
binary.Read(buffer, binary.LittleEndian, &requestID)
binary.Read(buffer, binary.LittleEndian, &responseType)
response, err := buffer.ReadBytes(byte(0))
2012-09-05 05:42:40 +02:00
if err != nil && err != io.EOF {
return 0, 0, nil, err
}
if err == nil {
// if we didn't hit EOF, we have a null byte to remove
response = response[:len(response)-1]
}
return int(responseType), int(requestID), response, nil
2012-08-31 09:42:43 +02:00
}