This commit is contained in:
pompurin404 2024-09-23 22:09:27 +08:00
parent 59203d3855
commit bbb0efd1cf
No known key found for this signature in database
2 changed files with 11 additions and 5 deletions

View File

@ -264,7 +264,7 @@ export function isEncryptionAvailable(): boolean {
return safeStorage.isEncryptionAvailable()
}
export async function getDefaultService(password?: string): Promise<string> {
export async function getDefaultDevice(password?: string): Promise<string> {
const execPromise = promisify(exec)
let sudo = ''
if (password) sudo = `echo "${password}" | sudo -S `
@ -272,6 +272,14 @@ export async function getDefaultService(password?: string): Promise<string> {
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<string> {
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')

View File

@ -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<string | undefined> {
if (process.platform === 'win32') {
@ -79,11 +79,9 @@ async function getSSIDByAirport(): Promise<string | undefined> {
async function getSSIDByNetworksetup(): Promise<string | undefined> {
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()
}