Files
nxxmdata/backend/fix-network-access.js
2025-09-12 20:08:42 +08:00

123 lines
3.7 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 网络访问问题修复脚本
* 解决外部用户无法访问开发服务器的问题
*/
const os = require('os');
const net = require('net');
console.log('🔧 开始修复网络访问问题...\n');
// 获取网络接口信息
function getNetworkInfo() {
const interfaces = os.networkInterfaces();
const results = [];
for (const name of Object.keys(interfaces)) {
for (const iface of interfaces[name]) {
if (iface.family === 'IPv4' && !iface.internal) {
results.push({
name: name,
address: iface.address,
netmask: iface.netmask,
mac: iface.mac
});
}
}
}
return results;
}
// 检查端口是否可以监听
function checkPortAccess(port) {
return new Promise((resolve) => {
const server = net.createServer();
server.listen(port, '0.0.0.0', () => {
console.log(`✅ 端口 ${port} 可以监听所有网络接口`);
server.close();
resolve(true);
});
server.on('error', (err) => {
if (err.code === 'EADDRINUSE') {
console.log(`⚠️ 端口 ${port} 已被占用`);
resolve(false);
} else {
console.log(`❌ 端口 ${port} 监听失败: ${err.message}`);
resolve(false);
}
});
});
}
// 生成防火墙配置命令
function generateFirewallCommands() {
const commands = [
'# Windows防火墙配置命令以管理员身份运行PowerShell',
'',
'# 允许Node.js通过防火墙',
'netsh advfirewall firewall add rule name="Node.js Frontend" dir=in action=allow protocol=TCP localport=5300',
'netsh advfirewall firewall add rule name="Node.js Backend" dir=in action=allow protocol=TCP localport=5350',
'',
'# 或者允许所有Node.js程序',
'netsh advfirewall firewall add rule name="Node.js" dir=in action=allow program="C:\\Program Files\\nodejs\\node.exe" enable=yes',
'',
'# 检查现有规则',
'netsh advfirewall firewall show rule name="Node.js Frontend"',
'netsh advfirewall firewall show rule name="Node.js Backend"'
];
return commands.join('\n');
}
// 主函数
async function runDiagnostic() {
console.log('🔍 网络诊断开始...\n');
// 获取网络接口信息
const networkInfo = getNetworkInfo();
console.log('📡 可用的网络接口:');
if (networkInfo.length === 0) {
console.log(' ❌ 没有找到可用的网络接口');
return;
}
networkInfo.forEach(iface => {
console.log(` - ${iface.name}: ${iface.address}`);
});
// 检查端口访问
console.log('\n🔌 检查端口访问:');
const frontendPortOk = await checkPortAccess(5300);
const backendPortOk = await checkPortAccess(5350);
// 提供访问建议
console.log('\n💡 访问建议:');
networkInfo.forEach(iface => {
console.log(` 前端: http://${iface.address}:5300`);
console.log(` 后端: http://${iface.address}:5350`);
});
// 生成防火墙配置
console.log('\n🔧 防火墙配置命令:');
console.log(generateFirewallCommands());
// 提供解决方案
console.log('\n📋 解决步骤:');
console.log('1. 重启后端服务器: npm start');
console.log('2. 以管理员身份运行PowerShell执行上述防火墙命令');
console.log('3. 让其他用户访问您的IP地址不是localhost');
console.log('4. 确保其他用户和您在同一个局域网内');
if (!frontendPortOk || !backendPortOk) {
console.log('\n⚠ 端口被占用,请先停止其他服务');
}
console.log('\n🎉 诊断完成!');
}
// 运行诊断
runDiagnostic().catch(console.error);