1 | import vm from 'vm';
|
2 | import url from 'url';
|
3 | import fetch from 'node-fetch';
|
4 | import { WINDOW } from "./data.mjs";
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | function request(options, cb) {
|
13 | let response = {
|
14 | statusCode: 0,
|
15 | headers: {}
|
16 | };
|
17 | const {
|
18 | encoding
|
19 | } = options;
|
20 | (async () => {
|
21 | const res = await fetch(options.url, {
|
22 | method: options.method || 'GET',
|
23 | headers: {
|
24 | 'User-Agent': '-',
|
25 | ...(options.headers || {})
|
26 | },
|
27 | compress: !!options.gzip
|
28 | });
|
29 | const {
|
30 | status,
|
31 | headers
|
32 | } = res;
|
33 | const headersRaw = headers.raw();
|
34 | const headersObject = {};
|
35 |
|
36 | for (const p of Object.keys(headersRaw)) {
|
37 | headersObject[p] = headersRaw[p].join(', ');
|
38 | }
|
39 |
|
40 | response = {
|
41 | statusCode: status,
|
42 | headers: headersObject
|
43 | };
|
44 | const data = await res.buffer();
|
45 | return encoding === null ? data : data.toString(encoding);
|
46 | })().then(data => {
|
47 | cb(null, response, data);
|
48 | }, err => {
|
49 | cb(err, response, null);
|
50 | });
|
51 | }
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | async function requestP(req, options) {
|
62 | const r = await new Promise((resolve, reject) => {
|
63 | req(options, (error, response, body) => {
|
64 | if (error) {
|
65 | reject(error);
|
66 | return;
|
67 | }
|
68 |
|
69 | resolve({
|
70 | response,
|
71 | body
|
72 | });
|
73 | });
|
74 | });
|
75 | return r;
|
76 | }
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | function createSandbox() {
|
85 |
|
86 |
|
87 |
|
88 | const ctxObj = Object.create(null);
|
89 |
|
90 | if (ctxObj.toString) {
|
91 | throw new Error('Failed to create object without prototype');
|
92 | }
|
93 |
|
94 | const ctx = vm.createContext(ctxObj);
|
95 | return {
|
96 | |
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | run: (code, opts) => {
|
103 | let error = false;
|
104 |
|
105 | try {
|
106 | vm.runInContext(code, ctx, opts);
|
107 | } catch (err) {
|
108 | error = true;
|
109 | }
|
110 |
|
111 | if (error) {
|
112 | throw new Error('Error running sandboxed script');
|
113 | }
|
114 | },
|
115 |
|
116 | |
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | data: (data, opts) => {
|
124 | const body = Object.entries(data).map(a => `${JSON.stringify(a[0])}:${a[1]}`).join(',');
|
125 | const script = `(""+JSON.stringify({${body}}))`;
|
126 | let r = null;
|
127 |
|
128 | try {
|
129 |
|
130 |
|
131 | r = JSON.parse(
|
132 | '' + vm.runInContext(script, ctx, opts));
|
133 | } catch (err) {
|
134 | }
|
135 |
|
136 | if (!r) {
|
137 | throw new Error('Error running sandboxed script');
|
138 | }
|
139 |
|
140 | return r;
|
141 | }
|
142 | };
|
143 | }
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | function codeWindow(body) {
|
153 | return `(${WINDOW})(this,${JSON.stringify(body)})`;
|
154 | }
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | export async function extract(uri, req = null) {
|
165 | const requester = req || request;
|
166 | const {
|
167 | response,
|
168 | body
|
169 | } = await requestP(requester, {
|
170 | url: uri,
|
171 | gzip: true
|
172 | });
|
173 | const {
|
174 | statusCode
|
175 | } = response;
|
176 |
|
177 | if (statusCode !== 200) {
|
178 | throw new Error(`Invalid status code: ${statusCode}`);
|
179 | }
|
180 |
|
181 | const bodyType = typeof body;
|
182 |
|
183 | if (bodyType !== 'string') {
|
184 | throw new Error(`Invalid body type: ${bodyType}`);
|
185 | }
|
186 |
|
187 | const sandbox = createSandbox();
|
188 | const timeout = 1000;
|
189 |
|
190 | sandbox.run(codeWindow(body.toString()), {});
|
191 |
|
192 | const info = sandbox.data({
|
193 | scripts: '(function(i,r,l){' + 'while(++i<l.length){' + 'r.push(l[i].textContent)' + '}' + 'return r' + '})(-1,[],document.getElementsByTagName("script"))'
|
194 | }, {
|
195 | timeout
|
196 | });
|
197 |
|
198 | for (const script of info.scripts) {
|
199 | if (script.includes('dlbutton')) {
|
200 | sandbox.run(script, {
|
201 | timeout
|
202 | });
|
203 | }
|
204 | }
|
205 |
|
206 |
|
207 | const result = sandbox.data({
|
208 | dlbutton: 'document.getElementById("dlbutton").href'
|
209 | }, {
|
210 | timeout
|
211 | });
|
212 |
|
213 | if (!result.dlbutton) {
|
214 | throw new Error('Failed to extract info');
|
215 | }
|
216 |
|
217 |
|
218 | const u = new url.URL(result.dlbutton, uri);
|
219 | return {
|
220 | download: u.href,
|
221 | filename: decodeURI(u.pathname.split('/').pop() || '') || null
|
222 | };
|
223 | }
|
224 |
|