-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
197 lines (166 loc) · 5.07 KB
/
index.js
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
/*
* RTMP Interceptor - @Taigah @Spartanz51
* RTMP Spec source:
* https://wwwimages2.adobe.com/content/dam/acom/en/devnet/rtmp/pdf/rtmp_specification_1.0.pdf
*/
const net = require('net')
const { once } = require('events')
const fs = require('fs').promises
class RTMPInterceptor {
constructor (listenPort) {
this.listenPort = listenPort
this.startService()
}
async startService() {
/*
* Load binaries replies
*/
const hsr = Buffer.from(await fs.readFile(__dirname+'/chunks/handshake.bin'))
const tcr = Buffer.from(await fs.readFile(__dirname+'/chunks/tcurl.bin'))
const c3r = Buffer.from(await fs.readFile(__dirname+'/chunks/c3.bin'))
const skr = Buffer.from(await fs.readFile(__dirname+'/chunks/skey.bin'))
this.binaryChunks = {
hsr: hsr,
tcr: tcr,
c3r: c3r,
skr: skr
}
this.server = net.createServer(client => { this.onstream(client) })
this.server.listen(this.listenPort)
}
async onstream(client) {
let server = null
this.bindClientEvents(client, server)
const hs = await this.handshake(client) /* RTMP handshake */
await client.write(this.binaryChunks.hsr)
const tc = await this.getTcUrl(client) /* Intercept tcUrl */
await client.write(this.binaryChunks.tcr)
const c3 = await this.c3(client) /* Intercept chunk3 */
await client.write(this.binaryChunks.c3r)
const sk = await this.getSKey(client, tc.tcUrl) /* Intercept Stream Key */
const payload = await this.ondata(client, tc.tcUrl, sk.streamKey)
if(payload) {
await client.write(this.binaryChunks.skr) /* Send confirmation to the client */
let tcChunks = tc.chunks
let skChunks = sk.chunks
if(payload.tcChunks){
tcChunks = payload.tcChunks
}
if(payload.skChunks){
skChunks = payload.skChunks
}
const chunks = hs.chunks
.concat(tcChunks)
.concat(c3.chunks)
.concat(skChunks)
server = await this.proxify(payload, chunks, client)
this.bindServerEvents(client, server)
}else{
await client.destroy()
}
}
bindClientEvents(client, server) {
client.on('close', ()=>{
client.destroy()
if(server) {
server.destroy()
}
if(client.onleave){
client.onleave()
}
})
client.on('error', ()=>{
client.destroy()
if(server) {
server.destroy()
}
})
}
bindServerEvents(client, server) {
server.on('close', () => {
client.destroy()
if(server) {
server.destroy()
}
})
server.on('error', err => {
client.destroy()
if(server) {
server.destroy()
}
})
}
async handshake (client) { /* WARN: Doesn't verify handshake integrity */
await once(client, 'readable')
const c0 = await once(client, 'data')
return {chunks: c0}
}
async c3 (client) {
await once(client, 'readable')
const c3 = await once(client, 'data')
return {chunks: c3}
}
async getTcUrl (client) {
let resultChunks = []
let tcUrl
await once(client, 'readable')
let chunks = await once(client, 'data')
while(!chunks.toString().replace(/[^\x20-\x7E]/g, '').includes('rtmp://')){
for (const chunk of chunks) { /* Send intercepted chunks */
resultChunks.push(chunk)
}
chunks = await once(client, 'data') /* Skip bad chunk */
}
for (const chunk of chunks) {
const matches = chunk.toString().match(/rtmp[^\0]+/)
if (tcUrl === undefined && matches) {
tcUrl = matches[0].replace(/\s/g, '')
}
}
if (tcUrl === undefined) { /* Verify tcUrl */
console.log('tcUrl not received')
client.destroy()
return
}
for (const chunk of chunks) { /* Send intercepted chunks */
resultChunks.push(chunk)
}
return {chunks: resultChunks, tcUrl: tcUrl}
}
async getSKey (client, tcUrl) {
let resultChunks = []
let chunks = await once(client, 'data')
while(!chunks.toString().replace(/[^\x20-\x7E]/g, '').includes('publish')){
for (const chunk of chunks) { /* Send intercepted chunks */
resultChunks.push(chunk)
}
chunks = await once(client, 'data') /* Skip bad chunk */
}
let streamKey
for (const chunk of chunks) {
const matches = chunk.toString().replace(/[^\x20-\x7E]/g, '').match(/publish[@]{0,1}(.+)live/)
if (matches) {
streamKey = matches[1].replace(/\s/g, '')
}
}
for (const chunk of chunks) {
resultChunks.push(chunk)
}
return {chunks: resultChunks, streamKey: streamKey}
}
async proxify(payload, chunks, client) {
const server = await net.createConnection(payload.port, payload.host)
for (let c of chunks) {
await server.write(c)
}
client.pipe(server)
server.pipe(client)
return server
}
async ondata() {}
}
function listen(payload, cb) {
const r = new RTMPInterceptor(payload.listenPort)
r.ondata = cb
}
module.exports = { listen }