首次提交

This commit is contained in:
2025-07-25 23:03:30 +08:00
commit d661fb4752
110 changed files with 39608 additions and 0 deletions
+418
View File
@@ -0,0 +1,418 @@
// 系统检测工具类
import { exec } from 'child_process';
import axios from 'axios';
import fs from 'fs';
import os from 'os';
import path from 'path';
import net from 'net';
import { ipcRenderer } from 'electron';
import iconv from 'iconv-lite';
// 系统检测日志记录
async function logSystemCheck(message) {
console.log(`[System Check] ${message}`);
}
// 发送消息到主进程
function sendMessage(data) {
ipcRenderer.send('send-message', JSON.stringify(data));
}
// 响应系统检测数据
function respondSystemCheckData(data) {
const response = {
status: true,
msg: '请求获取设备信息成功',
data: data
};
process.send(JSON.stringify(response));
}
// 检查系统主机名
export async function checkSystemHostName(params) {
const testItem = '系统主机名';
const description = '获取当前使用计算机的主机名';
let result = '';
let suggestion = '';
try {
const hostname = os.hostname();
await logSystemCheck(`HOSTNAME: ${hostname}`);
result = hostname || '无法获取主机名';
if (!hostname) suggestion = '暂时获取不了主机名';
} catch (error) {
result = '获取失败';
suggestion = error.message;
}
return {
user_id: params.user_id || '',
type: params.type,
checkout_type: params.checkout_type,
test_item: testItem,
project_description: description,
checkout_result: result,
repairing_suggestion: suggestion
};
}
// 检查网络连接状态
export async function checkNetworkConnectionState(params) {
const testItem = '客户端网络状态';
const description = '获取客户端网络是否通畅';
let result = '';
let suggestion = '';
try {
const response = await axios.get('https://www.baidu.com', { timeout: 5000 });
if (response.status === 200) {
result = '网络通畅';
} else {
result = '网络不通';
suggestion = '请检查网线是否正确插拔';
}
await logSystemCheck(response.status === 200 ? '网络通畅' : '网络不通');
} catch (error) {
result = '网络不通';
suggestion = '请检查网络连接';
await logSystemCheck(`networkstate_error: 网络连通 ${error.message}`);
}
return {
user_id: params.user_id || '',
type: params.type,
checkout_type: params.checkout_type,
test_item: testItem,
project_description: description,
checkout_result: result,
repairing_suggestion: suggestion
};
}
// 检查系统防火墙状态
export async function checkSystemFirewallStatus(params) {
const testItem = '检查防火墙设置';
const description = '检查防火墙有没有开启';
let result = '';
let suggestion = '';
return new Promise((resolve) => {
exec('netsh advfirewall show currentprofile', async (error, stdout, stderr) => {
if (error || stderr) {
await logSystemCheck(`error: ${error?.message || stderr}`);
resolve({
status: false,
msg: '未知错误',
data: {
user_id: params.user_id || '',
type: params.type,
checkout_type: params.checkout_type
}
});
return;
}
const isEnabled = stdout.includes('State ON');
await logSystemCheck(`防火墙状态: ${isEnabled ? '开启' : '关闭'}`);
result = isEnabled ? '开启' : '关闭';
if (!isEnabled) suggestion = '请自行百度开启防火墙的方法';
resolve({
user_id: params.user_id || '',
type: params.type,
checkout_type: params.checkout_type,
test_item: testItem,
project_description: description,
checkout_result: result,
repairing_suggestion: suggestion
});
});
});
}
// 检查核心程序文件
export async function checkCoreProgramFiles(params) {
const testItem = '核心文件';
const description = '检查核心启动文件是否存在';
let result = '';
let suggestion = '';
try {
const corePath = path.join(process.cwd(), 'release', '1.0.0', 'win-unpacked', '云企通安全云锁客户端.exe');
const exists = fs.existsSync(corePath);
await logSystemCheck(`核心文件是否存在: ${exists ? '存在' : '不存在'}`);
result = exists ? '存在' : '不存在';
if (!exists) suggestion = '请前去官网下载程序,然后重新安装';
} catch (error) {
result = '检查失败';
suggestion = error.message;
}
return {
user_id: params.user_id || '',
type: params.type,
checkout_type: params.checkout_type,
test_item: testItem,
project_description: description,
checkout_result: result,
repairing_suggestion: suggestion
};
}
// 检查系统驱动
export async function checkSystemDriver(params) {
const testItem = '检查驱动';
const description = '检查驱动是否存在';
let result = '';
let suggestion = '';
try {
const driverPath = 'C://Windows//System32//drivers//usbip_vhci_ude.sys';
const exists = fs.existsSync(driverPath);
await logSystemCheck(`驱动文件是否存在: ${exists ? '存在' : '不存在'}`);
result = exists ? '存在' : '不存在';
if (!exists) suggestion = '请重新安装程序试试';
} catch (error) {
result = '检查失败';
suggestion = error.message;
}
return {
user_id: params.user_id || '',
type: params.type,
checkout_type: params.checkout_type,
test_item: testItem,
project_description: description,
checkout_result: result,
repairing_suggestion: suggestion
};
}
// 检查系统测试模式
export async function checkSystemTestMode(params) {
const testItem = '测试模式';
const description = '检查测试模式是否开启,如果电脑为win7,则需要开启测试模式';
let result = '';
let suggestion = '';
return new Promise((resolve) => {
exec('bcdedit', async (error, stdout) => {
if (error) {
await logSystemCheck(`执行错误: ${error.message}`);
resolve({
status: false,
msg: '未知错误',
data: {
user_id: params.user_id || '',
type: params.type,
checkout_type: params.checkout_type
}
});
return;
}
let testMode = '';
const lines = stdout.split('\n');
for (let line of lines) {
if (line.includes('testsigning')) {
testMode = line.split(' ').pop().trim() === 'Yes' ? '是' : '否';
break;
}
}
result = testMode || '否';
if (result === '否') suggestion = '请在命令行执行命令开启';
resolve({
user_id: params.user_id || '',
type: params.type,
checkout_type: params.checkout_type,
test_item: testItem,
project_description: description,
checkout_result: result,
repairing_suggestion: suggestion
});
});
});
}
// 检查系统证书
export async function checkSystemCertificate(params) {
const testItem = '检查证书';
const description = '检查证书是否存在';
let result = '';
let suggestion = '';
return new Promise((resolve) => {
if (os.platform() === 'win32' && os.release().startsWith('7')) {
result = '存在';
resolve({
user_id: params.user_id || '',
type: params.type,
checkout_type: params.checkout_type,
test_item: testItem,
project_description: description,
checkout_result: result,
repairing_suggestion: suggestion
});
return;
}
exec('certutil -store root "USBIP Test"', async (error, stdout, stderr) => {
if (error || stderr) {
await logSystemCheck(`执行错误: ${error?.message || stderr}`);
resolve({
status: false,
msg: '未知错误',
data: {
user_id: params.user_id || '',
type: params.type,
checkout_type: params.checkout_type
}
});
return;
}
const exists = stdout.includes('USBIP Test');
await logSystemCheck(`证书是否存在: ${exists ? '存在' : '不存在'}`);
result = exists ? '存在' : '不存在';
if (!exists) suggestion = '请重新安装程序试试';
resolve({
user_id: params.user_id || '',
type: params.type,
checkout_type: params.checkout_type,
test_item: testItem,
project_description: description,
checkout_result: result,
repairing_suggestion: suggestion
});
});
});
}
// 检查端口服务
export async function checkPortService(port, host = '127.0.0.1') {
return new Promise((resolve) => {
const socket = new net.Socket();
const timeout = 2000;
socket.setTimeout(timeout);
socket.on('connect', () => {
socket.destroy();
resolve(true);
});
socket.on('timeout', () => {
socket.destroy();
resolve(false);
});
socket.on('error', () => {
resolve(false);
});
socket.connect(port, host);
});
}
// 检查系统管理员权限
export async function checkSystemAdminRights(params) {
const testItem = '管理员权限';
const description = '检查当前用户是否拥有管理员权限';
let result = '';
let suggestion = '';
return new Promise((resolve) => {
exec('net session', async (error) => {
await logSystemCheck(`当前访问是否具有管理员权限: ${error ? '没有' : '有'}`);
result = error ? '没有' : '有';
if (error) suggestion = '请点击右键以管理员权限打开本程序';
resolve({
user_id: params.user_id || '',
type: params.type,
checkout_type: params.checkout_type,
test_item: testItem,
project_description: description,
checkout_result: result,
repairing_suggestion: suggestion
});
});
});
}
// 检查程序完整性
export async function checkApplicationIntegrity(params) {
return {
user_id: params.user_id || '',
type: params.type,
checkout_type: params.checkout_type,
test_item: '程序完整',
project_description: '检查程序文件是否完整',
checkout_result: '完整',
repairing_suggestion: ''
};
}
// 执行系统检测
export async function performSystemCheck(params) {
await logSystemCheck(`checkout_type: ${params.checkout_type}`);
try {
switch (params.checkout_type) {
case 'host_name':
return await checkSystemHostName(params);
case 'network_state':
return await checkNetworkConnectionState(params);
case 'firewall_status':
return await checkSystemFirewallStatus(params);
case 'files_exist':
return await checkCoreProgramFiles(params);
case 'certificate_exist':
return await checkSystemCertificate(params);
case 'test_pattern':
return await checkSystemTestMode(params);
case 'drive_exist':
return await checkSystemDriver(params);
case 'ownership':
return await checkSystemAdminRights(params);
case 'web_serve':
const webStatus = await checkPortService(25525);
return {
user_id: params.user_id || '',
type: params.type,
checkout_type: params.checkout_type,
test_item: 'web服务',
project_description: '检查web服务是否开启成功',
checkout_result: webStatus ? '已开启' : '关闭',
repairing_suggestion: webStatus ? '' : '请重启程序尝试一下'
};
case 'socket_serve':
const socketStatus = await checkPortService(801);
return {
user_id: params.user_id || '',
type: params.type,
checkout_type: params.checkout_type,
test_item: 'socket服务',
project_description: '检查socket服务是否开启成功',
checkout_result: socketStatus ? '已开启' : '关闭',
repairing_suggestion: socketStatus ? '' : '请重启程序尝试一下'
};
case 'program_integrity':
return await checkApplicationIntegrity(params);
default:
return {
status: false,
msg: '不存在的检测类型',
data: { user_id: params.user_id || '' }
};
}
} catch (error) {
return {
status: false,
msg: `检测失败: ${error.message}`,
data: { user_id: params.user_id || '' }
};
}
}