diff --git a/config/config.go b/config/config.go index 01fbb4e2..e147a3b6 100644 --- a/config/config.go +++ b/config/config.go @@ -56,7 +56,7 @@ 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"` LogDbg string `default:""` 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/example/homer7_config/heplify-server.toml b/example/homer7_config/heplify-server.toml index 8edd3b52..30e9dd2a 100644 --- a/example/homer7_config/heplify-server.toml +++ b/example/homer7_config/heplify-server.toml @@ -61,7 +61,7 @@ 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"] +# 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" 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++ } }