observeSession subscribes to a session's observe channel and forwards events to C2. Re-subscribes automatically if the channel closes while the bridge is still running.
(sessionID string)
| 11 | // observeSession subscribes to a session's observe channel and forwards events to C2. |
| 12 | // Re-subscribes automatically if the channel closes while the bridge is still running. |
| 13 | func (b *Bridge) observeSession(sessionID string) { |
| 14 | subID := "bridge-observe-" + sessionID |
| 15 | for { |
| 16 | mgr := sessions.Global() |
| 17 | if mgr == nil { |
| 18 | return |
| 19 | } |
| 20 | ch := mgr.SubscribeObserve(sessionID, subID) |
| 21 | if ch == nil { |
| 22 | log.Warnf("[bridge] failed to subscribe observe for session %s", sessionID) |
| 23 | return |
| 24 | } |
| 25 | log.Infof("[bridge] observeSession started for %s", sessionID) |
| 26 | |
| 27 | done := false |
| 28 | for !done { |
| 29 | select { |
| 30 | case event, ok := <-ch: |
| 31 | if !ok { |
| 32 | done = true |
| 33 | } else { |
| 34 | b.forwardObserveEvent(event) |
| 35 | } |
| 36 | case <-b.ctx.Done(): |
| 37 | if mgr := sessions.Global(); mgr != nil { |
| 38 | mgr.UnsubscribeObserve(sessionID, subID) |
| 39 | } |
| 40 | return |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | if mgr := sessions.Global(); mgr != nil { |
| 45 | mgr.UnsubscribeObserve(sessionID, subID) |
| 46 | } |
| 47 | |
| 48 | if b.ctx.Err() != nil { |
| 49 | return |
| 50 | } |
| 51 | log.Infof("[bridge] observe channel closed for %s, re-subscribing", sessionID) |
| 52 | time.Sleep(1 * time.Second) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // checkinSession sends a single checkin ping for a session. |
| 57 | func (b *Bridge) checkinSession(sessionID string) error { |