This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
120 lines (96 loc) · 3.32 KB
/
config.go
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
package transportc
import (
"math/rand"
"net"
"time"
"github.com/pion/ice/v2"
"github.com/pion/webrtc/v3"
)
const (
MTU_DEFAULT = 1024
MAX_RECV_TIMEOUT_DEFAULT = time.Second * 10
)
// Config is the configuration for the Dialer and Listener.
type Config struct {
// ListenerDTLSRole defines the DTLS role when Listening.
// MUST be either DTLSRoleClient or DTLSRoleServer, as defined in RFC4347
// DTLSRoleClient will send the ClientHello and start the handshake.
// DTLSRoleServer will wait for the ClientHello.
ListenerDTLSRole DTLSRole
/**** OPTIONAL FIELDS ****/
// SignalMethod offers the automatic signaling when establishing the DataChannel.
SignalMethod SignalMethod
// IPs includes a slice of IP addresses and one single ICE Candidate Type.
// If set, will add these IPs as ICE Candidates
IPs *NAT1To1IPs
// PortRange is the range of ports to use for the DataChannel.
PortRange *PortRange
// UDPMux allows serving multiple DataChannels over the one or more pre-established UDP socket.
UDPMux ice.UDPMux
// CandidateNetworkTypes restricts ICE agent to gather
// on only selected types of networks.
CandidateNetworkTypes []webrtc.NetworkType
// InterfaceFilter restricts ICE agent to gather ICE candidates
// on only selected interfaces.
InterfaceFilter func(interfaceName string) (allowed bool)
}
// NewDialer creates a new Dialer from the given configuration.
func (c *Config) NewDialer(pConf *webrtc.Configuration) (*Dialer, error) {
settingEngine, err := c.BuildSettingEngine()
if err != nil {
return nil, err
}
return &Dialer{
SignalMethod: SignalMethodManual,
MTU: MTU_DEFAULT,
settingEngine: settingEngine,
configuration: pConf,
}, nil
}
// NewListener creates a new Listener from the given configuration.
func (c *Config) NewListener(pConf *webrtc.Configuration) (*Listener, error) {
settingEngine, err := c.BuildSettingEngine()
if err != nil {
return nil, err
}
settingEngine.SetAnsweringDTLSRole(c.ListenerDTLSRole) // ignore if any error
l := &Listener{
SignalMethod: SignalMethodManual,
MTU: MTU_DEFAULT,
MaxAcceptTimeout: MAX_RECV_TIMEOUT_DEFAULT,
runningStatus: LISTENER_NEW,
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
settingEngine: settingEngine,
configuration: pConf,
peerConnections: make(map[uint64]*webrtc.PeerConnection),
conns: make(chan net.Conn),
abortAccept: make(chan bool),
}
return l, nil
}
// BuildSettingEngine builds a SettingEngine from the configuration.
func (c *Config) BuildSettingEngine() (webrtc.SettingEngine, error) {
var settingEngine webrtc.SettingEngine = webrtc.SettingEngine{}
if c.IPs != nil {
settingEngine.SetNAT1To1IPs(c.IPs.IPs, c.IPs.Type)
}
if c.PortRange != nil {
err := settingEngine.SetEphemeralUDPPortRange(c.PortRange.Min, c.PortRange.Max)
if err != nil {
return webrtc.SettingEngine{}, err
}
}
if c.UDPMux != nil {
settingEngine.SetICEUDPMux(c.UDPMux)
}
if c.CandidateNetworkTypes != nil {
settingEngine.SetNetworkTypes(c.CandidateNetworkTypes)
}
if c.InterfaceFilter != nil {
settingEngine.SetInterfaceFilter(c.InterfaceFilter)
}
// GW: Making sure we will get a detached DataChannel as
// a datachannel.ReadWriteCloser upon datachannel.onOpen event.
settingEngine.DetachDataChannels()
return settingEngine, nil
}