Skip to content

Commit

Permalink
fix: 更新hack函数
Browse files Browse the repository at this point in the history
  • Loading branch information
duan602728596 committed May 16, 2024
1 parent f145bc7 commit c62d03a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ script.

/**
* Rewrite the WebSocket's send method to fix the verification problem.
* @class HACK_INTERCEPTS_SEND_Websocket
* @class HACK_INTERCEPTS_SEND_NIM_Websocket
*/
class HACK_INTERCEPTS_SEND_Websocket extends WebSocket {}
class HACK_INTERCEPTS_SEND_NIM_Websocket extends WebSocket {}

HACK_INTERCEPTS_SEND_Websocket.prototype.ORIGINAL_send = HACK_INTERCEPTS_SEND_Websocket.prototype.send;
HACK_INTERCEPTS_SEND_NIM_Websocket.prototype.ORIGINAL_send = HACK_INTERCEPTS_SEND_NIM_Websocket.prototype.send;

HACK_INTERCEPTS_SEND_Websocket.prototype.send = function() {
HACK_INTERCEPTS_SEND_NIM_Websocket.prototype.send = function () {
if (/3:::/.test(arguments[0])) {
const message = arguments[0].replace(/3:::/, '');
let data = null;

try {
data = JSON.parse(message);
} catch {}
} catch { /* noop */ }

if (data && data?.SER === 1 && data?.Q?.length) {
if (data && data?.SER === 1 && data?.SID === 2 && data?.Q?.length) {
for (const Q of data.Q) {
if (/Property/i.test(Q.t) && Q.v && objectSome(Q.v, (k, v) => /Electron/i.test(v))) {
Q.v['3'] = 2;
Expand All @@ -51,5 +51,38 @@ script.
return this.ORIGINAL_send.apply(this, arguments);
};

globalThis.HACK_INTERCEPTS_SEND_Websocket = HACK_INTERCEPTS_SEND_Websocket;
globalThis.HACK_INTERCEPTS_SEND_NIM_Websocket = HACK_INTERCEPTS_SEND_NIM_Websocket;

/**
* Rewrite the WebSocket's send method to fix the verification problem.
* @class HACK_INTERCEPTS_SEND_QCHAT_Websocket
*/
class HACK_INTERCEPTS_SEND_QCHAT_Websocket extends WebSocket {}

HACK_INTERCEPTS_SEND_QCHAT_Websocket.prototype.ORIGINAL_send = HACK_INTERCEPTS_SEND_QCHAT_Websocket.prototype.send;

HACK_INTERCEPTS_SEND_QCHAT_Websocket.prototype.send = function() {
if (/3:::/.test(arguments[0])) {
const message = arguments[0].replace(/3:::/, '');
let data = null;

try {
data = JSON.parse(message);
} catch { /* noop */ }

if (data && data?.SER === 1 && data?.SID === 24 && data?.Q?.length) {
for (const Q of data.Q) {
if (/Property/i.test(Q.t) && Q.v && objectSome(Q.v, (k, v) => /Electron/i.test(v))) {
Q.v['6'] = 2;
arguments[0] = `3:::${ JSON.stringify(data) }`;
break;
}
}
}
}

return this.ORIGINAL_send.apply(this, arguments);
};

globalThis.HACK_INTERCEPTS_SEND_QCHAT_Websocket = HACK_INTERCEPTS_SEND_QCHAT_Websocket;
})();
12 changes: 7 additions & 5 deletions scripts/fixTypesError.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { cwd } from './utils.mjs';

const nodeModules = path.join(cwd, 'node_modules');

async function replaceWebsocket(fp) {
async function replaceWebsocket(fp, ws) {
const filePath = path.join(nodeModules, fp);
const file = await fsP.readFile(filePath, { encoding: 'utf8' });
const newFile = file.replace(
/window\.WebSocket/g, 'window.HACK_INTERCEPTS_SEND_Websocket||window.WebSocket');
/window\.WebSocket/g, `window.${ ws }||window.WebSocket`);

await fsP.writeFile(filePath, newFile, { encoding: 'utf8' });
}
Expand All @@ -21,16 +21,18 @@ async function fixTypesError() {

// 替换window.WebSocket
await Promise.all([
replaceWebsocket('nim-web-sdk-ng/dist/NIM_BROWSER_SDK.js'),
replaceWebsocket('nim-web-sdk-ng/dist/QCHAT_BROWSER_SDK.js')
replaceWebsocket('nim-web-sdk-ng/dist/NIM_BROWSER_SDK.js', 'HACK_INTERCEPTS_SEND_NIM_Websocket'),
replaceWebsocket('nim-web-sdk-ng/dist/QCHAT_BROWSER_SDK.js', 'HACK_INTERCEPTS_SEND_QCHAT_Websocket')
]);

/*
const NIMWebSDKFilePath = path.join(nodeModules, '@yxim/nim-web-sdk/dist/SDK/NIM_Web_SDK.js');
const NIMWebSDKFile = await fsP.readFile(NIMWebSDKFilePath, { encoding: 'utf8' });
const newNIMWebSDKFile = NIMWebSDKFile.replace(
/\.MozWebSocket/g, '.MozWebSocket||window.HACK_INTERCEPTS_SEND_Websocket');
/\.MozWebSocket/g, '.MozWebSocket||window.HACK_INTERCEPTS_SEND_NIM_CHATROOM_Websocket');
await fsP.writeFile(NIMWebSDKFilePath, newNIMWebSDKFile, { encoding: 'utf8' });
*/
}

fixTypesError();

0 comments on commit c62d03a

Please sign in to comment.