2021-11-08 15:43:36 +00:00
|
|
|
package tunnelstate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/connection"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ConnTracker struct {
|
2024-11-22 15:23:46 +00:00
|
|
|
mutex sync.RWMutex
|
2022-08-11 20:31:36 +00:00
|
|
|
// int is the connection Index
|
|
|
|
connectionInfo map[uint8]ConnectionInfo
|
|
|
|
log *zerolog.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConnectionInfo struct {
|
|
|
|
IsConnected bool
|
|
|
|
Protocol connection.Protocol
|
2021-11-08 15:43:36 +00:00
|
|
|
}
|
|
|
|
|
2024-11-22 15:23:46 +00:00
|
|
|
func NewConnTracker(
|
|
|
|
log *zerolog.Logger,
|
|
|
|
) *ConnTracker {
|
2021-11-08 15:43:36 +00:00
|
|
|
return &ConnTracker{
|
2022-08-11 20:31:36 +00:00
|
|
|
connectionInfo: make(map[uint8]ConnectionInfo, 0),
|
|
|
|
log: log,
|
2021-11-08 15:43:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ct *ConnTracker) OnTunnelEvent(c connection.Event) {
|
|
|
|
switch c.EventType {
|
|
|
|
case connection.Connected:
|
2024-11-22 15:23:46 +00:00
|
|
|
ct.mutex.Lock()
|
2022-08-11 20:31:36 +00:00
|
|
|
ci := ConnectionInfo{
|
|
|
|
IsConnected: true,
|
|
|
|
Protocol: c.Protocol,
|
|
|
|
}
|
|
|
|
ct.connectionInfo[c.Index] = ci
|
2024-11-22 15:23:46 +00:00
|
|
|
ct.mutex.Unlock()
|
2021-11-08 15:43:36 +00:00
|
|
|
case connection.Disconnected, connection.Reconnecting, connection.RegisteringTunnel, connection.Unregistering:
|
2024-11-22 15:23:46 +00:00
|
|
|
ct.mutex.Lock()
|
2022-08-11 20:31:36 +00:00
|
|
|
ci := ct.connectionInfo[c.Index]
|
|
|
|
ci.IsConnected = false
|
|
|
|
ct.connectionInfo[c.Index] = ci
|
2024-11-22 15:23:46 +00:00
|
|
|
ct.mutex.Unlock()
|
2021-11-08 15:43:36 +00:00
|
|
|
default:
|
|
|
|
ct.log.Error().Msgf("Unknown connection event case %v", c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ct *ConnTracker) CountActiveConns() uint {
|
2024-11-22 15:23:46 +00:00
|
|
|
ct.mutex.RLock()
|
|
|
|
defer ct.mutex.RUnlock()
|
2021-11-08 15:43:36 +00:00
|
|
|
active := uint(0)
|
2022-08-11 20:31:36 +00:00
|
|
|
for _, ci := range ct.connectionInfo {
|
|
|
|
if ci.IsConnected {
|
2021-11-08 15:43:36 +00:00
|
|
|
active++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return active
|
|
|
|
}
|
2022-08-11 20:31:36 +00:00
|
|
|
|
|
|
|
// HasConnectedWith checks if we've ever had a successful connection to the edge
|
|
|
|
// with said protocol.
|
|
|
|
func (ct *ConnTracker) HasConnectedWith(protocol connection.Protocol) bool {
|
2024-11-22 15:23:46 +00:00
|
|
|
ct.mutex.RLock()
|
|
|
|
defer ct.mutex.RUnlock()
|
2022-08-11 20:31:36 +00:00
|
|
|
for _, ci := range ct.connectionInfo {
|
|
|
|
if ci.Protocol == protocol {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|