guoyu/Test/ps1/配置防火墙.ps1

103 lines
3.8 KiB
PowerShell
Raw Normal View History

# 国语教育平台 - 防火墙配置脚本
# 请右键点击,选择"使用PowerShell运行"
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " 国语教育平台 - 防火墙配置" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
# 检查管理员权限
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
Write-Host "[错误] 请以管理员身份运行此脚本!" -ForegroundColor Red
Write-Host ""
Write-Host "右键点击此文件,选择'以管理员身份运行'" -ForegroundColor Yellow
Write-Host ""
Read-Host "按Enter键退出"
exit 1
}
Write-Host "[1/2] 检查现有防火墙规则..." -ForegroundColor Green
Write-Host ""
# 检查规则是否存在
$existingRule = Get-NetFirewallRule -DisplayName "国语教育平台后端 30091" -ErrorAction SilentlyContinue
if ($existingRule) {
Write-Host "○ 防火墙规则已存在" -ForegroundColor Yellow
Write-Host ""
$confirm = Read-Host "是否删除并重新创建?(y/n)"
if ($confirm -eq 'y' -or $confirm -eq 'Y') {
Remove-NetFirewallRule -DisplayName "国语教育平台后端 30091" -ErrorAction SilentlyContinue
Write-Host "✓ 已删除旧规则" -ForegroundColor Green
} else {
Write-Host "保持现有规则" -ForegroundColor Yellow
Write-Host ""
Read-Host "按Enter键退出"
exit 0
}
}
Write-Host ""
Write-Host "[2/2] 创建防火墙规则..." -ForegroundColor Green
Write-Host ""
try {
# 创建入站规则
New-NetFirewallRule `
-DisplayName "国语教育平台后端 30091" `
-Direction Inbound `
-Protocol TCP `
-LocalPort 30091 `
-Action Allow `
-Profile Any `
-Description "允许访问国语教育平台后端服务端口30091" `
-ErrorAction Stop | Out-Null
Write-Host "✓ 防火墙规则创建成功!" -ForegroundColor Green
Write-Host ""
Write-Host "规则详情:" -ForegroundColor Cyan
Write-Host " - 名称: 国语教育平台后端 30091"
Write-Host " - 方向: 入站"
Write-Host " - 协议: TCP"
Write-Host " - 端口: 30091"
Write-Host " - 操作: 允许"
Write-Host " - 配置文件: 所有"
Write-Host ""
# 验证规则
$rule = Get-NetFirewallRule -DisplayName "国语教育平台后端 30091"
if ($rule.Enabled -eq $true) {
Write-Host "✓ 规则已启用" -ForegroundColor Green
}
} catch {
Write-Host "✗ 防火墙规则创建失败!" -ForegroundColor Red
Write-Host "错误信息: $($_.Exception.Message)" -ForegroundColor Red
Write-Host ""
Write-Host "请尝试手动配置:" -ForegroundColor Yellow
Write-Host "1. 打开 控制面板 -> Windows Defender 防火墙 -> 高级设置"
Write-Host "2. 点击 入站规则 -> 新建规则"
Write-Host "3. 选择 端口 -> TCP -> 特定本地端口: 30091"
Write-Host "4. 允许连接 -> 完成"
Write-Host ""
Read-Host "按Enter键退出"
exit 1
}
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " 配置完成!" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "后端服务访问地址: http://192.168.137.1:30091" -ForegroundColor Green
Write-Host ""
Write-Host "下一步:" -ForegroundColor Yellow
Write-Host " 1. 启动后端服务"
Write-Host " 2. 在浏览器测试: http://192.168.137.1:30091"
Write-Host " 3. 在手机浏览器测试确保同一WiFi"
Write-Host ""
Read-Host "按Enter键退出"