Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ› fix: fix socks5 proxy not work problem (#9053)

Shinji-Lib13563cb3437
diff --git a/apps/desktop/package.json b/apps/desktop/package.json
index a4b573817ba..d6be54c2bff 100644
--- a/apps/desktop/package.json
+++ b/apps/desktop/package.json
@@ -31,6 +31,7 @@
"dependencies": {
"electron-updater": "^6.6.2",
"electron-window-state": "^5.0.3",
+ "fetch-socks": "^1.3.2",
"get-port-please": "^3.1.2",
"pdfjs-dist": "4.10.38"
},
diff --git a/apps/desktop/src/main/modules/networkProxy/dispatcher.ts b/apps/desktop/src/main/modules/networkProxy/dispatcher.ts
index 254df75eb1b..5457f7e9414 100644
--- a/apps/desktop/src/main/modules/networkProxy/dispatcher.ts
+++ b/apps/desktop/src/main/modules/networkProxy/dispatcher.ts
@@ -1,4 +1,5 @@
import { NetworkProxySettings } from '@lobechat/electron-client-ipc';
+import { SocksProxies, socksDispatcher } from 'fetch-socks';
import { Agent, ProxyAgent, getGlobalDispatcher, setGlobalDispatcher } from 'undici';
import { createLogger } from '@/utils/logger';
@@ -91,8 +92,29 @@ export class ProxyDispatcherManager {
*/
static createProxyAgent(proxyType: string, proxyUrl: string) {
try {
- // undici ηš„ ProxyAgent ζ”―ζŒ http, https ε’Œ socks5
- return new ProxyAgent({ uri: proxyUrl });
+ if (proxyType === 'socks5') {
+ // 解析 SOCKS5 代理 URL
+ const url = new URL(proxyUrl);
+ const socksProxies: SocksProxies = [
+ {
+ host: url.hostname,
+ port: parseInt(url.port, 10),
+ type: 5,
+ ...(url.username && url.password
+ ? {
+ password: url.password,
+ userId: url.username,
+ }
+ : {}),
+ },
+ ];
+
+ // 使用 fetch-socks 倄理 SOCKS5 代理
+ return socksDispatcher(socksProxies);
+ } else {
+ // undici ηš„ ProxyAgent ζ”―ζŒ http, https
+ return new ProxyAgent({ uri: proxyUrl });
+ }
} catch (error) {
logger.error(`Failed to create proxy agent for ${proxyType}:`, error);
throw new Error(