fix: remove undici dependency and clean up ProxyManager code (#6020)

This commit is contained in:
MyPrototypeWhat
2025-05-15 22:09:53 +08:00
committed by GitHub
parent 0d7f965e29
commit 1e40bd013d
6 changed files with 48 additions and 45 deletions
+10 -9
View File
@@ -8,7 +8,6 @@ import { IpcChannel } from '@shared/IpcChannel'
import { Shortcut, ThemeMode } from '@types'
import { BrowserWindow, ipcMain, nativeTheme, session, shell } from 'electron'
import log from 'electron-log'
import { getFolderSizeBin } from 'go-get-folder-size'
import { titleBarOverlayDark, titleBarOverlayLight } from './config'
import AppUpdater from './services/AppUpdater'
@@ -30,7 +29,7 @@ import storeSyncService from './services/StoreSyncService'
import { TrayService } from './services/TrayService'
import { setOpenLinkExternal } from './services/WebviewService'
import { windowService } from './services/WindowService'
import { getResourcePath } from './utils'
import { calculateDirectorySize, getResourcePath } from './utils'
import { decrypt, encrypt } from './utils/aes'
import { getCacheDir, getConfigDir, getFilesDir } from './utils/file'
import { compress, decompress } from './utils/zip'
@@ -184,14 +183,16 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) {
// get cache size
ipcMain.handle(IpcChannel.App_GetCacheSize, async () => {
const cachePath = getCacheDir()
const size = await getFolderSizeBin(cachePath, true, {
// ignore files that we can't access
loose: true
}).catch((err) => {
log.error('Failed to get cache size:', err)
})
log.info(`Calculating cache size for path: ${cachePath}`)
return size || '0MB'
try {
const sizeInBytes = await calculateDirectorySize(cachePath)
const sizeInMB = (sizeInBytes / (1024 * 1024)).toFixed(2)
return `${sizeInMB}`
} catch (error: any) {
log.error(`Failed to calculate cache size for ${cachePath}: ${error.message}`)
return '0'
}
})
// check for update
+17 -18
View File
@@ -1,8 +1,7 @@
import { ProxyConfig as _ProxyConfig, session } from 'electron'
import { socksDispatcher } from 'fetch-socks'
import { getSystemProxy } from 'os-proxy-config'
import { ProxyAgent as GeneralProxyAgent } from 'proxy-agent'
import { ProxyAgent, setGlobalDispatcher } from 'undici'
// import { ProxyAgent, setGlobalDispatcher } from 'undici'
type ProxyMode = 'system' | 'custom' | 'none'
@@ -121,22 +120,22 @@ export class ProxyManager {
return this.config.url || ''
}
setGlobalProxy() {
const proxyUrl = this.config.url
if (proxyUrl) {
const [protocol, address] = proxyUrl.split('://')
const [host, port] = address.split(':')
if (!protocol.includes('socks')) {
setGlobalDispatcher(new ProxyAgent(proxyUrl))
} else {
global[Symbol.for('undici.globalDispatcher.1')] = socksDispatcher({
port: parseInt(port),
type: protocol === 'socks5' ? 5 : 4,
host: host
})
}
}
}
// setGlobalProxy() {
// const proxyUrl = this.config.url
// if (proxyUrl) {
// const [protocol, address] = proxyUrl.split('://')
// const [host, port] = address.split(':')
// if (!protocol.includes('socks')) {
// setGlobalDispatcher(new ProxyAgent(proxyUrl))
// } else {
// global[Symbol.for('undici.globalDispatcher.1')] = socksDispatcher({
// port: parseInt(port),
// type: protocol === 'socks5' ? 5 : 4,
// host: host
// })
// }
// }
// }
}
export const proxyManager = new ProxyManager()
+18
View File
@@ -1,4 +1,5 @@
import fs from 'node:fs'
import fsAsync from 'node:fs/promises'
import path from 'node:path'
import { app } from 'electron'
@@ -52,3 +53,20 @@ export function makeSureDirExists(dir: string) {
fs.mkdirSync(dir, { recursive: true })
}
}
export async function calculateDirectorySize(directoryPath: string): Promise<number> {
let totalSize = 0
const items = await fsAsync.readdir(directoryPath)
for (const item of items) {
const itemPath = path.join(directoryPath, item)
const stats = await fsAsync.stat(itemPath)
if (stats.isFile()) {
totalSize += stats.size
} else if (stats.isDirectory()) {
totalSize += await calculateDirectorySize(itemPath)
}
}
return totalSize
}
@@ -240,7 +240,7 @@ const DataSettings: FC = () => {
<SettingRow>
<SettingRowTitle>
{t('settings.data.clear_cache.title')}
<CacheText>({cacheSize})</CacheText>
{cacheSize && <CacheText>({cacheSize}MB)</CacheText>}
</SettingRowTitle>
<HStack gap="5px">
<Button onClick={handleClearCache} danger>