Skip to content

Commit b6c8547

Browse files
committed
Move more attributes to AllocationConfig
1 parent d73784e commit b6c8547

File tree

6 files changed

+36
-33
lines changed

6 files changed

+36
-33
lines changed

client.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,9 @@ func (c *Client) Allocate() (net.PacketConn, error) {
314314
relayedConn = client.NewUDPConn(&client.AllocationConfig{
315315
Client: c,
316316
RelayedAddr: relayedAddr,
317+
ServerAddr: c.turnServerAddr,
318+
Realm: c.realm,
319+
Username: c.username,
317320
Integrity: c.integrity,
318321
Nonce: nonce,
319322
Lifetime: lifetime.Duration,
@@ -350,6 +353,9 @@ func (c *Client) AllocateTCP() (*client.TCPAllocation, error) {
350353
allocation = client.NewTCPAllocation(&client.AllocationConfig{
351354
Client: c,
352355
RelayedAddr: relayedAddr,
356+
ServerAddr: c.turnServerAddr,
357+
Realm: c.realm,
358+
Username: c.username,
353359
Integrity: c.integrity,
354360
Nonce: nonce,
355361
Lifetime: lifetime.Duration,

internal/client/allocation.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ import (
2020
type AllocationConfig struct {
2121
Client Client
2222
RelayedAddr net.Addr
23+
ServerAddr net.Addr
2324
Integrity stun.MessageIntegrity
2425
Nonce stun.Nonce
26+
Username stun.Username
27+
Realm stun.Realm
2528
Lifetime time.Duration
2629
Net transport.Net
2730
Log logging.LeveledLogger
@@ -30,8 +33,11 @@ type AllocationConfig struct {
3033
type allocation struct {
3134
client Client // Read-only
3235
relayedAddr net.Addr // Read-only
36+
serverAddr net.Addr // Read-only
3337
permMap *permissionMap // Thread-safe
3438
integrity stun.MessageIntegrity // Read-only
39+
username stun.Username // Read-only
40+
realm stun.Realm // Read-only
3541
_nonce stun.Nonce // Needs mutex x
3642
_lifetime time.Duration // Needs mutex x
3743
net transport.Net // Thread-safe
@@ -58,8 +64,8 @@ func (a *allocation) refreshAllocation(lifetime time.Duration, dontWait bool) er
5864
stun.TransactionID,
5965
stun.NewType(stun.MethodRefresh, stun.ClassRequest),
6066
proto.Lifetime{Duration: lifetime},
61-
a.client.Username(),
62-
a.client.Realm(),
67+
a.username,
68+
a.realm,
6369
a.nonce(),
6470
a.integrity,
6571
stun.Fingerprint,
@@ -69,7 +75,7 @@ func (a *allocation) refreshAllocation(lifetime time.Duration, dontWait bool) er
6975
}
7076

7177
a.log.Debugf("send refresh request (dontWait=%v)", dontWait)
72-
trRes, err := a.client.PerformTransaction(msg, a.client.TURNServerAddr(), dontWait)
78+
trRes, err := a.client.PerformTransaction(msg, a.serverAddr, dontWait)
7379
if err != nil {
7480
return fmt.Errorf("%w: %s", errFailedToRefreshAllocation, err.Error())
7581
}

internal/client/client.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ import (
1212

1313
// Client is an interface for the public turn.Client in order to break cyclic dependencies
1414
type Client interface {
15-
TURNServerAddr() net.Addr
16-
Username() stun.Username
17-
Realm() stun.Realm
1815
WriteTo(data []byte, to net.Addr) (int, error)
1916
PerformTransaction(msg *stun.Message, to net.Addr, dontWait bool) (TransactionResult, error)
2017
OnDeallocated(relayedAddr net.Addr)

internal/client/tcp_alloc.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ func NewTCPAllocation(config *AllocationConfig) *TCPAllocation {
4242
allocation: allocation{
4343
client: config.Client,
4444
relayedAddr: config.RelayedAddr,
45+
serverAddr: config.ServerAddr,
46+
username: config.Username,
47+
realm: config.Realm,
4548
permMap: newPermissionMap(),
4649
integrity: config.Integrity,
4750
_nonce: config.Nonce,
@@ -81,8 +84,8 @@ func (a *TCPAllocation) Connect(peer net.Addr) (proto.ConnectionID, error) {
8184
stun.TransactionID,
8285
stun.NewType(stun.MethodConnect, stun.ClassRequest),
8386
addr2PeerAddress(peer),
84-
a.client.Username(),
85-
a.client.Realm(),
87+
a.username,
88+
a.realm,
8689
a.nonce(),
8790
a.integrity,
8891
stun.Fingerprint,
@@ -94,7 +97,7 @@ func (a *TCPAllocation) Connect(peer net.Addr) (proto.ConnectionID, error) {
9497
}
9598

9699
a.log.Debugf("Send connect request (peer=%v)", peer)
97-
trRes, err := a.client.PerformTransaction(msg, a.client.TURNServerAddr(), false)
100+
trRes, err := a.client.PerformTransaction(msg, a.serverAddr, false)
98101
if err != nil {
99102
return 0, err
100103
}
@@ -143,7 +146,7 @@ func (a *TCPAllocation) DialWithConn(conn net.Conn, network, rAddrStr string) (*
143146
// DialTCP acts like Dial for TCP networks.
144147
func (a *TCPAllocation) DialTCP(network string, lAddr, rAddr *net.TCPAddr) (*TCPConn, error) {
145148
var rAddrServer *net.TCPAddr
146-
if addr, ok := a.client.TURNServerAddr().(*net.TCPAddr); ok {
149+
if addr, ok := a.serverAddr.(*net.TCPAddr); ok {
147150
rAddrServer = &net.TCPAddr{
148151
IP: addr.IP,
149152
Port: addr.Port,
@@ -216,8 +219,8 @@ func (a *TCPAllocation) BindConnection(dataConn *TCPConn, cid proto.ConnectionID
216219
stun.TransactionID,
217220
stun.NewType(stun.MethodConnectionBind, stun.ClassRequest),
218221
cid,
219-
a.client.Username(),
220-
a.client.Realm(),
222+
a.username,
223+
a.realm,
221224
a.nonce(),
222225
a.integrity,
223226
stun.Fingerprint,
@@ -280,7 +283,7 @@ func (a *TCPAllocation) Accept() (net.Conn, error) {
280283

281284
// AcceptTCP accepts the next incoming call and returns the new connection.
282285
func (a *TCPAllocation) AcceptTCP() (transport.TCPConn, error) {
283-
addr, err := net.ResolveTCPAddr("tcp4", a.client.TURNServerAddr().String())
286+
addr, err := net.ResolveTCPAddr("tcp4", a.serverAddr.String())
284287
if err != nil {
285288
return nil, err
286289
}

internal/client/tcp_conn_test.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,6 @@ type dummyConnObserver struct {
5151
_onDeallocated func(relayedAddr net.Addr)
5252
}
5353

54-
func (obs *dummyConnObserver) TURNServerAddr() net.Addr {
55-
return obs.turnServerAddr
56-
}
57-
58-
func (obs *dummyConnObserver) Username() stun.Username {
59-
return obs.username
60-
}
61-
62-
func (obs *dummyConnObserver) Realm() stun.Realm {
63-
return obs.realm
64-
}
65-
6654
func (obs *dummyConnObserver) Net() transport.Net {
6755
if obs.net == nil {
6856
n, err := stdnet.NewNet()

internal/client/udp_conn.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@ func NewUDPConn(config *AllocationConfig) *UDPConn {
5050
allocation: allocation{
5151
client: config.Client,
5252
relayedAddr: config.RelayedAddr,
53+
serverAddr: config.ServerAddr,
5354
readTimer: time.NewTimer(time.Duration(math.MaxInt64)),
5455
permMap: newPermissionMap(),
56+
username: config.Username,
57+
realm: config.Realm,
5558
integrity: config.Integrity,
5659
_nonce: config.Nonce,
5760
_lifetime: config.Lifetime,
@@ -220,7 +223,7 @@ func (c *UDPConn) WriteTo(p []byte, addr net.Addr) (int, error) { //nolint: goco
220223

221224
// Indication has no transaction (fire-and-forget)
222225

223-
return c.client.WriteTo(msg.Raw, c.client.TURNServerAddr())
226+
return c.client.WriteTo(msg.Raw, c.serverAddr)
224227
}
225228

226229
// Binding is either ready
@@ -346,8 +349,8 @@ func (a *allocation) CreatePermissions(addrs ...net.Addr) error {
346349
}
347350

348351
setters = append(setters,
349-
a.client.Username(),
350-
a.client.Realm(),
352+
a.username,
353+
a.realm,
351354
a.nonce(),
352355
a.integrity,
353356
stun.Fingerprint)
@@ -357,7 +360,7 @@ func (a *allocation) CreatePermissions(addrs ...net.Addr) error {
357360
return err
358361
}
359362

360-
trRes, err := a.client.PerformTransaction(msg, a.client.TURNServerAddr(), false)
363+
trRes, err := a.client.PerformTransaction(msg, a.serverAddr, false)
361364
if err != nil {
362365
return err
363366
}
@@ -408,8 +411,8 @@ func (c *UDPConn) bind(b *binding) error {
408411
stun.NewType(stun.MethodChannelBind, stun.ClassRequest),
409412
addr2PeerAddress(b.addr),
410413
proto.ChannelNumber(b.number),
411-
c.client.Username(),
412-
c.client.Realm(),
414+
c.username,
415+
c.realm,
413416
c.nonce(),
414417
c.integrity,
415418
stun.Fingerprint,
@@ -420,7 +423,7 @@ func (c *UDPConn) bind(b *binding) error {
420423
return err
421424
}
422425

423-
trRes, err := c.client.PerformTransaction(msg, c.client.TURNServerAddr(), false)
426+
trRes, err := c.client.PerformTransaction(msg, c.serverAddr, false)
424427
if err != nil {
425428
c.bindingMgr.deleteByAddr(b.addr)
426429
return err
@@ -444,7 +447,7 @@ func (c *UDPConn) sendChannelData(data []byte, chNum uint16) (int, error) {
444447
Number: proto.ChannelNumber(chNum),
445448
}
446449
chData.Encode()
447-
_, err := c.client.WriteTo(chData.Raw, c.client.TURNServerAddr())
450+
_, err := c.client.WriteTo(chData.Raw, c.serverAddr)
448451
if err != nil {
449452
return 0, err
450453
}

0 commit comments

Comments
 (0)