2025-08-21 10:06:49 +00:00
|
|
|
|
import { ipcMain,app } from 'electron';
|
2025-08-22 10:59:43 +00:00
|
|
|
|
import { getDeviceId, getWiredConnectionName,netmaskToCidr } from '../utils/utils';
|
|
|
|
|
|
const { exec } = require('child_process');
|
|
|
|
|
|
const { promisify } = require('util');
|
|
|
|
|
|
const execAsync = promisify(exec);
|
2025-07-30 01:07:36 +00:00
|
|
|
|
|
2025-08-21 10:06:49 +00:00
|
|
|
|
const window = getBrowserWindowRuntime();
|
|
|
|
|
|
|
|
|
|
|
|
// 监听渲染进程发送的消息
|
2025-07-30 01:07:36 +00:00
|
|
|
|
ipcMain.handle('getPlatform', () => {
|
|
|
|
|
|
return `hi, i'm from ${process.platform}`;
|
|
|
|
|
|
});
|
2025-08-21 10:06:49 +00:00
|
|
|
|
|
|
|
|
|
|
// 窗口控制:最小化,退出全屏
|
|
|
|
|
|
ipcMain.on('close-app', () => {
|
|
|
|
|
|
app.quit();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
ipcMain.on('minimize-app', () => {
|
|
|
|
|
|
window?.minimize();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
ipcMain.on('exit-kiosk', () => {
|
|
|
|
|
|
if (window) {
|
|
|
|
|
|
window.setFullScreen(false);
|
|
|
|
|
|
}
|
2025-08-22 10:59:43 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ipcMain.handle('get-deviceid',async()=>{
|
|
|
|
|
|
const deviceId = await getDeviceId();
|
|
|
|
|
|
console.log(`Using device ID: ${deviceId}`);
|
|
|
|
|
|
// TODO:传给后端
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
/* IPC 处理应用有线网络配置 */
|
|
|
|
|
|
ipcMain.handle('apply-wired-config',async(event,config)=>{
|
|
|
|
|
|
// return {
|
|
|
|
|
|
// success: true,
|
|
|
|
|
|
// message: '网络配置已成功应用'
|
|
|
|
|
|
// };
|
|
|
|
|
|
try{
|
|
|
|
|
|
console.log('应用网络配置:', config);
|
|
|
|
|
|
// 获取有线连接名称
|
|
|
|
|
|
const connectionName = await getWiredConnectionName();
|
|
|
|
|
|
console.log('有线连接名称:', connectionName);
|
|
|
|
|
|
|
|
|
|
|
|
if(config.method==='static'){
|
|
|
|
|
|
// 使用nmcli配置静态IP,需要使用sudo权限,一次性设置所有参数
|
|
|
|
|
|
let modifyCmd = `echo "unis@123" | sudo -S nmcli connection modify "${connectionName}" ipv4.method manual ipv4.addresses "${config.ipv4}/${netmaskToCidr(config.subnetMask)}" ipv4.gateway "${config.ipv4Gateway}"`;
|
|
|
|
|
|
const dnsServers = [config.primaryDns, config.secondaryDns].filter(Boolean).join(',');
|
|
|
|
|
|
modifyCmd += ` ipv4.dns "${dnsServers}"`;
|
|
|
|
|
|
|
|
|
|
|
|
// 添加 IPv6 配置(如果存在 ipv6Gateway)????ipv6和长度需要吗?ui只写了ipv6网关
|
|
|
|
|
|
// ipv6PrefixLength 是 IPv6 地址的前缀长度,类似于 IPv4 中的子网掩码。????
|
|
|
|
|
|
if (config.ipv6 && config.ipv6Gateway) {
|
|
|
|
|
|
modifyCmd += ` ipv6.method manual ipv6.addresses "${config.ipv6}/${config.ipv6PrefixLength || 64}" ipv6.gateway "${config.ipv6Gateway}"`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 执行配置命令
|
|
|
|
|
|
console.log('执行命令:', modifyCmd.replace('unis@123', '***'));
|
|
|
|
|
|
await execAsync(modifyCmd);
|
|
|
|
|
|
|
|
|
|
|
|
// 重新激活连接
|
|
|
|
|
|
await execAsync(`echo "unis@123" | sudo -S nmcli connection up "${connectionName}"`);
|
|
|
|
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
// DHCP配置,一次性设置所有参数
|
|
|
|
|
|
const modifyCmd = `echo "unis@123" | sudo -S nmcli connection modify "${connectionName}" ipv4.method auto ipv4.addresses "" ipv4.gateway "" ipv4.dns ""`;
|
|
|
|
|
|
|
|
|
|
|
|
// 执行配置命令
|
|
|
|
|
|
console.log('执行命令:', modifyCmd.replace('unis@123', '***'));
|
|
|
|
|
|
await execAsync(modifyCmd);
|
|
|
|
|
|
|
|
|
|
|
|
// 重新激活连接
|
|
|
|
|
|
await execAsync(`echo "unis@123" | sudo -S nmcli connection up "${connectionName}"`);
|
|
|
|
|
|
}
|
|
|
|
|
|
return {
|
|
|
|
|
|
success: true,
|
|
|
|
|
|
message: '网络配置已成功应用'
|
|
|
|
|
|
};
|
|
|
|
|
|
}catch(error:unknown){
|
|
|
|
|
|
console.error('应用网络配置失败:', error);
|
|
|
|
|
|
return {
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: `配置失败: ${error instanceof Error ? error.message : String(error || '未知错误')}`
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|