Call webhook on player disconnect (#396)

This commit is contained in:
UrbaneChimp
2025-04-25 02:58:43 +01:00
committed by GitHub
parent 9601ceedcb
commit cc5d77e4c8
3 changed files with 27 additions and 1 deletions
+3
View File
@@ -409,6 +409,9 @@ func (c *Connector) findAndConnectBackend(ctx context.Context, frontendConn net.
} }
defer func() { defer func() {
if c.connectionNotifier != nil {
c.connectionNotifier.NotifyDisconnected(ctx, clientAddr, serverAddress, userInfo, backendHostPort)
}
c.metrics.ActiveConnections.Set(float64( c.metrics.ActiveConnections.Set(float64(
atomic.AddInt32(&c.activeConnections, -1))) atomic.AddInt32(&c.activeConnections, -1)))
if c.recordLogins && userInfo != nil { if c.recordLogins && userInfo != nil {
+4
View File
@@ -16,4 +16,8 @@ type ConnectionNotifier interface {
// NotifyConnected is called when the backend connection succeeded. // NotifyConnected is called when the backend connection succeeded.
NotifyConnected(ctx context.Context, NotifyConnected(ctx context.Context,
clientAddr net.Addr, serverAddress string, playerInfo *PlayerInfo, backendHostPort string) error 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
} }
+20 -1
View File
@@ -22,7 +22,8 @@ type WebhookNotifier struct {
} }
const ( const (
WebhookEventConnecting = "connect" WebhookEventConnecting = "connect"
WebhookEventDisconnecting = "disconnect"
) )
const ( const (
@@ -109,6 +110,24 @@ func (w *WebhookNotifier) NotifyConnected(ctx context.Context, clientAddr net.Ad
return w.send(ctx, payload) 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 { func (w *WebhookNotifier) send(ctx context.Context, payload *WebhookNotifierPayload) error {
jsonPayload, err := json.Marshal(payload) jsonPayload, err := json.Marshal(payload)
if err != nil { if err != nil {