-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtc.go
240 lines (198 loc) · 5.66 KB
/
rtc.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
package rtc
import (
"io"
"log"
"net"
"strconv"
"sync"
"time"
"github.com/pion/datachannel"
"github.com/pion/logging"
"github.com/pion/sctp"
"github.com/pion/turn/v2"
)
// Add:
// - TURN - seems a much simpler and broader mechanism to create remote listeners
// TCP is problematic, but QUIC seems possible.
// STUN/TURN/ICE - dealing with NATs and multi-network, alternative to SNI sniffing
// WebRTC data channels
// Protocols:
// - RTP/3550 - not used, WebRTC requires SRTP/3711
// - SRTCP - control portocol
// - SRTP null or AES-counter mode.
// - HMAC-SHA1 (160 bit truncated to 80 or 32)
// - ZRTP/6189 and MIKEY for master key
//
// DialDTLS uses low-level dtls.
func DialDTLS(dest string) (io.ReadWriteCloser, error){
a1, err := sctp.Client(sctp.Config{})
if err != nil {
return nil, err
}
return datachannel.Dial(a1, 1, &datachannel.Config{})
}
// DialSCTP uses low-level SCTP to connect to an address.
func DialSCTP() (io.ReadWriteCloser, error){
a1, err := sctp.Client(sctp.Config{})
if err != nil {
return nil, err
}
return datachannel.Dial(a1, 1, &datachannel.Config{})
}
// InitTURN creates a TURN server for the connected mesh nodes.
// By default, it will only forward to the mesh.
//
// TURN uses a username(max 513B) and 'message integrity code'
// Username is included in each message, followed by a HMAC-SHA1 using
// username:realm:pass
//
// If realm is set, server will send a nonce and realm, which will be included
// For short term, the key is the pass.
// This is the last field ( maybe followed by fingerprint)
//
// First request doesn't include user/nonce.
//
// Responses use the same password, and are verified ( but not encrypted - DTLS is used on top)
func InitTURN(publicIP string) error {
tcpListener, err := net.Listen("tcp4", "0.0.0.0:"+strconv.Itoa(3478))
if err != nil {
return err
}
udpListener, err := net.ListenPacket("udp4", "0.0.0.0:"+strconv.Itoa(3478))
if err != nil {
return err
}
ts, err := turn.NewServer(turn.ServerConfig{
Realm: "h.webinf.info",
ChannelBindTimeout: 10 * time.Minute,
// Set AuthHandler callback
// This is called everytime a user tries to authenticate with the TURN server
// Return the key for that user, or false when no user is found
AuthHandler: func(username string, realm string, srcAddr net.Addr) ([]byte, bool) {
if true {
return []byte("key"), true
}
return nil, false
},
// PacketConnConfigs is a list of UDP Listeners and the configuration around them
PacketConnConfigs: []turn.PacketConnConfig{
{
PacketConn: udpListener,
RelayAddressGenerator: &turn.RelayAddressGeneratorStatic{
RelayAddress: net.ParseIP(publicIP), // Claim that we are listening on IP passed by user (This should be your Public IP)
Address: "0.0.0.0", // But actually be listening on every interface
},
},
}, // ListenerConfig is a list of Listeners and the configuration around them
ListenerConfigs: []turn.ListenerConfig{
{
Listener: tcpListener,
RelayAddressGenerator: &turn.RelayAddressGeneratorStatic{
RelayAddress: net.ParseIP(publicIP),
Address: "0.0.0.0",
},
},
},
})
if err != nil {
return err
}
log.Println("TURN: ", ts)
return nil
}
// Blocking server.
// Issue: NetConn for a server needs to 'latch' to a port, to emulate a connection. Yet we want to use
// the same port on server side.
func (r *RTC) InitSCTPS(port int) error {
addr := net.UDPAddr{
IP: net.IPv4(0, 0, 0, 0),
Port: port,
}
conn, err := net.ListenUDP("udp", &addr)
if err != nil {
return err
}
defer conn.Close()
config := sctp.Config{
NetConn: &disconnectedPacketConn{pConn: conn},
LoggerFactory: logging.NewDefaultLoggerFactory(),
}
a, err := sctp.Server(config)
if err != nil {
return err
}
s, err := a.AcceptStream()
s.SetReliabilityParams(true, sctp.ReliabilityTypeTimed, 10)
s.WriteSCTP([]byte("hi"), sctp.PayloadTypeWebRTCBinary)
s.Write([]byte{1})
return nil
}
func (r *RTC) InitSCTP(addr string) error {
conn, err := net.Dial("udp", "127.0.0.1:5678")
if err != nil {
log.Panic(err)
}
defer conn.Close()
config := sctp.Config{
NetConn: conn,
LoggerFactory: logging.NewDefaultLoggerFactory(),
}
a, err := sctp.Client(config)
if err != nil {
return err
}
s, err := a.OpenStream(0, sctp.PayloadTypeWebRTCBinary)
s.SetReliabilityParams(true, sctp.ReliabilityTypeTimed, 10)
s.WriteSCTP([]byte("hi"), sctp.PayloadTypeWebRTCBinary)
s.Write([]byte{1})
return nil
}
type disconnectedPacketConn struct { // nolint: unused
mu sync.RWMutex
rAddr net.Addr
pConn net.PacketConn
}
// Read
func (c *disconnectedPacketConn) Read(p []byte) (int, error) {
i, rAddr, err := c.pConn.ReadFrom(p)
if err != nil {
return 0, err
}
c.mu.Lock()
c.rAddr = rAddr
c.mu.Unlock()
return i, err
}
// Write writes len(p) bytes from p to the DTLS connection
func (c *disconnectedPacketConn) Write(p []byte) (n int, err error) {
return c.pConn.WriteTo(p, c.RemoteAddr())
}
// Close closes the conn and releases any Read calls
func (c *disconnectedPacketConn) Close() error {
return c.pConn.Close()
}
// LocalAddr is a stub
func (c *disconnectedPacketConn) LocalAddr() net.Addr {
if c.pConn != nil {
return c.pConn.LocalAddr()
}
return nil
}
// RemoteAddr is a stub
func (c *disconnectedPacketConn) RemoteAddr() net.Addr {
c.mu.RLock()
defer c.mu.RUnlock()
return c.rAddr
}
// SetDeadline is a stub
func (c *disconnectedPacketConn) SetDeadline(t time.Time) error {
return nil
}
// SetReadDeadline is a stub
func (c *disconnectedPacketConn) SetReadDeadline(t time.Time) error {
return nil
}
// SetWriteDeadline is a stub
func (c *disconnectedPacketConn) SetWriteDeadline(t time.Time) error {
return nil
}