mirror of
https://github.com/onyx-and-iris/q3rcon-proxy.git
synced 2025-04-20 04:23: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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/onyx-and-iris/q3rcon-proxy/pkg/udpproxy"
|
"github.com/onyx-and-iris/q3rcon-proxy/pkg/udpproxy"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -29,6 +31,18 @@ var (
|
|||||||
proxies, host string
|
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() {
|
func init() {
|
||||||
proxies = os.Getenv("Q3RCON_PROXY")
|
proxies = os.Getenv("Q3RCON_PROXY")
|
||||||
if proxies == "" {
|
if proxies == "" {
|
||||||
@ -39,6 +53,18 @@ func init() {
|
|||||||
if host == "" {
|
if host == "" {
|
||||||
host = "0.0.0.0"
|
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() {
|
func main() {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package udpproxy
|
package udpproxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -15,7 +16,7 @@ type Session struct {
|
|||||||
updateTime time.Time
|
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)
|
serverConn, err := net.DialUDP("udp", nil, raddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -33,12 +34,36 @@ func createSession(caddr *net.UDPAddr, raddr *net.UDPAddr, proxyConn *net.UDPCon
|
|||||||
return session, nil
|
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 {
|
func (s *Session) listen() error {
|
||||||
for {
|
for {
|
||||||
buf := make([]byte, 2048)
|
buf := make([]byte, 2048)
|
||||||
n, err := s.serverConn.Read(buf)
|
n, err := s.serverConn.Read(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Error(err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,28 +72,44 @@ func (s *Session) listen() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) proxyFrom(buf []byte) 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()
|
s.updateTime = time.Now()
|
||||||
_, err := s.proxyConn.WriteToUDP(buf, s.caddr)
|
_, err := s.proxyConn.WriteToUDP(buf, s.caddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Error(err)
|
||||||
return 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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) proxyTo(buf []byte) error {
|
func (s *Session) proxyTo(buf []byte) error {
|
||||||
s.updateTime = time.Now()
|
if !s.isValidRequestPacket(buf) {
|
||||||
_, err := s.serverConn.Write(buf)
|
err := errors.New("not a rcon or query request packet")
|
||||||
if err != nil {
|
log.Error(err.Error())
|
||||||
log.Println(err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := string(buf)
|
s.updateTime = time.Now()
|
||||||
if cmd[:8] == "\xff\xff\xff\xffrcon" {
|
_, err := s.serverConn.Write(buf)
|
||||||
parts := strings.Split(cmd, " ")
|
if err != nil {
|
||||||
log.Info("From [", s.caddr.IP, "] To [", s.serverConn.RemoteAddr().String(), "] Command: ", strings.Join(parts[2:], " "))
|
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
|
return nil
|
||||||
|
@ -37,10 +37,6 @@ func New(port, target string) (*Client, error) {
|
|||||||
}, nil
|
}, 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 {
|
func (c *Client) ListenAndServe() error {
|
||||||
var err error
|
var err error
|
||||||
c.proxyConn, err = net.ListenUDP("udp", c.laddr)
|
c.proxyConn, err = net.ListenUDP("udp", c.laddr)
|
||||||
@ -54,18 +50,14 @@ func (c *Client) ListenAndServe() error {
|
|||||||
buf := make([]byte, 2048)
|
buf := make([]byte, 2048)
|
||||||
n, caddr, err := c.proxyConn.ReadFromUDP(buf)
|
n, caddr, err := c.proxyConn.ReadFromUDP(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Error(err)
|
||||||
}
|
|
||||||
|
|
||||||
if !c.isValidPacket(buf[:16]) {
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
session, found := c.sessions[caddr.String()]
|
session, found := c.sessions[caddr.String()]
|
||||||
if !found {
|
if !found {
|
||||||
session, err = createSession(caddr, c.raddr, c.proxyConn)
|
session, err = newSession(caddr, c.raddr, c.proxyConn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Error(err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user