revert(Proxy): remove proxyManager usage from multiple services (#3720)
* revert(Proxy): remove proxyManager usage from multiple services * refactor(ProxyManager): streamline proxy configuration and management
This commit is contained in:
parent
36966cfc14
commit
4927f98e59
@ -85,7 +85,7 @@
|
|||||||
"npx-scope-finder": "^1.2.0",
|
"npx-scope-finder": "^1.2.0",
|
||||||
"officeparser": "^4.1.1",
|
"officeparser": "^4.1.1",
|
||||||
"p-queue": "^8.1.0",
|
"p-queue": "^8.1.0",
|
||||||
"socks-proxy-agent": "^8.0.3",
|
"proxy-agent": "^6.5.0",
|
||||||
"tar": "^7.4.3",
|
"tar": "^7.4.3",
|
||||||
"tokenx": "^0.4.1",
|
"tokenx": "^0.4.1",
|
||||||
"undici": "^7.4.0",
|
"undici": "^7.4.0",
|
||||||
|
|||||||
@ -3,14 +3,12 @@ import { FileType } from '@types'
|
|||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
|
|
||||||
import { CacheService } from './CacheService'
|
import { CacheService } from './CacheService'
|
||||||
import { proxyManager } from './ProxyManager'
|
|
||||||
|
|
||||||
export class GeminiService {
|
export class GeminiService {
|
||||||
private static readonly FILE_LIST_CACHE_KEY = 'gemini_file_list'
|
private static readonly FILE_LIST_CACHE_KEY = 'gemini_file_list'
|
||||||
private static readonly CACHE_DURATION = 3000
|
private static readonly CACHE_DURATION = 3000
|
||||||
|
|
||||||
static async uploadFile(_: Electron.IpcMainInvokeEvent, file: FileType, apiKey: string) {
|
static async uploadFile(_: Electron.IpcMainInvokeEvent, file: FileType, apiKey: string) {
|
||||||
proxyManager.setGlobalProxy()
|
|
||||||
const fileManager = new GoogleAIFileManager(apiKey)
|
const fileManager = new GoogleAIFileManager(apiKey)
|
||||||
const uploadResult = await fileManager.uploadFile(file.path, {
|
const uploadResult = await fileManager.uploadFile(file.path, {
|
||||||
mimeType: 'application/pdf',
|
mimeType: 'application/pdf',
|
||||||
@ -31,7 +29,6 @@ export class GeminiService {
|
|||||||
file: FileType,
|
file: FileType,
|
||||||
apiKey: string
|
apiKey: string
|
||||||
): Promise<FileMetadataResponse | undefined> {
|
): Promise<FileMetadataResponse | undefined> {
|
||||||
proxyManager.setGlobalProxy()
|
|
||||||
const fileManager = new GoogleAIFileManager(apiKey)
|
const fileManager = new GoogleAIFileManager(apiKey)
|
||||||
|
|
||||||
const cachedResponse = CacheService.get<any>(GeminiService.FILE_LIST_CACHE_KEY)
|
const cachedResponse = CacheService.get<any>(GeminiService.FILE_LIST_CACHE_KEY)
|
||||||
@ -55,13 +52,11 @@ export class GeminiService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async listFiles(_: Electron.IpcMainInvokeEvent, apiKey: string) {
|
static async listFiles(_: Electron.IpcMainInvokeEvent, apiKey: string) {
|
||||||
proxyManager.setGlobalProxy()
|
|
||||||
const fileManager = new GoogleAIFileManager(apiKey)
|
const fileManager = new GoogleAIFileManager(apiKey)
|
||||||
return await fileManager.listFiles()
|
return await fileManager.listFiles()
|
||||||
}
|
}
|
||||||
|
|
||||||
static async deleteFile(_: Electron.IpcMainInvokeEvent, apiKey: string, fileId: string) {
|
static async deleteFile(_: Electron.IpcMainInvokeEvent, apiKey: string, fileId: string) {
|
||||||
proxyManager.setGlobalProxy()
|
|
||||||
const fileManager = new GoogleAIFileManager(apiKey)
|
const fileManager = new GoogleAIFileManager(apiKey)
|
||||||
await fileManager.deleteFile(fileId)
|
await fileManager.deleteFile(fileId)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,7 +24,6 @@ import { WebLoader } from '@llm-tools/embedjs-loader-web'
|
|||||||
import { AzureOpenAiEmbeddings, OpenAiEmbeddings } from '@llm-tools/embedjs-openai'
|
import { AzureOpenAiEmbeddings, OpenAiEmbeddings } from '@llm-tools/embedjs-openai'
|
||||||
import { addFileLoader } from '@main/loader'
|
import { addFileLoader } from '@main/loader'
|
||||||
import Reranker from '@main/reranker/Reranker'
|
import Reranker from '@main/reranker/Reranker'
|
||||||
import { proxyManager } from '@main/services/ProxyManager'
|
|
||||||
import { windowService } from '@main/services/WindowService'
|
import { windowService } from '@main/services/WindowService'
|
||||||
import { getInstanceName } from '@main/utils'
|
import { getInstanceName } from '@main/utils'
|
||||||
import { getAllFiles } from '@main/utils/file'
|
import { getAllFiles } from '@main/utils/file'
|
||||||
@ -125,14 +124,12 @@ class KnowledgeService {
|
|||||||
azureOpenAIApiVersion: apiVersion,
|
azureOpenAIApiVersion: apiVersion,
|
||||||
azureOpenAIApiDeploymentName: model,
|
azureOpenAIApiDeploymentName: model,
|
||||||
azureOpenAIApiInstanceName: getInstanceName(baseURL),
|
azureOpenAIApiInstanceName: getInstanceName(baseURL),
|
||||||
configuration: { httpAgent: proxyManager.getProxyAgent() },
|
|
||||||
dimensions,
|
dimensions,
|
||||||
batchSize
|
batchSize
|
||||||
})
|
})
|
||||||
: new OpenAiEmbeddings({
|
: new OpenAiEmbeddings({
|
||||||
model,
|
model,
|
||||||
apiKey,
|
apiKey,
|
||||||
configuration: { baseURL, httpAgent: proxyManager.getProxyAgent() },
|
|
||||||
dimensions,
|
dimensions,
|
||||||
batchSize
|
batchSize
|
||||||
})
|
})
|
||||||
@ -426,7 +423,6 @@ class KnowledgeService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public add = (_: Electron.IpcMainInvokeEvent, options: KnowledgeBaseAddItemOptions): Promise<LoaderReturn> => {
|
public add = (_: Electron.IpcMainInvokeEvent, options: KnowledgeBaseAddItemOptions): Promise<LoaderReturn> => {
|
||||||
proxyManager.setGlobalProxy()
|
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
const { base, item, forceReload = false } = options
|
const { base, item, forceReload = false } = options
|
||||||
const optionsNonNullableAttribute = { base, item, forceReload }
|
const optionsNonNullableAttribute = { base, item, forceReload }
|
||||||
|
|||||||
@ -1,25 +1,23 @@
|
|||||||
import { ProxyConfig as _ProxyConfig, session } from 'electron'
|
import { ProxyConfig as _ProxyConfig, session } from 'electron'
|
||||||
import { socksDispatcher } from 'fetch-socks'
|
import { socksDispatcher } from 'fetch-socks'
|
||||||
import { HttpsProxyAgent } from 'https-proxy-agent'
|
import { ProxyAgent as GeneralProxyAgent } from 'proxy-agent'
|
||||||
import { ProxyAgent, setGlobalDispatcher } from 'undici'
|
import { ProxyAgent, setGlobalDispatcher } from 'undici'
|
||||||
|
|
||||||
type ProxyMode = 'system' | 'custom' | 'none'
|
type ProxyMode = 'system' | 'custom' | 'none'
|
||||||
|
|
||||||
export interface ProxyConfig {
|
export interface ProxyConfig {
|
||||||
mode: ProxyMode
|
mode: ProxyMode
|
||||||
url?: string | null
|
url?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ProxyManager {
|
export class ProxyManager {
|
||||||
private config: ProxyConfig
|
private config: ProxyConfig
|
||||||
private proxyAgent: HttpsProxyAgent | null = null
|
private proxyAgent: GeneralProxyAgent | null = null
|
||||||
private proxyUrl: string | null = null
|
|
||||||
private systemProxyInterval: NodeJS.Timeout | null = null
|
private systemProxyInterval: NodeJS.Timeout | null = null
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.config = {
|
this.config = {
|
||||||
mode: 'none',
|
mode: 'none'
|
||||||
url: ''
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,7 +49,7 @@ export class ProxyManager {
|
|||||||
if (this.config.mode === 'system') {
|
if (this.config.mode === 'system') {
|
||||||
await this.setSystemProxy()
|
await this.setSystemProxy()
|
||||||
this.monitorSystemProxy()
|
this.monitorSystemProxy()
|
||||||
} else if (this.config.mode == 'custom') {
|
} else if (this.config.mode === 'custom') {
|
||||||
await this.setCustomProxy()
|
await this.setCustomProxy()
|
||||||
} else {
|
} else {
|
||||||
await this.clearProxy()
|
await this.clearProxy()
|
||||||
@ -73,11 +71,14 @@ export class ProxyManager {
|
|||||||
private async setSystemProxy(): Promise<void> {
|
private async setSystemProxy(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
await this.setSessionsProxy({ mode: 'system' })
|
await this.setSessionsProxy({ mode: 'system' })
|
||||||
const url = await this.resolveSystemProxy()
|
const proxyString = await session.defaultSession.resolveProxy('https://dummy.com')
|
||||||
if (url && url !== this.proxyUrl) {
|
const [protocol, address] = proxyString.split(';')[0].split(' ')
|
||||||
this.proxyUrl = url.toLowerCase()
|
console.log('protocol', protocol)
|
||||||
this.proxyAgent = new HttpsProxyAgent(this.proxyUrl)
|
const url = protocol === 'PROXY' ? `http://${address}` : null
|
||||||
this.setEnvironment(this.proxyUrl)
|
if (url && url !== this.config.url) {
|
||||||
|
this.config.url = url.toLowerCase()
|
||||||
|
this.setEnvironment(this.config.url)
|
||||||
|
this.proxyAgent = new GeneralProxyAgent()
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to set system proxy:', error)
|
console.error('Failed to set system proxy:', error)
|
||||||
@ -88,10 +89,9 @@ export class ProxyManager {
|
|||||||
private async setCustomProxy(): Promise<void> {
|
private async setCustomProxy(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
if (this.config.url) {
|
if (this.config.url) {
|
||||||
this.proxyUrl = this.config.url.toLowerCase()
|
this.setEnvironment(this.config.url)
|
||||||
this.proxyAgent = new HttpsProxyAgent(this.proxyUrl)
|
this.proxyAgent = new GeneralProxyAgent()
|
||||||
this.setEnvironment(this.proxyUrl)
|
await this.setSessionsProxy({ proxyRules: this.config.url })
|
||||||
await this.setSessionsProxy({ proxyRules: this.proxyUrl })
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to set custom proxy:', error)
|
console.error('Failed to set custom proxy:', error)
|
||||||
@ -99,45 +99,31 @@ export class ProxyManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async clearProxy(): Promise<void> {
|
private clearEnvironment(): void {
|
||||||
delete process.env.HTTP_PROXY
|
delete process.env.HTTP_PROXY
|
||||||
delete process.env.HTTPS_PROXY
|
delete process.env.HTTPS_PROXY
|
||||||
await this.setSessionsProxy({})
|
delete process.env.grpc_proxy
|
||||||
|
delete process.env.http_proxy
|
||||||
|
delete process.env.https_proxy
|
||||||
|
}
|
||||||
|
|
||||||
|
private async clearProxy(): Promise<void> {
|
||||||
|
this.clearEnvironment()
|
||||||
|
await this.setSessionsProxy({ mode: 'direct' })
|
||||||
this.config = { mode: 'none' }
|
this.config = { mode: 'none' }
|
||||||
this.proxyAgent = null
|
this.proxyAgent = null
|
||||||
this.proxyUrl = null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async resolveSystemProxy(): Promise<string | null> {
|
getProxyAgent(): GeneralProxyAgent | null {
|
||||||
try {
|
|
||||||
return await this.resolveElectronProxy()
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Failed to resolve system proxy:', error)
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private async resolveElectronProxy(): Promise<string | null> {
|
|
||||||
try {
|
|
||||||
const proxyString = await session.defaultSession.resolveProxy('https://dummy.com')
|
|
||||||
const [protocol, address] = proxyString.split(';')[0].split(' ')
|
|
||||||
return protocol === 'PROXY' ? `http://${address}` : null
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Failed to resolve electron proxy:', error)
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
getProxyAgent(): HttpsProxyAgent | null {
|
|
||||||
return this.proxyAgent
|
return this.proxyAgent
|
||||||
}
|
}
|
||||||
|
|
||||||
getProxyUrl(): string | null {
|
getProxyUrl(): string {
|
||||||
return this.proxyUrl
|
return this.config.url || ''
|
||||||
}
|
}
|
||||||
|
|
||||||
setGlobalProxy() {
|
setGlobalProxy() {
|
||||||
const proxyUrl = this.proxyUrl
|
const proxyUrl = this.config.url
|
||||||
if (proxyUrl) {
|
if (proxyUrl) {
|
||||||
const [protocol, address] = proxyUrl.split('://')
|
const [protocol, address] = proxyUrl.split('://')
|
||||||
const [host, port] = address.split(':')
|
const [host, port] = address.split(':')
|
||||||
|
|||||||
@ -1,7 +1,5 @@
|
|||||||
import { proxyManager } from '@main/services/ProxyManager'
|
|
||||||
import { WebDavConfig } from '@types'
|
import { WebDavConfig } from '@types'
|
||||||
import Logger from 'electron-log'
|
import Logger from 'electron-log'
|
||||||
import { HttpProxyAgent } from 'http-proxy-agent'
|
|
||||||
import Stream from 'stream'
|
import Stream from 'stream'
|
||||||
import { BufferLike, createClient, GetFileContentsOptions, PutFileContentsOptions, WebDAVClient } from 'webdav'
|
import { BufferLike, createClient, GetFileContentsOptions, PutFileContentsOptions, WebDAVClient } from 'webdav'
|
||||||
export default class WebDav {
|
export default class WebDav {
|
||||||
@ -10,15 +8,12 @@ export default class WebDav {
|
|||||||
|
|
||||||
constructor(params: WebDavConfig) {
|
constructor(params: WebDavConfig) {
|
||||||
this.webdavPath = params.webdavPath
|
this.webdavPath = params.webdavPath
|
||||||
const url = proxyManager.getProxyUrl()
|
|
||||||
|
|
||||||
this.instance = createClient(params.webdavHost, {
|
this.instance = createClient(params.webdavHost, {
|
||||||
username: params.webdavUser,
|
username: params.webdavUser,
|
||||||
password: params.webdavPass,
|
password: params.webdavPass,
|
||||||
maxBodyLength: Infinity,
|
maxBodyLength: Infinity,
|
||||||
maxContentLength: Infinity,
|
maxContentLength: Infinity
|
||||||
httpAgent: url ? new HttpProxyAgent(url) : undefined,
|
|
||||||
httpsAgent: proxyManager.getProxyAgent()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
this.putFileContents = this.putFileContents.bind(this)
|
this.putFileContents = this.putFileContents.bind(this)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user