Skip to content

Commit

Permalink
Share SIP user agent.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed Nov 29, 2023
1 parent 8ad35d7 commit 5628a60
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 29 deletions.
12 changes: 10 additions & 2 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os/signal"
"syscall"

"github.com/emiago/sipgo"
"github.com/urfave/cli/v2"

"github.com/livekit/protocol/logger"
Expand Down Expand Up @@ -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
}

Expand Down
19 changes: 11 additions & 8 deletions pkg/sip/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 4 additions & 5 deletions pkg/sip/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/sip/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
Expand Down
23 changes: 13 additions & 10 deletions pkg/sip/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

const (
userAgent = "LiveKit"
UserAgent = "LiveKit"
digestLimit = 500
)

Expand Down Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions test/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package main
import (
"flag"
"fmt"
"log"
"math/rand"
"net"
"os"
Expand Down Expand Up @@ -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 (
Expand Down

0 comments on commit 5628a60

Please sign in to comment.