Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse Via and add via_branch to custom header #491

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:""`
Expand Down
12 changes: 11 additions & 1 deletion database/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package database

import (
"strconv"
"strings"

"github.com/buger/jsonparser"
"github.com/negbie/logp"
Expand Down Expand Up @@ -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 + `"`)
Expand Down
2 changes: 1 addition & 1 deletion example/homer7_config/heplify-server.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
36 changes: 32 additions & 4 deletions sipparser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type SipMsg struct {
RTPStatVal string
ViaOne string
ViaOneBranch string
ViaCount int
Privacy string
RemotePartyIdVal string
DiversionVal string
Expand Down Expand Up @@ -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++
}
}

Expand Down