7 Commits

Author SHA1 Message Date
cbc8ee5543 add section 0.0.3 to CHANGELOG 2024-11-24 13:18:49 +00:00
3e1088d625 {Rcon}.login no longer exported.
split the cmd from its args when checking timeout map.

remove final TrimPrefix, it's already handled in {Rcon}.listen()
2024-11-24 13:13:07 +00:00
65ab17b9c9 add whitespace for readability 2024-11-22 13:20:27 +00:00
d180c455a3 fix typo 2024-11-09 18:59:01 +00:00
b20bca0c77 Request now uses reusable buffer 2024-11-08 15:31:30 +00:00
7d787324a7 move option functions into option.go 2024-11-08 15:31:13 +00:00
8c1bc0f04c move listen() into Rcon
rename channel variables
2024-11-08 15:30:47 +00:00
6 changed files with 117 additions and 82 deletions

2
.gitignore vendored
View File

@@ -25,4 +25,4 @@ go.work.sum
# env file
.env
cmd/aeiou
cmd/codrcon

View File

@@ -11,9 +11,20 @@ Before any major/minor/patch bump all unit tests will be run to verify they pass
- [x]
# [0.0.3] - 2024-11-24
### Changed
- {Rcon}.login is no longer exported since it's called internally by the constructor.
- When checking the timeouts map the cmd is split from its arguments. This allows setting a timeout value for all `map mp_` for example.
### Added
- Timeout values for commands in the timeouts map are now logged at Debug level.
# [0.0.1] - 2024-11-04
### Added
- Initial release, package implements Rcon using the Q3 procotocl.
- Initial release, package implements Rcon using the Q3 protocol.
- A basic CLI implementation accepting configuration flags.

View File

