Skip to content

Commit

Permalink
处理切换 Chrome/Chromium 数据会丢失的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
niuniuland committed Mar 22, 2024
1 parent ded7bf1 commit 2ee47a6
Show file tree
Hide file tree
Showing 21 changed files with 76 additions and 104 deletions.
2 changes: 1 addition & 1 deletion packages/main/src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {getDbPath} from '../utils/getDBPath';
import {getDbPath} from '../utils/get-db-path';

export const DB_CONFIG = {
client: 'sqlite3',
Expand Down
4 changes: 2 additions & 2 deletions packages/main/src/db/window.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {db} from '.';
import type {DB, SafeAny} from '../../../shared/types/db';
import type {IWindowTemplate} from '../types/windowTemplate';
import type {IWindowTemplate} from '../types/window-template';
import {GroupDB} from './group';
import {ProxyDB} from './proxy';
import {randomUniqueProfileId} from '../../../shared/utils/random';
import { randomFingerprint } from '../services/windowService';
import { randomFingerprint } from '../services/window-service';

const all = async () => {
return await db('window')
Expand Down
57 changes: 35 additions & 22 deletions packages/main/src/fingerprint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import {join} from 'path';
import {ProxyDB} from '../db/proxy';
import {WindowDB} from '../db/window';
// import {getChromePath} from './device';
import {BrowserWindow, app} from 'electron';
import {BrowserWindow} from 'electron';
import type {Page} from 'puppeteer';
import puppeteer from 'puppeteer';
import {execSync, spawn} from 'child_process';
import * as portscanner from 'portscanner';
import {sleep} from '../utils/sleep';
import SocksServer from '../proxy-server/socksServer';
import SocksServer from '../proxy-server/socks-server';
import type {DB} from '../../../shared/types/db';
import type {IP} from '../../../shared/types/ip';
import {type IncomingMessage, type Server, type ServerResponse} from 'http';
Expand All @@ -20,8 +20,8 @@ import * as ProxyChain from 'proxy-chain';
import api from '../../../shared/api/api';
import {getSettings} from '../utils/get-settings';
import {getPort} from '../server/index';
import {randomFingerprint} from '../services/windowService';
import { getClientPort } from '../mainWindow';
import {randomFingerprint} from '../services/window-service';
import {bridgeMessageToUI, getClientPort} from '../mainWindow';

const logger = createLogger(WINDOW_LOGGER_LABEL);

Expand All @@ -40,9 +40,13 @@ const attachFingerprintToPuppeteer = async (page: Page, ipInfo: IP) => {
}, title);
}

await page.setGeolocation({latitude: ipInfo.ll[0], longitude: ipInfo.ll[1]});
await page.setGeolocation({latitude: ipInfo.ll?.[0], longitude: ipInfo.ll?.[1]});
await page.emulateTimezone(ipInfo.timeZone);
} catch (error) {
bridgeMessageToUI({
type: 'error',
text: (error as {message: string}).message,
});
logger.error(error);
}
});
Expand All @@ -51,7 +55,12 @@ const attachFingerprintToPuppeteer = async (page: Page, ipInfo: IP) => {
);
};

