Skip to content

Commit cc5d77e

Browse files
authored
Call webhook on player disconnect (#396)
1 parent 9601cee commit cc5d77e

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

server/connector.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,9 @@ func (c *Connector) findAndConnectBackend(ctx context.Context, frontendConn net.
409409
}
410410

411411
defer func() {
412+
if c.connectionNotifier != nil {
413+
c.connectionNotifier.NotifyDisconnected(ctx, clientAddr, serverAddress, userInfo, backendHostPort)
414+
}
412415
c.metrics.ActiveConnections.Set(float64(
413416
atomic.AddInt32(&c.activeConnections, -1)))
414417
if c.recordLogins && userInfo != nil {

server/notifier.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ type ConnectionNotifier interface {
1616
// NotifyConnected is called when the backend connection succeeded.
1717
NotifyConnected(ctx context.Context,
1818
clientAddr net.Addr, serverAddress string, playerInfo *PlayerInfo, backendHostPort string) error
19+
20+
// NotifyDisconnected is called when the backend connection terminates.
21+
NotifyDisconnected(ctx context.Context,
22+
clientAddr net.Addr, serverAddress string, playerInfo *PlayerInfo, backendHostPort string) error
1923
}

server/webhook_notifier.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ type WebhookNotifier struct {
2222
}
2323

2424
const (
25-
WebhookEventConnecting = "connect"
25+
WebhookEventConnecting = "connect"
26+
WebhookEventDisconnecting = "disconnect"
2627
)
2728

2829
const (
@@ -109,6 +110,24 @@ func (w *WebhookNotifier) NotifyConnected(ctx context.Context, clientAddr net.Ad
109110
return w.send(ctx, payload)
110111
}
111112

113+
func (w *WebhookNotifier) NotifyDisconnected(ctx context.Context, clientAddr net.Addr, serverAddress string, playerInfo *PlayerInfo, backendHostPort string) error {
114+
if w.requireUser && playerInfo == nil {
115+
return nil
116+
}
117+
118+
payload := &WebhookNotifierPayload{
119+
Event: WebhookEventDisconnecting,
120+
Timestamp: time.Now(),
121+
Status: WebhookStatusSuccess,
122+
Client: ClientInfoFromAddr(clientAddr),
123+
Server: serverAddress,
124+
PlayerInfo: playerInfo,
125+
BackendHostPort: backendHostPort,
126+
}
127+
128+
return w.send(ctx, payload)
129+
}
130+
112131
func (w *WebhookNotifier) send(ctx context.Context, payload *WebhookNotifierPayload) error {
113132
jsonPayload, err := json.Marshal(payload)
114133
if err != nil {

0 commit comments

Comments
 (0)