diff --git a/.changeset/late-rats-cheat.md b/.changeset/late-rats-cheat.md new file mode 100644 index 000000000..0e7efc487 --- /dev/null +++ b/.changeset/late-rats-cheat.md @@ -0,0 +1,5 @@ +--- +"github.com/livekit/protocol": patch +--- + +Updating how ListSIPInboundTrunkRequest filtering occurs by normalising phone number that are being compared diff --git a/livekit/sip.go b/livekit/sip.go index e99281050..b07e1bf4e 100644 --- a/livekit/sip.go +++ b/livekit/sip.go @@ -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 @@ -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( + " ", "", + "-", "", + "(", "", + ")", "", + ) +) diff --git a/sip/sip.go b/sip/sip.go index f1f1d9f13..0daa50792 100644 --- a/sip/sip.go +++ b/sip/sip.go @@ -22,7 +22,6 @@ import ( "maps" "math" "net/netip" - "regexp" "sort" "strings" @@ -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 {