Skip to content
This repository has been archived by the owner on Feb 5, 2025. It is now read-only.

Commit

Permalink
fix ssid
Browse files Browse the repository at this point in the history
  • Loading branch information
pompurin404 committed Sep 23, 2024
1 parent 4507593 commit 683b334
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
10 changes: 9 additions & 1 deletion src/main/core/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,22 @@ 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 `
const { stdout: deviceOut } = await execPromise(`${sudo}route -n get default`)
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')
Expand Down
6 changes: 2 additions & 4 deletions src/main/sys/ssid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit 683b334

Please sign in to comment.