mirror of
https://github.com/onyx-and-iris/q3rcon-proxy.git
synced 2026-04-07 15:53:29 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 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"
|
||||
)
|
||||
|
||||
@@ -26,9 +28,21 @@ func start(proxy string) {
|
||||
}
|
||||
|
||||
var (
|
||||
proxies, host string
|
||||
proxies, host, debug 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() {
|
||||
|
||||
@@ -15,7 +15,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,6 +33,14 @@ func createSession(caddr *net.UDPAddr, raddr *net.UDPAddr, proxyConn *net.UDPCon
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func (s *Session) isRconPacket(buf []byte) bool {
|
||||
return string(buf[:8]) == "\xff\xff\xff\xffrcon"
|
||||
}
|
||||
|
||||
func (s *Session) isResponsePacket(buf []byte) bool {
|
||||
return string(buf[:9]) == "\xff\xff\xff\xffprint"
|
||||
}
|
||||
|
||||
func (s *Session) listen() error {
|
||||
for {
|
||||
buf := make([]byte, 2048)
|
||||
@@ -54,6 +62,11 @@ func (s *Session) proxyFrom(buf []byte) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if s.isResponsePacket(buf) {
|
||||
parts := strings.Split(string(buf[10:]), " ")
|
||||
log.Debugf("Response: %s", strings.Join(parts, " "))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -65,10 +78,9 @@ func (s *Session) proxyTo(buf []byte) 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:], " "))
|
||||
if s.isRconPacket(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,8 +37,16 @@ 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) isRconPacket(buf []byte) bool {
|
||||
return string(buf[:8]) == "\xff\xff\xff\xffrcon"
|
||||
}
|
||||
|
||||
func (c *Client) isQueryPacket(buf []byte) bool {
|
||||
return string(buf[:13]) == "\xff\xff\xff\xffgetstatus" || string(buf[:11]) == "\xff\xff\xff\xffgetinfo"
|
||||
}
|
||||
|
||||
func (c *Client) isValidPacket(buf []byte) bool {
|
||||
return c.isRconPacket(buf) || c.isQueryPacket(buf)
|
||||
}
|
||||
|
||||
func (c *Client) ListenAndServe() error {
|
||||
@@ -57,13 +65,13 @@ func (c *Client) ListenAndServe() error {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
if !c.isValidPacket(buf[:16]) {
|
||||
if !c.isValidPacket(buf[:n]) {
|
||||
continue
|
||||
}
|
||||
|
||||
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)
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user