Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix phind #793

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"md5": "^2.3.0",
"prettier": "^3.1.1",
"prettier-plugin-vue": "^1.1.6",
"raw-loader": "^4.0.2",
"sse.js": "^2.2.0"
},
"eslintConfig": {
Expand Down
60 changes: 42 additions & 18 deletions src/background.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"use strict";

import { BrowserWindow, app, ipcMain, nativeTheme, protocol } from "electron";
import { createProtocol } from "vue-cli-plugin-electron-builder/lib";
import installExtension, { VUEJS3_DEVTOOLS } from "electron-devtools-installer";
import fs from "fs";
import path from "path";
import { createProtocol } from "vue-cli-plugin-electron-builder/lib";
import { setMenuItems } from "./menu";
import updateApp from "./update";
import { sendPhind } from "./windows/phind/phind";
const isDevelopment = process.env.NODE_ENV !== "production";

const DEFAULT_USER_AGENT = ""; // Empty string to use the Electron default
let mainWindow = null;

// start - makes application a Single Instance Application
const singleInstanceLock = app.requestSingleInstanceLock();
Expand All @@ -19,9 +19,9 @@ if (!singleInstanceLock) {
} else {
app.on("second-instance", () => {
// Someone tried to run a second instance, we should focus our window.
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.focus();
if (global.mainWindow) {
if (global.mainWindow.isMinimized()) global.mainWindow.restore();
global.mainWindow.focus();
}
});
}
Expand Down Expand Up @@ -157,7 +157,7 @@ async function createWindow() {
},
});

mainWindow = win;
global.mainWindow = win;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

⚠️ Potential issue

Refactor createWindow for better separation of concerns and security

The createWindow function has been updated with new functionality, but there are some concerns:

  1. Cookie Handling: Forcing SameSite to 'None' for all cookies can have security implications. This should be done selectively only for cookies that absolutely require it.

  2. Function Length: The function is handling multiple concerns (window creation, cookie handling, request interception). Consider breaking these into separate functions for better maintainability.

  3. Security: Modifying cookies and headers can potentially introduce security vulnerabilities. Ensure that these modifications are absolutely necessary and well-documented.

Suggestions:

  1. Extract cookie handling logic into a separate function:
function setupCookieHandling(win) {
  win.webContents.session.cookies.on('changed', async (event, cookie, cause, removed) => {
    // ... existing cookie handling logic ...
  });
}
  1. Extract request interception logic into a separate function:
function setupRequestInterception(win) {
  win.webContents.session.webRequest.onBeforeSendHeaders((details, callback) => {
    // ... existing request interception logic ...
  });
}
  1. Update createWindow:
async function createWindow() {
  const win = new BrowserWindow({
    // ... existing options ...
  });

  global.mainWindow = win;

  setupCookieHandling(win);
  setupRequestInterception(win);

  // ... rest of the function ...
}
  1. Add comments explaining why certain security-sensitive operations (like modifying SameSite attributes) are necessary.

Also applies to: 164-209


