Compare commits

...

3 commits

Author SHA1 Message Date
Augusto Dwenger J. 3e2320a5f4
Add top level package information 2022-02-07 20:58:41 +01:00
Augusto Dwenger J. 447d07372b
Fix usage example
- Add Close() call with defer
- Fix code not exiting on error
2022-02-07 20:33:47 +01:00
Augusto Dwenger J. 9994b2e641
Fix formatting 2022-02-07 20:32:57 +01:00
2 changed files with 23 additions and 11 deletions

View file

@ -8,22 +8,24 @@ This is a fork from [james4k/rcon](https://github.com/james4k/rcon) with the sup
// Establish a connection.
remoteConsole, err := rcon.Dial("127.0.0.1", "password")
if err != nil {
fmt.Println(err)
log.Fatal(err)
}
// Close the connection at the end to free the used resources.
defer remoteConsole.Close()
// Send a command.
requestID, err := remoteConsole.Write("command")
if err != nil {
fmt.Println(err)
log.Fatal(err)
}
// Read the response
// Read the response.
response, responseID, err := remoteConsole.Read()
if err != nil {
fmt.Println(err)
log.Fatal(err)
}
if requestID != responseID {
fmt.Println("response id doesn't match the request id!")
log.Fatal("response id doesn't match the request id!")
}
fmt.Println(response)

22
rcon.go
View file

@ -1,3 +1,12 @@
/*
A Go written library for the RCON Protocol from Valve.
Information to the protocol can be found under:
https://developer.valvesoftware.com/wiki/Source_RCON_Protocol
This is a fork from https://github.com/james4k/rcon with the support for go
modules and with a rework of the original implementation for better readability.
*/
package rcon
import (
@ -10,8 +19,6 @@ import (
"time"
)
// Information to the protocol can be found under: https://developer.valvesoftware.com/wiki/Source_RCON_Protocol
const (
typeAuth = 3
typeExecCommand = 2
@ -78,13 +85,16 @@ func Dial(host, password string) (*RemoteConsole, error) {
return nil, err
}
r := &RemoteConsole{conn: conn, readBuff: make([]byte, maxPackageSize+fieldPackageSize)}
r.auth(password, timeout)
remoteConsole := &RemoteConsole{
conn: conn,
readBuff: make([]byte, maxPackageSize+fieldPackageSize),
}
remoteConsole.auth(password, timeout)
if err != nil {
return nil, err
}
return r, nil
return remoteConsole, nil
}
// LocalAddr returns the local network address.
@ -187,7 +197,7 @@ func (r *RemoteConsole) writeCmd(reqID, pkgType int32, cmd string) error {
// body
buffer.WriteString(cmd)
// double null termination
binary.Write(buffer, binary.LittleEndian, byte(0))
binary.Write(buffer, binary.LittleEndian, byte(0))