diff --git a/pkg/config/config.go b/pkg/config/config.go index 3ff6dcb8..a8529408 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -16,6 +16,7 @@ package config import ( "fmt" + "net" "os" "github.com/sirupsen/logrus" @@ -133,3 +134,19 @@ func (c *Config) GetLoggerFields() logrus.Fields { return fields } + +func GetLocalIP() (string, error) { + addrs, err := net.InterfaceAddrs() + if err != nil { + return "", nil + } + for _, address := range addrs { + if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { + if ipnet.IP.To4() != nil { + return ipnet.IP.String(), nil + } + } + } + + return "", fmt.Errorf("No local IP found") +} diff --git a/pkg/sip/service_test.go b/pkg/sip/service_test.go new file mode 100644 index 00000000..6aee3e8e --- /dev/null +++ b/pkg/sip/service_test.go @@ -0,0 +1,39 @@ +package sip + +import ( + "fmt" + "math/rand" + "testing" + + "github.com/livekit/mediatransportutil/pkg/rtcconfig" + "github.com/livekit/sip/pkg/config" + "github.com/stretchr/testify/require" +) + +const ( + testPortSIPMin = 30000 + testPortSIPMax = 30050 + + testPortRTPMin = 30100 + testPortRTPMax = 30150 +) + +// Test service E2E that +func TestService(t *testing.T) { + sipPort := rand.Intn(testPortSIPMax-testPortSIPMin) + testPortSIPMin + + s, err := NewService(&config.Config{ + SIPPort: sipPort, + RTPPort: rtcconfig.PortRange{Start: testPortRTPMin, End: testPortRTPMax}, + }) + require.NoError(t, err) + require.NotNil(t, s) + + s.SetAuthHandler(func(_, _, _, _ string) (string, string, error) { + return "", "", fmt.Errorf("Auth Failure") + }) + + require.NoError(t, s.Start()) + + s.Stop() +} diff --git a/test/client/main.go b/test/client/main.go index c58af721..a98f5fd7 100644 --- a/test/client/main.go +++ b/test/client/main.go @@ -29,6 +29,7 @@ import ( "github.com/emiago/sipgo" "github.com/emiago/sipgo/sip" "github.com/icholy/digest" + "github.com/livekit/sip/pkg/config" "github.com/livekit/sip/pkg/media/ulaw" "github.com/pion/rtp" "github.com/pion/sdp/v2" @@ -113,22 +114,6 @@ func startMediaListener() *net.UDPConn { return conn } -func getLocalIP() string { - addrs, err := net.InterfaceAddrs() - if err != nil { - panic(err) - } - for _, address := range addrs { - if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { - if ipnet.IP.To4() != nil { - return ipnet.IP.String() - } - } - } - - panic("No IP Found") -} - func getResponse(tx sip.ClientTransaction) *sip.Response { select { case <-tx.Done(): @@ -153,13 +138,13 @@ func createOffer(port int) ([]byte, error) { SessionVersion: sessionId, NetworkType: "IN", AddressType: "IP4", - UnicastAddress: getLocalIP(), + UnicastAddress: config.GetLocalIP(), }, SessionName: "LiveKit", ConnectionInformation: &sdp.ConnectionInformation{ NetworkType: "IN", AddressType: "IP4", - Address: &sdp.Address{Address: getLocalIP()}, + Address: &sdp.Address{Address: config.GetLocalIP()}, }, TimeDescriptions: []sdp.TimeDescription{ sdp.TimeDescription{ @@ -261,7 +246,7 @@ func attemptInvite(sipClient *sipgo.Client, offer []byte, authorizationHeaderVal inviteRequest.SetDestination(*sipServer) inviteRequest.SetBody(offer) inviteRequest.AppendHeader(sip.NewHeader("Content-Type", "application/sdp")) - inviteRequest.AppendHeader(sip.NewHeader("Contact", fmt.Sprintf("", getLocalIP()))) + inviteRequest.AppendHeader(sip.NewHeader("Contact", fmt.Sprintf("", config.GetLocalIP()))) inviteRequest.AppendHeader(sip.NewHeader("Allow", "INVITE, ACK, CANCEL, BYE, NOTIFY, REFER, MESSAGE, OPTIONS, INFO, SUBSCRIBE")) if authorizationHeaderValue != "" { @@ -281,7 +266,7 @@ func main() { flag.Parse() if *sipServer == "" { - *sipServer = getLocalIP() + ":5060" + *sipServer = config.GetLocalIP() + ":5060" } mediaConn := startMediaListener() @@ -297,7 +282,7 @@ func main() { panic(err) } - sipClient, err := sipgo.NewClient(ua, sipgo.WithClientHostname(getLocalIP())) + sipClient, err := sipgo.NewClient(ua, sipgo.WithClientHostname(config.GetLocalIP())) if err != nil { panic(err) }