// Force the SameSite attribute to None for all cookies
// This is required for the cross-origin request to work
Expand Down Expand Up @@ -295,34 +295,37 @@ function createNewWindow(url, userAgent = "") {
if (url.startsWith("https://moss.fastnlp.top/")) {
// Get the secret of MOSS
const secret = await getLocalStorage("flutter.token");
mainWindow.webContents.send("moss-secret", secret);
global.mainWindow.webContents.send("moss-secret", secret);
} else if (url.startsWith("https://qianwen.aliyun.com/")) {
// Get QianWen bot's XSRF-TOKEN
const token = await getCookie("XSRF-TOKEN");
mainWindow.webContents.send("QIANWEN-XSRF-TOKEN", token);
global.mainWindow.webContents.send("QIANWEN-XSRF-TOKEN", token);
} else if (url.startsWith("https://chat.tiangong.cn/")) {
// Get the tokens of SkyWork
const inviteToken = await getLocalStorage("aiChatQueueWaitToken");
const token = await getLocalStorage("aiChatResearchToken");
mainWindow.webContents.send("SKYWORK-TOKENS", { inviteToken, token });
global.mainWindow.webContents.send("SKYWORK-TOKENS", {
inviteToken,
token,
});
} else if (url.startsWith("https://character.ai/")) {
const token = await getLocalStorage("char_token");
mainWindow.webContents.send("CHARACTER-AI-TOKENS", token);
global.mainWindow.webContents.send("CHARACTER-AI-TOKENS", token);
} else if (url.startsWith("https://claude.ai/")) {
const org = await getCookie("lastActiveOrg");
mainWindow.webContents.send("CLAUDE-2-ORG", org);
global.mainWindow.webContents.send("CLAUDE-2-ORG", org);
} else if (url.startsWith("https://poe.com/")) {
const formkey = await newWin.webContents.executeJavaScript(
"window.ereNdsRqhp2Rd3LEW();",
);
mainWindow.webContents.send("POE-FORMKEY", formkey);
global.mainWindow.webContents.send("POE-FORMKEY", formkey);
} else if (url.startsWith("https://chatglm.cn/")) {
const token = await getCookie("chatglm_token");
mainWindow.webContents.send("CHATGLM-TOKENS", { token });
global.mainWindow.webContents.send("CHATGLM-TOKENS", { token });
} else if (url.startsWith("https://kimi.moonshot.cn/")) {
const access_token = await getLocalStorage("access_token");
const refresh_token = await getLocalStorage("refresh_token");
mainWindow.webContents.send("KIMI-TOKENS", {
global.mainWindow.webContents.send("KIMI-TOKENS", {
access_token,
refresh_token,
});
Expand All @@ -333,12 +336,13 @@ function createNewWindow(url, userAgent = "") {

newWin.destroy(); // Destroy the window manually
// Tell renderer process to check aviability
mainWindow.webContents.send("CHECK-AVAILABILITY", url);
global.mainWindow.webContents.send("CHECK-AVAILABILITY", url);
});
return newWin;
}

async function getCookies(filter) {
const cookies = await mainWindow.webContents.session.cookies.get({
const cookies = await global.mainWindow.webContents.session.cookies.get({
...filter,
});
return cookies;
Expand All @@ -348,6 +352,26 @@ ipcMain.handle("create-new-window", (event, url, userAgent) => {
createNewWindow(url, userAgent);
});

/** @type {Object<string, BrowserWindow>}*/
const windows = {};
ipcMain.handle("create-chat-window", async (event, options) => {
const { winName, url, userAgent } = options;
switch (winName) {
case "phind":
const win = createNewWindow(url, userAgent);
windows[winName] = win;
await sendPhind({ win, ...options });
break;
default:
break;
}
});

ipcMain.handle("close-chat-window", (event, winName) => {
windows[winName]?.close();
windows[winName] = undefined;
});

ipcMain.handle("get-native-theme", () => {
return Promise.resolve({
shouldUseDarkColors: nativeTheme.shouldUseDarkColors,
Expand Down Expand Up @@ -389,15 +413,15 @@ ipcMain.handle("save-proxy-and-restart", async () => {
// Proxy Setting End

ipcMain.handle("set-is-show-menu-bar", (_, isShowMenuBar) => {
mainWindow.setMenuBarVisibility(isShowMenuBar);
global.mainWindow.setMenuBarVisibility(isShowMenuBar);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Miscellaneous changes look good, but there's a potential bug in updateApp call

The changes to use global.mainWindow in the IPC handlers are consistent with the earlier modifications. The new proxy setting handlers also seem well-implemented.

However, there's a potential bug in the updateApp function call:

updateApp(mainWindow);

This is still using the local mainWindow variable, which no longer exists. It should be updated to use global.mainWindow:

updateApp(global.mainWindow);

Please update this to ensure the app update functionality works correctly.

Also applies to: 424-424, 474-474

});

ipcMain.handle("get-cookies", async (event, filter) => {
return await getCookies(filter);
});

nativeTheme.on("updated", () => {
mainWindow.webContents.send("on-updated-system-theme");
global.mainWindow.webContents.send("on-updated-system-theme");
});

// Quit when all windows are closed.
Expand Down
Loading