-
Notifications
You must be signed in to change notification settings - Fork 0
/
MovementFaker.js
135 lines (119 loc) · 3.78 KB
/
MovementFaker.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
const https = require('https');
const fs = require('fs/promises');
const { R_OK } = require('fs').constants;
const vm = require('vm');
const URL = 'https://wbbny.m.jd.com/babelDiy/Zeus/2rtpffK8wqNyPBH6wyUDuBKoAbCt/index.html';
// const REG_MODULE = /(\d+)\:function\(.*(?=smashUtils\.get_risk_result)/gm;
const SYNTAX_MODULE = '!function(n){var r={};function o(e){if(r[e])';
const REG_SCRIPT = /<script type="text\/javascript" src="([^><]+\/(app\.\w+\.js))\">/gm;
const REG_ENTRY = /(__webpack_require__\(__webpack_require__.s=)(\d+)(?=\)})/;
const needModuleId = 355
const DATA = {appid:'50085',sceneid:'OY217hPageh5'};
let smashUtils;
class MovementFaker {
constructor(cookie) {
// this.secretp = secretp;
this.cookie = cookie;
this.ua = require('./USER_AGENTS.js').USER_AGENT;
}
async run() {
if (!smashUtils) {
await this.init();
}
var t = Math.floor(1e7 + 9e7 * Math.random()).toString();
var e = smashUtils.get_risk_result({
id: t,
data: {
random: t
}
}).log;
var o = JSON.stringify({
extraData: {
log: e || -1,
// log: encodeURIComponent(e),
sceneid: DATA.sceneid,
},
// secretp: this.secretp,
random: t
})
// console.log(o);
return o;
}
async init() {
try {
console.time('MovementFaker');
process.chdir(__dirname);
const html = await MovementFaker.httpGet(URL);
const script = REG_SCRIPT.exec(html);
if (script) {
const [, scriptUrl, filename] = script;
const jsContent = await this.getJSContent(filename, scriptUrl);
const fnMock = new Function;
const ctx = {
window: { addEventListener: fnMock },
document: {
addEventListener: fnMock,
removeEventListener: fnMock,
cookie: this.cookie,
},
navigator: { userAgent: this.ua },
};
vm.createContext(ctx);
vm.runInContext(jsContent, ctx);
smashUtils = ctx.window.smashUtils;
smashUtils.init(DATA);
// console.log(ctx);
}
// console.log(html);
// console.log(script[1],script[2]);
console.timeEnd('MovementFaker');
} catch (e) {
console.log(e)
}
}
async getJSContent(cacheKey, url) {
try {
await fs.access(cacheKey, R_OK);
const rawFile = await fs.readFile(cacheKey, { encoding: 'utf8' });
return rawFile;
} catch (e) {
let jsContent = await MovementFaker.httpGet(url);
const moduleIndex = jsContent.indexOf(SYNTAX_MODULE, 1);
const findEntry = REG_ENTRY.test(jsContent);
//console.log(jsContent)
if (!(moduleIndex && findEntry)) {
throw new Error('Module not found.');
}
// const needModuleId = jsContent.substring(moduleIndex-20, moduleIndex).match(/(\d+):function/)[1]
jsContent = jsContent.replace(REG_ENTRY, `$1${needModuleId}`);
fs.writeFile(cacheKey, jsContent);
return jsContent;
REG_ENTRY.lastIndex = 0;
const entry = REG_ENTRY.exec(jsContent);
//(moduleIndex, needModuleId);
//console.log(entry[1], entry[2]);
}
}
static httpGet(url) {
return new Promise((resolve, reject) => {
const protocol = url.indexOf('http') !== 0 ? 'https:' : '';
const req = https.get(protocol + url, (res) => {
res.setEncoding('utf-8');
let rawData = '';
res.on('error', reject);
res.on('data', chunk => rawData += chunk);
res.on('end', () => resolve(rawData));
});
req.on('error', reject);
req.end();
});
}
}
async function getBody($) {
const zf = new MovementFaker($.cookie);
// const zf = new MovementFaker($.secretp, $.cookie);
const ss = await zf.run();
return ss;
}
MovementFaker.getBody = getBody;
module.exports = MovementFaker;