-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
287 lines (272 loc) · 7.81 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
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
const http = require('http');
const got = require('got');
const cdtv_map = {
"1": "1", // 新闻
"2": "2", // 经济
"3": "3", // 都市
"4": "45", // 影视
"5": "5", //公共
"6": "6" //少儿
}
const cqtv_map = {
"1": {
ts_url_prefix: '',
m3u8_url: '',
last_timestamp: -1
},
"2": { //重庆卫视
ts_url_prefix: '',
m3u8_url: '',
last_timestamp: -1
},
"3": { //重庆新闻
ts_url_prefix: '',
m3u8_url: '',
last_timestamp: -1
},
"4": { //睛彩重庆
ts_url_prefix: '',
m3u8_url: '',
last_timestamp: -1
},
"5": { //重庆汽摩
ts_url_prefix: '',
m3u8_url: '',
last_timestamp: -1
},
"6": { //重庆影视
ts_url_prefix: '',
m3u8_url: '',
last_timestamp: -1
},
"9": { //重庆科教
ts_url_prefix: '',
m3u8_url: '',
last_timestamp: -1
},
"10": { //重庆都市
ts_url_prefix: '',
m3u8_url: '',
last_timestamp: -1
},
"11": { //重庆娱乐
ts_url_prefix: '',
m3u8_url: '',
last_timestamp: -1
},
"12": { //重庆生活
ts_url_prefix: '',
m3u8_url: '',
last_timestamp: -1
},
"13": { //重庆国际
ts_url_prefix: '',
m3u8_url: '',
last_timestamp: -1
},
"17": { //重庆时尚
ts_url_prefix: '',
m3u8_url: '',
last_timestamp: -1
},
"16": { //重庆移动
ts_url_prefix: '',
m3u8_url: '',
last_timestamp: -1
},
"18": { //重庆农村
ts_url_prefix: '',
m3u8_url: '',
last_timestamp: -1
},
"19": { //重庆少儿
ts_url_prefix: '',
m3u8_url: '',
last_timestamp: -1
},
"20": { //重庆手持
ts_url_prefix: '',
m3u8_url: '',
last_timestamp: -1
},
}
let sctv_cache = []
var server = http.createServer(async function (req, res) {
if (req.url.indexOf('/cqtv') > -1) {
try {
cqtv(req, res)
} catch (e) {
console.log(e)
}
} else if (req.url.indexOf("/sctv") > -1 || req.url.indexOf('Kangba') > -1) {
try {
sctv(req, res)
} catch (e) {
console.log(e)
}
} else if (req.url.indexOf('cdtv') > -1) {
try {
cdtv(req, res)
} catch (e) {
console.log(e)
}
} else {
res.end('not found')
}
});
server.keepAliveTimeout = 300 * 1000
/**
* proxy cdtv hls flv
* @param {*} req
* @param {*} res
*/
async function cdtv(req, res) {
//hls url : http://www.cditv.cn/api.php?op=live&type=live&catid=192&id=1
var headers = {
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Referer': 'http://www.cditv.cn/html/cdrtv/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3610.2 Safari/537.36',
"X-Requested-With": 'ShockwaveFlash/32.0.0.101'
};
var url = req.url
var index = url.substr(url.lastIndexOf('/') - 1, 1)
index = cdtv_map[index] ? cdtv_map[index] : "1"
var type = 'type=live'
if (url.indexOf('m3u8') > -1) {
type = 'videotype=m3u8'
}
var flv = 'http://www.cditv.cn/api.php?op=live&' + type + '&catid=192&id=' + index
console.log('get file:' + flv)
var content = (await got.get(flv, {
headers: headers
}).on('error', err => { console.log(err) })).body
res.writeHead(302, { 'Location': content })
console.log('redirect video_url:' + content)
return res.end()
}
/**
* proxy sctv hls
* @param {*} req
* @param {*} res
*/
async function sctv(req, res) {
var headers = {
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Referer': 'http://www.sctv.com/live/video/SCTV4/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3610.2 Safari/537.36',
"X-Requested-With": 'ShockwaveFlash/32.0.0.101'
};
var url = req.url;
var index = url.substr(url.lastIndexOf('/') - 1, 1)
if (url.indexOf('Kangba') > -1) {
index = 'Kangba'
} else {
index = 'SCTV' + index
}
if (url.indexOf('m3u8') > -1) {
var m3u8 = 'http://m3u8.sctv.com/tvlive/' + index + '/index.m3u8'
console.log('get m3u8 file:' + m3u8)
var content = (await got.get(m3u8, {
headers: headers
}).on('error', err => { console.log(err) })).body
var reg = /(.*).ts/ig
var list = []
while (result = reg.exec(content)) {
list.push(result[0])
}
if (sctv_cache.length == 0) {
sctv_cache = ['', '', '', '', '', '']
}
var cache_count = sctv_cache.length
for (var i = list.length - 1; i >= 0; i--) {
var ts = list[i]
var cache_index = --cache_count
if (cache_index < 0) {
break;
}
var cache_ts = sctv_cache[cache_index]
if (cache_ts && cache_ts > ts) {
break;
}
sctv_cache[cache_index] = ts
}
var m3u8_str = '#EXTM3U\r\n#EXT-X-VERSION:3\r\n#EXT-X-TARGETDURATION:10\r\n'
for (var i in sctv_cache) {
var ts = sctv_cache[i]
if (i == 0) {
m3u8_str += '#EXT-X-MEDIA-SEQUENCE:' + ts.substr(ts.indexOf('-') + 1, ts.indexOf('.ts') - 2) + '\r\n'
}
m3u8_str += '#EXTINF:10.000, \r\n'
m3u8_str += ts + '\r\n'
}
return res.end(m3u8_str)
} else if (url.indexOf('.ts') > -1) {
var file = url.substr(url.lastIndexOf('/') + 1)
var ts_file_url = 'http://m3u8.sctv.com/tvlive/' + index + '/' + file
console.log('get ts file:' + ts_file_url)
try {
got.stream(ts_file_url, { headers: headers, retry: 3 }).on('error', err => { console.log(err) }).pipe(res);
} catch (e) {
console.error(e)
}
return
}
}
/**
* proxy cqtv hls
* @param {*} req
* @param {*} res
*/
async function cqtv(req, res) {
var headers = {
//'Referer': 'http://www.cbg.cn/1ive/',
//'Referer': 'http://www.cbg.cn/live/index.shtml',
'Referer': 'http://www.cbg.cn/live/Index.shtml,http://www.cbg.cn/live/lndex.shtml,http://www.cbg.cn/live/index.shtml',
//'Referer': 'http://www.cbg.cn/live/lndex.shtml',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3610.2 Safari/537.36'
};
var url = req.url;
var reg = /\/cqtv(\d+)\//
var match = reg.exec(url)
var index = url.substr(url.lastIndexOf('/') - 1, 1)
if (match && match.length > 0) {
index = match[1]
}
if (!cqtv_map[index]) {
index = '1';
}
var map = cqtv_map[index]
if (req.url.indexOf('m3u8') > 0) { //hls m3u8 file
var m3u8 = 'http://app.cbg.cn/?app=activity&controller=wwsp&action=hlive_md5&callback=jQuery&ch=%2Fapp_2%2F_definst_%2Fls_' + index + '.stream%2Fchunklist.m3u8&_=' + new Date().valueOf();
if (map.last_timestamp == -1 || new Date().valueOf() - map.last_timestamp > 1000 * 60) {
var content = (await got.get(m3u8, {
headers: headers
}).on('error', err => { console.log(err) })).body;
var url = content.replace(/.*jQuery\(\"/, '')
.replace('\")', '')
.replace(/\\/ig, '');
map.m3u8_url = url
map.last_timestamp = new Date().valueOf()
map.ts_url_prefix = url.substr(0, url.lastIndexOf('/') + 1)
}
var body = (await got.get(map.m3u8_url, { headers: headers })).body
console.log('body')
body = body.replace('http://sjlivecdnx.cbg.cn/1ive/stream_' + index + '.php', 'stream_' + index + '.php')
console.log('get m3u8 file:' + map.m3u8_url)
return res.end(body)
} else if (req.url.indexOf('stream') > 0) { //hls aes key file
var stream_url = 'http://sjlivecdnx.cbg.cn/1ive/' + req.url.substr(req.url.lastIndexOf('/') + 1)
console.log('get key file:' + stream_url)
got.stream(stream_url, { headers: headers }).pipe(res);
return
} else if (req.url.indexOf('media') > 0) { //hls ts file
var ts_url = map.ts_url_prefix + req.url.substr(req.url.lastIndexOf('/') + 1)
console.log('get ts file:' + ts_url)
got.stream(ts_url, { headers: headers, retry: 3 }).on('error', err => { console.log(err) }).pipe(res);
return
}
}
server.listen(5050);
console.log("listening on port 5050")