2023-01-08 09:26:04 +00:00
|
|
|
package udpproxy
|
|
|
|
|
|
|
|
import (
|
2024-03-20 18:02:55 +00:00
|
|
|
"errors"
|
2023-01-08 09:26:04 +00:00
|
|
|
"net"
|
2024-03-08 02:29:01 +00:00
|
|
|
"strings"
|
2023-01-08 09:26:04 +00:00
|
|
|
"time"
|
2024-03-08 02:29:01 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2023-01-08 09:26:04 +00:00
|
|
|
)
|
|
|
|
|
2024-03-26 10:47:53 +00:00
|
|
|
type session struct {
|
2023-01-08 09:26:04 +00:00
|
|
|
serverConn *net.UDPConn
|
|
|
|
proxyConn *net.UDPConn
|
|
|
|
caddr *net.UDPAddr
|
|
|
|
updateTime time.Time
|
2024-03-26 10:47:53 +00:00
|
|
|
|
|
|
|
validator
|
2023-01-08 09:26:04 +00:00
|
|
|
}
|
|
|
|
|
2024-03-26 10:47:53 +00:00
|
|
|
func newSession(caddr *net.UDPAddr, raddr *net.UDPAddr, proxyConn *net.UDPConn) (*session, error) {
|
2023-01-08 09:26:04 +00:00
|
|
|
serverConn, err := net.DialUDP("udp", nil, raddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-26 10:47:53 +00:00
|
|
|
session := &session{
|
2023-01-08 09:26:04 +00:00
|
|
|
serverConn: serverConn,
|
|
|
|
proxyConn: proxyConn,
|
|
|
|
caddr: caddr,
|
|
|
|
updateTime: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
go session.listen()
|
|
|
|
|
|
|
|
return session, nil
|
|
|
|
}
|
|
|
|
|
2024-03-26 10:47:53 +00:00
|
|
|
func (s *session) listen() error {
|
2023-01-08 09:26:04 +00:00
|
|
|
for {
|
2023-01-08 10:59:43 +00:00
|
|
|
buf := make([]byte, 2048)
|
|
|
|
n, err := s.serverConn.Read(buf)
|
2023-01-08 09:26:04 +00:00
|
|
|
if err != nil {
|
2024-03-20 18:02:55 +00:00
|
|
|
log.Error(err)
|
2023-01-08 09:26:04 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-01-08 11:04:44 +00:00
|
|
|
go s.proxyFrom(buf[:n])
|
2023-01-08 09:26:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-26 10:47:53 +00:00
|
|
|
func (s *session) proxyFrom(buf []byte) error {
|
2024-03-20 21:03:37 +00:00
|
|
|
if !s.isValidResponsePacket(buf) {
|
|
|
|
err := errors.New("not a rcon or query response packet")
|
2024-03-20 18:10:15 +00:00
|
|
|
log.Error(err.Error())
|
|
|
|
return err
|
2024-03-20 18:02:55 +00:00
|
|
|
}
|
|
|
|
|
2023-01-08 09:26:04 +00:00
|
|
|
s.updateTime = time.Now()
|
|
|
|
_, err := s.proxyConn.WriteToUDP(buf, s.caddr)
|
|
|
|
if err != nil {
|
2024-03-20 18:02:55 +00:00
|
|
|
log.Error(err)
|
2023-01-08 09:26:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-21 07:35:07 +00:00
|
|
|
if s.isRconResponsePacket(buf) {
|
2024-04-03 20:54:10 +01:00
|
|
|
if s.isBadRconRequest(buf) {
|
|
|
|
log.Infof("Response: Bad rcon from %s", s.caddr.IP)
|
|
|
|
} else {
|
|
|
|
log.Debugf("Response: %s", string(buf[10:]))
|
|
|
|
}
|
2024-03-20 17:05:02 +00:00
|
|
|
}
|
|
|
|
|
2023-01-08 09:26:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-26 10:47:53 +00:00
|
|
|
func (s *session) proxyTo(buf []byte) error {
|
2024-03-20 21:03:37 +00:00
|
|
|
if !s.isValidRequestPacket(buf) {
|
|
|
|
err := errors.New("not a rcon or query request packet")
|
2024-03-20 18:02:55 +00:00
|
|
|
log.Error(err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-08 09:26:04 +00:00
|
|
|
s.updateTime = time.Now()
|
|
|
|
_, err := s.serverConn.Write(buf)
|
|
|
|
if err != nil {
|
2024-03-20 18:02:55 +00:00
|
|
|
log.Error(err)
|
2023-01-08 09:26:04 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-03-08 21:42:39 +00:00
|
|
|
|
2024-03-20 21:03:37 +00:00
|
|
|
if s.isRconRequestPacket(buf) {
|
2024-03-20 17:05:02 +00:00
|
|
|
parts := strings.Split(string(buf), " ")
|
2024-03-24 12:29:48 +00:00
|
|
|
log.Infof("From [%s] To [%s] Command: %s", s.caddr.IP, s.serverConn.RemoteAddr(), strings.Join(parts[2:], " "))
|
2024-03-08 21:42:39 +00:00
|
|
|
}
|
2023-01-08 09:26:04 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|