Skip to content

Commit

Permalink
🐛 修复切换代理闪退
Browse files Browse the repository at this point in the history
  • Loading branch information
InfernalAzazel committed Jul 23, 2023
1 parent 605bd7b commit 6b52fee
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 57 deletions.
1 change: 0 additions & 1 deletion auto-imports.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable */
/* prettier-ignore */
// @ts-nocheck
// noinspection JSUnusedGlobalSymbols
// Generated by unplugin-auto-import
export {}
declare global {
Expand Down
91 changes: 58 additions & 33 deletions electron/main.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
import { app, BrowserWindow, ipcMain, dialog, clipboard, Notification } from 'electron'
import type { IpcMainEvent } from 'electron'
import {spawn} from "child_process";
import * as path from "path";
import * as fs from 'fs';

const userDataPath = app.getPath('userData');
const filePath = path.join(userDataPath, 'setting.yaml');

let childProcess; // 保存子进程对象
let abortController; // 保存AbortController对象
const showNotification = () => {
const notification = {
title: '通知',
body: '操作成功'
}
new Notification(notification).show()
}
app.whenReady().then(() => {
let childPID = 0

const win = new BrowserWindow({
title: 'gostX',
width: 1000,
height: 800,
webPreferences: {
contextIsolation: false,
nodeIntegration: true,
nodeIntegrationInWorker: true,
}
})
const userDataPath = app.getPath('userData');
const filePath = path.join(userDataPath, 'setting.yaml');
console.log(filePath)
const ensureSettingFileExists = async () => {
fs.access(filePath, fs.constants.F_OK, (err) => {
if (err) {
const setting = 'path:\nproxy:'
Expand All @@ -40,39 +31,73 @@ app.whenReady().then(() => {
console.log('File exists');
}
});
};
const runCommand = (event: IpcMainEvent, command, args) => {
if (childProcess) {
childProcess.kill()
}
const options = {
shell: false,
detached: false,
};
childProcess = spawn(command, args, options as any);
childProcess.stdout.on('data', (data) => {
event.reply('command-output', data.toString())
});
childProcess.stderr.on('data', (data) => {
event.reply('command-output', data.toString())
});
childProcess.on('close', (code) => {
event.reply('command-close', code)
})
}


app.whenReady().then(async () => {
let childPID = 0

const win = new BrowserWindow({
title: 'gostX',
width: 1000,
height: 800,
webPreferences: {
contextIsolation: false,
nodeIntegration: true,
nodeIntegrationInWorker: true,
}
})
await ensureSettingFileExists()

if(process.env.NODE_ENV !== 'development'){
win.setMenuBarVisibility(false)
}

// You can use `process.env.VITE_DEV_SERVER_URL` when the vite command is called `serve`
if (process.env.VITE_DEV_SERVER_URL) {
win.loadURL(process.env.VITE_DEV_SERVER_URL)
await win.loadURL(process.env.VITE_DEV_SERVER_URL)
} else {
// Load your file
win.loadFile('dist/index.html');
await win.loadFile('dist/index.html');
}

ipcMain.on('run-command', (event, command, args) => {
console.log(command, args)
const child = spawn(command, args)
childPID = child.pid
child.stdout.on('data', (data) => {
event.reply('command-output', data.toString())
})
child.stderr.on('data', (data) => {
event.reply('command-error', data.toString())
})
child.on('close', (code) => {
childPID = 0
event.reply('command-close', code)
})
runCommand(event, command, args)
})
ipcMain.on('get-command-pid', (event, args) => {
event.reply('get-command-pid', childPID)
ipcMain.on('get-command-state', (event, args) => {
if (childProcess){
event.reply('get-command-state', true)
}else {
event.reply('get-command-state', true)
}

})
ipcMain.on('command-kill', (event, args) => {
process.kill(childPID)
if (childProcess) {
childProcess.kill()
console.log('kill command')
}
childProcess = null
})
ipcMain.on('read-setting', (event, args) => {
fs.readFile(filePath, 'utf8', (err, data) => {
Expand Down
2 changes: 1 addition & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
<script lang="ts" setup>
import {provideProxyStore, provideProcessesStore} from "@/store";
provideProxyStore([])
provideProcessesStore(0)
provideProcessesStore(false)
</script>
2 changes: 1 addition & 1 deletion src/layout/base.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<el-container class="h-screen">
<el-header class="flex items-center bg-slate-50">
<p class="italic text-clip text-3xl text-blue-400">Gost</p>
<p class="italic text-clip text-3xl text-blue-400">Gostx</p>
<div class="flex flex-row-reverse w-full">
<div class="flex items-center">
<el-button type="info" icon="Star" @click="openLink" link>关于</el-button>
Expand Down
29 changes: 8 additions & 21 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,24 @@ export const [provideProxyStore, useProxyStore] = createInjectionState((initialV
return {proxy, gostPath, readSetting, writeSetting}
})

export const [provideProcessesStore, useProcessesStore] = createInjectionState((initialValue: number) => {
export const [provideProcessesStore, useProcessesStore] = createInjectionState((initialValue: boolean) => {
// state
const pid = ref(initialValue)
const runState = ref<boolean>(initialValue)
const cmdID = ref<number>(-1)
const outputs = ref<string[]>([])

const running = ref<Proxy>({id: -1, name: '', cmd: ''})
const runCommand = (id: number, command: string | undefined, value: string) => {
if (pid.value !== 0) {
kill();
}
cmdID.value = id
let args = value.split(" ");
ipcRenderer.send('run-command',command, args)
// 监听命令的输出
ipcRenderer.on('command-output', (event, data) => {
outputs.value.push(data)
})

// 监听命令的错误输出
ipcRenderer.on('command-error', (event, data) => {
outputs.value.push(data)
})

// 监听命令结束
ipcRenderer.on('command-close', (event, code) => {
console.log('Command finished with code', code)
})
ipcRenderer.send('get-command-pid')
ipcRenderer.on('get-command-pid', (event, childPID) => {
pid.value = childPID;
ipcRenderer.send('get-command-state')
ipcRenderer.on('get-command-state', (event, state) => {
runState.value = state;
})

}
Expand All @@ -69,9 +57,8 @@ export const [provideProcessesStore, useProcessesStore] = createInjectionState((
cmdID.value = -1
running.value = {id: -1, name: '', cmd: ''}
}
ipcRenderer.send('command-kill', pid.value)
pid.value = 0
ipcRenderer.send('command-kill')
}

return {pid, cmdID, running, outputs, runCommand, kill}
return {cmdID, running, outputs, runCommand, kill}
})

0 comments on commit 6b52fee

Please sign in to comment.