-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathsession.go
289 lines (238 loc) · 7.13 KB
/
session.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package srtp
import (
"bytes"
"fmt"
"io"
"net"
"sync"
"github.com/pions/webrtc/pkg/rtcp"
"github.com/pions/webrtc/pkg/rtp"
)
// SessionSRTP implements io.ReadWriteCloser and provides a bi-directional SRTP session
// SRTP itself does not have a design like this, but it is common in most applications
// for local/remote to each have their own keying material. This provides those patterns
// instead of making everyone re-implement
type SessionSRTP struct {
session
writeStream *WriteStream
}
// CreateSessionSRTP creates a new SessionSRTP
func CreateSessionSRTP() *SessionSRTP {
s := &SessionSRTP{}
s.writeStream = &WriteStream{s}
s.session.initalize()
return s
}
// Start initializes any crypto context and allows reading/writing to begin
func (s *SessionSRTP) Start(localMasterKey, localMasterSalt, remoteMasterKey, remoteMasterSalt []byte, profile ProtectionProfile, nextConn net.Conn) error {
s.session.nextConn = nextConn
return s.session.start(localMasterKey, localMasterSalt, remoteMasterKey, remoteMasterSalt, profile, s)
}
// OpenWriteStream returns the global write stream for the Session
func (s *SessionSRTP) OpenWriteStream() (*WriteStream, error) {
return s.writeStream, nil
}
// OpenReadStream opens a read stream for the given SSRC, it can be used
// if you want a certain SSRC, but don't want to wait for AcceptStream
func (s *SessionSRTP) OpenReadStream(SSRC uint32) (*ReadStream, error) {
r, _ := s.session.getOrCreateReadStream(SSRC, s)
return r, nil
}
// AcceptStream returns a stream to handle RTCP for a single SSRC
func (s *SessionSRTP) AcceptStream() (*ReadStream, uint32, error) {
stream, ok := <-s.newStream
if !ok {
return nil, 0, fmt.Errorf("SessionSRTP has been closed")
}
return stream, stream.GetSSRC(), nil
}
// Close ends the session
func (s *SessionSRTP) Close() error {
return nil
}
func (s *SessionSRTP) write(buf []byte) (int, error) {
_, ok := <-s.session.started
if ok {
return 0, fmt.Errorf("started channel used incorrectly, should only be closed")
}
s.session.localContextMutex.Lock()
defer s.session.localContextMutex.Unlock()
encrypted, err := s.localContext.EncryptRTP(nil, buf, nil)
if err != nil {
return 0, err
}
return s.session.nextConn.Write(encrypted)
}
func (s *SessionSRTP) decrypt(buf []byte) error {
decrypted, err := s.remoteContext.DecryptRTP(nil, buf, nil)
if err != nil {
return err
}
p := &rtp.Packet{}
if err := p.Unmarshal(decrypted); err != nil {
return err
}
r, isNew := s.session.getOrCreateReadStream(p.SSRC, s)
if r == nil {
return nil // Session has been closed
} else if isNew {
s.session.newStream <- r // Notify AcceptStream
}
r.decrypted <- decrypted
return nil
}
// SessionSRTCP implements io.ReadWriteCloser and provides a bi-directional SRTP session
// SRTP itself does not have a design like this, but it is common in most applications
// for local/remote to each have their own keying material. This provides those patterns
// instead of making everyone re-implement
type SessionSRTCP struct {
session
writeStream *WriteStream
}
// CreateSessionSRTCP creates a new SessionSRTCP
func CreateSessionSRTCP() *SessionSRTCP {
s := &SessionSRTCP{}
s.writeStream = &WriteStream{s}
s.session.initalize()
return s
}
// Start initializes any crypto context and allows reading/writing to begin
func (s *SessionSRTCP) Start(localMasterKey, localMasterSalt, remoteMasterKey, remoteMasterSalt []byte, profile ProtectionProfile, nextConn net.Conn) error {
s.session.nextConn = nextConn
return s.session.start(localMasterKey, localMasterSalt, remoteMasterKey, remoteMasterSalt, profile, s)
}
// OpenWriteStream returns the global write stream for the Session
func (s *SessionSRTCP) OpenWriteStream() (*WriteStream, error) {
return s.writeStream, nil
}
// OpenReadStream opens a read stream for the given SSRC, it can be used
// if you want a certain SSRC, but don't want to wait for AcceptStream
func (s *SessionSRTCP) OpenReadStream(SSRC uint32) (*ReadStream, error) {
r, _ := s.session.getOrCreateReadStream(SSRC, s)
return r, nil
}
// AcceptStream returns a stream to handle RTCP for a single SSRC
func (s *SessionSRTCP) AcceptStream() (*ReadStream, uint32, error) {
stream, ok := <-s.newStream
if !ok {
return nil, 0, fmt.Errorf("SessionSRTP has been closed")
}
return stream, stream.GetSSRC(), nil
}
// Close ends the session
func (s *SessionSRTCP) Close() error {
return nil
}
func (s *SessionSRTCP) write(buf []byte) (int, error) {
_, ok := <-s.session.started
if ok {
return 0, fmt.Errorf("started channel used incorrectly, should only be closed")
}
s.session.localContextMutex.Lock()
defer s.session.localContextMutex.Unlock()
encrypted, err := s.localContext.EncryptRTCP(nil, buf, nil)
if err != nil {
return 0, err
}
return s.session.nextConn.Write(encrypted)
}
func (s *SessionSRTCP) decrypt(buf []byte) error {
decrypted, err := s.remoteContext.DecryptRTCP(nil, buf, nil)
if err != nil {
return err
}
compoundPacket := rtcp.NewReader(bytes.NewReader(decrypted))
for {
_, rawrtcp, err := compoundPacket.ReadPacket()
if err != nil {
if err == io.EOF {
return nil
}
return err
}
var report rtcp.Packet
report, _, err = rtcp.Unmarshal(rawrtcp)
if err != nil {
return err
}
for _, ssrc := range report.DestinationSSRC() {
r, isNew := s.session.getOrCreateReadStream(ssrc, s)
if r == nil {
return nil // Session has been closed
} else if isNew {
s.session.newStream <- r // Notify AcceptStream
}
r.decrypted <- decrypted
}
}
}
/*
Private
*/
type session struct {
localContextMutex sync.Mutex
localContext, remoteContext *Context
newStream chan *ReadStream
started chan interface{}
readStreamsClosed bool
readStreams map[uint32]*ReadStream
readStreamsLock sync.Mutex
nextConn net.Conn
}
func (s *session) getOrCreateReadStream(ssrc uint32, child streamSession) (*ReadStream, bool) {
s.readStreamsLock.Lock()
defer s.readStreamsLock.Unlock()
if s.readStreamsClosed {
return nil, false
}
isNew := false
r, ok := s.readStreams[ssrc]
if !ok {
r = &ReadStream{s: child, decrypted: make(chan []byte), ssrc: ssrc}
s.readStreams[ssrc] = r
isNew = true
}
return r, isNew
}
func (s *session) initalize() {
s.readStreams = map[uint32]*ReadStream{}
s.newStream = make(chan *ReadStream)
s.started = make(chan interface{})
}
func (s *session) start(localMasterKey, localMasterSalt, remoteMasterKey, remoteMasterSalt []byte, profile ProtectionProfile, child streamSession) error {
var err error
s.localContext, err = CreateContext(localMasterKey, localMasterSalt, profile)
if err != nil {
return err
}
s.remoteContext, err = CreateContext(remoteMasterKey, remoteMasterSalt, profile)
if err != nil {
return err
}
go func() {
defer func() {
close(s.newStream)
s.readStreamsLock.Lock()
s.readStreamsClosed = true
for _, r := range s.readStreams {
close(r.decrypted)
}
s.readStreamsLock.Unlock()
}()
b := make([]byte, 8192)
for {
var i int
i, err = s.nextConn.Read(b)
if err != nil {
fmt.Println(err)
return
}
if err = child.decrypt(b[:i]); err != nil {
fmt.Println(err)
return
}
}
}()
close(s.started)
return nil
}