@@ -1,19 +1,15 @@
package conn
import (
"bytes"
"fmt"
"net"
"strings"
"time"
"github.com/onyx-and-iris/q3rcon/internal/packet"
log "github.com/sirupsen/logrus"
)
type UDPConn struct {
conn *net.UDPConn
response packet.Response
}
func New(host string, port int) (UDPConn, error) {
@@ -29,7 +25,6 @@ func New(host string, port int) (UDPConn, error) {
return UDPConn{
conn: conn,
response: packet.NewResponse(),
}, nil
}
@@ -42,39 +37,13 @@ func (c UDPConn) Write(buf []byte) (int, error) {
return n, nil
}
func (c UDPConn) Listen(timeout time.Duration, resp chan<- string, errChan chan<- error) {
c.conn.SetReadDeadline(time.Now().Add(timeout))
done := make(chan struct{})
var sb strings.Builder
buf := make([]byte, 2048)
for {
select {
case <-done:
resp <- sb.String()
return
default:
func (c UDPConn) ReadUntil(timeout time.Time, buf []byte) (int, error) {
c.conn.SetReadDeadline(timeout)
rlen, _, err := c.conn.ReadFromUDP(buf)
if err != nil {
e, ok := err.(net.Error)
if ok {
if e.Timeout() {
close(done)
} else {
errChan <- e
return
}
}
}
if rlen < len(c.response.Header()) {
continue
}
if bytes.HasPrefix(buf, c.response.Header()) {
sb.Write(buf[len(c.response.Header()):rlen])
}
}
return 0, err
}
return rlen, nil
}
func (c UDPConn) Close() error {

View File

@@ -1,16 +1,23 @@
package packet
import "fmt"
import (
"bytes"
"fmt"
)
const bufSz = 512
type Request struct {
magic []byte
password string
buf *bytes.Buffer
}
func NewRequest(password string) Request {
return Request{
magic: []byte{'\xff', '\xff', '\xff', '\xff'},
password: password,
buf: bytes.NewBuffer(make([]byte, bufSz)),
}
}
@@ -19,7 +26,8 @@ func (r Request) Header() []byte {
}
func (r Request) Encode(cmd string) []byte {
datagram := r.Header()
datagram = append(datagram, fmt.Sprintf(" %s %s", r.password, cmd)...)
return datagram
r.buf.Reset()
r.buf.Write(r.Header())
r.buf.WriteString(fmt.Sprintf(" %s %s", r.password, cmd))
return r.buf.Bytes()
}

27
option.go Normal file
View File

@@ -0,0 +1,27 @@
package q3rcon
import "time"
// Option is a functional option type that allows us to configure the VbanTxt.
type Option func(*Rcon)
// WithLoginTimeout is a functional option to set the login timeout
func WithLoginTimeout(timeout time.Duration) Option {
return func(rcon *Rcon) {
rcon.loginTimeout = timeout
}
}
// WithDefaultTimeout is a functional option to set the default response timeout
func WithDefaultTimeout(timeout time.Duration) Option {
return func(rcon *Rcon) {
rcon.defaultTimeout = timeout
}
}
// WithTimeouts is a functional option to set the timeouts for responses per command
func WithTimeouts(timeouts map[string]time.Duration) Option {
return func(rcon *Rcon) {
rcon.timeouts = timeouts
}
}

View File

@@ -1,37 +1,19 @@
package q3rcon
import (
"bytes"
"errors"
"net"
"strings"
"time"
log "github.com/sirupsen/logrus"
"github.com/onyx-and-iris/q3rcon/internal/conn"
"github.com/onyx-and-iris/q3rcon/internal/packet"
)
// Option is a functional option type that allows us to configure the VbanTxt.
type Option func(*Rcon)
// WithLoginTimeout is a functional option to set the login timeout
func WithLoginTimeout(timeout time.Duration) Option {
return func(rcon *Rcon) {
rcon.loginTimeout = timeout
}
}
// WithDefaultTimeout is a functional option to set the default response timeout
func WithDefaultTimeout(timeout time.Duration) Option {
return func(rcon *Rcon) {
rcon.defaultTimeout = timeout
}
}
// WithTimeouts is a functional option to set the timeouts for responses per command
func WithTimeouts(timeouts map[string]time.Duration) Option {
return func(rcon *Rcon) {
rcon.timeouts = timeouts
}
}
const respBufSiz = 2048
type Rcon struct {
conn conn.UDPConn
@@ -41,8 +23,6 @@ type Rcon struct {
loginTimeout time.Duration
defaultTimeout time.Duration
timeouts map[string]time.Duration
resp chan string
}
func New(host string, port int, password string, options ...Option) (*Rcon, error) {
@@ -58,7 +38,8 @@ func New(host string, port int, password string, options ...Option) (*Rcon, erro
r := &Rcon{
conn: conn,
request: packet.NewRequest(password),
resp: make(chan string),
response: packet.NewResponse(),
loginTimeout: 5 * time.Second,
defaultTimeout: 20 * time.Millisecond,
timeouts: make(map[string]time.Duration),
@@ -68,14 +49,14 @@ func New(host string, port int, password string, options ...Option) (*Rcon, erro
o(r)
}
if err = r.Login(); err != nil {
if err = r.login(); err != nil {
return nil, err
}
return r, nil
}
func (r Rcon) Login() error {
func (r Rcon) login() error {
timeout := time.After(r.loginTimeout)
for {
select {
@@ -99,24 +80,63 @@ func (r Rcon) Login() error {
}
}
func (r Rcon) Send(cmd string) (string, error) {
func (r Rcon) Send(cmdWithArgs string) (string, error) {
cmd, _, _ := strings.Cut(string(cmdWithArgs), " ")
timeout, ok := r.timeouts[cmd]
if !ok {
timeout = r.defaultTimeout
} else {
log.Debugf("%s in timeouts map, using timeout %v", cmd, timeout)
}
e := make(chan error)
go r.conn.Listen(timeout, r.resp, e)
_, err := r.conn.Write(r.request.Encode(cmd))
respChan := make(chan string)
errChan := make(chan error)
go r.listen(timeout, respChan, errChan)
_, err := r.conn.Write(r.request.Encode(cmdWithArgs))
if err != nil {
return "", err
}
select {
case err := <-e:
case err := <-errChan:
return "", err
case resp := <-r.resp:
return strings.TrimPrefix(resp, string(r.response.Header())), nil
case resp := <-respChan:
return resp, nil
}
}
func (r Rcon) listen(timeout time.Duration, respChan chan<- string, errChan chan<- error) {
done := make(chan struct{})
respBuf := make([]byte, respBufSiz)
var sb strings.Builder
for {
select {
case <-done:
respChan <- sb.String()
return
default:
rlen, err := r.conn.ReadUntil(time.Now().Add(timeout), respBuf)
if err != nil {
e, ok := err.(net.Error)
if ok {
if e.Timeout() {
close(done)
} else {
errChan <- e
return
}
}
}
if rlen > len(r.response.Header()) {
if bytes.HasPrefix(respBuf, r.response.Header()) {
sb.Write(respBuf[len(r.response.Header()):rlen])
}
}
}
}
}