mirror of
https://github.com/onyx-and-iris/q3rcon-proxy.git
synced 2026-04-07 15:53:29 +00:00
Compare commits
35 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c42df03858 | |||
| 64c1df645d | |||
| 51f22f480b | |||
| 3d1c8f90f3 | |||
| 2fc0f7a74f | |||
| 6cd90ae1dd | |||
| 826756eb6e | |||
| 05fb7bdd0f | |||
| e8f213fded | |||
| 3a8758a4d7 | |||
| 2c8ae43303 | |||
| 0935fc1190 | |||
| 3e039824de | |||
| 0c054377ba | |||
| 5ac3f805e2 | |||
| ab548d669a | |||
| 107f914d3b | |||
| 0fdf9d10c3 | |||
| 690fabddaf | |||
| f6f0044a84 | |||
| 725e6cfb3d | |||
| ca33a6a390 | |||
| dfcdb6a96a | |||
| 15e4cf6e42 | |||
| f2fd9354f0 | |||
| 94a683fb3f | |||
| 7138515904 | |||
| a01df2e905 | |||
| d8015c921c | |||
| a513c7ecc6 | |||
| 6f49cc5b63 | |||
| 88c90f1447 | |||
| 561b1ca654 | |||
| a3219e63c2 | |||
| ceb87e14fd |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -19,3 +19,7 @@
|
|||||||
|
|
||||||
# Go workspace file
|
# Go workspace file
|
||||||
go.work
|
go.work
|
||||||
|
|
||||||
|
# testing
|
||||||
|
run.sh
|
||||||
|
server.yaml
|
||||||
37
CHANGELOG.md
37
CHANGELOG.md
@@ -11,12 +11,47 @@ Before any major/minor/patch bump all unit tests will be run to verify they pass
|
|||||||
|
|
||||||
- [x]
|
- [x]
|
||||||
|
|
||||||
|
## [0.6.0] - 2024-03-21
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- new environment variable `Q3RCON_DEBUG` for enabling debug logging. Defaults to 0.
|
||||||
|
- rcon responses are now logged at debug level
|
||||||
|
- invalid responses (rcon and query) now logged
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- All packet header checking methods moved into Session struct.
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
- a bug causing the proxy not to send back query responses
|
||||||
|
|
||||||
|
## [0.3.0] - 2024-03-08
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- outgoing rcon requests now logged at info level
|
||||||
|
- new environment variable `Q3RCON_HOST` for specifying which ip to bind the proxy to. Defaults to `0.0.0.0`.
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- now using [logrus][logrus] package for logging.
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
- a `slice bounds out of range` error due to query packets being logged.
|
||||||
|
|
||||||
## [0.1.0] - 2024-01-27
|
## [0.1.0] - 2024-01-27
|
||||||
|
|
||||||
- ignore any packets whose header does match a q3 rcon/query packet.
|
### Added
|
||||||
|
|
||||||
|
- only forward packets if the header matches q3 rcon/query.
|
||||||
|
|
||||||
## [0.0.1] - 2024-01-27
|
## [0.0.1] - 2024-01-27
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
- All source files for lilproxy including full commit history.
|
- All source files for lilproxy including full commit history.
|
||||||
|
|
||||||
|
[logrus]: https://github.com/sirupsen/logrus
|
||||||
|
|||||||
14
Dockerfile
14
Dockerfile
@@ -1,12 +1,14 @@
|
|||||||
FROM golang:alpine
|
FROM golang:1.21
|
||||||
|
|
||||||
WORKDIR /dist
|
WORKDIR /usr/src/app
|
||||||
|
|
||||||
COPY . .
|
# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change
|
||||||
|
COPY go.mod go.sum ./
|
||||||
# build binary and place into /usr/local/bin
|
|
||||||
RUN go mod download && go mod verify
|
RUN go mod download && go mod verify
|
||||||
RUN go build -v -o /usr/local/bin/q3rcon-proxy ./cmd/q3rcon-proxy
|
|
||||||
|
# build binary and place into /usr/local/bin/
|
||||||
|
COPY . .
|
||||||
|
RUN go build -v -o /usr/local/bin/q3rcon-proxy ./cmd/q3rcon-proxy/
|
||||||
|
|
||||||
# Command to run when starting the container
|
# Command to run when starting the container
|
||||||
ENTRYPOINT [ "q3rcon-proxy" ]
|
ENTRYPOINT [ "q3rcon-proxy" ]
|
||||||
16
README.md
16
README.md
@@ -2,6 +2,10 @@
|
|||||||
|
|
||||||
A modification of [lilproxy][lilproxy_url] that forwards only Q3 rcon/query packets. Useful for separating the rcon port from the game server port.
|
A modification of [lilproxy][lilproxy_url] that forwards only Q3 rcon/query packets. Useful for separating the rcon port from the game server port.
|
||||||
|
|
||||||
|
### Why
|
||||||
|
|
||||||
|
Unfortunately the Q3Rcon engine ties the rcon port to the game servers public port used for client connections. This proxy will allow you to run rcon through a separate whitelisted port.
|
||||||
|
|
||||||
### Use
|
### Use
|
||||||
|
|
||||||
Run one or multiple rcon proxies by setting an environment variable `Q3RCON_PROXY`
|
Run one or multiple rcon proxies by setting an environment variable `Q3RCON_PROXY`
|
||||||
@@ -12,17 +16,19 @@ for example:
|
|||||||
export Q3RCON_PROXY="20000:28960;20001:28961;20002:28962"
|
export Q3RCON_PROXY="20000:28960;20001:28961;20002:28962"
|
||||||
```
|
```
|
||||||
|
|
||||||
This would run 3 proxy servers listening on ports `20000`, `20001` and `20002` that redirect rcon requests to game servers on ports `28960`, `28961` and `28962` respectively.
|
This would configure q3rcon-proxy to run 3 proxy servers listening on ports `20000`, `20001` and `20002` that redirect rcon requests to game servers on ports `28960`, `28961` and `28962` respectively.
|
||||||
|
|
||||||
### Why
|
Then just run the binary which you can compile yourself, download from `Releases` or use the included Dockerfile.
|
||||||
|
|
||||||
Avoid sending plaintext rcon requests (that include the password) to public ports. Instead send them to whitelisted ports.
|
### Logging
|
||||||
|
|
||||||
Gives you the option to disable remote rcon entirely and have the server accept requests only from localhost.
|
Set the log level with environment variable `Q3RCON_LOGLEVEL`:
|
||||||
|
|
||||||
|
`0 = Panic, 1 = Fatal, 2 = Error, 3 = Warning, 4 = Info, 5 = Debug, 6 = Trace`
|
||||||
|
|
||||||
### Special Thanks
|
### Special Thanks
|
||||||
|
|
||||||
[Dylan][user_link] For writing this proxy.
|
[Dylan][user_link] For writing [lilproxy][lilproxy_url].
|
||||||
|
|
||||||
[lilproxy_url]: https://github.com/dgparker/lilproxy
|
[lilproxy_url]: https://github.com/dgparker/lilproxy
|
||||||
[user_link]: https://github.com/dgparker
|
[user_link]: https://github.com/dgparker
|
||||||
|
|||||||
@@ -2,38 +2,62 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/onyx-and-iris/q3rcon-proxy/pkg/udpproxy"
|
"github.com/onyx-and-iris/q3rcon-proxy/pkg/udpproxy"
|
||||||
)
|
)
|
||||||
|
|
||||||
func start(proxy string) {
|
func main() {
|
||||||
port, target := func() (string, string) {
|
logLevel, err := getEnvInt("Q3RCON_LOGLEVEL")
|
||||||
x := strings.Split(proxy, ":")
|
|
||||||
return x[0], x[1]
|
|
||||||
}()
|
|
||||||
|
|
||||||
c, err := udpproxy.New(fmt.Sprintf("0.0.0.0:%s", port), fmt.Sprintf("127.0.0.1:%s", target))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatalf("unable to parse Q3RCON_LEVEL: %s", err.Error())
|
||||||
|
}
|
||||||
|
if slices.Contains(log.AllLevels, log.Level(logLevel)) {
|
||||||
|
log.SetLevel(log.Level(logLevel))
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("q3rcon-proxy initialized: [proxy] (0.0.0.0:%s) [target] (127.0.0.1:%s)", port, target)
|
|
||||||
|
|
||||||
log.Fatal(c.ListenAndServe())
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
proxies := os.Getenv("Q3RCON_PROXY")
|
proxies := os.Getenv("Q3RCON_PROXY")
|
||||||
if proxies == "" {
|
if proxies == "" {
|
||||||
log.Fatal("env Q3RCON_PROXY required")
|
log.Fatal("env Q3RCON_PROXY required")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
host := os.Getenv("Q3RCON_HOST")
|
||||||
|
if host == "" {
|
||||||
|
host = "0.0.0.0"
|
||||||
|
}
|
||||||
|
|
||||||
|
staleTimeout, err := getEnvInt("Q3RCON_STALE_SESSION_TIMEOUT")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("unable to parse Q3RCON_STALE_SESSION_TIMEOUT: %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
for _, proxy := range strings.Split(proxies, ";") {
|
for _, proxy := range strings.Split(proxies, ";") {
|
||||||
go start(proxy)
|
go start(host, proxy, staleTimeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
<-make(chan int)
|
<-make(chan int)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func start(host, proxy string, staleTimeout int) {
|
||||||
|
port, target := func() (string, string) {
|
||||||
|
x := strings.Split(proxy, ":")
|
||||||
|
return x[0], x[1]
|
||||||
|
}()
|
||||||
|
|
||||||
|
c, err := udpproxy.New(
|
||||||
|
fmt.Sprintf("%s:%s", host, port),
|
||||||
|
fmt.Sprintf("127.0.0.1:%s", target),
|
||||||
|
udpproxy.WithStaleTimeout(time.Duration(staleTimeout)*time.Minute))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("q3rcon-proxy initialized: [proxy] (%s:%s) [target] (127.0.0.1:%s)", host, port, target)
|
||||||
|
|
||||||
|
log.Fatal(c.ListenAndServe())
|
||||||
|
}
|
||||||
|
|||||||
18
cmd/q3rcon-proxy/util.go
Normal file
18
cmd/q3rcon-proxy/util.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
18
debian/q3rcon-proxy.service
vendored
Normal file
18
debian/q3rcon-proxy.service
vendored
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Q3Rcon Proxy Service
|
||||||
|
Wants=network.target
|
||||||
|
After=network.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
User=gameservers
|
||||||
|
Environment="Q3RCON_PROXY=20000:28960;20001:28961;20002:28962"
|
||||||
|
Environment="Q3RCON_HOST=0.0.0.0"
|
||||||
|
Environment="Q3RCON_DEBUG=0"
|
||||||
|
|
||||||
|
ExecStart=/usr/local/bin/q3rcon-proxy
|
||||||
|
Restart=always
|
||||||
|
RestartSec=3
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
4
go.mod
4
go.mod
@@ -1,3 +1,7 @@
|
|||||||
module github.com/onyx-and-iris/q3rcon-proxy
|
module github.com/onyx-and-iris/q3rcon-proxy
|
||||||
|
|
||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
|
require github.com/sirupsen/logrus v1.9.3
|
||||||
|
|
||||||
|
require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
|
||||||
|
|||||||
15
go.sum
15
go.sum
@@ -0,0 +1,15 @@
|
|||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
||||||
|
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
|
||||||
|
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
@@ -1,29 +1,36 @@
|
|||||||
package udpproxy
|
package udpproxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Session struct {
|
type session struct {
|
||||||
serverConn *net.UDPConn
|
serverConn *net.UDPConn
|
||||||
proxyConn *net.UDPConn
|
proxyConn *net.UDPConn
|
||||||
caddr *net.UDPAddr
|
caddr *net.UDPAddr
|
||||||
updateTime time.Time
|
updateTime time.Time
|
||||||
|
|
||||||
|
validator
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
session := &Session{
|
session := &session{
|
||||||
serverConn: serverConn,
|
serverConn: serverConn,
|
||||||
proxyConn: proxyConn,
|
proxyConn: proxyConn,
|
||||||
caddr: caddr,
|
caddr: caddr,
|
||||||
updateTime: time.Now(),
|
updateTime: time.Now(),
|
||||||
|
validator: newValidator(),
|
||||||
}
|
}
|
||||||
|
|
||||||
go session.listen()
|
go session.listen()
|
||||||
@@ -31,12 +38,12 @@ func createSession(caddr *net.UDPAddr, raddr *net.UDPAddr, proxyConn *net.UDPCon
|
|||||||
return session, nil
|
return session, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) listen() error {
|
func (s *session) listen() error {
|
||||||
|
buf := make([]byte, 2048)
|
||||||
for {
|
for {
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,24 +51,55 @@ 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) {
|
||||||
|
if s.isBadRconResponse(buf) {
|
||||||
|
log.Infof("Response: Bad rcon from %s", s.caddr.IP)
|
||||||
|
} else {
|
||||||
|
log.Debugf("Response: %s", string(buf[len(s.rconResponseHeader):]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) proxyTo(buf []byte) error {
|
func (s *session) proxyTo(buf []byte) error {
|
||||||
|
if !s.isValidRequestPacket(buf) {
|
||||||
|
var err error
|
||||||
|
if s.isChallengeRequestPacket(buf) {
|
||||||
|
parts := strings.SplitN(string(buf), " ", 3)
|
||||||
|
err = fmt.Errorf("invalid challenge from %s with GUID: %s", s.caddr.IP, parts[len(parts)-1])
|
||||||
|
} else {
|
||||||
|
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.Println(err)
|
log.Error(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if s.isRconRequestPacket(buf) {
|
||||||
|
parts := strings.SplitN(string(buf), " ", 3)
|
||||||
|
log.Infof("From [%s] To [%s] Command: %s", s.caddr.IP, s.serverConn.RemoteAddr(), parts[len(parts)-1])
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,28 @@
|
|||||||
package udpproxy
|
package udpproxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Option is a functional option type that allows us to configure the Client.
|
||||||
|
type Option func(*Client)
|
||||||
|
|
||||||
|
// WithStaleTimeout is a functional option to set the stale session timeout
|
||||||
|
func WithStaleTimeout(timeout time.Duration) Option {
|
||||||
|
return func(c *Client) {
|
||||||
|
if timeout < time.Minute {
|
||||||
|
log.Warnf("cannot set stale session timeout to less than 1 minute.. defaulting to 5 minutes")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.timeout = timeout
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
laddr *net.UDPAddr
|
laddr *net.UDPAddr
|
||||||
raddr *net.UDPAddr
|
raddr *net.UDPAddr
|
||||||
@@ -14,10 +30,12 @@ type Client struct {
|
|||||||
proxyConn *net.UDPConn
|
proxyConn *net.UDPConn
|
||||||
|
|
||||||
mutex sync.RWMutex
|
mutex sync.RWMutex
|
||||||
sessions map[string]*Session
|
sessions map[string]*session
|
||||||
|
|
||||||
|
timeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(port, target string) (*Client, error) {
|
func New(port, target string, options ...Option) (*Client, error) {
|
||||||
laddr, err := net.ResolveUDPAddr("udp", port)
|
laddr, err := net.ResolveUDPAddr("udp", port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -28,16 +46,19 @@ func New(port, target string) (*Client, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Client{
|
c := &Client{
|
||||||
laddr: laddr,
|
laddr: laddr,
|
||||||
raddr: raddr,
|
raddr: raddr,
|
||||||
mutex: sync.RWMutex{},
|
mutex: sync.RWMutex{},
|
||||||
sessions: map[string]*Session{},
|
sessions: map[string]*session{},
|
||||||
}, nil
|
timeout: 5 * time.Minute,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) isValidPacket(header []byte) bool {
|
for _, o := range options {
|
||||||
return string(header[:8]) == "\xff\xff\xff\xffrcon" || string(header[:13]) == "\xff\xff\xff\xffgetstatus" || string(header[:11]) == "\xff\xff\xff\xffgetinfo"
|
o(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) ListenAndServe() error {
|
func (c *Client) ListenAndServe() error {
|
||||||
@@ -49,22 +70,18 @@ func (c *Client) ListenAndServe() error {
|
|||||||
|
|
||||||
go c.pruneSessions()
|
go c.pruneSessions()
|
||||||
|
|
||||||
|
buf := make([]byte, 2048)
|
||||||
for {
|
for {
|
||||||
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]) {
|
session, ok := c.sessions[caddr.String()]
|
||||||
continue
|
if !ok {
|
||||||
}
|
session, err = newSession(caddr, c.raddr, c.proxyConn)
|
||||||
|
|
||||||
session, found := c.sessions[caddr.String()]
|
|
||||||
if !found {
|
|
||||||
session, err = createSession(caddr, c.raddr, c.proxyConn)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Error(err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,8 +100,9 @@ func (c *Client) pruneSessions() {
|
|||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
for _, session := range c.sessions {
|
for _, session := range c.sessions {
|
||||||
c.mutex.RLock()
|
c.mutex.RLock()
|
||||||
if time.Since(session.updateTime) > time.Minute*5 {
|
if time.Since(session.updateTime) > c.timeout {
|
||||||
delete(c.sessions, session.caddr.String())
|
delete(c.sessions, session.caddr.String())
|
||||||
|
log.Tracef("session for %s deleted", session.caddr)
|
||||||
}
|
}
|
||||||
c.mutex.RUnlock()
|
c.mutex.RUnlock()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,83 +0,0 @@
|
|||||||
package udpproxy
|
|
||||||
|
|
||||||
import (
|
|
||||||
"log"
|
|
||||||
"net"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestSendAndReceive(t *testing.T) {
|
|
||||||
go runLilProxy()
|
|
||||||
go runUDPServer()
|
|
||||||
|
|
||||||
paddr, err := net.ResolveUDPAddr("udp", "localhost:9000")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
conn, err := net.DialUDP("udp", nil, paddr)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
buf := make([]byte, 2048)
|
|
||||||
_, _, err = conn.ReadFromUDP(buf)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("response received: %s", string(buf))
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
for {
|
|
||||||
time.Sleep(1 * time.Second)
|
|
||||||
_, err = conn.Write([]byte("hi\n"))
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func runLilProxy() {
|
|
||||||
port := ":9000"
|
|
||||||
target := "localhost:9001"
|
|
||||||
|
|
||||||
c, err := New(port, target)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Fatal(c.ListenAndServe())
|
|
||||||
}
|
|
||||||
|
|
||||||
func runUDPServer() {
|
|
||||||
taddr, err := net.ResolveUDPAddr("udp", ":9001")
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
conn, err := net.ListenUDP("udp", taddr)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
|
||||||
buf := make([]byte, 2048)
|
|
||||||
_, caddr, err := conn.ReadFromUDP(buf)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("request received: %s", string(buf))
|
|
||||||
|
|
||||||
_, err = conn.WriteToUDP([]byte("bye\n"), caddr)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
65
pkg/udpproxy/validator.go
Normal file
65
pkg/udpproxy/validator.go
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
package udpproxy
|
||||||
|
|
||||||
|
import "bytes"
|
||||||
|
|
||||||
|
type validator struct {
|
||||||
|
rconRequestHeader []byte
|
||||||
|
getstatusRequestHeader []byte
|
||||||
|
getinfoRequestHeader []byte
|
||||||
|
getchallengeRequestHeader []byte
|
||||||
|
rconResponseHeader []byte
|
||||||
|
getstatusResponseHeader []byte
|
||||||
|
getinfoResponseHeader []byte
|
||||||
|
badRconIdentifier []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func newValidator() validator {
|
||||||
|
return validator{
|
||||||
|
rconRequestHeader: []byte("\xff\xff\xff\xffrcon"),
|
||||||
|
getstatusRequestHeader: []byte("\xff\xff\xff\xffgetstatus"),
|
||||||
|
getinfoRequestHeader: []byte("\xff\xff\xff\xffgetinfo"),
|
||||||
|
getchallengeRequestHeader: []byte("\xff\xff\xff\xffgetchallenge"),
|
||||||
|
rconResponseHeader: []byte("\xff\xff\xff\xffprint\n"),
|
||||||
|
getstatusResponseHeader: []byte("\xff\xff\xff\xffstatusResponse\n"),
|
||||||
|
getinfoResponseHeader: []byte("\xff\xff\xff\xffinfoResponse\n"),
|
||||||
|
badRconIdentifier: []byte("Bad rcon"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v validator) compare(buf, c []byte) bool {
|
||||||
|
return bytes.Equal(buf[:len(c)], c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v validator) isRconRequestPacket(buf []byte) bool {
|
||||||
|
return v.compare(buf, v.rconRequestHeader)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v validator) isQueryRequestPacket(buf []byte) bool {
|
||||||
|
return v.compare(buf, v.getstatusRequestHeader) ||
|
||||||
|
v.compare(buf, v.getinfoRequestHeader)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v validator) isValidRequestPacket(buf []byte) bool {
|
||||||
|
return v.isRconRequestPacket(buf) || v.isQueryRequestPacket(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v validator) isChallengeRequestPacket(buf []byte) bool {
|
||||||
|
return v.compare(buf, v.getchallengeRequestHeader)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v validator) isRconResponsePacket(buf []byte) bool {
|
||||||
|
return v.compare(buf, v.rconResponseHeader)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v validator) isQueryResponsePacket(buf []byte) bool {
|
||||||
|
return v.compare(buf, v.getstatusResponseHeader) ||
|
||||||
|
v.compare(buf, v.getinfoResponseHeader)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v validator) isValidResponsePacket(buf []byte) bool {
|
||||||
|
return v.isRconResponsePacket(buf) || v.isQueryResponsePacket(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v validator) isBadRconResponse(buf []byte) bool {
|
||||||
|
return v.compare(buf[len(v.rconResponseHeader):], v.badRconIdentifier)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user