From cc5d77e4c870c90d8de60574777e9b7f41dfb12d Mon Sep 17 00:00:00 2001 From: UrbaneChimp Date: Fri, 25 Apr 2025 02:58:43 +0100 Subject: [PATCH] Call webhook on player disconnect (#396) --- server/connector.go | 3 +++ server/notifier.go | 4 ++++ server/webhook_notifier.go | 21 ++++++++++++++++++++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/server/connector.go b/server/connector.go index 388ac4d..a6efeef 100644 --- a/server/connector.go +++ b/server/connector.go @@ -409,6 +409,9 @@ func (c *Connector) findAndConnectBackend(ctx context.Context, frontendConn net. } defer func() { + if c.connectionNotifier != nil { + c.connectionNotifier.NotifyDisconnected(ctx, clientAddr, serverAddress, userInfo, backendHostPort) + } c.metrics.ActiveConnections.Set(float64( atomic.AddInt32(&c.activeConnections, -1))) if c.recordLogins && userInfo != nil { diff --git a/server/notifier.go b/server/notifier.go index db01f19..ad17c51 100644 --- a/server/notifier.go +++ b/server/notifier.go @@ -16,4 +16,8 @@ type ConnectionNotifier interface { // NotifyConnected is called when the backend connection succeeded. NotifyConnected(ctx context.Context, clientAddr net.Addr, serverAddress string, playerInfo *PlayerInfo, backendHostPort string) error + + // NotifyDisconnected is called when the backend connection terminates. + NotifyDisconnected(ctx context.Context, + clientAddr net.Addr, serverAddress string, playerInfo *PlayerInfo, backendHostPort string) error } diff --git a/server/webhook_notifier.go b/server/webhook_notifier.go index c58c130..88ebd93 100644 --- a/server/webhook_notifier.go +++ b/server/webhook_notifier.go @@ -22,7 +22,8 @@ type WebhookNotifier struct { } const ( - WebhookEventConnecting = "connect" + WebhookEventConnecting = "connect" + WebhookEventDisconnecting = "disconnect" ) const ( @@ -109,6 +110,24 @@ func (w *WebhookNotifier) NotifyConnected(ctx context.Context, clientAddr net.Ad return w.send(ctx, payload) } +func (w *WebhookNotifier) NotifyDisconnected(ctx context.Context, clientAddr net.Addr, serverAddress string, playerInfo *PlayerInfo, backendHostPort string) error { + if w.requireUser && playerInfo == nil { + return nil + } + + payload := &WebhookNotifierPayload{ + Event: WebhookEventDisconnecting, + Timestamp: time.Now(), + Status: WebhookStatusSuccess, + Client: ClientInfoFromAddr(clientAddr), + Server: serverAddress, + PlayerInfo: playerInfo, + BackendHostPort: backendHostPort, + } + + return w.send(ctx, payload) +} + func (w *WebhookNotifier) send(ctx context.Context, payload *WebhookNotifierPayload) error { jsonPayload, err := json.Marshal(payload) if err != nil {