2024-11-04 13:33:53 +00:00
|
|
|
package q3rcon
|
|
|
|
|
|
|
|
import (
|
2024-11-08 15:30:47 +00:00
|
|
|
"bytes"
|
2024-11-04 13:33:53 +00:00
|
|
|
"errors"
|
2024-11-08 15:30:47 +00:00
|
|
|
"net"
|
2024-11-04 13:33:53 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/onyx-and-iris/q3rcon/internal/conn"
|
|
|
|
"github.com/onyx-and-iris/q3rcon/internal/packet"
|
|
|
|
)
|
|
|
|
|
2024-11-08 15:30:47 +00:00
|
|
|
const respBufSiz = 2048
|
2024-11-04 13:33:53 +00:00
|
|
|
|
|
|
|
type Rcon struct {
|
|
|
|
conn conn.UDPConn
|
|
|
|
request packet.Request
|
|
|
|
response packet.Response
|
|
|
|
|
2024-11-04 16:13:31 +00:00
|
|
|
loginTimeout time.Duration
|
2024-11-04 13:33:53 +00:00
|
|
|
defaultTimeout time.Duration
|
|
|
|
timeouts map[string]time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(host string, port int, password string, options ...Option) (*Rcon, error) {
|
|
|
|
if password == "" {
|
|
|
|
return nil, errors.New("no password provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := conn.New(host, port)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r := &Rcon{
|
2024-11-08 15:30:47 +00:00
|
|
|
conn: conn,
|
|
|
|
request: packet.NewRequest(password),
|
|
|
|
response: packet.NewResponse(),
|
|
|
|
|
2024-11-04 16:13:31 +00:00
|
|
|
loginTimeout: 5 * time.Second,
|
2024-11-04 13:33:53 +00:00
|
|
|
defaultTimeout: 20 * time.Millisecond,
|
|
|
|
timeouts: make(map[string]time.Duration),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range options {
|
|
|
|
o(r)
|
|
|
|
}
|
|
|
|
|
2024-11-04 16:13:31 +00:00
|
|
|
if err = r.Login(); err != nil {
|
2024-11-04 13:33:53 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r Rcon) Login() error {
|
2024-11-04 16:13:31 +00:00
|
|
|
timeout := time.After(r.loginTimeout)
|
2024-11-04 13:33:53 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
return errors.New("timeout logging in")
|
|
|
|
default:
|
|
|
|
resp, err := r.Send("login")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resp == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(resp, "Bad rcon") {
|
|
|
|
return errors.New("bad rcon password provided")
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r Rcon) Send(cmd string) (string, error) {
|
|
|
|
timeout, ok := r.timeouts[cmd]
|
|
|
|
if !ok {
|
|
|
|
timeout = r.defaultTimeout
|
|
|
|
}
|
|
|
|
|
2024-11-08 15:30:47 +00:00
|
|
|
respChan := make(chan string)
|
|
|
|
errChan := make(chan error)
|
2024-11-22 13:20:27 +00:00
|
|
|
|
2024-11-08 15:30:47 +00:00
|
|
|
go r.listen(timeout, respChan, errChan)
|
2024-11-22 13:20:27 +00:00
|
|
|
|
2024-11-04 13:33:53 +00:00
|
|
|
_, err := r.conn.Write(r.request.Encode(cmd))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2024-11-04 14:42:31 +00:00
|
|
|
select {
|
2024-11-08 15:30:47 +00:00
|
|
|
case err := <-errChan:
|
2024-11-04 14:42:31 +00:00
|
|
|
return "", err
|
2024-11-08 15:30:47 +00:00
|
|
|
case resp := <-respChan:
|
2024-11-04 14:42:31 +00:00
|
|
|
return strings.TrimPrefix(resp, string(r.response.Header())), nil
|
|
|
|
}
|
2024-11-04 13:33:53 +00:00
|
|
|
}
|
|
|
|
|
2024-11-08 15:30:47 +00:00
|
|
|
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])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-04 13:33:53 +00:00
|
|
|
func (r Rcon) Close() {
|
|
|
|
r.conn.Close()
|
|
|
|
}
|