Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
Conn net.PacketConn // Listening socket (net.PacketConn)
Net transport.Net
LoggerFactory logging.LoggerFactory

IgnoreTURNResolveErrors bool // TURN server address is not required for some configurations (e.g. proxy)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should add a config, there are many reason, but it feels like a single use flag.
What if we make a new constructor NewClientWithResolver and essentially make the nested condition after if len(config.TURNServerAddr) > 0 { customizable, we can then make the current NewClient call this new constructor with the old resolver logic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even better, we can use Pion options, pattern, and make NewClientWithOptions and make a custom option WithResolver.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can make a PR for this!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JoeTurki So you mean something like

if len(config.TURNServerAddr) > 0 {
	turnServ, err = config.ResolveTURN(config.TURNServerAddr)
	if err != nil {
		return nil, err
	}
}

And then in some cases use there WithResolveTURN(...) that will return nil, nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JoeTurki Any updates on this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amanakin hello, sorry, I didn't get a notification for this, do you still wanna merge this?

}

// Client is a STUN server client.
Expand Down Expand Up @@ -74,7 +76,7 @@

// NewClient returns a new Client instance. listeningAddress is the address and port to listen on,
// default "0.0.0.0:0".
func NewClient(config *ClientConfig) (*Client, error) {

Check failure on line 79 in client.go

View workflow job for this annotation

GitHub Actions / lint / Go

calculated cyclomatic complexity for function NewClient is 11, max is 10 (cyclop)
loggerFactory := config.LoggerFactory
if loggerFactory == nil {
loggerFactory = logging.NewDefaultLoggerFactory()
Expand Down Expand Up @@ -114,10 +116,13 @@
if len(config.TURNServerAddr) > 0 {
turnServ, err = config.Net.ResolveUDPAddr("udp4", config.TURNServerAddr)
if err != nil {
return nil, err
if !config.IgnoreTURNResolveErrors {
return nil, err
}
log.Debugf("Failed to resolve TURN server %s: %s", config.TURNServerAddr, err)
} else {
log.Debugf("Resolved TURN server %s to %s", config.TURNServerAddr, turnServ)

Check warning on line 124 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L119-L124

Added lines #L119 - L124 were not covered by tests
}

log.Debugf("Resolved TURN server %s to %s", config.TURNServerAddr, turnServ)
}

client := &Client{
Expand Down
24 changes: 24 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,27 @@ func TestTCPClientWithoutAddress(t *testing.T) {
assert.NoError(t, conn.Close())
assert.NoError(t, server.Close())
}

func TestClientTURNResolving(t *testing.T) {
listener, err := net.Listen("tcp4", "0.0.0.0:13478") //nolint: gosec
assert.NoError(t, err)

conn, err := net.Dial("tcp", "127.0.0.1:13478")
assert.NoError(t, err)

_, err = NewClient(&ClientConfig{
TURNServerAddr: "unresolvable.turn.server.address:13478",
Conn: NewSTUNConn(conn),
})
assert.Error(t, err)

_, err = NewClient(&ClientConfig{
TURNServerAddr: "unresolvable.turn.server.address:13478",
Conn: NewSTUNConn(conn),
IgnoreTURNResolveErrors: true,
})
assert.NoError(t, err)

assert.NoError(t, conn.Close())
assert.NoError(t, listener.Close())
}
Loading