Skip to content

Commit

Permalink
Move UserAgent setting to SipStack, and add original recipient to ses…
Browse files Browse the repository at this point in the history
…sion.makeRequest.
  • Loading branch information
cloudwebrtc committed Mar 30, 2021
1 parent e97754f commit 0c1b23c
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 42 deletions.
5 changes: 3 additions & 2 deletions examples/b2bua/b2bua/b2bua.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func NewB2BUA() *B2BUA {
}

stack := stack.NewSipStack(&stack.SipStackConfig{
UserAgent: "Go B2BUA/1.0.0",
Extensions: []string{"replaces", "outbound"},
Dns: "8.8.8.8",
ServerAuthManager: stack.ServerAuthManager{
Expand Down Expand Up @@ -100,8 +101,8 @@ func NewB2BUA() *B2BUA {
}

ua := ua.NewUserAgent(&ua.UserAgentConfig{
UserAgent: "Go B2BUA/1.0.0",
SipStack: stack,

SipStack: stack,
}, logger)

ua.InviteStateHandler = func(sess *session.Session, req *sip.Request, resp *sip.Response, state session.Status) {
Expand Down
15 changes: 9 additions & 6 deletions examples/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ func createUdp() *rtp.RtpUDPStream {
func main() {
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGTERM, syscall.SIGINT)
stack := stack.NewSipStack(&stack.SipStackConfig{Extensions: []string{"replaces", "outbound"}, Dns: "8.8.8.8"}, logger)
stack := stack.NewSipStack(&stack.SipStackConfig{
UserAgent: "Go Sip Client/example-client",
Extensions: []string{"replaces", "outbound"},
Dns: "8.8.8.8"}, logger)

listen := "0.0.0.0:5080"
logger.Infof("Listen => %s", listen)
Expand All @@ -62,8 +65,7 @@ func main() {
}

ua := ua.NewUserAgent(&ua.UserAgentConfig{
UserAgent: "Go Sip Client/1.0.0",
SipStack: stack,
SipStack: stack,
}, logger)

ua.InviteStateHandler = func(sess *session.Session, req *sip.Request, resp *sip.Response, state session.Status) {
Expand Down Expand Up @@ -94,7 +96,7 @@ func main() {
logger.Error(err)
}

profile := account.NewProfile(uri.Clone(), "goSIP",
profile := account.NewProfile(uri.Clone(), "goSIP/example-client",
&account.AuthInfo{
AuthUser: "100",
Password: "100",
Expand All @@ -104,7 +106,7 @@ func main() {
stack,
)

recipient, err := parser.ParseSipUri("sip:[email protected]:5060;transport=udp")
recipient, err := parser.ParseSipUri("sip:[email protected]:5081;transport=wss")
if err != nil {
logger.Error(err)
}
Expand All @@ -121,10 +123,11 @@ func main() {
logger.Error(err)
}

recipient, err = parser.ParseSipUri("sip:[email protected]:5060;transport=udp")
recipient, err = parser.ParseSipUri("sip:[email protected]:5081;transport=wss")
if err != nil {
logger.Error(err)
}

go ua.Invite(profile, called, recipient, &sdp)

<-stop
Expand Down
8 changes: 5 additions & 3 deletions examples/register/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ func main() {

stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGTERM, syscall.SIGINT)
stack := stack.NewSipStack(&stack.SipStackConfig{Extensions: []string{"replaces", "outbound"}, Dns: "8.8.8.8"}, logger)
stack := stack.NewSipStack(&stack.SipStackConfig{
UserAgent: "Go Sip Client/example-register",
Extensions: []string{"replaces", "outbound"},
Dns: "8.8.8.8"}, logger)

if err := stack.Listen("udp", "0.0.0.0:5066"); err != nil {
logger.Panic(err)
}

ua := ua.NewUserAgent(&ua.UserAgentConfig{
UserAgent: "Go Sip Client/1.0.0",
SipStack: stack,
SipStack: stack,
}, logger)

ua.RegisterStateHandler = func(state account.RegisterState) {
Expand Down
9 changes: 1 addition & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/ghettovoice/gosip v0.0.0-20210322152317-7858ea172631 h1:U4vU1rDtPKYCXl183LGxsXpoFZU3or2wq5S4qDKVusM=
github.com/ghettovoice/gosip v0.0.0-20210322152317-7858ea172631/go.mod h1:yTr3BEYSFe9As6XM7ldyrVgqsPwlnw8Ahc4N28VFM2g=
github.com/ghettovoice/gosip v0.0.0-20210329153648-7b01f22594e2 h1:gOjs4IPhn8bqWpwHhyxkMf7cYSOS00slfLe5bU7mUlE=
github.com/ghettovoice/gosip v0.0.0-20210329153648-7b01f22594e2/go.mod h1:yTr3BEYSFe9As6XM7ldyrVgqsPwlnw8Ahc4N28VFM2g=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
Expand Down Expand Up @@ -374,8 +372,7 @@ golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210220050731-9a76102bfb43/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210223095934-7937bea0104d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210305230114-8fe3ee5dd75b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210314195730-07df6a141424 h1:+39ahH47SWi1PhMRAHfIrm8f69HRZ5K2koXH6dmO8TQ=
golang.org/x/sys v0.0.0-20210314195730-07df6a141424/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 h1:EZ2mChiOa8udjfp6rRmswTbtZN/QzUQp4ptM4rnjHvc=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down Expand Up @@ -462,8 +459,6 @@ google.golang.org/api v0.35.0/go.mod h1:/XrVsuzM0rZmrsbjJutiuftIzeuTQcEeaYcSk/mQ
google.golang.org/api v0.36.0/go.mod h1:+z5ficQTmoYpPn8LCUNVpK5I7hwkpjbcgqA7I34qYtE=
google.golang.org/api v0.40.0/go.mod h1:fYKFpnQN0DsDSKRVRcQSDQNtqWPfM9i+zNPxepjRCQ8=
google.golang.org/api v0.41.0/go.mod h1:RkxM5lITDfTzmyKFPt+wGrCJbVfniCr2ool8kTBzRTU=
google.golang.org/api v0.42.0 h1:uqATLkpxiBrhrvFoebXUjvyzE9nQf+pVyy0Z0IHE+fc=
google.golang.org/api v0.42.0/go.mod h1:+Oj4s6ch2SEGtPjGqfUfZonBH0GjQH89gTeKKAEGZKI=
google.golang.org/api v0.43.0 h1:4sAyIHT6ZohtAQDoxws+ez7bROYmUlOVvsUscYCDTqA=
google.golang.org/api v0.43.0/go.mod h1:nQsDGjRXMo4lvh5hP0TKqF244gqhGcr/YSIykhUk/94=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
Expand Down Expand Up @@ -512,8 +507,6 @@ google.golang.org/genproto v0.0.0-20210108203827-ffc7fda8c3d7/go.mod h1:FWY/as6D
google.golang.org/genproto v0.0.0-20210222152913-aa3ee6e6a81c/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/genproto v0.0.0-20210303154014-9728d6b83eeb/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/genproto v0.0.0-20210310155132-4ce2db91004e/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/genproto v0.0.0-20210312152112-fc591d9ea70f h1:YRBxgxUW6GFi+AKsn8WGA9k1SZohK+gGuEqdeT5aoNQ=
google.golang.org/genproto v0.0.0-20210312152112-fc591d9ea70f/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/genproto v0.0.0-20210319143718-93e7006c17a6 h1:4Xw2NwItrJOFR5s6PnK98PI6Bgw1LhMP1j/rO5WP0S4=
google.golang.org/genproto v0.0.0-20210319143718-93e7006c17a6/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
Expand Down
29 changes: 15 additions & 14 deletions pkg/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ func (s *Session) Provisional(statusCode sip.StatusCode, reason string) {
}

func (s *Session) makeRequest(uaType string, method sip.RequestMethod, msgID sip.MessageID, inviteRequest sip.Request, inviteResponse sip.Response) sip.Request {
byeRequest := sip.NewRequest(
newRequest := sip.NewRequest(
msgID,
method,
s.remoteTarget,
Expand All @@ -366,31 +366,32 @@ func (s *Session) makeRequest(uaType string, method sip.RequestMethod, msgID sip
)

from := s.localURI.Clone().AsFromHeader()
byeRequest.AppendHeader(from)
newRequest.AppendHeader(from)
to := s.remoteURI.Clone().AsToHeader()
byeRequest.AppendHeader(to)
newRequest.AppendHeader(to)
newRequest.SetRecipient(s.request.Recipient())

if uaType == "UAC" {
sip.CopyHeaders("Via", inviteRequest, byeRequest)
sip.CopyHeaders("Via", inviteRequest, newRequest)
if len(inviteRequest.GetHeaders("Route")) > 0 {
sip.CopyHeaders("Route", inviteRequest, byeRequest)
sip.CopyHeaders("Route", inviteRequest, newRequest)
}
} else if uaType == "UAS" {
sip.CopyHeaders("Via", inviteRequest, byeRequest)
sip.CopyHeaders("Via", inviteRequest, newRequest)
if len(inviteResponse.GetHeaders("Route")) > 0 {
sip.CopyHeaders("Route", inviteResponse, byeRequest)
sip.CopyHeaders("Route", inviteResponse, newRequest)
}
byeRequest.SetDestination(inviteResponse.Destination())
byeRequest.SetSource(inviteResponse.Source())
newRequest.SetDestination(inviteResponse.Destination())
newRequest.SetSource(inviteResponse.Source())
}

maxForwardsHeader := sip.MaxForwards(70)
byeRequest.AppendHeader(&maxForwardsHeader)
sip.CopyHeaders("Call-ID", inviteRequest, byeRequest)
sip.CopyHeaders("CSeq", inviteRequest, byeRequest)
cseq, _ := byeRequest.CSeq()
newRequest.AppendHeader(&maxForwardsHeader)
sip.CopyHeaders("Call-ID", inviteRequest, newRequest)
sip.CopyHeaders("CSeq", inviteRequest, newRequest)
cseq, _ := newRequest.CSeq()
cseq.SeqNo++
cseq.MethodName = method

return byeRequest
return newRequest
}
32 changes: 29 additions & 3 deletions pkg/stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ type SipStackConfig struct {
Extensions []string
MsgMapper sip.MessageMapper
ServerAuthManager ServerAuthManager
UserAgent string
}

// SipStack a golang SIP Stack
type SipStack struct {
config *SipStackConfig
listenPorts map[string]*sip.Port
tp transport.Layer
tx transaction.Layer
Expand Down Expand Up @@ -112,6 +114,7 @@ func NewSipStack(config *SipStackConfig, logger log.Logger) *SipStack {
}

s := &SipStack{
config: config,
listenPorts: make(map[string]*sip.Port),
host: host,
ip: ip,
Expand Down Expand Up @@ -241,6 +244,21 @@ func (s *SipStack) handleRequest(req sip.Request, tx sip.ServerTransaction) {
if !ok {
logger.Warnf("SIP request %v handler not found", req.Method())

go func(tx sip.ServerTransaction, logger log.Logger) {
for {
select {
case <-s.tx.Done():
return
case err, ok := <-tx.Errors():
if !ok {
return
}

logger.Warnf("error from SIP server transaction %s: %s", tx, err)
}
}
}(tx, logger)

res := sip.NewResponseFromRequest("", req, 405, "Method Not Allowed", "")
if _, err := s.Respond(res); err != nil {
logger.Errorf("respond '405 Method Not Allowed' failed: %s", err)
Expand All @@ -252,7 +270,7 @@ func (s *SipStack) handleRequest(req sip.Request, tx sip.ServerTransaction) {
if s.authenticator != nil {
authenticator := s.authenticator.Authenticator
requiresChallenge := s.authenticator.RequiresChallenge
if requiresChallenge(req) == true {
if requiresChallenge(req) {
go func() {
if _, ok := authenticator.Authenticate(req, tx); ok {
handler(req, tx)
Expand Down Expand Up @@ -493,8 +511,16 @@ func (s *SipStack) appendAutoHeaders(msg sip.Message) {
}

if hdrs := msg.GetHeaders("User-Agent"); len(hdrs) == 0 {
userAgent := sip.UserAgentHeader(DefaultUserAgent)
msg.AppendHeader(&userAgent)
userAgent := DefaultUserAgent
if len(s.config.UserAgent) > 0 {
userAgent = s.config.UserAgent
}
userAgentHeader := sip.UserAgentHeader(userAgent)
msg.AppendHeader(&userAgentHeader)
} else if len(s.config.UserAgent) > 0 {
msg.RemoveHeader("User-Agent")
userAgentHeader := sip.UserAgentHeader(s.config.UserAgent)
msg.AppendHeader(&userAgentHeader)
}

if s.tp.IsStreamed(msg.Transport()) {
Expand Down
8 changes: 2 additions & 6 deletions pkg/ua/ua.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import (

// UserAgentConfig .
type UserAgentConfig struct {
UserAgent string
SipStack *stack.SipStack
SipStack *stack.SipStack
}

//InviteSessionHandler .
Expand Down Expand Up @@ -93,16 +92,13 @@ func (ua *UserAgent) buildRequest(
builder.SetCallID(callID)
}

userAgent := sip.UserAgentHeader(ua.config.UserAgent)
builder.SetUserAgent(&userAgent)

req, err := builder.Build()
if err != nil {
ua.Log().Errorf("err => %v", err)
return nil, err
}

ua.Log().Infof("buildRequest %s => \n%v", method, req)
//ua.Log().Infof("buildRequest %s => \n%v", method, req)
return &req, nil
}

Expand Down

0 comments on commit 0c1b23c

Please sign in to comment.