Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/late-rats-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"github.com/livekit/protocol": patch
---

Updating how ListSIPInboundTrunkRequest filtering occurs by normalising phone number that are being compared
37 changes: 37 additions & 0 deletions livekit/sip.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,16 @@ func (p *ListSIPInboundTrunkRequest) Filter(info *SIPInboundTrunkInfo) bool {
ok = true
break
}
normalizedNum := NormalizeNumber(num)
for _, reqNum := range p.Numbers {
if NormalizeNumber(reqNum) == normalizedNum {
ok = true
break
}
}
if ok {
break
}
}
if !ok {
return false
Expand Down Expand Up @@ -797,3 +807,30 @@ func (p *ListSIPDispatchRuleRequest) FilterSlice(arr []*SIPDispatchRuleInfo) []*
})
return filterSlice(arr, p.Filter)
}

// NormalizeNumber normalizes a phone number by removing formatting characters and ensuring it starts with a "+".
// If the input is empty, it returns an empty string.
// If the input doesn't match the expected number pattern, it returns the original input unchanged.
func NormalizeNumber(num string) string {
if num == "" {
return ""
}
if !reNumber.MatchString(num) {
return num
}
num = reNumberRepl.Replace(num)
if !strings.HasPrefix(num, "+") {
return "+" + num
}
return num
}

var (
reNumber = regexp.MustCompile(`^\+?[\d\- ()]+$`)
reNumberRepl = strings.NewReplacer(
" ", "",
"-", "",
"(", "",
")", "",
)
)
25 changes: 3 additions & 22 deletions sip/sip.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"maps"
"math"
"net/netip"
"regexp"
"sort"
"strings"

Expand Down Expand Up @@ -275,28 +274,10 @@ func printNumbers(numbers []string) string {
return fmt.Sprintf("%q", numbers)
}

var (
reNumber = regexp.MustCompile(`^\+?[\d\- ()]+$`)
reNumberRepl = strings.NewReplacer(
" ", "",
"-", "",
"(", "",
")", "",
)
)

// NormalizeNumber normalizes a phone number by removing formatting characters and ensuring it starts with a "+".
// This function delegates to livekit.NormalizeNumber for the actual implementation.
func NormalizeNumber(num string) string {
if num == "" {
return ""
}
if !reNumber.MatchString(num) {
return num
}
num = reNumberRepl.Replace(num)
if !strings.HasPrefix(num, "+") {
return "+" + num
}
return num
return livekit.NormalizeNumber(num)
}

func validateTrunkInbound(byInbound map[string]*livekit.SIPInboundTrunkInfo, t *livekit.SIPInboundTrunkInfo, opt *matchTrunkOpts) error {
Expand Down
Loading