mirror of
https://github.com/onyx-and-iris/q3rcon.git
synced 2026-04-19 00:03:31 +00:00
first commit
This commit is contained in:
85
internal/conn/conn.go
Normal file
85
internal/conn/conn.go
Normal file
@@ -0,0 +1,85 @@
|
||||
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) {
|
||||
udpAddr, err := net.ResolveUDPAddr("udp4", fmt.Sprintf("%s:%d", host, port))
|
||||
if err != nil {
|
||||
return UDPConn{}, err
|
||||
}
|
||||
conn, err := net.DialUDP("udp4", nil, udpAddr)
|
||||
if err != nil {
|
||||
return UDPConn{}, err
|
||||
}
|
||||
log.Infof("Outgoing address %s", conn.RemoteAddr())
|
||||
|
||||
return UDPConn{
|
||||
conn: conn,
|
||||
response: packet.NewResponse(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c UDPConn) Write(buf []byte) (int, error) {
|
||||
n, err := c.conn.Write(buf)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (c UDPConn) Listen(timeout time.Duration, resp chan<- string) {
|
||||
c.conn.SetReadDeadline(time.Now().Add(timeout))
|
||||
ch := make(chan struct{})
|
||||
var sb strings.Builder
|
||||
buf := make([]byte, 2048)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ch:
|
||||
resp <- sb.String()
|
||||
return
|
||||
default:
|
||||
rlen, _, err := c.conn.ReadFromUDP(buf)
|
||||
if err != nil {
|
||||
e, ok := err.(net.Error)
|
||||
if ok {
|
||||
if e.Timeout() {
|
||||
close(ch)
|
||||
} else {
|
||||
log.Error(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
if rlen == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
if bytes.HasPrefix(buf, c.response.Header()) {
|
||||
sb.Write(buf[len(c.response.Header()):rlen])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c UDPConn) Close() error {
|
||||
err := c.conn.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
25
internal/packet/request.go
Normal file
25
internal/packet/request.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package packet
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Request struct {
|
||||
magic []byte
|
||||
password string
|
||||
}
|
||||
|
||||
func NewRequest(password string) Request {
|
||||
return Request{
|
||||
magic: []byte{'\xff', '\xff', '\xff', '\xff'},
|
||||
password: password,
|
||||
}
|
||||
}
|
||||
|
||||
func (r Request) Header() []byte {
|
||||
return append(r.magic, []byte("rcon")...)
|
||||
}
|
||||
|
||||
func (r Request) Encode(cmd string) []byte {
|
||||
datagram := r.Header()
|
||||
datagram = append(datagram, fmt.Sprintf(" %s %s", r.password, cmd)...)
|
||||
return datagram
|
||||
}
|
||||
13
internal/packet/response.go
Normal file
13
internal/packet/response.go
Normal file
@@ -0,0 +1,13 @@
|
||||
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, []byte("print\n")...)
|
||||
}
|
||||
Reference in New Issue
Block a user