2025-02-25 11:57:54 +08:00
|
|
|
import ProxyRepository from "../repository/ProxyRepository";
|
2025-02-26 16:12:11 +08:00
|
|
|
import FrpcProcessService from "./FrpcProcessService";
|
2025-02-23 21:07:44 +08:00
|
|
|
|
|
|
|
class ProxyService {
|
2025-02-25 11:57:54 +08:00
|
|
|
private readonly _proxyDao: ProxyRepository;
|
2025-02-26 16:12:11 +08:00
|
|
|
private readonly _frpcProcessService: FrpcProcessService;
|
2025-02-25 11:57:54 +08:00
|
|
|
|
2025-02-26 16:12:11 +08:00
|
|
|
constructor(
|
|
|
|
public proxyDao: ProxyRepository,
|
|
|
|
frpcProcessService: FrpcProcessService
|
|
|
|
) {
|
2025-02-23 21:07:44 +08:00
|
|
|
this._proxyDao = proxyDao;
|
2025-02-26 16:12:11 +08:00
|
|
|
this._frpcProcessService = frpcProcessService;
|
2025-02-23 21:07:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async insertProxy(proxy: FrpcProxy) {
|
2025-02-26 16:12:11 +08:00
|
|
|
const proxy2 = await this._proxyDao.insert(proxy);
|
|
|
|
await this._frpcProcessService.reloadFrpcProcess();
|
|
|
|
return proxy2;
|
2025-02-23 21:07:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async updateProxy(proxy: FrpcProxy) {
|
2025-02-26 16:12:11 +08:00
|
|
|
const proxy2 = await this._proxyDao.updateById(proxy._id, proxy);
|
|
|
|
await this._frpcProcessService.reloadFrpcProcess();
|
|
|
|
return proxy2;
|
2025-02-23 21:07:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async deleteProxy(proxyId: string) {
|
2025-02-26 16:12:11 +08:00
|
|
|
await this._proxyDao.deleteById(proxyId);
|
|
|
|
await this._frpcProcessService.reloadFrpcProcess();
|
2025-02-23 21:07:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async getLocalPorts(): Promise<Array<LocalPort>> {
|
|
|
|
const command =
|
|
|
|
process.platform === "win32"
|
|
|
|
? "netstat -a -n"
|
|
|
|
: "netstat -an | grep LISTEN";
|
|
|
|
return new Promise((resolve, reject) => {
|
2025-02-26 16:12:11 +08:00
|
|
|
require("child_process").exec(command, (error, stdout, stderr) => {
|
2025-02-23 21:07:44 +08:00
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
if (stderr) {
|
|
|
|
reject(stderr);
|
|
|
|
}
|
|
|
|
let ports: Array<LocalPort> = [];
|
|
|
|
if (stdout) {
|
|
|
|
if (process.platform === "win32") {
|
|
|
|
// window
|
|
|
|
ports = stdout
|
|
|
|
.split("\r\n")
|
|
|
|
.filter(f => f.indexOf("TCP") !== -1 || f.indexOf("UDP") !== -1)
|
|
|
|
.map(m => {
|
|
|
|
const cols = m.split(" ").filter(f => f != "");
|
|
|
|
const local = cols[1];
|
|
|
|
const s = local.lastIndexOf(":");
|
|
|
|
let localIP = local.slice(0, s);
|
|
|
|
let localPort = local.slice(s - local.length + 1);
|
|
|
|
const singe: LocalPort = {
|
|
|
|
protocol: cols[0],
|
|
|
|
ip: localIP,
|
|
|
|
port: localPort
|
|
|
|
};
|
|
|
|
|
|
|
|
return singe;
|
|
|
|
});
|
|
|
|
} else if (process.platform === "darwin") {
|
|
|
|
// mac
|
|
|
|
ports = stdout
|
|
|
|
.split("\n")
|
|
|
|
.filter(m => {
|
|
|
|
const cols = m.split(" ").filter(f => f != "");
|
|
|
|
const local = cols[3];
|
|
|
|
return local;
|
|
|
|
})
|
|
|
|
.map(m => {
|
|
|
|
const cols = m.split(" ").filter(f => f != "");
|
|
|
|
const local = cols[3];
|
|
|
|
const s = local.lastIndexOf(".");
|
|
|
|
let localIP = local.slice(0, s);
|
|
|
|
let localPort = local.slice(s - local.length + 1);
|
|
|
|
const singe: LocalPort = {
|
|
|
|
protocol: cols[0],
|
|
|
|
ip: localIP,
|
|
|
|
port: localPort
|
|
|
|
};
|
|
|
|
return singe;
|
|
|
|
});
|
|
|
|
} else if (process.platform === "linux") {
|
|
|
|
ports = stdout
|
|
|
|
.split("\n")
|
|
|
|
.filter(
|
|
|
|
f =>
|
|
|
|
f.indexOf("tcp") !== -1 ||
|
|
|
|
f.indexOf("tcp6") !== -1 ||
|
|
|
|
f.indexOf("udp") !== -1 ||
|
|
|
|
f.indexOf("udp6") !== -1
|
|
|
|
)
|
|
|
|
.map(m => {
|
|
|
|
const cols = m.split(" ").filter(f => f != "");
|
|
|
|
const local = cols[3];
|
|
|
|
const s = local.lastIndexOf(":");
|
|
|
|
let localIP = local.slice(0, s);
|
|
|
|
let localPort = local.slice(s - local.length + 1);
|
|
|
|
const singe: LocalPort = {
|
|
|
|
protocol: cols[0],
|
|
|
|
ip: localIP,
|
|
|
|
port: localPort
|
|
|
|
};
|
|
|
|
return singe;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ports.sort((a, b) => a.port - b.port);
|
|
|
|
|
|
|
|
resolve(ports);
|
|
|
|
});
|
|
|
|
// exec(command, (error, stdout, stderr) => {
|
|
|
|
// if (error) {
|
|
|
|
// logError(LogModule.APP, `getLocalPorts - error: ${error.message}`);
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// if (stderr) {
|
|
|
|
// logWarn(LogModule.APP, `getLocalPorts - stderr: ${stderr}`);
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// logDebug(LogModule.APP, `Command output: ${stdout}`);
|
|
|
|
// let ports = [];
|
|
|
|
|
|
|
|
//
|
|
|
|
// event.reply("local.getLocalPorts.hook", {
|
|
|
|
// data: ports
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ProxyService;
|