From 9a204b2b35d2cd97968fd7b94dc31c93bb4f96f5 Mon Sep 17 00:00:00 2001 From: _ChingC Date: Mon, 29 Aug 2022 20:10:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0main=E4=B8=ADaxios?= =?UTF-8?q?=E7=9A=84proxy=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/componentManager/componentInstaller.ts | 16 +++++++++++++++- packages/main/deviceDetector/utils.ts | 1 - packages/main/hooks/asst.ts | 2 +- packages/main/utils/proxy.ts | 9 +++++++++ 4 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 packages/main/utils/proxy.ts diff --git a/packages/main/componentManager/componentInstaller.ts b/packages/main/componentManager/componentInstaller.ts index 35c383db..7b186f2b 100644 --- a/packages/main/componentManager/componentInstaller.ts +++ b/packages/main/componentManager/componentInstaller.ts @@ -4,6 +4,8 @@ import fs from 'fs' import path from 'path' import logger from '@main/utils/logger' import DownloadManager from '@main/downloadManager' +import detectSystemProxy from '@main/utils/proxy' +import WindowManager from '@main/windowManager' interface DownloadHandle { handleDownloadUpdate: (task: DownloadTask) => void @@ -83,9 +85,21 @@ abstract class ComponentInstaller { if (this.releaseTemp && Date.now() - this.releaseTemp.updated < 5 * 60 * 1000) { return this.releaseTemp.data } + const proxyUrl = await detectSystemProxy(new WindowManager().getWindow()) + const { protocol, hostname, port, username, password } = new URL(proxyUrl) + const url = 'https://api.github.com/repos/MaaAssistantArknights/MaaAssistantArknights/releases' const releaseResponse = await axios.get(url, { - adapter: require('axios/lib/adapters/http.js') + adapter: require('axios/lib/adapters/http.js'), + proxy: { + host: hostname, + port: Number(port), + protocol, + auth: { + username, + password + } + } }) this.releaseTemp = { data: releaseResponse.data[0], updated: Date.now() } return this.releaseTemp.data diff --git a/packages/main/deviceDetector/utils.ts b/packages/main/deviceDetector/utils.ts index cc0d9790..44e4a448 100644 --- a/packages/main/deviceDetector/utils.ts +++ b/packages/main/deviceDetector/utils.ts @@ -26,7 +26,6 @@ export async function getDeviceUuid (address: string, adbPath = defaultAdbPath): const connectResult = (await $$(adbPath, ['connect', address])).stdout if (connectResult.includes('connected')) { const ret = await $`"${adbPath}" -s ${address} shell settings get secure android_id` - console.log(ret.stdout) if (ret) return _.trim(ret.stdout) } return false diff --git a/packages/main/hooks/asst.ts b/packages/main/hooks/asst.ts index 2c3a5fb7..c3311eab 100644 --- a/packages/main/hooks/asst.ts +++ b/packages/main/hooks/asst.ts @@ -93,9 +93,9 @@ export default function useAsstHooks (): void { }) ipcMainHandle('main.CoreLoader:dispose', (_event) => { - core.dispose() for (const eventName of Object.keys(asstHooks)) { ipcMainRemove(eventName as IpcMainHandleEvent) } + core.dispose() }) } diff --git a/packages/main/utils/proxy.ts b/packages/main/utils/proxy.ts new file mode 100644 index 00000000..5e804bd4 --- /dev/null +++ b/packages/main/utils/proxy.ts @@ -0,0 +1,9 @@ +import type { BrowserWindow } from 'electron' + +async function detectSystemProxy (window: BrowserWindow): Promise { + const ses = window.webContents.session + const proxy = await ses.resolveProxy('https://www.google.com') + return `http://${proxy.replace('PROXY', '').trim()}` +} + +export default detectSystemProxy