From 9ebb7c76ea4102ee4150556b257dc043cdb87bb7 Mon Sep 17 00:00:00 2001 From: Sean DuBois Date: Tue, 27 Jun 2023 16:08:32 -0400 Subject: [PATCH] Partial revert of 79fbb4e3 This commit broke public API, this reverts those changes Resolves #339 --- client.go | 32 ++++++++-- client_test.go | 15 ++--- examples/turn-client/tcp-alloc/main.go | 4 +- examples/turn-client/tcp/main.go | 13 ++-- examples/turn-client/udp/main.go | 10 +-- lt_cred_test.go | 5 +- server_test.go | 87 ++------------------------ 7 files changed, 47 insertions(+), 119 deletions(-) diff --git a/client.go b/client.go index 7d3c9398..ece3096c 100644 --- a/client.go +++ b/client.go @@ -37,8 +37,8 @@ const ( // ClientConfig is a bag of config parameters for Client. type ClientConfig struct { - STUNServerAddr net.Addr // STUN server address - TURNServerAddr net.Addr // TURN server address + STUNServerAddr string // STUN server address (e.g. "stun.abc.com:3478") + TURNServerAddr string // TURN server address (e.g. "turn.abc.com:3478") Username string Password string Realm string @@ -98,10 +98,34 @@ func NewClient(config *ClientConfig) (*Client, error) { config.Net = n } + var ( + stunServ, turnServ net.Addr + stunServStr, turnServStr string + err error + ) + if len(config.STUNServerAddr) > 0 { + log.Debugf("resolving %s", config.STUNServerAddr) + stunServ, err = config.Net.ResolveUDPAddr("udp4", config.STUNServerAddr) + if err != nil { + return nil, err + } + stunServStr = stunServ.String() + log.Debugf("stunServ: %s", stunServStr) + } + if len(config.TURNServerAddr) > 0 { + log.Debugf("resolving %s", config.TURNServerAddr) + turnServ, err = config.Net.ResolveUDPAddr("udp4", config.TURNServerAddr) + if err != nil { + return nil, err + } + turnServStr = turnServ.String() + log.Debugf("turnServ: %s", turnServStr) + } + c := &Client{ conn: config.Conn, - stunServerAddr: config.STUNServerAddr, - turnServerAddr: config.TURNServerAddr, + stunServerAddr: stunServ, + turnServerAddr: turnServ, username: stun.NewUsername(config.Username), password: config.Password, realm: stun.NewRealm(config.Realm), diff --git a/client_test.go b/client_test.go index 1867898f..2da97629 100644 --- a/client_test.go +++ b/client_test.go @@ -41,10 +41,7 @@ func createListeningTestClientWithSTUNServ(t *testing.T, loggerFactory logging.L conn, err := net.ListenPacket("udp4", "0.0.0.0:0") assert.NoError(t, err) - addr, err := net.ResolveUDPAddr("udp", "stun1.l.google.com:19302") - if err != nil { - t.Fatalf("failed to resolve: %s", err) - } + addr := "stun1.l.google.com:19302" c, err := NewClient(&ClientConfig{ STUNServerAddr: addr, @@ -164,10 +161,8 @@ func TestClientNonceExpiration(t *testing.T) { conn, err := net.ListenPacket("udp4", "0.0.0.0:0") assert.NoError(t, err) - addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:3478") - if err != nil { - t.Fatalf("failed to resolve: %s", err) - } + // nolint: goconst + addr := "127.0.0.1:3478" client, err := NewClient(&ClientConfig{ Conn: conn, @@ -223,7 +218,7 @@ func TestTCPClient(t *testing.T) { conn, err := net.Dial("tcp", "127.0.0.1:13478") require.NoError(t, err) - serverAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:13478") + serverAddr := "127.0.0.1:13478" require.NoError(t, err) client, err := NewClient(&ClientConfig{ @@ -236,7 +231,7 @@ func TestTCPClient(t *testing.T) { require.NoError(t, err) require.NoError(t, client.Listen()) - require.Equal(t, serverAddr, client.STUNServerAddr()) + require.Equal(t, serverAddr, client.STUNServerAddr().String()) allocation, err := client.AllocateTCP() require.NoError(t, err) diff --git a/examples/turn-client/tcp-alloc/main.go b/examples/turn-client/tcp-alloc/main.go index 752cfa57..f6470671 100644 --- a/examples/turn-client/tcp-alloc/main.go +++ b/examples/turn-client/tcp-alloc/main.go @@ -96,8 +96,8 @@ func main() { // Start a new TURN Client and wrap our net.Conn in a STUNConn // This allows us to simulate datagram based communication over a net.Conn cfg := &turn.ClientConfig{ - STUNServerAddr: turnServerAddr, - TURNServerAddr: turnServerAddr, + STUNServerAddr: turnServerAddrStr, + TURNServerAddr: turnServerAddrStr, Conn: turn.NewSTUNConn(conn), Username: cred[0], Password: cred[1], diff --git a/examples/turn-client/tcp/main.go b/examples/turn-client/tcp/main.go index 70f834a7..e8a7a2e9 100644 --- a/examples/turn-client/tcp/main.go +++ b/examples/turn-client/tcp/main.go @@ -25,21 +25,16 @@ func main() { flag.Parse() if len(*host) == 0 { - log.Fatalf("Parameter 'host' is required") + log.Fatalf("'host' is required") } if len(*user) == 0 { - log.Fatalf("Parameter 'user' is required") + log.Fatalf("'user' is required") } // Dial TURN Server - hostPort := fmt.Sprintf("%s:%d", *host, *port) - turnServerAddr, err := net.ResolveTCPAddr("tcp", hostPort) - if err != nil { - log.Fatalf("Failed to resolve %s: %s", hostPort, err) - } - - conn, err := net.DialTCP("tcp", nil, turnServerAddr) + turnServerAddr := fmt.Sprintf("%s:%d", *host, *port) + conn, err := net.Dial("tcp", turnServerAddr) if err != nil { log.Panicf("Failed to connect to TURN server: %s", err) } diff --git a/examples/turn-client/udp/main.go b/examples/turn-client/udp/main.go index 148f3a5d..9ca0820f 100644 --- a/examples/turn-client/udp/main.go +++ b/examples/turn-client/udp/main.go @@ -34,14 +34,8 @@ func main() { cred := strings.SplitN(*user, "=", 2) - hostPort := fmt.Sprintf("%s:%d", *host, *port) - turnServerAddr, err := net.ResolveUDPAddr("udp", hostPort) - if err != nil { - log.Fatalf("Failed to resolve %s: %s", hostPort, err) - } - // TURN client won't create a local listening socket by itself. - conn, err := net.ListenUDP("udp", nil) + conn, err := net.ListenPacket("udp4", "0.0.0.0:0") if err != nil { log.Panicf("Failed to listen: %s", err) } @@ -51,6 +45,8 @@ func main() { } }() + turnServerAddr := fmt.Sprintf("%s:%d", *host, *port) + cfg := &turn.ClientConfig{ STUNServerAddr: turnServerAddr, TURNServerAddr: turnServerAddr, diff --git a/lt_cred_test.go b/lt_cred_test.go index f2192e4d..83164de1 100644 --- a/lt_cred_test.go +++ b/lt_cred_test.go @@ -54,10 +54,7 @@ func TestNewLongTermAuthHandler(t *testing.T) { username, password, err := GenerateLongTermCredentials(sharedSecret, time.Minute) assert.NoError(t, err) - addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:3478") - if err != nil { - t.Fatalf("failed to resolve: %s", err) - } + addr := "127.0.0.1:3478" client, err := NewClient(&ClientConfig{ STUNServerAddr: addr, diff --git a/server_test.go b/server_test.go index fc857f78..255f3e2f 100644 --- a/server_test.go +++ b/server_test.go @@ -152,8 +152,7 @@ func TestServer(t *testing.T) { conn, err := net.ListenPacket("udp4", "127.0.0.1:54321") assert.NoError(t, err) - addr, err := net.ResolveUDPAddr("udp4", "127.0.0.1:3478") - assert.NoError(t, err) + addr := "127.0.0.1:3478" client, err := NewClient(&ClientConfig{ STUNServerAddr: addr, @@ -433,7 +432,7 @@ func TestServerVNet(t *testing.T) { assert.NoError(t, lconn.Close()) }() - stunAddr, _ := net.ResolveUDPAddr("udp", "1.2.3.4:3478") + stunAddr := "1.2.3.4:3478" log.Debug("creating a client.") client, err := NewClient(&ClientConfig{ @@ -456,84 +455,6 @@ func TestServerVNet(t *testing.T) { // to the LAN router. assert.True(t, udpAddr.IP.Equal(net.IPv4(5, 6, 7, 8)), "should match") }) - - t.Run("Echo via relay", func(t *testing.T) { - v, err := buildVNet() - assert.NoError(t, err) - - lconn, err := v.netL0.ListenPacket("udp4", "0.0.0.0:0") - assert.NoError(t, err) - - stunAddr, _ := v.netL0.ResolveUDPAddr("udp", "stun.pion.ly:3478") - turnAddr, _ := v.netL0.ResolveUDPAddr("udp", "turn.pion.ly:3478") - - log.Debug("creating a client.") - client, err := NewClient(&ClientConfig{ - STUNServerAddr: stunAddr, - TURNServerAddr: turnAddr, - Username: "user", - Password: "pass", - Conn: lconn, - LoggerFactory: loggerFactory, - }) - - assert.NoError(t, err) - assert.NoError(t, client.Listen()) - - log.Debug("sending a binding request.") - conn, err := client.Allocate() - assert.NoError(t, err) - - log.Debugf("laddr: %s", conn.LocalAddr().String()) - - echoConn, err := v.net1.ListenPacket("udp4", "1.2.3.5:5678") - assert.NoError(t, err) - - // Ensure allocation is counted - assert.Equal(t, 1, v.server.AllocationCount()) - - go func() { - buf := make([]byte, 1600) - for { - n, from, err2 := echoConn.ReadFrom(buf) - if err2 != nil { - break - } - - // Verify the message was received from the relay address - assert.Equal(t, conn.LocalAddr().String(), from.String(), "should match") - assert.Equal(t, "Hello", string(buf[:n]), "should match") - - // Echo the data - _, err2 = echoConn.WriteTo(buf[:n], from) - assert.NoError(t, err2) - } - }() - - buf := make([]byte, 1600) - - for i := 0; i < 10; i++ { - log.Debug("sending \"Hello\"..") - _, err = conn.WriteTo([]byte("Hello"), echoConn.LocalAddr()) - assert.NoError(t, err) - - _, from, err2 := conn.ReadFrom(buf) - assert.NoError(t, err2) - - // Verify the message was received from the relay address - assert.Equal(t, echoConn.LocalAddr().String(), from.String(), "should match") - - time.Sleep(100 * time.Millisecond) - } - - time.Sleep(100 * time.Millisecond) - client.Close() - - assert.NoError(t, conn.Close(), "should succeed") - assert.NoError(t, echoConn.Close(), "should succeed") - assert.NoError(t, lconn.Close(), "should succeed") - assert.NoError(t, v.Close(), "should succeed") - }) } func TestConsumeSingleTURNFrame(t *testing.T) { @@ -636,8 +557,8 @@ func RunBenchmarkServer(b *testing.B, clientNum int) { defer clientConn.Close() //nolint:errcheck client, err := NewClient(&ClientConfig{ - STUNServerAddr: serverAddr, - TURNServerAddr: serverAddr, + STUNServerAddr: "127.0.0.1:3478", + TURNServerAddr: "127.0.0.1:3478", Conn: clientConn, Username: "user", Password: "pass",