SIP: Implement GetSIPTrunkAuthentication. (#2244)
This commit is contained in:
@@ -215,10 +215,6 @@ func (s *IOInfoService) UpdateIngressState(ctx context.Context, req *rpc.UpdateI
|
|||||||
return &emptypb.Empty{}, nil
|
return &emptypb.Empty{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *IOInfoService) GetSIPTrunkAuthentication(ctx context.Context, req *rpc.GetSIPTrunkAuthenticationRequest) (*rpc.GetSIPTrunkAuthenticationResponse, error) {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *IOInfoService) Stop() {
|
func (s *IOInfoService) Stop() {
|
||||||
close(s.shutdown)
|
close(s.shutdown)
|
||||||
|
|
||||||
|
|||||||
@@ -127,7 +127,7 @@ func sipGetPinAndRoom(info *livekit.SIPDispatchRuleInfo) (room, pin string, err
|
|||||||
|
|
||||||
// sipMatchTrunk finds a SIP Trunk definition matching the request.
|
// sipMatchTrunk finds a SIP Trunk definition matching the request.
|
||||||
// Returns nil if no rules matched or an error if there are conflicting definitions.
|
// Returns nil if no rules matched or an error if there are conflicting definitions.
|
||||||
func sipMatchTrunk(trunks []*livekit.SIPTrunkInfo, req *rpc.EvaluateSIPDispatchRulesRequest) (*livekit.SIPTrunkInfo, error) {
|
func sipMatchTrunk(trunks []*livekit.SIPTrunkInfo, calling, called string) (*livekit.SIPTrunkInfo, error) {
|
||||||
var (
|
var (
|
||||||
selectedTrunk *livekit.SIPTrunkInfo
|
selectedTrunk *livekit.SIPTrunkInfo
|
||||||
defaultTrunk *livekit.SIPTrunkInfo
|
defaultTrunk *livekit.SIPTrunkInfo
|
||||||
@@ -143,7 +143,7 @@ func sipMatchTrunk(trunks []*livekit.SIPTrunkInfo, req *rpc.EvaluateSIPDispatchR
|
|||||||
logger.Errorw("cannot parse SIP trunk regexp", err, "trunkID", tr.SipTrunkId)
|
logger.Errorw("cannot parse SIP trunk regexp", err, "trunkID", tr.SipTrunkId)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if re.MatchString(req.CallingNumber) {
|
if re.MatchString(calling) {
|
||||||
matches = true
|
matches = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -155,10 +155,10 @@ func sipMatchTrunk(trunks []*livekit.SIPTrunkInfo, req *rpc.EvaluateSIPDispatchR
|
|||||||
// Default/wildcard trunk.
|
// Default/wildcard trunk.
|
||||||
defaultTrunk = tr
|
defaultTrunk = tr
|
||||||
defaultTrunkCnt++
|
defaultTrunkCnt++
|
||||||
} else if tr.OutboundNumber == req.CalledNumber {
|
} else if tr.OutboundNumber == called {
|
||||||
// Trunk specific to the number.
|
// Trunk specific to the number.
|
||||||
if selectedTrunk != nil {
|
if selectedTrunk != nil {
|
||||||
return nil, fmt.Errorf("Multiple SIP Trunks matched for %q", req.CalledNumber)
|
return nil, fmt.Errorf("Multiple SIP Trunks matched for %q", called)
|
||||||
}
|
}
|
||||||
selectedTrunk = tr
|
selectedTrunk = tr
|
||||||
// Keep searching! We want to know if there are any conflicting Trunk definitions.
|
// Keep searching! We want to know if there are any conflicting Trunk definitions.
|
||||||
@@ -168,7 +168,7 @@ func sipMatchTrunk(trunks []*livekit.SIPTrunkInfo, req *rpc.EvaluateSIPDispatchR
|
|||||||
return selectedTrunk, nil
|
return selectedTrunk, nil
|
||||||
}
|
}
|
||||||
if defaultTrunkCnt > 1 {
|
if defaultTrunkCnt > 1 {
|
||||||
return nil, fmt.Errorf("Multiple default SIP Trunks matched for %q", req.CalledNumber)
|
return nil, fmt.Errorf("Multiple default SIP Trunks matched for %q", called)
|
||||||
}
|
}
|
||||||
// Could still be nil here.
|
// Could still be nil here.
|
||||||
return defaultTrunk, nil
|
return defaultTrunk, nil
|
||||||
@@ -261,12 +261,12 @@ func sipMatchDispatchRule(trunk *livekit.SIPTrunkInfo, rules []*livekit.SIPDispa
|
|||||||
|
|
||||||
// matchSIPTrunk finds a SIP Trunk definition matching the request.
|
// matchSIPTrunk finds a SIP Trunk definition matching the request.
|
||||||
// Returns nil if no rules matched or an error if there are conflicting definitions.
|
// Returns nil if no rules matched or an error if there are conflicting definitions.
|
||||||
func (s *IOInfoService) matchSIPTrunk(ctx context.Context, req *rpc.EvaluateSIPDispatchRulesRequest) (*livekit.SIPTrunkInfo, error) {
|
func (s *IOInfoService) matchSIPTrunk(ctx context.Context, calling, called string) (*livekit.SIPTrunkInfo, error) {
|
||||||
trunks, err := s.ss.ListSIPTrunk(ctx)
|
trunks, err := s.ss.ListSIPTrunk(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return sipMatchTrunk(trunks, req)
|
return sipMatchTrunk(trunks, calling, called)
|
||||||
}
|
}
|
||||||
|
|
||||||
// matchSIPDispatchRule finds the best dispatch rule matching the request parameters. Returns an error if no rule matched.
|
// matchSIPDispatchRule finds the best dispatch rule matching the request parameters. Returns an error if no rule matched.
|
||||||
@@ -282,7 +282,7 @@ func (s *IOInfoService) matchSIPDispatchRule(ctx context.Context, trunk *livekit
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *IOInfoService) EvaluateSIPDispatchRules(ctx context.Context, req *rpc.EvaluateSIPDispatchRulesRequest) (*rpc.EvaluateSIPDispatchRulesResponse, error) {
|
func (s *IOInfoService) EvaluateSIPDispatchRules(ctx context.Context, req *rpc.EvaluateSIPDispatchRulesRequest) (*rpc.EvaluateSIPDispatchRulesResponse, error) {
|
||||||
trunk, err := s.matchSIPTrunk(ctx, req)
|
trunk, err := s.matchSIPTrunk(ctx, req.CallingNumber, req.CalledNumber)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -327,3 +327,14 @@ func (s *IOInfoService) EvaluateSIPDispatchRules(ctx context.Context, req *rpc.E
|
|||||||
ParticipantIdentity: fromName,
|
ParticipantIdentity: fromName,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *IOInfoService) GetSIPTrunkAuthentication(ctx context.Context, req *rpc.GetSIPTrunkAuthenticationRequest) (*rpc.GetSIPTrunkAuthenticationResponse, error) {
|
||||||
|
trunk, err := s.matchSIPTrunk(ctx, req.From, req.To)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &rpc.GetSIPTrunkAuthenticationResponse{
|
||||||
|
Username: trunk.Username,
|
||||||
|
Password: trunk.Password,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -113,11 +113,7 @@ func TestSIPMatchTrunk(t *testing.T) {
|
|||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
c := c
|
c := c
|
||||||
t.Run(c.name, func(t *testing.T) {
|
t.Run(c.name, func(t *testing.T) {
|
||||||
req := &rpc.EvaluateSIPDispatchRulesRequest{
|
got, err := sipMatchTrunk(c.trunks, sipNumber1, sipNumber2)
|
||||||
CallingNumber: sipNumber1,
|
|
||||||
CalledNumber: sipNumber2,
|
|
||||||
}
|
|
||||||
got, err := sipMatchTrunk(c.trunks, req)
|
|
||||||
if c.expErr {
|
if c.expErr {
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
require.Nil(t, got)
|
require.Nil(t, got)
|
||||||
|
|||||||
Reference in New Issue
Block a user