forked from gortc/ice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent_option.go
101 lines (91 loc) · 2.08 KB
/
agent_option.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
package ice
import (
"strings"
"go.uber.org/zap"
"github.com/gortc/stun"
"github.com/gortc/turn"
)
// AgentOption represents configuration option for Agent.
type AgentOption func(a *Agent) error
// WithRole sets agent mode to Controlling or Controlled.
func WithRole(r Role) AgentOption {
return func(a *Agent) error {
a.role = r
return nil
}
}
// WithLogger sets *zap.Logger for Agent.
func WithLogger(l *zap.Logger) AgentOption {
return func(a *Agent) error {
a.log = l
return nil
}
}
// WithServer configures ICE server or servers for Agent.
func WithServer(servers ...Server) AgentOption {
return func(a *Agent) error {
for _, s := range servers {
for _, uri := range s.URI {
if strings.HasPrefix(uri, stun.Scheme) {
u, err := stun.ParseURI(uri)
if err != nil {
return err
}
a.stun = append(a.stun, stunServerOptions{
username: s.Username,
password: s.Credential,
uri: u,
})
} else {
u, err := turn.ParseURI(uri)
if err != nil {
return err
}
a.turn = append(a.turn, turnServerOptions{
username: s.Username,
password: s.Credential,
uri: u,
})
}
}
}
return nil
}
}
// WithSTUN configures Agent to use STUN server.
//
// Use WithServer to add STUN with credentials or multiple servers at once.
func WithSTUN(uri string) AgentOption {
return func(a *Agent) error {
u, err := stun.ParseURI(uri)
if err != nil {
return err
}
a.stun = append(a.stun, stunServerOptions{
uri: u,
})
return nil
}
}
// WithTURN configures Agent to use TURN server.
//
// Use WithServer to add multiple servers at once.
func WithTURN(uri, username, credential string) AgentOption {
return func(a *Agent) error {
u, err := turn.ParseURI(uri)
if err != nil {
return err
}
a.turn = append(a.turn, turnServerOptions{
password: credential,
username: username,
uri: u,
})
return nil
}
}
// WithIPv4Only enables IPv4-only mode, where IPv6 candidates are not used.
var WithIPv4Only AgentOption = func(a *Agent) error {
a.ipv4Only = true
return nil
}