mirror of
https://github.com/onyx-and-iris/q3rcon-proxy.git
synced 2025-04-20 12:33:48 +01:00
Compare commits
7 Commits
7138515904
...
f6f0044a84
Author | SHA1 | Date | |
---|---|---|---|
f6f0044a84 | |||
725e6cfb3d | |||
ca33a6a390 | |||
dfcdb6a96a | |||
15e4cf6e42 | |||
f2fd9354f0 | |||
94a683fb3f |
@ -2,10 +2,12 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/onyx-and-iris/q3rcon-proxy/pkg/udpproxy"
|
||||
)
|
||||
|
||||
@ -29,6 +31,18 @@ var (
|
||||
proxies, host string
|
||||
)
|
||||
|
||||
func getenvInt(key string) (int, error) {
|
||||
s := os.Getenv(key)
|
||||
if s == "" {
|
||||
return 0, nil
|
||||
}
|
||||
v, err := strconv.Atoi(s)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
proxies = os.Getenv("Q3RCON_PROXY")
|
||||
if proxies == "" {
|
||||
@ -39,6 +53,18 @@ func init() {
|
||||
if host == "" {
|
||||
host = "0.0.0.0"
|
||||
}
|
||||
|
||||
debug, err := getenvInt("Q3RCON_DEBUG")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if debug == 1 {
|
||||
log.SetLevel(log.DebugLevel)
|
||||
} else {
|
||||
log.SetLevel(log.InfoLevel)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
@ -1,6 +1,7 @@
|
||||
package udpproxy
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
@ -15,7 +16,7 @@ type Session struct {
|
||||
updateTime time.Time
|
||||
}
|
||||
|
||||
func createSession(caddr *net.UDPAddr, raddr *net.UDPAddr, proxyConn *net.UDPConn) (*Session, error) {
|
||||
func newSession(caddr *net.UDPAddr, raddr *net.UDPAddr, proxyConn *net.UDPConn) (*Session, error) {
|
||||
serverConn, err := net.DialUDP("udp", nil, raddr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -33,12 +34,36 @@ func createSession(caddr *net.UDPAddr, raddr *net.UDPAddr, proxyConn *net.UDPCon
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func (s *Session) isRconRequestPacket(buf []byte) bool {
|
||||
return string(buf[:8]) == "\xff\xff\xff\xffrcon"
|
||||
}
|
||||
|
||||
func (s *Session) isQueryRequestPacket(buf []byte) bool {
|
||||
return string(buf[:13]) == "\xff\xff\xff\xffgetstatus" || string(buf[:11]) == "\xff\xff\xff\xffgetinfo"
|
||||
}
|
||||
|
||||
func (s *Session) isValidRequestPacket(buf []byte) bool {
|
||||
return s.isRconRequestPacket(buf) || s.isQueryRequestPacket(buf)
|
||||
}
|
||||
|
||||
func (s *Session) isRconResponsePacket(buf []byte) bool {
|
||||
return string(buf[:9]) == "\xff\xff\xff\xffprint"
|
||||
}
|
||||
|
||||
func (s *Session) isQueryResponsePacket(buf []byte) bool {
|
||||
return string(buf[:18]) == "\xff\xff\xff\xffstatusResponse" || string(buf[:16]) == "\xff\xff\xff\xffinfoResponse"
|
||||
}
|
||||
|
||||
func (s *Session) isValidResponsePacket(buf []byte) bool {
|
||||
return s.isRconResponsePacket(buf) || s.isQueryResponsePacket(buf)
|
||||
}
|
||||
|
||||
func (s *Session) listen() error {
|
||||
for {
|
||||
buf := make([]byte, 2048)
|
||||
n, err := s.serverConn.Read(buf)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
log.Error(err)
|
||||
continue
|
||||
}
|
||||
|
||||
@ -47,28 +72,44 @@ func (s *Session) listen() error {
|
||||
}
|
||||
|
||||
func (s *Session) proxyFrom(buf []byte) error {
|
||||
if !s.isValidResponsePacket(buf) {
|
||||
err := errors.New("not a rcon or query response packet")
|
||||
log.Error(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
s.updateTime = time.Now()
|
||||
_, err := s.proxyConn.WriteToUDP(buf, s.caddr)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
log.Error(err)
|
||||
return err
|
||||
}
|
||||
|
||||
if s.isRconResponsePacket(buf) && log.GetLevel() == log.DebugLevel {
|
||||
parts := strings.Split(string(buf[10:]), " ")
|
||||
log.Debugf("Response: %s", strings.Join(parts, " "))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Session) proxyTo(buf []byte) error {
|
||||
s.updateTime = time.Now()
|
||||
_, err := s.serverConn.Write(buf)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
if !s.isValidRequestPacket(buf) {
|
||||
err := errors.New("not a rcon or query request packet")
|
||||
log.Error(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
cmd := string(buf)
|
||||
if cmd[:8] == "\xff\xff\xff\xffrcon" {
|
||||
parts := strings.Split(cmd, " ")
|
||||
log.Info("From [", s.caddr.IP, "] To [", s.serverConn.RemoteAddr().String(), "] Command: ", strings.Join(parts[2:], " "))
|
||||
s.updateTime = time.Now()
|
||||
_, err := s.serverConn.Write(buf)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return err
|
||||
}
|
||||
|
||||
if s.isRconRequestPacket(buf) {
|
||||
parts := strings.Split(string(buf), " ")
|
||||
log.Infof("From [%s] To [%s] Command: %s", s.caddr.IP.String(), s.serverConn.RemoteAddr().String(), strings.Join(parts[2:], " "))
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -37,10 +37,6 @@ func New(port, target string) (*Client, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *Client) isValidPacket(header []byte) bool {
|
||||
return string(header[:8]) == "\xff\xff\xff\xffrcon" || string(header[:13]) == "\xff\xff\xff\xffgetstatus" || string(header[:11]) == "\xff\xff\xff\xffgetinfo"
|
||||
}
|
||||
|
||||
func (c *Client) ListenAndServe() error {
|
||||
var err error
|
||||
c.proxyConn, err = net.ListenUDP("udp", c.laddr)
|
||||
@ -54,18 +50,14 @@ func (c *Client) ListenAndServe() error {
|
||||
buf := make([]byte, 2048)
|
||||
n, caddr, err := c.proxyConn.ReadFromUDP(buf)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
if !c.isValidPacket(buf[:16]) {
|
||||
continue
|
||||
log.Error(err)
|
||||
}
|
||||
|
||||
session, found := c.sessions[caddr.String()]
|
||||
if !found {
|
||||
session, err = createSession(caddr, c.raddr, c.proxyConn)
|
||||
session, err = newSession(caddr, c.raddr, c.proxyConn)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
log.Error(err)
|
||||
continue
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user