mirror of
https://github.com/onyx-and-iris/q3rcon.git
synced 2026-03-02 17:09:19 +00:00
remove internal packages, move everything into q3rcon
{request}.buf initialised with 0 length.
encoder, decoder interfaces added.
This commit is contained in:
parent
caffd65cb3
commit
3f45588afb
@ -1,9 +1,8 @@
|
|||||||
package conn
|
package q3rcon
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
)
|
)
|
||||||
@ -12,23 +11,23 @@ type UDPConn struct {
|
|||||||
conn *net.UDPConn
|
conn *net.UDPConn
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(host string, port int) (UDPConn, error) {
|
func newUDPConn(host string, port int) (*UDPConn, error) {
|
||||||
udpAddr, err := net.ResolveUDPAddr("udp4", fmt.Sprintf("%s:%d", host, port))
|
udpAddr, err := net.ResolveUDPAddr("udp4", net.JoinHostPort(host, fmt.Sprintf("%d", port)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return UDPConn{}, err
|
return nil, err
|
||||||
}
|
}
|
||||||
conn, err := net.DialUDP("udp4", nil, udpAddr)
|
conn, err := net.DialUDP("udp4", nil, udpAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return UDPConn{}, err
|
return nil, err
|
||||||
}
|
}
|
||||||
log.Infof("Outgoing address %s", conn.RemoteAddr())
|
log.Infof("Outgoing address %s", conn.RemoteAddr())
|
||||||
|
|
||||||
return UDPConn{
|
return &UDPConn{
|
||||||
conn: conn,
|
conn: conn,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c UDPConn) Write(buf []byte) (int, error) {
|
func (c *UDPConn) Write(buf []byte) (int, error) {
|
||||||
n, err := c.conn.Write(buf)
|
n, err := c.conn.Write(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@ -37,8 +36,7 @@ func (c UDPConn) Write(buf []byte) (int, error) {
|
|||||||
return n, nil
|
return n, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c UDPConn) ReadUntil(timeout time.Time, buf []byte) (int, error) {
|
func (c *UDPConn) Read(buf []byte) (int, error) {
|
||||||
c.conn.SetReadDeadline(timeout)
|
|
||||||
rlen, _, err := c.conn.ReadFromUDP(buf)
|
rlen, _, err := c.conn.ReadFromUDP(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@ -46,7 +44,7 @@ func (c UDPConn) ReadUntil(timeout time.Time, buf []byte) (int, error) {
|
|||||||
return rlen, nil
|
return rlen, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c UDPConn) Close() error {
|
func (c *UDPConn) Close() error {
|
||||||
err := c.conn.Close()
|
err := c.conn.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -1,36 +0,0 @@
|
|||||||
package packet
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/charmbracelet/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
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)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r Request) Header() []byte {
|
|
||||||
return append(r.magic, "rcon"...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r Request) Encode(cmd string) []byte {
|
|
||||||
r.buf.Reset()
|
|
||||||
r.buf.Write(r.Header())
|
|
||||||
r.buf.WriteString(fmt.Sprintf(" %s %s", r.password, cmd))
|
|
||||||
log.Debugf("Encoded request: %s", r.buf.String())
|
|
||||||
return r.buf.Bytes()
|
|
||||||
}
|
|
||||||
@ -1,13 +0,0 @@
|
|||||||
package packet
|
|
||||||
|
|
||||||
type Response struct {
|
|
||||||
magic []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewResponse() Response {
|
|
||||||
return Response{magic: []byte{'\xff', '\xff', '\xff', '\xff'}}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r Response) Header() []byte {
|
|
||||||
return append(r.magic, "print\n"...)
|
|
||||||
}
|
|
||||||
60
q3rcon.go
60
q3rcon.go
@ -1,24 +1,31 @@
|
|||||||
package q3rcon
|
package q3rcon
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
|
|
||||||
"github.com/onyx-and-iris/q3rcon/internal/conn"
|
|
||||||
"github.com/onyx-and-iris/q3rcon/internal/packet"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const respBufSiz = 2048
|
const respBufSiz = 2048
|
||||||
|
|
||||||
|
type encoder interface {
|
||||||
|
Encode(cmd string) ([]byte, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type decoder interface {
|
||||||
|
IsValid(buf []byte) bool
|
||||||
|
Decode(buf []byte) string
|
||||||
|
}
|
||||||
|
|
||||||
type Rcon struct {
|
type Rcon struct {
|
||||||
conn conn.UDPConn
|
conn io.ReadWriteCloser
|
||||||
request packet.Request
|
request encoder
|
||||||
response packet.Response
|
response decoder
|
||||||
|
|
||||||
loginTimeout time.Duration
|
loginTimeout time.Duration
|
||||||
defaultTimeout time.Duration
|
defaultTimeout time.Duration
|
||||||
@ -30,15 +37,15 @@ func New(host string, port int, password string, options ...Option) (*Rcon, erro
|
|||||||
return nil, errors.New("no password provided")
|
return nil, errors.New("no password provided")
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := conn.New(host, port)
|
conn, err := newUDPConn(host, port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("error creating UDP connection: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
r := &Rcon{
|
r := &Rcon{
|
||||||
conn: conn,
|
conn: conn,
|
||||||
request: packet.NewRequest(password),
|
request: newRequest(password),
|
||||||
response: packet.NewResponse(),
|
response: newResponse(),
|
||||||
|
|
||||||
loginTimeout: 5 * time.Second,
|
loginTimeout: 5 * time.Second,
|
||||||
defaultTimeout: 20 * time.Millisecond,
|
defaultTimeout: 20 * time.Millisecond,
|
||||||
@ -50,7 +57,7 @@ func New(host string, port int, password string, options ...Option) (*Rcon, erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err = r.login(); err != nil {
|
if err = r.login(); err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("error logging in: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return r, nil
|
return r, nil
|
||||||
@ -65,7 +72,7 @@ func (r Rcon) login() error {
|
|||||||
default:
|
default:
|
||||||
resp, err := r.Send("login")
|
resp, err := r.Send("login")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("error sending login command: %w", err)
|
||||||
}
|
}
|
||||||
if resp == "" {
|
if resp == "" {
|
||||||
continue
|
continue
|
||||||
@ -94,9 +101,14 @@ func (r Rcon) Send(cmdWithArgs string) (string, error) {
|
|||||||
|
|
||||||
go r.listen(timeout, respChan, errChan)
|
go r.listen(timeout, respChan, errChan)
|
||||||
|
|
||||||
_, err := r.conn.Write(r.request.Encode(cmdWithArgs))
|
encodedCmd, err := r.request.Encode(cmdWithArgs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", fmt.Errorf("error encoding command: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = r.conn.Write(encodedCmd)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("error writing command to connection: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
@ -118,7 +130,17 @@ func (r Rcon) listen(timeout time.Duration, respChan chan<- string, errChan chan
|
|||||||
respChan <- sb.String()
|
respChan <- sb.String()
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
rlen, err := r.conn.ReadUntil(time.Now().Add(timeout), respBuf)
|
c, ok := r.conn.(*UDPConn)
|
||||||
|
if !ok {
|
||||||
|
errChan <- errors.New("connection is not a UDPConn")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err := c.conn.SetReadDeadline(time.Now().Add(timeout))
|
||||||
|
if err != nil {
|
||||||
|
errChan <- fmt.Errorf("error setting read deadline: %w", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
rlen, err := r.conn.Read(respBuf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
e, ok := err.(net.Error)
|
e, ok := err.(net.Error)
|
||||||
if ok {
|
if ok {
|
||||||
@ -131,10 +153,8 @@ func (r Rcon) listen(timeout time.Duration, respChan chan<- string, errChan chan
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if rlen > len(r.response.Header()) {
|
if r.response.IsValid(respBuf[:rlen]) {
|
||||||
if bytes.HasPrefix(respBuf, r.response.Header()) {
|
sb.WriteString(r.response.Decode(respBuf[:rlen]))
|
||||||
sb.Write(respBuf[len(r.response.Header()):rlen])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
35
request.go
Normal file
35
request.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package q3rcon
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
bufSz = 1024
|
||||||
|
requestHeader = "\xff\xff\xff\xffrcon"
|
||||||
|
)
|
||||||
|
|
||||||
|
type request struct {
|
||||||
|
password string
|
||||||
|
buf *bytes.Buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
func newRequest(password string) request {
|
||||||
|
return request{
|
||||||
|
password: password,
|
||||||
|
buf: bytes.NewBuffer(make([]byte, 0, bufSz)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r request) Encode(cmd string) ([]byte, error) {
|
||||||
|
if cmd == "" {
|
||||||
|
return nil, errors.New("command cannot be empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
r.buf.Reset()
|
||||||
|
r.buf.WriteString(requestHeader)
|
||||||
|
r.buf.WriteString(fmt.Sprintf(" %s %s", r.password, cmd))
|
||||||
|
return r.buf.Bytes(), nil
|
||||||
|
}
|
||||||
21
response.go
Normal file
21
response.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package q3rcon
|
||||||
|
|
||||||
|
import "bytes"
|
||||||
|
|
||||||
|
const (
|
||||||
|
responseHeader = "\xff\xff\xff\xffprint\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
type response struct{}
|
||||||
|
|
||||||
|
func newResponse() response {
|
||||||
|
return response{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r response) IsValid(buf []byte) bool {
|
||||||
|
return len(buf) > len(responseHeader) && bytes.HasPrefix(buf, []byte(responseHeader))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r response) Decode(buf []byte) string {
|
||||||
|
return string(buf[len(responseHeader):])
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user