From 07791ffc00e9bd2276b9391192012726d729ee09 Mon Sep 17 00:00:00 2001 From: Denys Smirnov Date: Fri, 26 Apr 2024 13:57:39 +0300 Subject: [PATCH] Pass new SIP metadata. Update protocol. (#2683) --- go.mod | 2 +- go.sum | 4 ++-- pkg/service/ioservice_sip.go | 36 +++++++++++++++++++++++++++--------- pkg/service/sip.go | 33 +++++++++++++++++++++------------ 4 files changed, 51 insertions(+), 24 deletions(-) diff --git a/go.mod b/go.mod index ed85a6780..42c4858cc 100644 --- a/go.mod +++ b/go.mod @@ -20,7 +20,7 @@ require ( github.com/jxskiss/base62 v1.1.0 github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1 github.com/livekit/mediatransportutil v0.0.0-20240416023643-881d3dc5423e - github.com/livekit/protocol v1.12.1-0.20240426063020-fd19ad24b86b + github.com/livekit/protocol v1.14.1-0.20240426104403-e7962f444464 github.com/livekit/psrpc v0.5.3-0.20240426045048-8ba067a45715 github.com/mackerelio/go-osstat v0.2.4 github.com/magefile/mage v1.15.0 diff --git a/go.sum b/go.sum index 28895fb95..d2a138a9c 100644 --- a/go.sum +++ b/go.sum @@ -120,8 +120,8 @@ github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1 h1:jm09419p0lqTkD github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1/go.mod h1:Rs3MhFwutWhGwmY1VQsygw28z5bWcnEYmS1OG9OxjOQ= github.com/livekit/mediatransportutil v0.0.0-20240416023643-881d3dc5423e h1:ss4VwrouYiDpuNJ9BUTH+WsW+GDdJS70iZp8ii3/0Lc= github.com/livekit/mediatransportutil v0.0.0-20240416023643-881d3dc5423e/go.mod h1:jwKUCmObuiEDH0iiuJHaGMXwRs3RjrB4G6qqgkr/5oE= -github.com/livekit/protocol v1.12.1-0.20240426063020-fd19ad24b86b h1:hPgkp/LJzhx+U2CHOc68yxGIyfFspagsyupAaqx1Ulw= -github.com/livekit/protocol v1.12.1-0.20240426063020-fd19ad24b86b/go.mod h1:pnn0Dv+/0K0OFqKHX6J6SreYO1dZxl6tDuAZ1ns8L/w= +github.com/livekit/protocol v1.14.1-0.20240426104403-e7962f444464 h1:5IxCPDkibpvnAYN6+djltH6Gj4dMOL0hNecHn5jZKmk= +github.com/livekit/protocol v1.14.1-0.20240426104403-e7962f444464/go.mod h1:pnn0Dv+/0K0OFqKHX6J6SreYO1dZxl6tDuAZ1ns8L/w= github.com/livekit/psrpc v0.5.3-0.20240426045048-8ba067a45715 h1:vhDMOe8fxEc/amYTFo799LySPM12Fk3vc+Nc6o4gYZQ= github.com/livekit/psrpc v0.5.3-0.20240426045048-8ba067a45715/go.mod h1:CQUBSPfYYAaevg1TNCc6/aYsa8DJH4jSRFdCeSZk5u0= github.com/mackerelio/go-osstat v0.2.4 h1:qxGbdPkFo65PXOb/F/nhDKpF2nGmGaCFDLXoZjJTtUs= diff --git a/pkg/service/ioservice_sip.go b/pkg/service/ioservice_sip.go index 133044af9..e893dafb5 100644 --- a/pkg/service/ioservice_sip.go +++ b/pkg/service/ioservice_sip.go @@ -47,38 +47,56 @@ func (s *IOInfoService) matchSIPDispatchRule(ctx context.Context, trunk *livekit } func (s *IOInfoService) EvaluateSIPDispatchRules(ctx context.Context, req *rpc.EvaluateSIPDispatchRulesRequest) (*rpc.EvaluateSIPDispatchRulesResponse, error) { + log := logger.GetLogger() + log = log.WithValues("to-user", req.CalledNumber, "from-user", req.CallingNumber) trunk, err := s.matchSIPTrunk(ctx, req.CallingNumber, req.CalledNumber) if err != nil { return nil, err } + trunkID := "" if trunk != nil { - logger.Debugw("SIP trunk matched", "trunkID", trunk.SipTrunkId, "called", req.CalledNumber, "calling", req.CallingNumber) + trunkID = trunk.SipTrunkId + } + log = log.WithValues("sip-trunk", trunkID) + if trunk != nil { + log.Debugw("SIP trunk matched") } else { - logger.Debugw("No SIP trunk matched", "trunkID", "", "called", req.CalledNumber, "calling", req.CallingNumber) + log.Debugw("No SIP trunk matched") } best, err := s.matchSIPDispatchRule(ctx, trunk, req) if err != nil { if e := (*sip.ErrNoDispatchMatched)(nil); errors.As(err, &e) { - return &rpc.EvaluateSIPDispatchRulesResponse{Result: rpc.SIPDispatchResult_DROP}, nil + return &rpc.EvaluateSIPDispatchRulesResponse{ + SipTrunkId: trunkID, + Result: rpc.SIPDispatchResult_DROP, + }, nil } return nil, err } - logger.Debugw("SIP dispatch rule matched", "dispatchRule", best.SipDispatchRuleId, "called", req.CalledNumber, "calling", req.CallingNumber) - return sip.EvaluateDispatchRule(best, req) + log.Debugw("SIP dispatch rule matched", "sip-rule", best.SipDispatchRuleId) + resp, err := sip.EvaluateDispatchRule(best, req) + if err != nil { + return nil, err + } + resp.SipTrunkId = trunkID + return resp, err } func (s *IOInfoService) GetSIPTrunkAuthentication(ctx context.Context, req *rpc.GetSIPTrunkAuthenticationRequest) (*rpc.GetSIPTrunkAuthenticationResponse, error) { + log := logger.GetLogger() + log = log.WithValues("to-user", req.To, "from-user", req.From) trunk, err := s.matchSIPTrunk(ctx, req.From, req.To) if err != nil { return nil, err } if trunk == nil { - logger.Debugw("No SIP trunk matched for auth", "trunkID", "", "called", req.To, "calling", req.From) + log.Debugw("No SIP trunk matched for auth", "sip-trunk", "") return &rpc.GetSIPTrunkAuthenticationResponse{}, nil } - logger.Debugw("SIP trunk matched for auth", "trunkID", trunk.SipTrunkId, "called", req.To, "calling", req.From) + log.Debugw("SIP trunk matched for auth", "sip-trunk", trunk.SipTrunkId) return &rpc.GetSIPTrunkAuthenticationResponse{ - Username: trunk.InboundUsername, - Password: trunk.InboundPassword, + SipTrunkId: trunk.SipTrunkId, + Username: trunk.InboundUsername, + Password: trunk.InboundPassword, }, nil } diff --git a/pkg/service/sip.go b/pkg/service/sip.go index 1bcf8f603..172b46e5c 100644 --- a/pkg/service/sip.go +++ b/pkg/service/sip.go @@ -76,6 +76,8 @@ func (s *SIPService) CreateSIPTrunk(ctx context.Context, req *livekit.CreateSIPT InboundPassword: req.InboundPassword, OutboundUsername: req.OutboundUsername, OutboundPassword: req.OutboundPassword, + Name: req.Name, + Metadata: req.Metadata, } // Validate all trunks including the new one first. @@ -136,6 +138,8 @@ func (s *SIPService) CreateSIPDispatchRule(ctx context.Context, req *livekit.Cre Rule: req.Rule, TrunkIds: req.TrunkIds, HidePhoneNumber: req.HidePhoneNumber, + Name: req.Name, + Metadata: req.Metadata, } // Validate all rules including the new one first. @@ -190,28 +194,32 @@ func (s *SIPService) CreateSIPParticipantWithToken(ctx context.Context, req *liv if s.store == nil { return nil, ErrSIPNotConnected } + callID := sip.NewCallID() + log := logger.GetLogger() + log = log.WithValues("call-id", callID, "roomName", req.RoomName, "sip-trunk", req.SipTrunkId, "to-user", req.SipCallTo) - AppendLogFields(ctx, "room", req.RoomName, "trunk", req.SipTrunkId, "to", req.SipCallTo) ireq := &rpc.InternalCreateSIPParticipantRequest{ + SipCallId: callID, CallTo: req.SipCallTo, RoomName: req.RoomName, ParticipantIdentity: req.ParticipantIdentity, + ParticipantName: req.ParticipantName, + ParticipantMetadata: req.ParticipantMetadata, Dtmf: req.Dtmf, PlayRingtone: req.PlayRingtone, WsUrl: wsUrl, Token: token, } - if req.SipTrunkId != "" { - trunk, err := s.store.LoadSIPTrunk(ctx, req.SipTrunkId) - if err != nil { - logger.Errorw("cannot get trunk to update sip participant", err) - return nil, err - } - ireq.Address = trunk.OutboundAddress - ireq.Number = trunk.OutboundNumber - ireq.Username = trunk.OutboundUsername - ireq.Password = trunk.OutboundPassword + trunk, err := s.store.LoadSIPTrunk(ctx, req.SipTrunkId) + if err != nil { + log.Errorw("cannot get trunk to update sip participant", err) + return nil, err } + log = log.WithValues("from-user", trunk.OutboundNumber, "to-host", trunk.OutboundAddress) + ireq.Address = trunk.OutboundAddress + ireq.Number = trunk.OutboundNumber + ireq.Username = trunk.OutboundUsername + ireq.Password = trunk.OutboundPassword // CreateSIPParticipant will wait for LiveKit Participant to be created and that can take some time. // Thus, we must set a higher deadline for it, if it's not set already. @@ -226,13 +234,14 @@ func (s *SIPService) CreateSIPParticipantWithToken(ctx context.Context, req *liv } resp, err := s.psrpcClient.CreateSIPParticipant(ctx, "", ireq, psrpc.WithRequestTimeout(timeout)) if err != nil { - logger.Errorw("cannot update sip participant", err) + log.Errorw("cannot update sip participant", err) return nil, err } return &livekit.SIPParticipantInfo{ ParticipantId: resp.ParticipantId, ParticipantIdentity: resp.ParticipantIdentity, RoomName: req.RoomName, + SipCallId: callID, }, nil } func (s *SIPService) CreateSIPParticipant(ctx context.Context, req *livekit.CreateSIPParticipantRequest) (*livekit.SIPParticipantInfo, error) {