-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathrelay_address_generator_static.go
More file actions
120 lines (99 loc) · 3.54 KB
/
Copy pathrelay_address_generator_static.go
File metadata and controls
120 lines (99 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package turn
import (
"context"
"fmt"
"net"
"strconv"
"github.com/pion/transport/v4"
"github.com/pion/transport/v4/reuseport"
"github.com/pion/transport/v4/stdnet"
)
// RelayAddressGeneratorStatic can be used to return static IP address each time a relay is created.
// This can be used when you have a single static IP address that you want to use.
type RelayAddressGeneratorStatic struct {
// RelayAddress is the IP returned to the user when the relay is created
RelayAddress net.IP
// Address is passed to Listen/ListenPacket when creating the Relay
Address string
Net transport.Net
}
// Validate is called on server startup and confirms the RelayAddressGenerator is properly configured.
func (r *RelayAddressGeneratorStatic) Validate() error {
if r.Net == nil {
var err error
r.Net, err = stdnet.NewNet()
if err != nil {
return fmt.Errorf("failed to create network: %w", err)
}
}
switch {
case r.RelayAddress == nil:
return errRelayAddressInvalid
case r.Address == "":
return errListeningAddressInvalid
default:
return nil
}
}
// AllocatePacketConn generates a new PacketConn to receive traffic on and the IP/Port
// to populate the allocation response with.
func (r *RelayAddressGeneratorStatic) AllocatePacketConn(
conf AllocateListenerConfig,
) (net.PacketConn, net.Addr, error) {
conn, err := r.Net.ListenPacket(conf.Network,
net.JoinHostPort(r.Address, strconv.Itoa(conf.RequestedPort))) // nolint: noctx
if err != nil {
return nil, nil, err
}
// Replace actual listening IP with the user requested one of RelayAddressGeneratorStatic
relayAddr, ok := conn.LocalAddr().(*net.UDPAddr)
if !ok {
return nil, nil, errNilConn
}
relayAddr.IP = r.RelayAddress
return conn, relayAddr, nil
}
// AllocateListener generates a new Listener to receive traffic on and the IP/Port
// to populate the allocation response with.
func (r *RelayAddressGeneratorStatic) AllocateListener(conf AllocateListenerConfig) (net.Listener, net.Addr, error) {
// AllocateListener can be called independently of Validate (e.g. in tests),
// so ensure we're initialized to avoid nil dereferences.
if r.Net == nil || r.Address == "" || r.RelayAddress == nil {
if err := r.Validate(); err != nil {
return nil, nil, err
}
}
tcpAddr, err := r.Net.ResolveTCPAddr(conf.Network, net.JoinHostPort(r.Address, strconv.Itoa(conf.RequestedPort)))
if err != nil {
return nil, nil, err
}
listenConfig := r.Net.CreateListenConfig(&net.ListenConfig{
// Enable SO_REUSEADDR and SO_REUSEPORT where needed to let multiple connnections
// bind to the same relay address.
Control: reuseport.Control,
})
ln, err := listenConfig.Listen(context.TODO(), conf.Network, tcpAddr.String())
if err != nil {
return nil, nil, err
}
// Replace actual listening IP with the user requested one of RelayAddressGeneratorStatic
relayAddr, ok := ln.Addr().(*net.TCPAddr)
if !ok {
_ = ln.Close()
return nil, nil, errNilConn
}
relayAddr.IP = r.RelayAddress
return ln, relayAddr, nil
}
// AllocateConn creates a new outgoing TCP connection bound to the relay address to send traffic to a peer.
func (r *RelayAddressGeneratorStatic) AllocateConn(conf AllocateConnConfig) (net.Conn, error) {
dialer := r.Net.CreateDialer(&net.Dialer{
LocalAddr: conf.LocalAddr,
// Enable SO_REUSEADDR and SO_REUSEPORT where needed to let multiple connnections
// bind to the same relay address.
Control: reuseport.Control,
})
return dialer.Dial(conf.Network, conf.RemoteAddr.String())
}