From 5628a6038852e880c092077ac4c1671ec41c74f2 Mon Sep 17 00:00:00 2001 From: Denys Smirnov Date: Fri, 24 Nov 2023 17:22:18 +0200 Subject: [PATCH] Share SIP user agent. --- cmd/server/main.go | 12 ++++++++++-- pkg/sip/client.go | 19 +++++++++++-------- pkg/sip/config.go | 9 ++++----- pkg/sip/inbound.go | 2 +- pkg/sip/server.go | 23 +++++++++++++---------- test/client/main.go | 5 ++--- 6 files changed, 41 insertions(+), 29 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index bc462d0a..14cfdf5a 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -21,6 +21,7 @@ import ( "os/signal" "syscall" + "github.com/emiago/sipgo" "github.com/urfave/cli/v2" "github.com/livekit/protocol/logger" @@ -84,15 +85,22 @@ func runService(c *cli.Context) error { killChan := make(chan os.Signal, 1) signal.Notify(killChan, syscall.SIGINT) + ua, err := sipgo.NewUA( + sipgo.WithUserAgent(sip.UserAgent), + ) + if err != nil { + return err + } + sipCli := sip.NewClient(conf) - if err = sipCli.Start(); err != nil { + if err = sipCli.Start(ua); err != nil { return err } svc := service.NewService(conf, sipCli, psrpcClient, bus) sipSrv := sip.NewServer(conf, svc.HandleTrunkAuthentication, svc.HandleDispatchRules) - if err = sipSrv.Start(); err != nil { + if err = sipSrv.Start(ua); err != nil { return err } diff --git a/pkg/sip/client.go b/pkg/sip/client.go index 8f5742ae..bb6cfb84 100644 --- a/pkg/sip/client.go +++ b/pkg/sip/client.go @@ -46,15 +46,18 @@ func NewClient(conf *config.Config) *Client { return c } -func (c *Client) Start() error { - ua, err := sipgo.NewUA( - sipgo.WithUserAgent(userAgent), - ) - if err != nil { - return err +func (c *Client) Start(agent *sipgo.UserAgent) error { + if agent == nil { + ua, err := sipgo.NewUA( + sipgo.WithUserAgent(UserAgent), + ) + if err != nil { + return err + } + agent = ua } - - c.sipCli, err = sipgo.NewClient(ua, sipgo.WithClientHostname(c.publicIp)) + var err error + c.sipCli, err = sipgo.NewClient(agent, sipgo.WithClientHostname(c.publicIp)) if err != nil { return err } diff --git a/pkg/sip/config.go b/pkg/sip/config.go index 6a7e0439..e31becf7 100644 --- a/pkg/sip/config.go +++ b/pkg/sip/config.go @@ -17,31 +17,30 @@ package sip import ( "encoding/json" "io" - "log" "net/http" ) func getPublicIP() string { req, err := http.Get("http://ip-api.com/json/") if err != nil { - log.Fatal(err) + panic(err) } defer req.Body.Close() body, err := io.ReadAll(req.Body) if err != nil { - log.Fatal(err) + panic(err) } ip := struct { Query string }{} if err = json.Unmarshal(body, &ip); err != nil { - log.Fatal(err) + panic(err) } if ip.Query == "" { - log.Fatal("Query entry was not populated") + panic("Query entry was not populated") } return ip.Query diff --git a/pkg/sip/inbound.go b/pkg/sip/inbound.go index 51beabe2..72f9ef1a 100644 --- a/pkg/sip/inbound.go +++ b/pkg/sip/inbound.go @@ -53,7 +53,7 @@ func (s *Server) handleInviteAuth(req *sip.Request, tx sip.ServerTransaction, fr h := req.GetHeader("Proxy-Authorization") if h == nil { inviteState.challenge = digest.Challenge{ - Realm: userAgent, + Realm: UserAgent, Nonce: fmt.Sprintf("%d", time.Now().UnixMicro()), Algorithm: "MD5", } diff --git a/pkg/sip/server.go b/pkg/sip/server.go index 82c6c875..aac3ba73 100644 --- a/pkg/sip/server.go +++ b/pkg/sip/server.go @@ -29,7 +29,7 @@ import ( ) const ( - userAgent = "LiveKit" + UserAgent = "LiveKit" digestLimit = 500 ) @@ -104,17 +104,20 @@ func logOnError(err error) { } } -func (s *Server) Start() error { - ua, err := sipgo.NewUA( - sipgo.WithUserAgent(userAgent), - ) - if err != nil { - log.Fatal(err) +func (s *Server) Start(agent *sipgo.UserAgent) error { + if agent == nil { + ua, err := sipgo.NewUA( + sipgo.WithUserAgent(UserAgent), + ) + if err != nil { + return err + } + agent = ua } - - s.sipSrv, err = sipgo.NewServer(ua) + var err error + s.sipSrv, err = sipgo.NewServer(agent) if err != nil { - log.Fatal(err) + return err } s.sipSrv.OnInvite(s.onInvite) diff --git a/test/client/main.go b/test/client/main.go index 0c79d217..8b6d2b93 100644 --- a/test/client/main.go +++ b/test/client/main.go @@ -17,7 +17,6 @@ package main import ( "flag" "fmt" - "log" "math/rand" "net" "os" @@ -235,12 +234,12 @@ func main() { sipgo.WithUserAgent(*from), ) if err != nil { - log.Fatal(err) + panic(err) } sipClient, err := sipgo.NewClient(ua, sipgo.WithClientHostname(getLocalIP())) if err != nil { - log.Fatal(err) + panic(err) } var (