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"
|
|
|
|
)
|
|
|
|
|
2021-03-02 21:03:20 +01:00
|
|
|
// 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
|
|
|
)
|
|
|
|
|
2021-03-02 21:03:20 +01: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
|
2021-03-02 21:03:20 +01:00
|
|
|
|
|
|
|
// maxPackageSize of a request/response package.
|
2021-03-04 00:41:07 +01:00
|
|
|
// This size does not include the size field.
|
2021-03-02 21:03:20 +01:00
|
|
|
// https://developer.valvesoftware.com/wiki/Source_RCON_Protocol#Packet_Size
|
|
|
|
const maxPackageSize = 4096
|
2012-09-23 23:41:40 +02:00
|
|
|
|
2021-03-02 21:03:20 +01:00
|
|
|
// 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 (
|
2021-03-02 21:03:20 +01:00
|
|
|
// 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.
|
2021-03-02 21:03:20 +01:00
|
|
|
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")
|
2021-03-02 21:03:20 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
)
|
|
|
|
|
2021-03-02 21:03:20 +01: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.
|
2012-09-23 23:54:19 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-03-04 00:41:07 +01:00
|
|
|
r := &RemoteConsole{conn: conn, readBuff: make([]byte, maxPackageSize+fieldPackageSize)}
|
2021-03-03 23:15:25 +01:00
|
|
|
r.auth(password, timeout)
|
2012-08-31 09:42:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2021-03-02 21:03:20 +01:00
|
|
|
// LocalAddr returns the local network address.
|
2012-09-23 22:46:46 +02:00
|
|
|
func (r *RemoteConsole) LocalAddr() net.Addr {
|
|
|
|
return r.conn.LocalAddr()
|
|
|
|
}
|
|
|
|
|
2021-03-02 21:03:20 +01:00
|
|
|
// RemoteAddr returns the remote network address.
|
2012-09-23 22:46:46 +02:00
|
|
|
func (r *RemoteConsole) RemoteAddr() net.Addr {
|
|
|
|
return r.conn.RemoteAddr()
|
|
|
|
}
|
|
|
|
|
2021-03-06 16:50:34 +01:00
|
|
|
// 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.
|
2021-03-02 21:03:20 +01:00
|
|
|
func (r *RemoteConsole) Write(cmd string) (requestID int, err error) {
|
2021-03-03 23:37:37 +01:00
|
|
|
requestID = int(newRequestID())
|
|
|
|
err = r.writeCmd(int32(requestID), typeExecCommand, cmd)
|
|
|
|
|
|
|
|
return
|
2012-08-31 09:42:43 +02:00
|
|
|
}
|
|
|
|
|
2021-03-06 16:50:34 +01: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
|
2021-03-02 21:03:20 +01:00
|
|
|
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
|
2021-03-02 21:03:20 +01:00
|
|
|
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 = ""
|
2021-03-02 21:03:20 +01:00
|
|
|
requestID = 0
|
2012-09-05 05:42:40 +02:00
|
|
|
} else {
|
|
|
|
response = string(respBytes)
|
2012-08-31 09:42:43 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-02 21:03:20 +01:00
|
|
|
// Close the connection to the server.
|
2012-08-31 09:48:25 +02:00
|
|
|
func (r *RemoteConsole) Close() error {
|
2012-09-03 00:05:48 +02:00
|
|
|
return r.conn.Close()
|
2012-08-31 09:48:25 +02:00
|
|
|
}
|
|
|
|
|
2021-03-03 23:05:24 +01:00
|
|
|
func newRequestID() int32 {
|
|
|
|
return int32((time.Now().UnixNano() / 100000) % 100000)
|
2012-08-31 09:42:43 +02:00
|
|
|
}
|
|
|
|
|
2021-03-03 23:15:25 +01:00
|
|
|
func (r *RemoteConsole) auth(password string, timeout time.Duration) error {
|
2021-03-03 23:37:37 +01:00
|
|
|
reqID := newRequestID()
|
|
|
|
err := r.writeCmd(reqID, typeAuth, password)
|
2021-03-03 23:15:25 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-03 23:37:37 +01:00
|
|
|
respType, responseID, _, err := r.readResponse(timeout)
|
2021-03-03 23:15:25 +01:00
|
|
|
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 {
|
2021-03-03 23:37:37 +01:00
|
|
|
respType, responseID, _, err = r.readResponse(timeout)
|
2021-03-03 23:15:25 +01:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if respType != typeAuthResponse {
|
|
|
|
return ErrInvalidAuthResponse
|
|
|
|
}
|
2021-03-03 23:37:37 +01:00
|
|
|
if responseID != int(reqID) {
|
2021-03-03 23:15:25 +01:00
|
|
|
return ErrAuthFailed
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-03 23:37:37 +01:00
|
|
|
func (r *RemoteConsole) writeCmd(reqID, pkgType int32, cmd string) error {
|
2021-03-05 11:31:24 +01:00
|
|
|
if len(cmd) > maxPackageSize-minPackageSize {
|
2021-03-03 23:37:37 +01:00
|
|
|
return ErrCommandTooLong
|
2012-09-05 05:42:40 +02:00
|
|
|
}
|
|
|
|
|
2021-03-03 23:37:37 +01:00
|
|
|
buffer := bytes.NewBuffer(make([]byte, 0, minPackageSize+fieldPackageSize+len(cmd)))
|
2012-08-31 09:42:43 +02:00
|
|
|
|
|
|
|
// packet size
|
2021-03-03 23:37:37 +01:00
|
|
|
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)
|
2021-03-03 23:37:37 +01:00
|
|
|
buffer.WriteString(cmd)
|
2012-09-03 00:05:48 +02:00
|
|
|
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
|
2012-09-03 00:05:48 +02:00
|
|
|
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())
|
2021-03-03 23:37:37 +01:00
|
|
|
|
|
|
|
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))
|
2021-03-04 00:36:51 +01:00
|
|
|
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)
|
2021-03-04 00:36:51 +01:00
|
|
|
readBytes = len(r.queuedBuff)
|
2021-03-03 21:08:37 +01:00
|
|
|
r.queuedBuff = nil
|
2012-09-05 05:42:40 +02:00
|
|
|
} else {
|
2021-03-04 00:36:51 +01:00
|
|
|
readBytes, err = r.conn.Read(r.readBuff)
|
2012-09-05 05:42:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, 0, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 00:36:51 +01:00
|
|
|
dataSize, readBytes, err := r.readResponsePackageSize(readBytes)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, nil, err
|
2012-09-05 05:42:40 +02:00
|
|
|
}
|
|
|
|
|
2021-03-02 21:03:20 +01: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
|
2021-03-04 00:52:20 +01:00
|
|
|
readBytes, err = r.readResponsePackage(totalPackageSize, readBytes)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, nil, err
|
2012-09-05 05:42:40 +02:00
|
|
|
}
|
|
|
|
|
2021-03-04 00:52:20 +01: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]
|
2021-03-04 00:52:20 +01:00
|
|
|
|
|
|
|
// Save not package related bytes for the next read.
|
2021-03-04 00:36:51 +01:00
|
|
|
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.
|
2021-03-04 00:52:20 +01:00
|
|
|
// The data has to be explicitly selected to prevent copying empty bytes.
|
2021-03-04 00:36:51 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-03-04 00:36:51 +01:00
|
|
|
// readResponsePackageSize wait until first 4 bytes are read to get the package size.
|
2021-03-04 00:41:07 +01:00
|
|
|
// Takes as param how many bytes are already read. The returned size does not include the size field.
|
2021-03-04 00:36:51 +01:00
|
|
|
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
|
2021-03-06 16:14:45 +01:00
|
|
|
b := bytes.NewBuffer(r.readBuff[:fieldPackageSize])
|
2021-03-04 00:36:51 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-03-04 00:52:20 +01:00
|
|
|
// 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) {
|
2021-03-02 21:03:20 +01:00
|
|
|
var requestID, responseType int32
|
2012-09-03 00:05:48 +02:00
|
|
|
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]
|
|
|
|
}
|
2021-03-02 21:03:20 +01:00
|
|
|
return int(responseType), int(requestID), response, nil
|
2012-08-31 09:42:43 +02:00
|
|
|
}
|