79 lines
3.4 KiB
PowerShell
79 lines
3.4 KiB
PowerShell
# PowerShell防火墙配置脚本
|
||
# 解决外部用户无法访问开发服务器的问题
|
||
|
||
Write-Host "🔧 正在配置Windows防火墙以允许外部访问..." -ForegroundColor Green
|
||
Write-Host ""
|
||
|
||
# 检查是否以管理员身份运行
|
||
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
|
||
Write-Host "❌ 错误:请以管理员身份运行此脚本" -ForegroundColor Red
|
||
Write-Host "右键点击此文件,选择'以管理员身份运行'" -ForegroundColor Yellow
|
||
Read-Host "按任意键退出"
|
||
exit 1
|
||
}
|
||
|
||
Write-Host "✅ 检测到管理员权限,继续配置..." -ForegroundColor Green
|
||
Write-Host ""
|
||
|
||
# 配置防火墙规则
|
||
Write-Host "添加防火墙规则..." -ForegroundColor Cyan
|
||
|
||
# 允许前端端口5300
|
||
try {
|
||
New-NetFirewallRule -DisplayName "Node.js Frontend Port 5300" -Direction Inbound -Protocol TCP -LocalPort 5300 -Action Allow -ErrorAction SilentlyContinue
|
||
Write-Host "✅ 前端端口5300规则添加成功" -ForegroundColor Green
|
||
} catch {
|
||
Write-Host "⚠️ 前端端口5300规则可能已存在" -ForegroundColor Yellow
|
||
}
|
||
|
||
# 允许后端端口5350
|
||
try {
|
||
New-NetFirewallRule -DisplayName "Node.js Backend Port 5350" -Direction Inbound -Protocol TCP -LocalPort 5350 -Action Allow -ErrorAction SilentlyContinue
|
||
Write-Host "✅ 后端端口5350规则添加成功" -ForegroundColor Green
|
||
} catch {
|
||
Write-Host "⚠️ 后端端口5350规则可能已存在" -ForegroundColor Yellow
|
||
}
|
||
|
||
# 允许Node.js程序
|
||
try {
|
||
$nodePath = "C:\Program Files\nodejs\node.exe"
|
||
if (Test-Path $nodePath) {
|
||
New-NetFirewallRule -DisplayName "Node.js Program" -Direction Inbound -Program $nodePath -Action Allow -ErrorAction SilentlyContinue
|
||
Write-Host "✅ Node.js程序规则添加成功" -ForegroundColor Green
|
||
} else {
|
||
Write-Host "⚠️ 未找到Node.js程序路径,跳过程序规则" -ForegroundColor Yellow
|
||
}
|
||
} catch {
|
||
Write-Host "⚠️ Node.js程序规则可能已存在" -ForegroundColor Yellow
|
||
}
|
||
|
||
Write-Host ""
|
||
Write-Host "检查已添加的规则..." -ForegroundColor Cyan
|
||
|
||
# 显示规则
|
||
Get-NetFirewallRule -DisplayName "*Node.js*" | Format-Table DisplayName, Direction, Action, Enabled -AutoSize
|
||
|
||
Write-Host ""
|
||
Write-Host "🎉 防火墙配置完成!" -ForegroundColor Green
|
||
Write-Host ""
|
||
Write-Host "现在其他用户可以通过以下地址访问您的服务:" -ForegroundColor Yellow
|
||
Write-Host "前端: http://172.28.112.1:5300" -ForegroundColor White
|
||
Write-Host "后端: http://172.28.112.1:5350" -ForegroundColor White
|
||
Write-Host ""
|
||
Write-Host "请确保:" -ForegroundColor Yellow
|
||
Write-Host "1. 服务器正在运行" -ForegroundColor White
|
||
Write-Host "2. 其他用户与您在同一个局域网内" -ForegroundColor White
|
||
Write-Host "3. 使用正确的IP地址(不是localhost)" -ForegroundColor White
|
||
Write-Host ""
|
||
|
||
# 获取所有可用的IP地址
|
||
Write-Host "可用的访问地址:" -ForegroundColor Cyan
|
||
$networkAdapters = Get-NetIPAddress -AddressFamily IPv4 | Where-Object { $_.IPAddress -notlike "127.*" -and $_.IPAddress -notlike "169.254.*" }
|
||
foreach ($adapter in $networkAdapters) {
|
||
Write-Host " 前端: http://$($adapter.IPAddress):5300" -ForegroundColor White
|
||
Write-Host " 后端: http://$($adapter.IPAddress):5350" -ForegroundColor White
|
||
}
|
||
|
||
Write-Host ""
|
||
Read-Host "按任意键退出"
|