Compare commits

..

3 commits

Author SHA1 Message Date
3e2320a5f4
Add top level package information 2022-02-07 20:58:41 +01:00
447d07372b
Fix usage example
- Add Close() call with defer
- Fix code not exiting on error
2022-02-07 20:33:47 +01:00
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. // Establish a connection.
remoteConsole, err := rcon.Dial("127.0.0.1", "password") remoteConsole, err := rcon.Dial("127.0.0.1", "password")
if err != nil { 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. // Send a command.
requestID, err := remoteConsole.Write("command") requestID, err := remoteConsole.Write("command")
if err != nil { if err != nil {
fmt.Println(err) log.Fatal(err)
} }
// Read the response // Read the response.
response, responseID, err := remoteConsole.Read() response, responseID, err := remoteConsole.Read()
if err != nil { if err != nil {
fmt.Println(err) log.Fatal(err)
} }
if requestID != responseID { 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) fmt.Println(response)

20
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 package rcon
import ( import (
@ -10,8 +19,6 @@ import (
"time" "time"
) )
// Information to the protocol can be found under: https://developer.valvesoftware.com/wiki/Source_RCON_Protocol
const ( const (
typeAuth = 3 typeAuth = 3
typeExecCommand = 2 typeExecCommand = 2
@ -78,13 +85,16 @@ func Dial(host, password string) (*RemoteConsole, error) {
return nil, err return nil, err
} }
r := &RemoteConsole{conn: conn, readBuff: make([]byte, maxPackageSize+fieldPackageSize)} remoteConsole := &RemoteConsole{
r.auth(password, timeout) conn: conn,
readBuff: make([]byte, maxPackageSize+fieldPackageSize),
}
remoteConsole.auth(password, timeout)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return r, nil return remoteConsole, nil
} }
// LocalAddr returns the local network address. // LocalAddr returns the local network address.