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

Commit

Permalink
use unix socket and namedpipe instead of http api
Browse files Browse the repository at this point in the history
  • Loading branch information
pompurin404 committed Sep 27, 2024
1 parent 4932b5b commit 7ed9af3
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 207 deletions.
4 changes: 0 additions & 4 deletions src/main/config/controledMihomo.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { controledMihomoConfigPath } from '../utils/dirs'
import { readFile, writeFile } from 'fs/promises'
import yaml from 'yaml'
import { getAxios } from '../core/mihomoApi'
import { generateProfile } from '../core/factory'
import { getAppConfig } from './app'
import { defaultControledMihomoConfig } from '../utils/template'
Expand Down Expand Up @@ -52,9 +51,6 @@ export async function patchControledMihomoConfig(patch: Partial<IMihomoConfig>):
if (process.platform === 'darwin') {
delete controledMihomoConfig?.tun?.device
}
if (patch['external-controller'] || patch.secret) {
await getAxios(true)
}
await generateProfile()
await writeFile(controledMihomoConfigPath(), yaml.stringify(controledMihomoConfig), 'utf-8')
}
41 changes: 27 additions & 14 deletions src/main/core/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,25 +113,19 @@ export async function startCore(detached = false): Promise<Promise<void>[]> {
})
return new Promise((resolve, reject) => {
child.stdout?.on('data', async (data) => {
if (data.toString().includes('configure tun interface: operation not permitted')) {
const str = data.toString()
if (str.includes('configure tun interface: operation not permitted')) {
patchControledMihomoConfig({ tun: { enable: false } })
mainWindow?.webContents.send('controledMihomoConfigUpdated')
ipcMain.emit('updateTrayMenu')
reject('虚拟网卡启动失败, 请尝试手动授予内核权限')
}
if (data.toString().includes('External controller listen error')) {
if (retry) {
retry--
try {
resolve(await startCore())
} catch (e) {
reject(e)
}
} else {
reject('内核连接失败, 请尝试修改外部控制端口或重启电脑')
}
}
if (data.toString().includes('RESTful API listening at')) {

if (
(process.platform !== 'win32' && str.includes('RESTful API unix listening at')) ||
(process.platform === 'win32' && str.includes('RESTful API pipe listening at'))
) {
await autoGrantUnixSocket()
resolve([
new Promise((resolve) => {
child.stdout?.on('data', async (data) => {
Expand Down Expand Up @@ -223,6 +217,25 @@ async function checkProfile(): Promise<void> {
}
}

async function autoGrantUnixSocket(): Promise<void> {
if (process.platform === 'win32') return
const { encryptedPassword } = await getAppConfig()
const { 'external-controller-unix': mihomoUnix = 'mihomo-party.sock' } =
await getControledMihomoConfig()
const execPromise = promisify(exec)
if (encryptedPassword && isEncryptionAvailable()) {
try {
const password = safeStorage.decryptString(Buffer.from(encryptedPassword))
await execPromise(
`echo "${password}" | sudo -S chmod 777 "${path.join(mihomoWorkDir(), mihomoUnix)}"`
)
} catch (error) {
patchAppConfig({ encryptedPassword: undefined })
throw error
}
}
}

export async function autoGrantCorePermition(corePath: string): Promise<void> {
if (process.platform === 'win32') return
const { encryptedPassword } = await getAppConfig()
Expand Down
Loading

0 comments on commit 7ed9af3

Please sign in to comment.