2018-05-08 05:16:01 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2019-07-07 18:53:09 +02:00
|
|
|
"bytes"
|
2018-05-08 05:16:01 +02:00
|
|
|
"context"
|
2021-12-12 01:20:19 +01:00
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
2022-08-06 03:50:32 +02:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2021-12-12 01:20:19 +01:00
|
|
|
"time"
|
|
|
|
|
2019-07-14 23:34:46 +02:00
|
|
|
"github.com/go-kit/kit/metrics"
|
2019-07-07 18:53:09 +02:00
|
|
|
"github.com/itzg/mc-router/mcproto"
|
2019-07-13 22:40:00 +02:00
|
|
|
"github.com/juju/ratelimit"
|
2021-11-13 20:38:20 +01:00
|
|
|
"github.com/pires/go-proxyproto"
|
2019-07-07 18:53:09 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-05-08 05:16:01 +02:00
|
|
|
)
|
|
|
|
|
2019-07-09 02:07:36 +02:00
|
|
|
const (
|
2019-07-13 22:40:34 +02:00
|
|
|
handshakeTimeout = 5 * time.Second
|
2019-07-09 02:07:36 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var noDeadline time.Time
|
|
|
|
|
2019-07-14 23:00:57 +02:00
|
|
|
type ConnectorMetrics struct {
|
2019-07-14 23:34:46 +02:00
|
|
|
Errors metrics.Counter
|
2019-07-14 23:00:57 +02:00
|
|
|
BytesTransmitted metrics.Counter
|
|
|
|
Connections metrics.Counter
|
|
|
|
ActiveConnections metrics.Gauge
|
|
|
|
}
|
|
|
|
|
2022-08-06 03:50:32 +02:00
|
|
|
func NewConnector(metrics *ConnectorMetrics, sendProxyProto bool) *Connector {
|
2019-07-14 23:00:57 +02:00
|
|
|
|
2022-08-06 03:50:32 +02:00
|
|
|
return &Connector{
|
|
|
|
metrics: metrics,
|
|
|
|
sendProxyProto: sendProxyProto,
|
|
|
|
connectionsCond: sync.NewCond(&sync.Mutex{}),
|
2019-07-14 23:00:57 +02:00
|
|
|
}
|
2019-07-13 22:40:34 +02:00
|
|
|
}
|
2018-05-08 05:16:01 +02:00
|
|
|
|
2022-08-06 03:50:32 +02:00
|
|
|
type Connector struct {
|
2021-11-13 20:38:20 +01:00
|
|
|
state mcproto.State
|
|
|
|
metrics *ConnectorMetrics
|
|
|
|
sendProxyProto bool
|
2022-08-06 03:50:32 +02:00
|
|
|
|
|
|
|
activeConnections int32
|
|
|
|
connectionsCond *sync.Cond
|
2018-05-08 05:16:01 +02:00
|
|
|
}
|
|
|
|
|
2022-08-06 03:50:32 +02:00
|
|
|
func (c *Connector) StartAcceptingConnections(ctx context.Context, listenAddress string, connRateLimit int) error {
|
2018-05-08 05:16:01 +02:00
|
|
|
|
|
|
|
ln, err := net.Listen("tcp", listenAddress)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Fatal("Unable to start listening")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.WithField("listenAddress", listenAddress).Info("Listening for Minecraft client connections")
|
|
|
|
|
2019-07-09 14:22:20 +02:00
|
|
|
go c.acceptConnections(ctx, ln, connRateLimit)
|
2018-05-08 05:16:01 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-06 03:50:32 +02:00
|
|
|
func (c *Connector) WaitForConnections() {
|
|
|
|
c.connectionsCond.L.Lock()
|
|
|
|
defer c.connectionsCond.L.Unlock()
|
|
|
|
|
|
|
|
for {
|
|
|
|
count := atomic.LoadInt32(&c.activeConnections)
|
|
|
|
if count > 0 {
|
|
|
|
logrus.Infof("Waiting on %d connection(s)", count)
|
|
|
|
c.connectionsCond.Wait()
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connector) acceptConnections(ctx context.Context, ln net.Listener, connRateLimit int) {
|
2019-07-09 02:07:36 +02:00
|
|
|
//noinspection GoUnhandledErrorResult
|
2018-05-08 05:16:01 +02:00
|
|
|
defer ln.Close()
|
|
|
|
|
2019-07-13 22:40:00 +02:00
|
|
|
bucket := ratelimit.NewBucketWithRate(float64(connRateLimit), int64(connRateLimit*2))
|
2019-07-09 02:07:36 +02:00
|
|
|
|
2018-05-08 05:16:01 +02:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
|
2019-07-13 22:40:00 +02:00
|
|
|
case <-time.After(bucket.Take(1)):
|
2018-05-08 05:16:01 +02:00
|
|
|
conn, err := ln.Accept()
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Error("Failed to accept connection")
|
|
|
|
} else {
|
|
|
|
go c.HandleConnection(ctx, conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-06 03:50:32 +02:00
|
|
|
func (c *Connector) HandleConnection(ctx context.Context, frontendConn net.Conn) {
|
2020-01-04 21:52:07 +01:00
|
|
|
c.metrics.Connections.With("side", "frontend").Add(1)
|
2019-07-09 02:07:36 +02:00
|
|
|
//noinspection GoUnhandledErrorResult
|
2018-05-08 05:16:01 +02:00
|
|
|
defer frontendConn.Close()
|
|
|
|
|
|
|
|
clientAddr := frontendConn.RemoteAddr()
|
2019-07-09 14:22:20 +02:00
|
|
|
logrus.
|
|
|
|
WithField("client", clientAddr).
|
|
|
|
Info("Got connection")
|
2019-07-13 22:40:34 +02:00
|
|
|
defer logrus.WithField("client", clientAddr).Debug("Closing frontend connection")
|
2018-05-08 05:16:01 +02:00
|
|
|
|
|
|
|
inspectionBuffer := new(bytes.Buffer)
|
|
|
|
|
|
|
|
inspectionReader := io.TeeReader(frontendConn, inspectionBuffer)
|
|
|
|
|
2019-07-14 23:00:57 +02:00
|
|
|
if err := frontendConn.SetReadDeadline(time.Now().Add(handshakeTimeout)); err != nil {
|
|
|
|
logrus.
|
|
|
|
WithError(err).
|
|
|
|
WithField("client", clientAddr).
|
|
|
|
Error("Failed to set read deadline")
|
2019-07-14 23:34:46 +02:00
|
|
|
c.metrics.Errors.With("type", "read_deadline").Add(1)
|
2019-07-14 23:00:57 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
packet, err := mcproto.ReadPacket(inspectionReader, clientAddr, c.state)
|
2018-05-08 05:16:01 +02:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).WithField("clientAddr", clientAddr).Error("Failed to read packet")
|
2019-07-14 23:34:46 +02:00
|
|
|
c.metrics.Errors.With("type", "read").Add(1)
|
2018-05-08 05:16:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-13 22:40:34 +02:00
|
|
|
logrus.
|
|
|
|
WithField("client", clientAddr).
|
|
|
|
WithField("length", packet.Length).
|
|
|
|
WithField("packetID", packet.PacketID).
|
|
|
|
Debug("Got packet")
|
2018-05-08 05:16:01 +02:00
|
|
|
|
|
|
|
if packet.PacketID == mcproto.PacketIdHandshake {
|
|
|
|
handshake, err := mcproto.ReadHandshake(packet.Data)
|
|
|
|
if err != nil {
|
2019-07-13 22:40:34 +02:00
|
|
|
logrus.WithError(err).WithField("clientAddr", clientAddr).
|
|
|
|
Error("Failed to read handshake")
|
2019-07-14 23:34:46 +02:00
|
|
|
c.metrics.Errors.With("type", "read").Add(1)
|
2018-05-08 05:16:01 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-13 22:40:34 +02:00
|
|
|
logrus.
|
|
|
|
WithField("client", clientAddr).
|
|
|
|
WithField("handshake", handshake).
|
|
|
|
Debug("Got handshake")
|
2018-05-08 05:16:01 +02:00
|
|
|
|
2019-07-13 22:40:34 +02:00
|
|
|
serverAddress := handshake.ServerAddress
|
2018-05-08 05:16:01 +02:00
|
|
|
|
2019-07-13 22:40:34 +02:00
|
|
|
c.findAndConnectBackend(ctx, frontendConn, clientAddr, inspectionBuffer, serverAddress)
|
|
|
|
} else if packet.PacketID == mcproto.PacketIdLegacyServerListPing {
|
|
|
|
handshake, ok := packet.Data.(*mcproto.LegacyServerListPing)
|
|
|
|
if !ok {
|
2019-07-09 02:07:36 +02:00
|
|
|
logrus.
|
2019-07-09 14:22:20 +02:00
|
|
|
WithField("client", clientAddr).
|
2019-07-13 22:40:34 +02:00
|
|
|
WithField("packet", packet).
|
|
|
|
Warn("Unexpected data type for PacketIdLegacyServerListPing")
|
2019-07-14 23:34:46 +02:00
|
|
|
c.metrics.Errors.With("type", "unexpected_content").Add(1)
|
2019-07-09 02:07:36 +02:00
|
|
|
return
|
|
|
|
}
|
2019-07-13 22:40:34 +02:00
|
|
|
|
|
|
|
logrus.
|
|
|
|
WithField("client", clientAddr).
|
|
|
|
WithField("handshake", handshake).
|
|
|
|
Debug("Got legacy server list ping")
|
|
|
|
|
|
|
|
serverAddress := handshake.ServerAddress
|
|
|
|
|
|
|
|
c.findAndConnectBackend(ctx, frontendConn, clientAddr, inspectionBuffer, serverAddress)
|
2018-05-08 05:16:01 +02:00
|
|
|
} else {
|
2019-07-09 02:07:36 +02:00
|
|
|
logrus.
|
2019-07-09 14:22:20 +02:00
|
|
|
WithField("client", clientAddr).
|
2019-07-09 02:07:36 +02:00
|
|
|
WithField("packetID", packet.PacketID).
|
|
|
|
Error("Unexpected packetID, expected handshake")
|
2019-07-14 23:34:46 +02:00
|
|
|
c.metrics.Errors.With("type", "unexpected_content").Add(1)
|
2019-07-09 02:07:36 +02:00
|
|
|
return
|
2018-05-08 05:16:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-06 03:50:32 +02:00
|
|
|
func (c *Connector) findAndConnectBackend(ctx context.Context, frontendConn net.Conn,
|
2019-07-13 22:40:34 +02:00
|
|
|
clientAddr net.Addr, preReadContent io.Reader, serverAddress string) {
|
|
|
|
|
2022-01-07 00:41:10 +01:00
|
|
|
backendHostPort, resolvedHost, waker := Routes.FindBackendForServerAddress(ctx, serverAddress)
|
|
|
|
if waker != nil {
|
|
|
|
if err := waker(ctx); err != nil {
|
|
|
|
logrus.WithFields(logrus.Fields{"serverAddress": serverAddress}).WithError(err).Error("failed to wake up backend")
|
|
|
|
c.metrics.Errors.With("type", "wakeup_failed").Add(1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-13 22:40:34 +02:00
|
|
|
if backendHostPort == "" {
|
2022-07-12 06:16:15 +02:00
|
|
|
logrus.
|
|
|
|
WithField("serverAddress", serverAddress).
|
|
|
|
WithField("resolvedHost", resolvedHost).
|
|
|
|
Warn("Unable to find registered backend")
|
2019-07-14 23:34:46 +02:00
|
|
|
c.metrics.Errors.With("type", "missing_backend").Add(1)
|
2019-07-13 22:40:34 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
logrus.
|
|
|
|
WithField("client", clientAddr).
|
|
|
|
WithField("server", serverAddress).
|
|
|
|
WithField("backendHostPort", backendHostPort).
|
|
|
|
Info("Connecting to backend")
|
|
|
|
backendConn, err := net.Dial("tcp", backendHostPort)
|
|
|
|
if err != nil {
|
|
|
|
logrus.
|
|
|
|
WithError(err).
|
|
|
|
WithField("client", clientAddr).
|
|
|
|
WithField("serverAddress", serverAddress).
|
|
|
|
WithField("backend", backendHostPort).
|
|
|
|
Warn("Unable to connect to backend")
|
2019-07-14 23:34:46 +02:00
|
|
|
c.metrics.Errors.With("type", "backend_failed").Add(1)
|
2019-07-13 22:40:34 +02:00
|
|
|
return
|
|
|
|
}
|
2019-07-14 23:34:46 +02:00
|
|
|
|
2020-01-04 21:52:07 +01:00
|
|
|
c.metrics.Connections.With("side", "backend", "host", resolvedHost).Add(1)
|
2022-08-06 03:50:32 +02:00
|
|
|
|
|
|
|
c.metrics.ActiveConnections.Set(float64(
|
|
|
|
atomic.AddInt32(&c.activeConnections, 1)))
|
|
|
|
defer func() {
|
|
|
|
c.metrics.ActiveConnections.Set(float64(
|
|
|
|
atomic.AddInt32(&c.activeConnections, -1)))
|
|
|
|
c.connectionsCond.Signal()
|
|
|
|
}()
|
2019-07-14 23:34:46 +02:00
|
|
|
|
2021-11-13 20:38:20 +01:00
|
|
|
// PROXY protocol implementation
|
|
|
|
if c.sendProxyProto {
|
|
|
|
remoteHostStr, _, _ := net.SplitHostPort(backendHostPort)
|
|
|
|
sourceAddrStr, sourcePortStr, _ := net.SplitHostPort(clientAddr.String())
|
|
|
|
sourcePort, _ := strconv.Atoi(sourcePortStr)
|
|
|
|
|
|
|
|
header := &proxyproto.Header{
|
|
|
|
Version: 2,
|
|
|
|
Command: proxyproto.PROXY,
|
|
|
|
TransportProtocol: proxyproto.TCPv4,
|
|
|
|
SourceAddr: &net.TCPAddr{
|
|
|
|
IP: net.ParseIP(sourceAddrStr),
|
|
|
|
Port: sourcePort,
|
|
|
|
},
|
|
|
|
DestinationAddr: &net.TCPAddr{
|
|
|
|
IP: net.ParseIP(remoteHostStr),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = header.WriteTo(backendConn)
|
|
|
|
if err != nil {
|
|
|
|
logrus.
|
|
|
|
WithError(err).
|
|
|
|
WithField("clientAddr", header.SourceAddr).
|
|
|
|
WithField("destAddr", header.DestinationAddr).
|
|
|
|
Error("Failed to write PROXY header")
|
|
|
|
c.metrics.Errors.With("type", "proxy_write").Add(1)
|
|
|
|
_ = backendConn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-13 22:40:34 +02:00
|
|
|
amount, err := io.Copy(backendConn, preReadContent)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Error("Failed to write handshake to backend connection")
|
2019-07-14 23:34:46 +02:00
|
|
|
c.metrics.Errors.With("type", "backend_failed").Add(1)
|
2019-07-13 22:40:34 +02:00
|
|
|
return
|
|
|
|
}
|
2021-11-13 20:38:20 +01:00
|
|
|
|
2019-07-13 22:40:34 +02:00
|
|
|
logrus.WithField("amount", amount).Debug("Relayed handshake to backend")
|
|
|
|
if err = frontendConn.SetReadDeadline(noDeadline); err != nil {
|
|
|
|
logrus.
|
|
|
|
WithError(err).
|
|
|
|
WithField("client", clientAddr).
|
|
|
|
Error("Failed to clear read deadline")
|
2019-07-14 23:34:46 +02:00
|
|
|
c.metrics.Errors.With("type", "read_deadline").Add(1)
|
2019-07-13 22:40:34 +02:00
|
|
|
return
|
|
|
|
}
|
2021-11-13 20:38:20 +01:00
|
|
|
|
2019-07-14 23:34:46 +02:00
|
|
|
c.pumpConnections(ctx, frontendConn, backendConn)
|
2019-07-13 22:40:34 +02:00
|
|
|
}
|
|
|
|
|
2022-08-06 03:50:32 +02:00
|
|
|
func (c *Connector) pumpConnections(ctx context.Context, frontendConn, backendConn net.Conn) {
|
2019-07-09 02:07:36 +02:00
|
|
|
//noinspection GoUnhandledErrorResult
|
2018-05-08 05:16:01 +02:00
|
|
|
defer backendConn.Close()
|
2019-07-13 22:40:34 +02:00
|
|
|
|
2019-07-09 14:22:20 +02:00
|
|
|
clientAddr := frontendConn.RemoteAddr()
|
2019-07-13 22:40:34 +02:00
|
|
|
defer logrus.WithField("client", clientAddr).Debug("Closing backend connection")
|
2018-05-08 05:16:01 +02:00
|
|
|
|
|
|
|
errors := make(chan error, 2)
|
|
|
|
|
2019-07-14 23:34:46 +02:00
|
|
|
go c.pumpFrames(backendConn, frontendConn, errors, "backend", "frontend", clientAddr)
|
|
|
|
go c.pumpFrames(frontendConn, backendConn, errors, "frontend", "backend", clientAddr)
|
2018-05-08 05:16:01 +02:00
|
|
|
|
2019-07-13 22:40:34 +02:00
|
|
|
select {
|
|
|
|
case err := <-errors:
|
|
|
|
if err != io.EOF {
|
|
|
|
logrus.WithError(err).
|
|
|
|
WithField("client", clientAddr).
|
|
|
|
Error("Error observed on connection relay")
|
2019-07-14 23:34:46 +02:00
|
|
|
c.metrics.Errors.With("type", "relay").Add(1)
|
2018-05-08 05:16:01 +02:00
|
|
|
}
|
2019-07-13 22:40:34 +02:00
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
logrus.Debug("Observed context cancellation")
|
2018-05-08 05:16:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-06 03:50:32 +02:00
|
|
|
func (c *Connector) pumpFrames(incoming io.Reader, outgoing io.Writer, errors chan<- error, from, to string, clientAddr net.Addr) {
|
2018-05-08 05:16:01 +02:00
|
|
|
amount, err := io.Copy(outgoing, incoming)
|
2019-07-09 14:22:20 +02:00
|
|
|
logrus.
|
|
|
|
WithField("client", clientAddr).
|
|
|
|
WithField("amount", amount).
|
|
|
|
Infof("Finished relay %s->%s", from, to)
|
2019-07-13 22:40:34 +02:00
|
|
|
|
2019-07-14 23:34:46 +02:00
|
|
|
c.metrics.BytesTransmitted.Add(float64(amount))
|
|
|
|
|
2019-07-13 22:40:34 +02:00
|
|
|
if err != nil {
|
|
|
|
errors <- err
|
|
|
|
} else {
|
|
|
|
// successful io.Copy return nil error, not EOF...to simulate that to trigger outer handling
|
|
|
|
errors <- io.EOF
|
|
|
|
}
|
2018-05-08 05:16:01 +02:00
|
|
|
}
|