87 lines
3.1 KiB
JavaScript
87 lines
3.1 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import vm from 'node:vm';
|
|
|
|
const code = fs.readFileSync(new URL('../js/qdreader_cookie.js', import.meta.url), 'utf8');
|
|
|
|
function run({ cookie = '', body = '', headerName = 'Cookie' }) {
|
|
const notices = [];
|
|
const donePayloads = [];
|
|
const context = {
|
|
$request: { headers: { [headerName]: cookie } },
|
|
$response: { body },
|
|
$notify: (...args) => notices.push(args),
|
|
$done: (payload) => { donePayloads.push(payload); },
|
|
atob: (s) => Buffer.from(s, 'base64').toString('binary'),
|
|
encodeURIComponent,
|
|
decodeURIComponent,
|
|
escape,
|
|
};
|
|
vm.runInNewContext(code, context);
|
|
return { notices, donePayloads };
|
|
}
|
|
|
|
{
|
|
const qdheader = Buffer.from('123456|device|more').toString('base64').replace(/=+$/, '');
|
|
const cookie = `QDHeader=${encodeURIComponent(qdheader)}; QDH=abc`;
|
|
const { notices } = run({ cookie });
|
|
assert.equal(notices[0][1], '抓取成功');
|
|
const body = notices[0][2];
|
|
assert.match(body, /^启点ck /);
|
|
assert.deepEqual(JSON.parse(body.replace(/^启点ck\s+/, '')), { '123456': cookie });
|
|
assert.equal(notices[0].length, 3);
|
|
}
|
|
|
|
{
|
|
const { notices } = run({
|
|
cookie: 'cmfuToken=abc; QDH=def',
|
|
body: JSON.stringify({ Data: { UserId: 987654 } }),
|
|
});
|
|
assert.equal(notices[0][1], '抓取成功');
|
|
}
|
|
|
|
{
|
|
const { notices } = run({
|
|
cookie: 'cmfuToken=abc; QDH=def',
|
|
body: JSON.stringify({ Data: { UserInfo: { UserId: 24680 } } }),
|
|
});
|
|
assert.equal(notices[0][1], '抓取成功');
|
|
}
|
|
|
|
{
|
|
const qdheader = Buffer.from('(null)|(null)|1179|2556|(null)|16.10|0|iOS/iPhone/(null)|0|(null)|(null)|475700975|1779006621000|0|(null)|||(null)|(null)|0').toString('base64').replace(/=+$/, '');
|
|
const { notices } = run({ cookie: `QDHeader=${qdheader}; ywguid=120098575768; QDH=abc` });
|
|
assert.equal(notices[0][1], '抓取成功');
|
|
}
|
|
|
|
{
|
|
const streamQdheader = 'KG51bGwpfChudWxsKXwxMTc5fDI1NTZ8KG51bGwpfDE2LjEwfDB8aU9TL2lQaG9uZS8obnVsbCl8MHwobnVsbCl8KG51bGwpfDQ3NTcwMDk3NXwxNzc5MDA5MTM2MDAwfDB8KG51bGwpfHx8KG51bGwpfChudWxsKXww';
|
|
const { notices } = run({
|
|
cookie: `QDHeader=${streamQdheader}; ywguid=120098575768; QDH=abc`,
|
|
body: JSON.stringify({ Data: { Guid: '120098575768', UserId: '475700975' }, Result: '0' }),
|
|
});
|
|
assert.equal(notices[0][1], '抓取成功');
|
|
assert.deepEqual(JSON.parse(notices[0][2].replace(/^启点ck\s+/, '')), { '475700975': `QDHeader=${streamQdheader}; ywguid=120098575768; QDH=abc` });
|
|
}
|
|
|
|
{
|
|
const { notices, donePayloads } = run({
|
|
cookie: 'cmfuToken=abc; QDH=def',
|
|
body: JSON.stringify({ Data: { UserId: 135790 } }),
|
|
headerName: 'cookie',
|
|
});
|
|
assert.equal(notices[0][0], '启点读书');
|
|
assert.equal(notices[0][1], '抓取成功');
|
|
assert.equal(notices[0][2], '启点ck {"135790":"cmfuToken=abc; QDH=def"}');
|
|
assert.equal(notices[0].length, 3);
|
|
assert.equal(JSON.stringify(donePayloads[0]), '{}');
|
|
}
|
|
|
|
{
|
|
const { notices } = run({ cookie: 'cmfuToken=abc; QDH=def', body: '{}' });
|
|
assert.equal(notices[0][1], 'Cookie 获取失败');
|
|
assert.match(notices[0][2], /无法识别 uid/);
|
|
}
|
|
|
|
console.log('ok - qdreader qx uid parsing');
|