async function connectBrowser(port: number, ipInfo: IP, windowId: number) {
async function connectBrowser(
port: number,
ipInfo: IP,
windowId: number,
openStartPage: boolean = true,
) {
const browserURL = `http://${HOST}:${port}`;
const {data} = await api.get(browserURL + '/json/version');
if (data.webSocketDebuggerUrl) {
Expand All @@ -69,14 +78,14 @@ async function connectBrowser(port: number, ipInfo: IP, windowId: number) {
const pages = await browser.pages();
const page =
pages.length &&
(pages[0].url() === 'about:blank' ||
!pages[0].url() ||
pages[0].url() === 'chrome://new-tab-page/')
? pages[0]
(pages?.[0]?.url() === 'about:blank' ||
!pages?.[0]?.url() ||
pages?.[0]?.url() === 'chrome://new-tab-page/')
? pages?.[0]
: await browser.newPage();
try {
await attachFingerprintToPuppeteer(page, ipInfo);
if (getClientPort()) {
if (getClientPort() && openStartPage) {
await page.goto(
`http://localhost:${getClientPort()}/#/start?windowId=${windowId}&serverPort=${getPort()}`,
);
Expand All @@ -102,29 +111,30 @@ export async function openFingerprintWindow(id: number) {
const windowData = await WindowDB.getById(id);
const proxyData = await ProxyDB.getById(windowData.proxy_id);
const proxyType = proxyData?.proxy_type?.toLowerCase();
const userDataPath = app.getPath('userData');
const settings = getSettings();

let cachePath;
if (settings.profileCachePath) {
cachePath = settings.profileCachePath;
} else {
cachePath = join(userDataPath, 'cache');
cachePath = join(process.resourcesPath, 'chromePowerCache');
}
const win = BrowserWindow.getAllWindows()[0];
const windowDataDir = `${cachePath}\\${windowData.profile_id}`;
const windowDataDir = join(
cachePath,
settings.useLocalChrome ? 'chrome' : 'chromium',
windowData.profile_id,
);
const driverPath = getDriverPath();

let ipInfo = {timeZone: '', ip: '', ll: [], country: ''};
if (windowData.proxy_id && proxyData.ip) {
ipInfo = await getProxyInfo(proxyData);
} else {
ipInfo = await getProxyInfo({});
}
if (!ipInfo?.ip) {
logger.error('ipInfo is empty');
return;
if (!ipInfo?.ip) {
logger.error('ipInfo is empty');
}
}

const fingerprint =
windowData.fingerprint && windowData.fingerprint !== '{}'
? JSON.parse(windowData.fingerprint)
Expand Down Expand Up @@ -183,7 +193,6 @@ export async function openFingerprintWindow(id: number) {
if (!chromeInstance) {
return;
}
await sleep(1);
await WindowDB.update(id, {
status: 2,
port: chromePort,
Expand Down Expand Up @@ -217,14 +226,18 @@ export async function openFingerprintWindow(id: number) {
await sleep(1);

try {
return connectBrowser(chromePort, ipInfo, windowData.id);
return connectBrowser(chromePort, ipInfo, windowData.id, !!windowData.proxy_id);
} catch (error) {
logger.error(error);
execSync(`taskkill /PID ${chromeInstance.pid} /F`);
closeFingerprintWindow(id, true);
return null;
}
} else {
bridgeMessageToUI({
type: 'error',
text: 'Driver path is empty',
});
logger.error('Driver path is empty');
return null;
}
Expand Down
81 changes: 24 additions & 57 deletions packages/main/src/fingerprint/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const getRealIP = async (proxy: DB.Proxy) => {
text: `获取真实IP失败: ${(error as {message: string}).message}`,
});
logger.error(`| Prepare | getRealIP | error: ${(error as {message: string}).message}`);
return '';
}
};

Expand Down Expand Up @@ -120,73 +121,39 @@ export async function testProxy(proxy: DB.Proxy) {
} catch (error) {
logger.error(error);
}
if (proxy.proxy) {
for (const pin of PIN_URL) {
const startTime = Date.now();
try {
const response = await axios.get(pin.url, {
proxy: requestProxy,
timeout: 5_000,
});
const endTime = Date.now();
const elapsedTime = endTime - startTime; // Calculate the time taken for the request
if (response.status === 200) {
result.connectivity.push({
name: pin.n,
status: 'connected',
elapsedTime: elapsedTime,
});
} else {
result.connectivity.push({
name: pin.n,
status: 'failed',
reason: `Status code: ${response.status}`,
elapsedTime: elapsedTime,
});
}
} catch (error) {
logger.error(`ping ${pin.name} failed:`, (error as AxiosError)?.message);
const endTime = Date.now();
const elapsedTime = endTime - startTime;
for (const pin of PIN_URL) {
const startTime = Date.now();
try {
const response = await axios.get(pin.url, {
proxy: proxy.proxy ? requestProxy : undefined,
timeout: 5_000,
});
const endTime = Date.now();
const elapsedTime = endTime - startTime; // Calculate the time taken for the request
if (response.status === 200) {
result.connectivity.push({
name: pin.n,
status: 'failed',
reason: (error as AxiosError)?.message,
status: 'connected',
elapsedTime: elapsedTime,
});
}
}
} else {
for (const pin of PIN_URL) {
const startTime = Date.now();
try {
const response = await axios.get(pin.url, {
timeout: 5_000,
});
if (response.status === 200) {
result.connectivity.push({
name: pin.n,
status: 'connected',
elapsedTime: 0,
});
} else {
result.connectivity.push({
name: pin.n,
status: 'failed',
reason: `Status code: ${response.status}`,
elapsedTime: 0,
});
}
} catch (error) {
const endTime = Date.now();
const elapsedTime = endTime - startTime;
} else {
result.connectivity.push({
name: pin.n,
status: 'failed',
reason: (error as AxiosError)?.message,
reason: `Status code: ${response.status}`,
elapsedTime: elapsedTime,
});
}
} catch (error) {
logger.error(`ping ${pin.name} failed:`, (error as AxiosError)?.message);
const endTime = Date.now();
const elapsedTime = endTime - startTime;
result.connectivity.push({
name: pin.n,
status: 'failed',
reason: (error as AxiosError)?.message,
elapsedTime: elapsedTime,
});
}
}
if (proxy.id) {
Expand Down
2 changes: 0 additions & 2 deletions packages/main/src/server/routes/ip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ router.get('/ping', async (req, res) => {
} catch (error) {
console.error(error);
}
// // test proxy
// const result = await testProxy(proxyData);
res.send({
pings: pings.connectivity,
});
Expand Down
3 changes: 0 additions & 3 deletions packages/main/src/server/routes/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@ router.get('/info', async (req, res) => {
if (windowData.proxy_id) {
proxyData = await ProxyDB.getById(windowData.proxy_id);
}
// get proxy info
ipInfo = await getProxyInfo(proxyData);
} catch (error) {
console.error(error);
}
// // test proxy
// const result = await testProxy(proxyData);
res.send({windowData, ipInfo});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ export const initCommonService = () => {
);

ipcMain.handle('common-save-settings', async (_, values: SettingOptions) => {
const userDataPath = app.getPath('userData');
const configFilePath = join(userDataPath, 'chrome-power-config.json');
const configFilePath = join(process.resourcesPath, 'chrome-power-config.json');

try {
writeFileSync(configFilePath, JSON.stringify(values), 'utf8');
Expand Down
File renamed without changes.
12 changes: 6 additions & 6 deletions packages/main/src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {initCommonService} from './commonService';
import {initGroupService} from './groupService';
import {initProxyService} from './proxyService';
import {initSyncService} from './syncService';
import {initTagService} from './tagService';
import {initWindowService} from './windowService';
import {initCommonService} from './common-service';
import {initGroupService} from './group-service';
import {initProxyService} from './proxy-service';
import {initSyncService} from './sync-service';
import {initTagService} from './tag-service';
import {initWindowService} from './window-service';

export async function initServices() {
initCommonService();
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {ipcMain} from 'electron';
import {readFileSync} from 'fs';
import {txtToJSON} from '../utils/txtToJson';
import {txtToJSON} from '../utils/txt-to-json';
import * as XLSX from 'xlsx';
import type {IWindowTemplate} from '../types/windowTemplate';
import type {IWindowTemplate} from '../types/window-template';
import type {DB, SafeAny} from '../../../shared/types/db';
import {WindowDB} from '../db/window';
import {closeFingerprintWindow, openFingerprintWindow} from '../fingerprint/index';
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 2 additions & 4 deletions packages/main/src/utils/get-settings.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import {app} from 'electron';
import {existsSync, readFileSync, writeFileSync} from 'fs';
import {join} from 'path';
import type {SettingOptions} from '../../../shared/types/common';
import {getChromePath} from '../fingerprint/device';

export const getSettings = (): SettingOptions => {
const userDataPath = app.getPath('userData');
const configFilePath = join(userDataPath, 'chrome-power-config.json');
const configFilePath = join(process.resourcesPath, 'chrome-power-config.json');
let settings = {
profileCachePath: `${userDataPath}\\cache`,
profileCachePath: join(process.resourcesPath, 'chromePowerCache'),
useLocalChrome: true,
localChromePath: '',
chromiumBinPath: '',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {IWindowTemplate} from '../types/windowTemplate';
import type {IWindowTemplate} from '../types/window-template';

export function txtToJSON(txt: string): IWindowTemplate[] {
const entries = txt.split('\n\n'); // 根据两个换行符来分割每一块数据
Expand Down
2 changes: 1 addition & 1 deletion packages/preload/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @module preload
*/

export {sha256sum} from './nodeCrypto';
export {sha256sum} from './node-crypto';
export {versions} from './versions';
export {customizeToolbarControl} from './customize-control';
export {WindowBridge} from './bridges/window';
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/renderer/src/pages/start/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export default function Start() {
return (
<main className="h-full bg-gradient-to-r from-purple-200 via-blue-300 to-pink-200 border-purple-200 mx-[25%]">
<header className="h-24 flex flex-col rounded-md items-center justify-center shadow-md bg-indigo-400 ">
<h1 className="text-2xl font-semibold text-white mb-2">IP: {moreInfo.ip}</h1>
<h1 className="text-2xl font-semibold text-white mb-2">IP: {moreInfo.ip || 'Disconnected'}</h1>
<div className="flex justify-center text-white">
<p>
{moreInfo.country} - {moreInfo.timeZone}
Expand Down

0 comments on commit 2ee47a6

Please sign in to comment.