This repository has been archived by the owner on May 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
agent_option.go
122 lines (110 loc) · 2.47 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package ice
import (
"errors"
"strings"
"time"
"go.uber.org/zap"
"gortc.io/stun"
"gortc.io/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
}
// WithTa sets Ta timer value which is technically time between candidates.
func WithTa(ta time.Duration) AgentOption {
return func(a *Agent) error {
if ta < 0 {
return errors.New("ta should be positive")
}
a.ta = ta
return nil
}
}
// WithMaxAttempts sets maximum attempts.
func WithMaxAttempts(n int) AgentOption {
return func(a *Agent) error {
a.maxAttempts = n
return nil
}
}