diff --git a/electron/main/index.ts b/electron/main/index.ts index 919879d..4ab2aa4 100644 --- a/electron/main/index.ts +++ b/electron/main/index.ts @@ -311,7 +311,10 @@ class FrpcDesktopApp { ); BeanFactory.setBean( "proxyService", - new ProxyService(BeanFactory.getBean("proxyRepository")) + new ProxyService( + BeanFactory.getBean("proxyRepository"), + BeanFactory.getBean("frpcProcessService"), + ) ); BeanFactory.setBean( "configController", diff --git a/electron/service/FrpcProcessService.ts b/electron/service/FrpcProcessService.ts index b908ac0..6ef9a4d 100644 --- a/electron/service/FrpcProcessService.ts +++ b/electron/service/FrpcProcessService.ts @@ -26,6 +26,10 @@ class FrpcProcessService { return false; } try { + Logger.debug( + `FrpcProcessService.isRunning`, + `pid: ${this._frpcProcess.pid}` + ); process.kill(this._frpcProcess.pid, 0); return true; } catch (err) { @@ -84,6 +88,45 @@ class FrpcProcessService { } } + async reloadFrpcProcess() { + if (!this.isRunning()) { + return; + } + const config = await this._serverService.getServerConfig(); + if (!config) { + throw new BusinessError(ResponseCode.NOT_CONFIG); + } + const version = await this._versionDao.findByGithubReleaseId( + config.frpcVersion + ); + const configPath = PathUtils.getTomlConfigFilePath(); + await this._serverService.genTomlConfig(configPath); + const command = `./${PathUtils.getFrpcFilename()} reload -c "${configPath}"`; + require("child_process").exec( + command, + { + cwd: version.localPath, + shell: true + }, + (error, stdout, stderr) => { + if (error) { + Logger.error(`FrpcProcessService.reloadFrpcProcess`, error); + return; + } + if (stderr) { + Logger.debug( + `FrpcProcessService.reloadFrpcProcess`, + `stderr: ${stderr}` + ); + } + Logger.debug( + `FrpcProcessService.reloadFrpcProcess`, + `stderr: ${stdout}` + ); + } + ); + } + watchFrpcProcess(listenerParam: ListenerParam) { this._frpcProcessListener = setInterval(() => { const running = this.isRunning(); diff --git a/electron/service/ProxyService.ts b/electron/service/ProxyService.ts index a956fc7..681cced 100644 --- a/electron/service/ProxyService.ts +++ b/electron/service/ProxyService.ts @@ -1,25 +1,33 @@ import ProxyRepository from "../repository/ProxyRepository"; - -const { exec } = require("child_process"); +import FrpcProcessService from "./FrpcProcessService"; class ProxyService { - private readonly _proxyDao: ProxyRepository; + private readonly _frpcProcessService: FrpcProcessService; - constructor(public proxyDao: ProxyRepository) { + constructor( + public proxyDao: ProxyRepository, + frpcProcessService: FrpcProcessService + ) { this._proxyDao = proxyDao; + this._frpcProcessService = frpcProcessService; } async insertProxy(proxy: FrpcProxy) { - return await this._proxyDao.insert(proxy); + const proxy2 = await this._proxyDao.insert(proxy); + await this._frpcProcessService.reloadFrpcProcess(); + return proxy2; } async updateProxy(proxy: FrpcProxy) { - return await this._proxyDao.updateById(proxy._id, proxy); + const proxy2 = await this._proxyDao.updateById(proxy._id, proxy); + await this._frpcProcessService.reloadFrpcProcess(); + return proxy2; } async deleteProxy(proxyId: string) { - return await this._proxyDao.deleteById(proxyId); + await this._proxyDao.deleteById(proxyId); + await this._frpcProcessService.reloadFrpcProcess(); } async getLocalPorts(): Promise> { @@ -28,7 +36,7 @@ class ProxyService { ? "netstat -a -n" : "netstat -an | grep LISTEN"; return new Promise((resolve, reject) => { - exec(command, (error, stdout, stderr) => { + require("child_process").exec(command, (error, stdout, stderr) => { if (error) { reject(error); }