-
Notifications
You must be signed in to change notification settings - Fork 0
/
bind.go
196 lines (160 loc) · 3.44 KB
/
bind.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//go:build ierr
package wgortc
import (
"errors"
"net"
"sync"
"github.com/pion/ice/v2"
"github.com/pion/webrtc/v3"
"github.com/shynome/wgortc/endpoint"
"github.com/shynome/wgortc/mux"
"github.com/shynome/wgortc/signaler"
"golang.zx2c4.com/wireguard/conn"
)
type Bind struct {
NewSettingEngine func() webrtc.SettingEngine
signaler.Channel
api *webrtc.API
mux ice.UDPMux
ICEServers []webrtc.ICEServer
msgCh chan packetMsg
closed bool
locker *sync.RWMutex
}
var _ conn.Bind = (*Bind)(nil)
func NewBind(signaler signaler.Channel) *Bind {
return &Bind{
Channel: signaler,
closed: false,
locker: &sync.RWMutex{},
}
}
func (b *Bind) Open(port uint16) (fns []conn.ReceiveFunc, actualPort uint16, ierr error) {
b.locker.Lock()
defer b.locker.Unlock()
fns = append(fns, b.receiveFunc)
b.msgCh = make(chan packetMsg, b.BatchSize()-1)
settingEngine := webrtc.SettingEngine{}
if b.NewSettingEngine != nil {
settingEngine = b.NewSettingEngine()
}
if mux.WithUDPMux != nil {
b.mux, ierr = mux.WithUDPMux(&settingEngine, &port)
actualPort = port
}
b.api = webrtc.NewAPI(webrtc.WithSettingEngine(settingEngine))
ch, ierr := b.Accept()
go func() {
for ev := range ch {
go b.handleConnect(ev)
}
}()
b.closed = false
return
}
type packetMsg struct {
data []byte
ep conn.Endpoint
}
func (b *Bind) receiveFunc(packets [][]byte, sizes []int, eps []conn.Endpoint) (n int, err error) {
if b.isClosed() {
return 0, net.ErrClosed
}
for i := 0; i < b.BatchSize(); i++ {
msg, ok := <-b.msgCh
if !ok {
return 0, net.ErrClosed
}
sizes[i] = copy(packets[i], msg.data)
eps[i] = msg.ep
n += 1
}
return
}
func (b *Bind) handleConnect(sess signaler.Session) {
var ierr error
_ = ierr
config := webrtc.Configuration{
ICEServers: b.ICEServers,
}
pc, ierr := b.api.NewPeerConnection(config)
defer pc.Close()
inbound := endpoint.NewInbound(sess, pc)
initiator, ierr := inbound.ExtractInitiator()
b.msgCh <- packetMsg{
data: initiator,
ep: inbound,
}
ch := inbound.Message()
for d := range ch {
if b.isClosed() {
break
}
b.msgCh <- packetMsg{
data: d,
ep: inbound,
}
}
return
}
func (b *Bind) isClosed() bool {
b.locker.RLock()
defer b.locker.RUnlock()
return b.closed
}
func (b *Bind) Close() (ierr error) {
b.locker.Lock()
defer b.locker.Unlock()
b.closed = true
if b.mux != nil {
ierr = b.mux.Close()
}
if b.Channel != nil {
ierr = b.Channel.Close()
}
if b.msgCh != nil {
close(b.msgCh)
}
return
}
func (b *Bind) ParseEndpoint(s string) (ep conn.Endpoint, err error) {
outbound := endpoint.NewOutbound(s, b)
go func() {
ch := outbound.Message()
for d := range ch {
if b.isClosed() {
break
}
b.msgCh <- packetMsg{
data: d,
ep: outbound,
}
}
}()
return outbound, nil
}
var _ endpoint.Hub = (*Bind)(nil)
func (b *Bind) NewPeerConnection() (*webrtc.PeerConnection, error) {
config := webrtc.Configuration{
ICEServers: b.ICEServers,
}
return b.api.NewPeerConnection(config)
}
func (b *Bind) Send(bufs [][]byte, ep conn.Endpoint) (err error) {
if b.isClosed() {
return net.ErrClosed
}
sender, ok := ep.(endpoint.Sender)
if !ok {
return ErrEndpointImpl
}
for _, buf := range bufs {
if err := sender.Send(buf); err != nil {
return err
}
}
return nil
}
var ErrEndpointImpl = errors.New("endpoint is not wgortc.Endpoint")
func (b *Bind) SetMark(mark uint32) error { return nil }
func (b *Bind) BatchSize() int { return 1 }