From 683b334c102bff46d8247e7883801292e151e0ed Mon Sep 17 00:00:00 2001 From: pompurin404 Date: Mon, 23 Sep 2024 22:09:27 +0800 Subject: [PATCH] fix ssid --- src/main/core/manager.ts | 10 +++++++++- src/main/sys/ssid.ts | 6 ++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/main/core/manager.ts b/src/main/core/manager.ts index 9194b3d..dc6cc1e 100644 --- a/src/main/core/manager.ts +++ b/src/main/core/manager.ts @@ -264,7 +264,7 @@ export function isEncryptionAvailable(): boolean { return safeStorage.isEncryptionAvailable() } -export async function getDefaultService(password?: string): Promise { +export async function getDefaultDevice(password?: string): Promise { const execPromise = promisify(exec) let sudo = '' if (password) sudo = `echo "${password}" | sudo -S ` @@ -272,6 +272,14 @@ export async function getDefaultService(password?: string): Promise { let device = deviceOut.split('\n').find((s) => s.includes('interface:')) device = device?.trim().split(' ').slice(1).join(' ') if (!device) throw new Error('Get device failed') + return device +} + +async function getDefaultService(password?: string): Promise { + const execPromise = promisify(exec) + let sudo = '' + if (password) sudo = `echo "${password}" | sudo -S ` + const device = await getDefaultDevice(password) const { stdout: order } = await execPromise(`${sudo}networksetup -listnetworkserviceorder`) const block = order.split('\n\n').find((s) => s.includes(`Device: ${device}`)) if (!block) throw new Error('Get networkservice failed') diff --git a/src/main/sys/ssid.ts b/src/main/sys/ssid.ts index 40132df..6ca2d45 100644 --- a/src/main/sys/ssid.ts +++ b/src/main/sys/ssid.ts @@ -4,7 +4,7 @@ import { getAppConfig, patchControledMihomoConfig } from '../config' import { patchMihomoConfig } from '../core/mihomoApi' import { mainWindow } from '..' import { ipcMain, net } from 'electron' -import { getDefaultService } from '../core/manager' +import { getDefaultDevice } from '../core/manager' export async function getCurrentSSID(): Promise { if (process.platform === 'win32') { @@ -79,11 +79,9 @@ async function getSSIDByAirport(): Promise { async function getSSIDByNetworksetup(): Promise { const execPromise = promisify(exec) if (net.isOnline()) { - const service = await getDefaultService() + const service = await getDefaultDevice() const { stdout } = await execPromise(`networksetup -listpreferredwirelessnetworks ${service}`) - console.log(stdout) if (stdout.trim().startsWith('Preferred networks on')) { - console.log(stdout.split('\n')) if (stdout.split('\n').length > 1) { return stdout.split('\n')[1].trim() }