From 76a65bc282834e7062ccbf6ec3add09879503c7b Mon Sep 17 00:00:00 2001 From: Nishad Musthafa Date: Thu, 3 Jul 2025 14:38:47 -0700 Subject: [PATCH 1/4] Normalizing the comparison of numbers in the ListSIPInboundTrunkRequest Filter 1. Added normalization to the comparison of trunk and caller number in ListSIPInboundTrunkRequest 2. Moved NormalizeNumber to a utils number to prevent import cycle --- livekit/sip.go | 11 ++++++++-- sip/sip.go | 25 +++------------------- utils/sip_processing.go | 47 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 24 deletions(-) create mode 100644 utils/sip_processing.go diff --git a/livekit/sip.go b/livekit/sip.go index e99281050..6847a5884 100644 --- a/livekit/sip.go +++ b/livekit/sip.go @@ -11,6 +11,7 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" + "github.com/livekit/protocol/utils" "github.com/livekit/protocol/utils/xtwirp" ) @@ -721,8 +722,14 @@ func (p *ListSIPInboundTrunkRequest) Filter(info *SIPInboundTrunkInfo) bool { if len(p.Numbers) != 0 && len(info.Numbers) != 0 { ok := false for _, num := range info.Numbers { - if slices.Contains(p.Numbers, num) { - ok = true + normalizedNum := utils.NormalizeNumber(num) + for _, reqNum := range p.Numbers { + if utils.NormalizeNumber(reqNum) == normalizedNum { + ok = true + break + } + } + if ok { break } } diff --git a/sip/sip.go b/sip/sip.go index f1f1d9f13..79bb95903 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 utils.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 utils.NormalizeNumber(num) } func validateTrunkInbound(byInbound map[string]*livekit.SIPInboundTrunkInfo, t *livekit.SIPInboundTrunkInfo, opt *matchTrunkOpts) error { diff --git a/utils/sip_processing.go b/utils/sip_processing.go new file mode 100644 index 000000000..b1029b91b --- /dev/null +++ b/utils/sip_processing.go @@ -0,0 +1,47 @@ +// Copyright 2023 LiveKit, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package utils + +import ( + "regexp" + "strings" +) + +var ( + reNumber = regexp.MustCompile(`^\+?[\d\- ()]+$`) + reNumberRepl = strings.NewReplacer( + " ", "", + "-", "", + "(", "", + ")", "", + ) +) + +// 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 +} From bf2b0e127f00f099420c2a919d5af7e49d3f819c Mon Sep 17 00:00:00 2001 From: Nishad Musthafa Date: Thu, 3 Jul 2025 14:53:31 -0700 Subject: [PATCH 2/4] Fixing another import cycle caused by utils importing livekit --- livekit/sip.go | 6 +++--- sip/sip.go | 5 +++-- utils/{ => sip}/sip_processing.go | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) rename utils/{ => sip}/sip_processing.go (98%) diff --git a/livekit/sip.go b/livekit/sip.go index 6847a5884..d8a2427e3 100644 --- a/livekit/sip.go +++ b/livekit/sip.go @@ -11,7 +11,7 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "github.com/livekit/protocol/utils" + siputils "github.com/livekit/protocol/utils/sip" "github.com/livekit/protocol/utils/xtwirp" ) @@ -722,9 +722,9 @@ func (p *ListSIPInboundTrunkRequest) Filter(info *SIPInboundTrunkInfo) bool { if len(p.Numbers) != 0 && len(info.Numbers) != 0 { ok := false for _, num := range info.Numbers { - normalizedNum := utils.NormalizeNumber(num) + normalizedNum := siputils.NormalizeNumber(num) for _, reqNum := range p.Numbers { - if utils.NormalizeNumber(reqNum) == normalizedNum { + if siputils.NormalizeNumber(reqNum) == normalizedNum { ok = true break } diff --git a/sip/sip.go b/sip/sip.go index 79bb95903..53c74f9b8 100644 --- a/sip/sip.go +++ b/sip/sip.go @@ -34,6 +34,7 @@ import ( "github.com/livekit/protocol/rpc" "github.com/livekit/protocol/utils" "github.com/livekit/protocol/utils/guid" + siputils "github.com/livekit/protocol/utils/sip" ) //go:generate stringer -type TrunkFilteredReason -trimprefix TrunkFiltered @@ -275,9 +276,9 @@ func printNumbers(numbers []string) string { } // NormalizeNumber normalizes a phone number by removing formatting characters and ensuring it starts with a "+". -// This function delegates to utils.NormalizeNumber for the actual implementation. +// This function delegates to siputils.NormalizeNumber for the actual implementation. func NormalizeNumber(num string) string { - return utils.NormalizeNumber(num) + return siputils.NormalizeNumber(num) } func validateTrunkInbound(byInbound map[string]*livekit.SIPInboundTrunkInfo, t *livekit.SIPInboundTrunkInfo, opt *matchTrunkOpts) error { diff --git a/utils/sip_processing.go b/utils/sip/sip_processing.go similarity index 98% rename from utils/sip_processing.go rename to utils/sip/sip_processing.go index b1029b91b..d2389cbf4 100644 --- a/utils/sip_processing.go +++ b/utils/sip/sip_processing.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package utils +package sip import ( "regexp" From 58f54de1c1edd2d14b8cdb6ae498a0e492928a11 Mon Sep 17 00:00:00 2001 From: Nishad Musthafa Date: Thu, 3 Jul 2025 15:22:01 -0700 Subject: [PATCH 3/4] Adding changeset --- .changeset/late-rats-cheat.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/late-rats-cheat.md 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 From 0a60480978e974ad8c02c3f027a8a391958b48e0 Mon Sep 17 00:00:00 2001 From: Nishad Musthafa Date: Sat, 12 Jul 2025 22:04:31 -0700 Subject: [PATCH 4/4] PR feedback --- livekit/sip.go | 36 +++++++++++++++++++++++++--- sip/sip.go | 5 ++-- utils/sip/sip_processing.go | 47 ------------------------------------- 3 files changed, 35 insertions(+), 53 deletions(-) delete mode 100644 utils/sip/sip_processing.go diff --git a/livekit/sip.go b/livekit/sip.go index d8a2427e3..b07e1bf4e 100644 --- a/livekit/sip.go +++ b/livekit/sip.go @@ -11,7 +11,6 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - siputils "github.com/livekit/protocol/utils/sip" "github.com/livekit/protocol/utils/xtwirp" ) @@ -722,9 +721,13 @@ func (p *ListSIPInboundTrunkRequest) Filter(info *SIPInboundTrunkInfo) bool { if len(p.Numbers) != 0 && len(info.Numbers) != 0 { ok := false for _, num := range info.Numbers { - normalizedNum := siputils.NormalizeNumber(num) + if slices.Contains(p.Numbers, num) { + ok = true + break + } + normalizedNum := NormalizeNumber(num) for _, reqNum := range p.Numbers { - if siputils.NormalizeNumber(reqNum) == normalizedNum { + if NormalizeNumber(reqNum) == normalizedNum { ok = true break } @@ -804,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 53c74f9b8..0daa50792 100644 --- a/sip/sip.go +++ b/sip/sip.go @@ -34,7 +34,6 @@ import ( "github.com/livekit/protocol/rpc" "github.com/livekit/protocol/utils" "github.com/livekit/protocol/utils/guid" - siputils "github.com/livekit/protocol/utils/sip" ) //go:generate stringer -type TrunkFilteredReason -trimprefix TrunkFiltered @@ -276,9 +275,9 @@ func printNumbers(numbers []string) string { } // NormalizeNumber normalizes a phone number by removing formatting characters and ensuring it starts with a "+". -// This function delegates to siputils.NormalizeNumber for the actual implementation. +// This function delegates to livekit.NormalizeNumber for the actual implementation. func NormalizeNumber(num string) string { - return siputils.NormalizeNumber(num) + return livekit.NormalizeNumber(num) } func validateTrunkInbound(byInbound map[string]*livekit.SIPInboundTrunkInfo, t *livekit.SIPInboundTrunkInfo, opt *matchTrunkOpts) error { diff --git a/utils/sip/sip_processing.go b/utils/sip/sip_processing.go deleted file mode 100644 index d2389cbf4..000000000 --- a/utils/sip/sip_processing.go +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright 2023 LiveKit, Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package sip - -import ( - "regexp" - "strings" -) - -var ( - reNumber = regexp.MustCompile(`^\+?[\d\- ()]+$`) - reNumberRepl = strings.NewReplacer( - " ", "", - "-", "", - "(", "", - ")", "", - ) -) - -// 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 -}