🏗️ refactor ProxyService to integrate FrpcProcessService and reload process on proxy modifications in electron service

This commit is contained in:
刘嘉伟 2025-02-26 16:12:11 +08:00
parent 62151d613e
commit dfbd9c93bd
3 changed files with 63 additions and 9 deletions

View File

@ -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",

View File

@ -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();

View File

@ -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<Array<LocalPort>> {
@ -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);
}