From 0c1b23cabfd581ebcd64371965b45214fed54f5c Mon Sep 17 00:00:00 2001 From: cloudwebrtc Date: Tue, 30 Mar 2021 08:41:47 +0800 Subject: [PATCH] Move UserAgent setting to SipStack, and add original recipient to session.makeRequest. --- examples/b2bua/b2bua/b2bua.go | 5 +++-- examples/client/main.go | 15 +++++++++------ examples/register/main.go | 8 +++++--- go.sum | 9 +-------- pkg/session/session.go | 29 +++++++++++++++-------------- pkg/stack/stack.go | 32 +++++++++++++++++++++++++++++--- pkg/ua/ua.go | 8 ++------ 7 files changed, 64 insertions(+), 42 deletions(-) diff --git a/examples/b2bua/b2bua/b2bua.go b/examples/b2bua/b2bua/b2bua.go index 7ffbab3..d6e6a24 100644 --- a/examples/b2bua/b2bua/b2bua.go +++ b/examples/b2bua/b2bua/b2bua.go @@ -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{ @@ -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) { diff --git a/examples/client/main.go b/examples/client/main.go index 1db8811..018ca69 100644 --- a/examples/client/main.go +++ b/examples/client/main.go @@ -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) @@ -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) { @@ -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", @@ -104,7 +106,7 @@ func main() { stack, ) - recipient, err := parser.ParseSipUri("sip:100@127.0.0.1:5060;transport=udp") + recipient, err := parser.ParseSipUri("sip:100@127.0.0.1:5081;transport=wss") if err != nil { logger.Error(err) } @@ -121,10 +123,11 @@ func main() { logger.Error(err) } - recipient, err = parser.ParseSipUri("sip:400@127.0.0.1:5060;transport=udp") + recipient, err = parser.ParseSipUri("sip:400@127.0.0.1:5081;transport=wss") if err != nil { logger.Error(err) } + go ua.Invite(profile, called, recipient, &sdp) <-stop diff --git a/examples/register/main.go b/examples/register/main.go index 7d3e21d..8213a73 100644 --- a/examples/register/main.go +++ b/examples/register/main.go @@ -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) { diff --git a/go.sum b/go.sum index 6263a80..2b77aea 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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= @@ -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= diff --git a/pkg/session/session.go b/pkg/session/session.go index da5ee92..070e60e 100644 --- a/pkg/session/session.go +++ b/pkg/session/session.go @@ -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, @@ -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 } diff --git a/pkg/stack/stack.go b/pkg/stack/stack.go index d9fe561..98ce276 100644 --- a/pkg/stack/stack.go +++ b/pkg/stack/stack.go @@ -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 @@ -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, @@ -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) @@ -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) @@ -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()) { diff --git a/pkg/ua/ua.go b/pkg/ua/ua.go index 64bc08f..65f3d2b 100644 --- a/pkg/ua/ua.go +++ b/pkg/ua/ua.go @@ -19,8 +19,7 @@ import ( // UserAgentConfig . type UserAgentConfig struct { - UserAgent string - SipStack *stack.SipStack + SipStack *stack.SipStack } //InviteSessionHandler . @@ -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 }