mirror of
https://github.com/onyx-and-iris/q3rcon-proxy.git
synced 2025-04-20 12:33:48 +01:00
Compare commits
No commits in common. "f6f0044a845a3af9e02afcb73d32146738cf6336" and "7138515904efca86c7f45e0590cb291edb64af16" have entirely different histories.
f6f0044a84
...
7138515904
@ -2,12 +2,10 @@ 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"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -31,18 +29,6 @@ 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 == "" {
|
||||||
@ -53,18 +39,6 @@ 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,7 +1,6 @@
|
|||||||
package udpproxy
|
package udpproxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -16,7 +15,7 @@ type Session struct {
|
|||||||
updateTime time.Time
|
updateTime time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSession(caddr *net.UDPAddr, raddr *net.UDPAddr, proxyConn *net.UDPConn) (*Session, error) {
|
func createSession(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
|
||||||
@ -34,36 +33,12 @@ func newSession(caddr *net.UDPAddr, raddr *net.UDPAddr, proxyConn *net.UDPConn)
|
|||||||
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.Error(err)
|
log.Println(err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,44 +47,28 @@ 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.Error(err)
|
log.Println(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 {
|
||||||
if !s.isValidRequestPacket(buf) {
|
|
||||||
err := errors.New("not a rcon or query request packet")
|
|
||||||
log.Error(err.Error())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
s.updateTime = time.Now()
|
s.updateTime = time.Now()
|
||||||
_, err := s.serverConn.Write(buf)
|
_, err := s.serverConn.Write(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Println(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.isRconRequestPacket(buf) {
|
cmd := string(buf)
|
||||||
parts := strings.Split(string(buf), " ")
|
if cmd[:8] == "\xff\xff\xff\xffrcon" {
|
||||||
log.Infof("From [%s] To [%s] Command: %s", s.caddr.IP.String(), s.serverConn.RemoteAddr().String(), strings.Join(parts[2:], " "))
|
parts := strings.Split(cmd, " ")
|
||||||
|
log.Info("From [", s.caddr.IP, "] To [", s.serverConn.RemoteAddr().String(), "] Command: ", strings.Join(parts[2:], " "))
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -37,6 +37,10 @@ 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)
|
||||||
@ -50,14 +54,18 @@ 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.Error(err)
|
log.Println(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 = newSession(caddr, c.raddr, c.proxyConn)
|
session, err = createSession(caddr, c.raddr, c.proxyConn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Println(err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user