Skip to content

Commit 4ba55a9

Browse files
authored
Add player info to connector logs (#398)
1 parent da52e70 commit 4ba55a9

File tree

1 file changed

+61
-38
lines changed

1 file changed

+61
-38
lines changed

server/connector.go

Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,22 @@ type PlayerInfo struct {
5959
Uuid uuid.UUID `json:"uuid"`
6060
}
6161

62+
func (p *PlayerInfo) String() string {
63+
if p == nil {
64+
return ""
65+
}
66+
return fmt.Sprintf("%s/%s", p.Name, p.Uuid)
67+
}
68+
6269
func NewConnector(metrics *ConnectorMetrics, sendProxyProto bool, receiveProxyProto bool, trustedProxyNets []*net.IPNet, recordLogins bool, autoScaleUpAllowDenyConfig *AllowDenyConfig) *Connector {
6370
return &Connector{
64-
metrics: metrics,
65-
sendProxyProto: sendProxyProto,
66-
connectionsCond: sync.NewCond(&sync.Mutex{}),
67-
receiveProxyProto: receiveProxyProto,
68-
trustedProxyNets: trustedProxyNets,
69-
recordLogins: recordLogins,
70-
autoScaleUpAllowDenyConfig: autoScaleUpAllowDenyConfig,
71+
metrics: metrics,
72+
sendProxyProto: sendProxyProto,
73+
connectionsCond: sync.NewCond(&sync.Mutex{}),
74+
receiveProxyProto: receiveProxyProto,
75+
trustedProxyNets: trustedProxyNets,
76+
recordLogins: recordLogins,
77+
autoScaleUpAllowDenyConfig: autoScaleUpAllowDenyConfig,
7178
}
7279
}
7380

@@ -79,11 +86,11 @@ type Connector struct {
7986
recordLogins bool
8087
trustedProxyNets []*net.IPNet
8188

82-
activeConnections int32
83-
connectionsCond *sync.Cond
84-
ngrokToken string
85-
clientFilter *ClientFilter
86-
autoScaleUpAllowDenyConfig *AllowDenyConfig
89+
activeConnections int32
90+
connectionsCond *sync.Cond
91+
ngrokToken string
92+
clientFilter *ClientFilter
93+
autoScaleUpAllowDenyConfig *AllowDenyConfig
8794

8895
connectionNotifier ConnectionNotifier
8996
}
@@ -264,9 +271,9 @@ func (c *Connector) HandleConnection(ctx context.Context, frontendConn net.Conn)
264271
WithField("handshake", handshake).
265272
Debug("Got handshake")
266273

267-
var userInfo *PlayerInfo = nil
274+
var playerInfo *PlayerInfo = nil
268275
if handshake.NextState == mcproto.StateLogin {
269-
userInfo, err = c.readUserInfo(bufferedReader, clientAddr, handshake.NextState)
276+
playerInfo, err = c.readUserInfo(bufferedReader, clientAddr, handshake.NextState)
270277
if err != nil {
271278
logrus.
272279
WithError(err).
@@ -277,11 +284,11 @@ func (c *Connector) HandleConnection(ctx context.Context, frontendConn net.Conn)
277284
}
278285
logrus.
279286
WithField("client", clientAddr).
280-
WithField("userInfo", userInfo).
287+
WithField("player", playerInfo).
281288
Debug("Got user info")
282289
}
283290

284-
c.findAndConnectBackend(ctx, frontendConn, clientAddr, inspectionBuffer, handshake.ServerAddress, userInfo, handshake.NextState)
291+
c.findAndConnectBackend(ctx, frontendConn, clientAddr, inspectionBuffer, handshake.ServerAddress, playerInfo, handshake.NextState)
285292

286293
} else if packet.PacketID == mcproto.PacketIdLegacyServerListPing {
287294
handshake, ok := packet.Data.(*mcproto.LegacyServerListPing)
@@ -333,15 +340,15 @@ func (c *Connector) readUserInfo(bufferedReader *bufio.Reader, clientAddr net.Ad
333340
}
334341

335342
func (c *Connector) findAndConnectBackend(ctx context.Context, frontendConn net.Conn,
336-
clientAddr net.Addr, preReadContent io.Reader, serverAddress string, userInfo *PlayerInfo, nextState mcproto.State) {
343+
clientAddr net.Addr, preReadContent io.Reader, serverAddress string, playerInfo *PlayerInfo, nextState mcproto.State) {
337344

338345
backendHostPort, resolvedHost, waker := Routes.FindBackendForServerAddress(ctx, serverAddress)
339346
if waker != nil && nextState > mcproto.StateStatus {
340-
serverAllowsPlayer := c.autoScaleUpAllowDenyConfig.ServerAllowsPlayer(serverAddress, userInfo)
347+
serverAllowsPlayer := c.autoScaleUpAllowDenyConfig.ServerAllowsPlayer(serverAddress, playerInfo)
341348
logrus.
342349
WithField("client", clientAddr).
343350
WithField("server", serverAddress).
344-
WithField("userInfo", userInfo).
351+
WithField("player", playerInfo).
345352
WithField("serverAllowsPlayer", serverAllowsPlayer).
346353
Debug("checked if player is allowed to wake up the server")
347354
if serverAllowsPlayer {
@@ -357,11 +364,15 @@ func (c *Connector) findAndConnectBackend(ctx context.Context, frontendConn net.
357364
logrus.
358365
WithField("serverAddress", serverAddress).
359366
WithField("resolvedHost", resolvedHost).
367+
WithField("player", playerInfo).
360368
Warn("Unable to find registered backend")
361369
c.metrics.Errors.With("type", "missing_backend").Add(1)
362370

363371
if c.connectionNotifier != nil {
364-
c.connectionNotifier.NotifyMissingBackend(ctx, clientAddr, serverAddress, userInfo)
372+
err := c.connectionNotifier.NotifyMissingBackend(ctx, clientAddr, serverAddress, playerInfo)
373+
if err != nil {
374+
logrus.WithError(err).Warn("failed to notify missing backend")
375+
}
365376
}
366377

367378
return
@@ -371,6 +382,7 @@ func (c *Connector) findAndConnectBackend(ctx context.Context, frontendConn net.
371382
WithField("client", clientAddr).
372383
WithField("server", serverAddress).
373384
WithField("backendHostPort", backendHostPort).
385+
WithField("player", playerInfo).
374386
Info("Connecting to backend")
375387

376388
backendConn, err := net.Dial("tcp", backendHostPort)
@@ -380,55 +392,64 @@ func (c *Connector) findAndConnectBackend(ctx context.Context, frontendConn net.
380392
WithField("client", clientAddr).
381393
WithField("serverAddress", serverAddress).
382394
WithField("backend", backendHostPort).
395+
WithField("player", playerInfo).
383396
Warn("Unable to connect to backend")
384397
c.metrics.Errors.With("type", "backend_failed").Add(1)
385398

386399
if c.connectionNotifier != nil {
387-
c.connectionNotifier.NotifyFailedBackendConnection(ctx, clientAddr, serverAddress, userInfo, backendHostPort, err)
400+
notifyErr := c.connectionNotifier.NotifyFailedBackendConnection(ctx, clientAddr, serverAddress, playerInfo, backendHostPort, err)
401+
if notifyErr != nil {
402+
logrus.WithError(notifyErr).Warn("failed to notify failed backend connection")
403+
}
388404
}
389405

390406
return
391407
}
392408

393409
if c.connectionNotifier != nil {
394-
c.connectionNotifier.NotifyConnected(ctx, clientAddr, serverAddress, userInfo, backendHostPort)
410+
err := c.connectionNotifier.NotifyConnected(ctx, clientAddr, serverAddress, playerInfo, backendHostPort)
411+
if err != nil {
412+
logrus.WithError(err).Warn("failed to notify connected")
413+
}
395414
}
396415

397416
c.metrics.ConnectionsBackend.With("host", resolvedHost).Add(1)
398417

399418
c.metrics.ActiveConnections.Set(float64(
400419
atomic.AddInt32(&c.activeConnections, 1)))
401-
if c.recordLogins && userInfo != nil {
420+
if c.recordLogins && playerInfo != nil {
402421
logrus.
403422
WithField("client", clientAddr).
404-
WithField("playerName", userInfo.Name).
405-
WithField("playerUUID", userInfo.Uuid).
423+
WithField("player", playerInfo).
406424
WithField("serverAddress", serverAddress).
407425
Info("Player attempted to login to server")
408426

409427
c.metrics.ServerActivePlayer.
410-
With("player_name", userInfo.Name).
411-
With("player_uuid", userInfo.Uuid.String()).
428+
With("player_name", playerInfo.Name).
429+
With("player_uuid", playerInfo.Uuid.String()).
412430
With("server_address", serverAddress).
413431
Set(1)
414432

415433
c.metrics.ServerLogins.
416-
With("player_name", userInfo.Name).
417-
With("player_uuid", userInfo.Uuid.String()).
434+
With("player_name", playerInfo.Name).
435+
With("player_uuid", playerInfo.Uuid.String()).
418436
With("server_address", serverAddress).
419437
Add(1)
420438
}
421439

422440
defer func() {
423441
if c.connectionNotifier != nil {
424-
c.connectionNotifier.NotifyDisconnected(ctx, clientAddr, serverAddress, userInfo, backendHostPort)
442+
err := c.connectionNotifier.NotifyDisconnected(ctx, clientAddr, serverAddress, playerInfo, backendHostPort)
443+
if err != nil {
444+
logrus.WithError(err).Warn("failed to notify disconnected")
445+
}
425446
}
426447
c.metrics.ActiveConnections.Set(float64(
427448
atomic.AddInt32(&c.activeConnections, -1)))
428-
if c.recordLogins && userInfo != nil {
449+
if c.recordLogins && playerInfo != nil {
429450
c.metrics.ServerActivePlayer.
430-
With("player_name", userInfo.Name).
431-
With("player_uuid", userInfo.Uuid.String()).
451+
With("player_name", playerInfo.Name).
452+
With("player_uuid", playerInfo.Uuid.String()).
432453
With("server_address", serverAddress).
433454
Set(0)
434455
}
@@ -492,10 +513,10 @@ func (c *Connector) findAndConnectBackend(ctx context.Context, frontendConn net.
492513
return
493514
}
494515

495-
c.pumpConnections(ctx, frontendConn, backendConn)
516+
c.pumpConnections(ctx, frontendConn, backendConn, playerInfo)
496517
}
497518

498-
func (c *Connector) pumpConnections(ctx context.Context, frontendConn, backendConn net.Conn) {
519+
func (c *Connector) pumpConnections(ctx context.Context, frontendConn, backendConn net.Conn, playerInfo *PlayerInfo) {
499520
//noinspection GoUnhandledErrorResult
500521
defer backendConn.Close()
501522

@@ -504,8 +525,8 @@ func (c *Connector) pumpConnections(ctx context.Context, frontendConn, backendCo
504525

505526
errors := make(chan error, 2)
506527

507-
go c.pumpFrames(backendConn, frontendConn, errors, "backend", "frontend", clientAddr)
508-
go c.pumpFrames(frontendConn, backendConn, errors, "frontend", "backend", clientAddr)
528+
go c.pumpFrames(backendConn, frontendConn, errors, "backend", "frontend", clientAddr, playerInfo)
529+
go c.pumpFrames(frontendConn, backendConn, errors, "frontend", "backend", clientAddr, playerInfo)
509530

510531
select {
511532
case err := <-errors:
@@ -521,11 +542,13 @@ func (c *Connector) pumpConnections(ctx context.Context, frontendConn, backendCo
521542
}
522543
}
523544

524-
func (c *Connector) pumpFrames(incoming io.Reader, outgoing io.Writer, errors chan<- error, from, to string, clientAddr net.Addr) {
545+
func (c *Connector) pumpFrames(incoming io.Reader, outgoing io.Writer, errors chan<- error, from, to string,
546+
clientAddr net.Addr, playerInfo *PlayerInfo) {
525547
amount, err := io.Copy(outgoing, incoming)
526548
logrus.
527549
WithField("client", clientAddr).
528550
WithField("amount", amount).
551+
WithField("player", playerInfo).
529552
Infof("Finished relay %s->%s", from, to)
530553

531554
c.metrics.BytesTransmitted.Add(float64(amount))

0 commit comments

Comments
 (0)