From 639ca5a2f2aaac0ee85762df58ce8f0c7727e0af Mon Sep 17 00:00:00 2001 From: silver0511 Date: Mon, 7 Feb 2022 19:40:14 +0800 Subject: [PATCH 1/2] parse Via and add via_branch to protocol_header --- config/config.go | 2 +- database/database.go | 2 +- decoder/decoder.go | 2 ++ example/homer7_config/heplify-server.toml | 2 +- sipparser/parser.go | 36 ++++++++++++++++++++--- 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/config/config.go b/config/config.go index 7bdff35e..915dc2e9 100644 --- a/config/config.go +++ b/config/config.go @@ -57,7 +57,7 @@ type HeplifyServer struct { ForceALegID bool `default:"false"` CustomHeader []string `default:""` IgnoreCaseCH bool `default:"false"` - SIPHeader []string `default:"ruri_user,ruri_domain,from_user,from_tag,to_user,callid,cseq,method,user_agent"` + SIPHeader []string `default:"ruri_user,ruri_domain,from_user,from_tag,to_user,callid,cseq,method,user_agent,via_branch"` LogDbg string `default:""` LogLvl string `default:"info"` LogStd bool `default:"false"` diff --git a/database/database.go b/database/database.go index 4f864b01..d93ef5a8 100644 --- a/database/database.go +++ b/database/database.go @@ -122,7 +122,7 @@ func buildTemplate() *fasttemplate.Template { var dataTemplate string sh := config.Setting.SIPHeader if len(sh) < 1 { - sh = []string{"ruri_user", "ruri_domain", "from_user", "from_tag", "to_user", "callid", "cseq", "method", "user_agent"} + sh = []string{"ruri_user", "ruri_domain", "from_user", "from_tag", "to_user", "callid", "cseq", "method", "user_agent", "via_branch"} } for _, v := range sh { diff --git a/decoder/decoder.go b/decoder/decoder.go index 6c3f3900..38db5e2e 100644 --- a/decoder/decoder.go +++ b/decoder/decoder.go @@ -217,6 +217,8 @@ func (h *HEP) EscapeFields(w io.Writer, tag string) (int, error) { return WriteJSONString(w, h.SIP.ToTag) case "via": return WriteJSONString(w, h.SIP.ViaOne) + case "via_branch": + return WriteJSONString(w, h.SIP.ViaOneBranch) case "contact_user": return WriteJSONString(w, h.SIP.ContactUser) case "contact_domain": diff --git a/example/homer7_config/heplify-server.toml b/example/homer7_config/heplify-server.toml index 8edd3b52..8152e890 100644 --- a/example/homer7_config/heplify-server.toml +++ b/example/homer7_config/heplify-server.toml @@ -62,7 +62,7 @@ ConfigHTTPAddr = "" # AlegIDs = ["X-CID","P-Charging-Vector,icid-value=\"?(.*?)(?:\"|;|$)","X-BroadWorks-Correlation-Info"] # DiscardMethod = ["OPTIONS","NOTIFY"] # CustomHeader = ["X-CustomerIP","X-Billing"] -# SIPHeader = ["callid","callid_aleg","method","ruri_user","ruri_domain","from_user","from_domain","from_tag","to_user","to_domain","to_tag","via","contact_user"] +# SIPHeader = ["callid","callid_aleg","method","ruri_user","ruri_domain","from_user","from_domain","from_tag","to_user","to_domain","to_tag","via","contact_user","via_branch"] # LogDbg = "hep,sql,loki" # LogLvl = "warning" # ConfigHTTPAddr = "0.0.0.0:9876" diff --git a/sipparser/parser.go b/sipparser/parser.go index 6ae73186..a0771ea4 100644 --- a/sipparser/parser.go +++ b/sipparser/parser.go @@ -80,6 +80,7 @@ type SipMsg struct { RTPStatVal string ViaOne string ViaOneBranch string + ViaCount int Privacy string RemotePartyIdVal string DiversionVal string @@ -598,15 +599,42 @@ func (s *SipMsg) parseVia(str string) { s.Via = append(s.Via, v) } */ + if len(s.ViaOne) == 0 { + s.ViaCount = 0 + } s.ViaOne = str - if a := strings.Index(str, "branch="); a > -1 && a < len(str) { + loopMaxCount := 0 + for { + a := strings.Index(str, "branch=") + if a < 0 || a >= len(str) { + break + } + if loopMaxCount > 100 { + break + } b := str[a:] l := len(b) - if c := strings.Index(b, ";"); c > -1 && c < l && l > 7 { - s.ViaOneBranch = b[7:c] + c := strings.Index(b, ";") + d := strings.Index(b, ",") + if d > -1 && d < c { + c = d + } + if c > -1 && c < l && l > 7 { + if len(s.ViaOneBranch) == 0 { + s.ViaOneBranch = b[7:c] + } else { + s.ViaOneBranch = s.ViaOneBranch + ";" + b[7:c] + } } else if l > 7 { - s.ViaOneBranch = b[7:] + if len(s.ViaOneBranch) == 0 { + s.ViaOneBranch = b[7:] + } else { + s.ViaOneBranch = s.ViaOneBranch + ";" + b[7:] + } } + str = b[7:] + s.ViaCount++ + loopMaxCount++ } } From 7186da9c087e4b6b910dd5c463b6e0f47584c834 Mon Sep 17 00:00:00 2001 From: silver0511 Date: Thu, 24 Feb 2022 15:25:06 +0800 Subject: [PATCH 2/2] move via_branch from sip_header to custom_header --- config/config.go | 4 ++-- database/database.go | 2 +- database/header.go | 12 +++++++++++- decoder/decoder.go | 2 -- example/homer7_config/heplify-server.toml | 4 ++-- 5 files changed, 16 insertions(+), 8 deletions(-) diff --git a/config/config.go b/config/config.go index 915dc2e9..74fea4c6 100644 --- a/config/config.go +++ b/config/config.go @@ -55,9 +55,9 @@ type HeplifyServer struct { CensorMethod []string `default:""` AlegIDs []string `default:""` ForceALegID bool `default:"false"` - CustomHeader []string `default:""` + CustomHeader []string `default:"via_branch"` IgnoreCaseCH bool `default:"false"` - SIPHeader []string `default:"ruri_user,ruri_domain,from_user,from_tag,to_user,callid,cseq,method,user_agent,via_branch"` + SIPHeader []string `default:"ruri_user,ruri_domain,from_user,from_tag,to_user,callid,cseq,method,user_agent"` LogDbg string `default:""` LogLvl string `default:"info"` LogStd bool `default:"false"` diff --git a/database/database.go b/database/database.go index d93ef5a8..4f864b01 100644 --- a/database/database.go +++ b/database/database.go @@ -122,7 +122,7 @@ func buildTemplate() *fasttemplate.Template { var dataTemplate string sh := config.Setting.SIPHeader if len(sh) < 1 { - sh = []string{"ruri_user", "ruri_domain", "from_user", "from_tag", "to_user", "callid", "cseq", "method", "user_agent", "via_branch"} + sh = []string{"ruri_user", "ruri_domain", "from_user", "from_tag", "to_user", "callid", "cseq", "method", "user_agent"} } for _, v := range sh { diff --git a/database/header.go b/database/header.go index 439d1f4e..3f39b9ae 100644 --- a/database/header.go +++ b/database/header.go @@ -2,6 +2,7 @@ package database import ( "strconv" + "strings" "github.com/buger/jsonparser" "github.com/negbie/logp" @@ -49,7 +50,16 @@ func makeSIPDataHeader(h *decoder.HEP, bb *bytebufferpool.ByteBuffer, t *fasttem t.ExecuteFunc(bb, h.EscapeFields) - if len(h.SIP.CHeader) > 0 || h.SIP.CustomHeader != nil { + if len(h.SIP.CHeader) > 0 { + for _, v := range h.SIP.CHeader { + if strings.EqualFold(v, "via_branch") { + bb.WriteString(`,"` + v + `":"`) + bb.WriteString(h.SIP.ViaOneBranch + `"`) + } + } + } + + if h.SIP.CustomHeader != nil { for k, v := range h.SIP.CustomHeader { bb.WriteString(`,"` + k + `":"`) bb.WriteString(v + `"`) diff --git a/decoder/decoder.go b/decoder/decoder.go index 38db5e2e..6c3f3900 100644 --- a/decoder/decoder.go +++ b/decoder/decoder.go @@ -217,8 +217,6 @@ func (h *HEP) EscapeFields(w io.Writer, tag string) (int, error) { return WriteJSONString(w, h.SIP.ToTag) case "via": return WriteJSONString(w, h.SIP.ViaOne) - case "via_branch": - return WriteJSONString(w, h.SIP.ViaOneBranch) case "contact_user": return WriteJSONString(w, h.SIP.ContactUser) case "contact_domain": diff --git a/example/homer7_config/heplify-server.toml b/example/homer7_config/heplify-server.toml index 8152e890..30e9dd2a 100644 --- a/example/homer7_config/heplify-server.toml +++ b/example/homer7_config/heplify-server.toml @@ -61,8 +61,8 @@ ConfigHTTPAddr = "" # PromTargetName = "sbc_access,sbc_core,kamailio,asterisk,pstn_gateway" # AlegIDs = ["X-CID","P-Charging-Vector,icid-value=\"?(.*?)(?:\"|;|$)","X-BroadWorks-Correlation-Info"] # DiscardMethod = ["OPTIONS","NOTIFY"] -# CustomHeader = ["X-CustomerIP","X-Billing"] -# SIPHeader = ["callid","callid_aleg","method","ruri_user","ruri_domain","from_user","from_domain","from_tag","to_user","to_domain","to_tag","via","contact_user","via_branch"] +# CustomHeader = ["X-CustomerIP","X-Billing","via_branch"] +# SIPHeader = ["callid","callid_aleg","method","ruri_user","ruri_domain","from_user","from_domain","from_tag","to_user","to_domain","to_tag","via","contact_user"] # LogDbg = "hep,sql,loki" # LogLvl = "warning" # ConfigHTTPAddr = "0.0.0.0:9876"