2023-03-20 23:11:57 +00:00
|
|
|
package databag
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"encoding/json"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-03-21 05:13:01 +00:00
|
|
|
var bridgeRelay BridgeRelay;
|
2023-03-24 07:37:06 +00:00
|
|
|
const BridgeKeepAlive = 15
|
2023-03-20 23:11:57 +00:00
|
|
|
|
|
|
|
type BridgeStatus struct {
|
|
|
|
status string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Bridge struct {
|
2023-03-21 20:56:09 +00:00
|
|
|
accountId uint
|
2023-03-21 05:13:01 +00:00
|
|
|
callId string
|
2023-03-21 20:56:09 +00:00
|
|
|
cardId string
|
2023-03-20 23:11:57 +00:00
|
|
|
expires int64
|
2023-03-21 20:56:09 +00:00
|
|
|
closed bool
|
2023-03-20 23:11:57 +00:00
|
|
|
callerToken string
|
|
|
|
calleeToken string
|
|
|
|
caller *websocket.Conn
|
|
|
|
callee *websocket.Conn
|
|
|
|
}
|
|
|
|
|
|
|
|
type BridgeRelay struct {
|
|
|
|
sync sync.Mutex
|
|
|
|
bridges []Bridge
|
|
|
|
}
|
|
|
|
|
2023-03-21 20:56:09 +00:00
|
|
|
func (s *BridgeRelay) AddBridge(accountId uint, callId string, callerToken string, calleeToken string) {
|
2023-03-20 23:11:57 +00:00
|
|
|
s.sync.Lock()
|
|
|
|
defer s.sync.Unlock()
|
|
|
|
bridge := Bridge{
|
2023-03-21 20:56:09 +00:00
|
|
|
accountId: accountId,
|
2023-03-21 05:13:01 +00:00
|
|
|
callId: callId,
|
|
|
|
expires: time.Now().Unix() + (BridgeKeepAlive * 3),
|
2023-03-21 20:56:09 +00:00
|
|
|
closed: false,
|
2023-03-20 23:11:57 +00:00
|
|
|
callerToken: callerToken,
|
|
|
|
calleeToken: calleeToken,
|
|
|
|
}
|
|
|
|
s.bridges = append(s.bridges, bridge)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setStatus(bridge Bridge, status string) {
|
|
|
|
msg, _ := json.Marshal(BridgeStatus{ status: status })
|
|
|
|
if bridge.caller != nil {
|
|
|
|
if err := bridge.caller.WriteMessage(websocket.TextMessage, msg); err != nil {
|
|
|
|
LogMsg("failed to notify bridge status");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if bridge.callee != nil {
|
|
|
|
if err := bridge.callee.WriteMessage(websocket.TextMessage, msg); err != nil {
|
|
|
|
LogMsg("failed to notify bridge status");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 20:56:09 +00:00
|
|
|
func (s *BridgeRelay) KeepAlive(accountId uint, callId string) {
|
2023-03-20 23:11:57 +00:00
|
|
|
s.sync.Lock()
|
|
|
|
defer s.sync.Unlock()
|
|
|
|
now := time.Now().Unix()
|
|
|
|
var bridges []Bridge
|
|
|
|
for _, bridge := range s.bridges {
|
|
|
|
if bridge.expires > now {
|
|
|
|
bridges = append(bridges, bridge)
|
2023-03-21 20:56:09 +00:00
|
|
|
if bridge.callId == callId && bridge.accountId == accountId {
|
2023-03-21 05:13:01 +00:00
|
|
|
bridge.expires = now + (BridgeKeepAlive * 3)
|
2023-03-20 23:11:57 +00:00
|
|
|
if bridge.caller != nil {
|
|
|
|
if err := bridge.caller.WriteMessage(websocket.PingMessage, nil); err != nil {
|
|
|
|
LogMsg("failed to ping caller signal");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if bridge.callee != nil {
|
|
|
|
if err := bridge.callee.WriteMessage(websocket.PingMessage, nil); err != nil {
|
|
|
|
LogMsg("failed to ping callee signal");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
setStatus(bridge, "closed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.bridges = bridges
|
|
|
|
}
|
|
|
|
|
2023-03-21 20:56:09 +00:00
|
|
|
func (s *BridgeRelay) RemoveBridge(accountId uint, callId string, cardId string) {
|
2023-03-20 23:11:57 +00:00
|
|
|
s.sync.Lock()
|
|
|
|
defer s.sync.Unlock()
|
|
|
|
var bridges []Bridge
|
|
|
|
for _, bridge := range s.bridges {
|
2023-03-21 20:56:09 +00:00
|
|
|
if bridge.callId == callId && bridge.accountId == accountId && (bridge.cardId == cardId || cardId == "") {
|
2023-03-20 23:11:57 +00:00
|
|
|
setStatus(bridge, "closed");
|
|
|
|
} else {
|
|
|
|
bridges = append(bridges, bridge)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.bridges = bridges
|
|
|
|
}
|
|
|
|
|
2023-03-21 05:13:01 +00:00
|
|
|
func (s *BridgeRelay) SetConnection(conn *websocket.Conn, token string) {
|
2023-03-20 23:11:57 +00:00
|
|
|
s.sync.Lock()
|
|
|
|
defer s.sync.Unlock()
|
|
|
|
for _, bridge := range s.bridges {
|
|
|
|
if bridge.callerToken == token {
|
|
|
|
bridge.caller = conn
|
|
|
|
if bridge.caller != nil && bridge.callee != nil {
|
|
|
|
setStatus(bridge, "connected")
|
|
|
|
} else {
|
|
|
|
setStatus(bridge, "connecting")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if bridge.calleeToken == token {
|
|
|
|
bridge.callee = conn
|
|
|
|
if bridge.caller != nil && bridge.callee != nil {
|
|
|
|
setStatus(bridge, "connected")
|
|
|
|
} else {
|
|
|
|
setStatus(bridge, "connecting")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 05:13:01 +00:00
|
|
|
func (s *BridgeRelay) ClearConnection(conn *websocket.Conn) {
|
2023-03-20 23:11:57 +00:00
|
|
|
s.sync.Lock()
|
|
|
|
defer s.sync.Unlock()
|
|
|
|
for _, bridge := range s.bridges {
|
|
|
|
if bridge.caller == conn {
|
|
|
|
bridge.caller = nil
|
|
|
|
setStatus(bridge, "connecting")
|
|
|
|
}
|
|
|
|
if bridge.callee == conn {
|
|
|
|
bridge.callee = nil
|
|
|
|
setStatus(bridge, "connecting")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 05:13:01 +00:00
|
|
|
func (s *BridgeRelay) RelayMessage(conn *websocket.Conn, msg []byte) {
|
2023-03-20 23:11:57 +00:00
|
|
|
s.sync.Lock()
|
|
|
|
defer s.sync.Unlock()
|
|
|
|
for _, bridge := range s.bridges {
|
|
|
|
if bridge.caller == conn && bridge.callee != nil {
|
|
|
|
if err := bridge.callee.WriteMessage(websocket.TextMessage, msg); err != nil {
|
|
|
|
LogMsg("failed to relay to callee");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if bridge.callee == conn && bridge.caller != nil {
|
|
|
|
if err := bridge.caller.WriteMessage(websocket.TextMessage, msg); err != nil {
|
|
|
|
LogMsg("failed to relay to caller");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-21 05:13:01 +00:00
|
|
|
|