-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
175 lines (146 loc) · 5.21 KB
/
index.mjs
File metadata and controls
175 lines (146 loc) · 5.21 KB
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
import express from "express"
import cors from "cors"
import got from "got"
import m3u8Parser from 'm3u8-parser'
import AnicliWrapper from "./engine/anicli_wrapper.mjs"
import { Transform } from 'stream'
import { fileURLToPath } from "url"
import path, { dirname } from 'path'
import fetch from "node-fetch"
import https from "https"
const PORT = process.env.PORT || 4000
const app = express()
const anicli = new AnicliWrapper()
function validURL(str) {
var pattern = /https?:\\?/i; // fragment locator
return !!pattern.test(str);
}
app.use(cors())
app.get('/player', function(req, res) {
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
res.sendFile(path.join(__dirname, '/player.html'));
});
app.get('/get_streaming_source', async (req, res) => {
const player_link = req.query.player_link
if (player_link === undefined) {
return res.status(400).send('player_link is required')
}
if (!validURL(player_link)) {
return res.status(400).send('player_link is invalid')
}
const decrypted_link = await anicli.decrypt_link(player_link)
console.log(decrypted_link)
let sources = []
await Promise.all(decrypted_link.map(async link => {
if (link.includes(".m3u8")) {
sources.push({
url: "/fetch_master_m3u8?url=" + encodeURIComponent(link),
type: 'application/x-mpegURL',
size: 'original'
})
}
if (link.includes(".mp4")) {
const quality_extrator_regex = [
/.(\d+)p.mp4/, // Local CDN
/-([a-zA-Z]+).mp4/ // Google Storage
]
const quality_filter = {
"original": 0,
"sd": 360,
"hd": 720,
}
const streaming_url = '/stream?url=' + encodeURIComponent(link) + '&referer=' + encodeURIComponent(player_link)
let quality = 0
quality_extrator_regex.some(regex => {
const quality_sort = link.match(regex)
if (quality_sort) {
quality = quality_sort[1].toLowerCase()
quality = quality_filter[quality] ? quality_filter[quality] : quality
return true
}
});
sources.push({
src: streaming_url,
type: 'video/mp4',
size: quality
})
}
}))
console.log(sources)
res.send(sources)
})
app.get('/fetch_master_m3u8', async (req, res) => {
const url = req.query.url
const base_url = url.split('/').slice(0, -1).join('/')
const m3u8_file = await fetch(url).then(res => res.text())
const regex = /(^.*\b\.m3u8\b.*)/gm
const is_valid_url = m3u8_file.match(regex).some(validURL)
const m3u8_file_filtered = m3u8_file.replace(regex, is_valid_url ? `fetch_m3u8?url=$1` : `/fetch_m3u8?url=${base_url}/$1`);
res.setHeader('content-type', 'application/x-mpegURL');
res.send(m3u8_file_filtered);
})
app.get('/fetch_m3u8', async (req, res) => {
const url = req.query.url
const base_url = url.split('/').slice(0, -1).join('/')
const m3u8_file = await fetch(url).then(res => res.text())
const regex = /(^.*\b\.ts\b.*)/gm
const is_valid_url = m3u8_file.match(regex).some(validURL)
const m3u8_file_filtered = m3u8_file.replace(regex, is_valid_url ? `fetch_ts?url=$1` : `/fetch_ts?url=${base_url}/$1`);
res.setHeader('content-type', 'application/x-mpegURL');
res.send(m3u8_file_filtered);
})
app.get('/fetch_ts', async (req, res) => {
const url = req.query.url
res.setHeader('content-type', 'video/mp2t');
console.log(url)
got.stream(url).pipe(res)
})
// app.get("/stream", (req, res) => {
// const url = req.query.url
// const referer = req.query.referer
// res.setHeader('Content-Type', 'video/mp4');
// got.stream(url, { headers: { "referer": referer }}).pipe(res);
// })
function getVideoStreamSize(url) {
return new Promise((resolve, reject) => {
const req = https.get(url);
req.once("response", (res) => {
req.abort();
const size = parseInt(res.headers["content-length"]);
resolve(size);
});
req.once("error", (err) => {
reject(err);
});
});
}
app.get("/stream", async function (req, res) {
const url = req.query.url;
const referer = req.query.referer;
const range = req.headers.range;
if (!range) {
res.status(400).send("Requires Range header");
}
const chunkSize = 10 ** 6; // 1MB
const videoSize = await getVideoStreamSize(url);
const start = Number(range.replace(/\D/g, ""));
const end = Math.min(start + chunkSize, videoSize - 1);
const contentLength = end - start + 1;
const headers = {
"Content-Range": `bytes ${start}-${end}/${videoSize}`,
"Accept-Ranges": "bytes",
"Content-Length": contentLength,
"Content-Type": "video/mp4",
};
res.writeHead(206, headers);
got.stream(url, {
headers: {
Referer: referer,
Range: `bytes=${start}-${end}`,
}
}).pipe(res)
})
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`)
})