You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

66 lines
4.2 KiB

param([switch]$ValidateOnly)
$ErrorActionPreference = 'Stop'
Add-Type -AssemblyName PresentationFramework
try {
# Only Windows' active mobile-hotspot virtual NIC is eligible. A physical
# Wi-Fi/Ethernet NIC or an ambiguous pair must never be reconfigured.
$hotspotAdapters = @(Get-NetAdapter -IncludeHidden | Where-Object {
$_.Status -eq 'Up' -and $_.InterfaceDescription -like '*Wi-Fi Direct Virtual Adapter*'
} | Where-Object {
@(Get-NetIPAddress -InterfaceIndex $_.ifIndex -AddressFamily IPv4 -ErrorAction SilentlyContinue |
Where-Object { $_.IPAddress -in @('192.168.137.1', '192.168.4.1') }).Count -gt 0
})
if ($hotspotAdapters.Count -ne 1) {
throw 'Windows 모바일 핫스팟을 먼저 켜 주세요. 192.168.137.1 또는 192.168.4.1을 가진 활성 핫스팟 어댑터가 정확히 하나여야 합니다.'
}
$hotspotAdapter = $hotspotAdapters[0]
$existingAddresses = @(Get-NetIPAddress -AddressFamily IPv4 | Where-Object { $_.IPAddress -eq '192.168.4.1' })
if (@($existingAddresses | Where-Object { $_.InterfaceIndex -ne $hotspotAdapter.ifIndex }).Count -gt 0) {
throw '192.168.4.1이 다른 어댑터에서 사용 중입니다. 기존 네트워크 설정을 변경하지 않았습니다.'
}
if (@($existingAddresses | Where-Object { $_.PrefixLength -ne 24 }).Count -gt 0) {
throw '기존 192.168.4.1의 접두사가 24가 아닙니다. 기존 설정을 변경하지 않았습니다.'
}
$hotspotIpInterface = Get-NetIPInterface -InterfaceIndex $hotspotAdapter.ifIndex -AddressFamily IPv4
if ($hotspotIpInterface.Dhcp -ne 'Disabled') {
throw '핫스팟 어댑터가 DHCP 클라이언트로 설정되어 있습니다. 자동 변경하지 않았습니다.'
}
if ($ValidateOnly) { return }
$addedAddress = $false
try {
if ($existingAddresses.Count -eq 0) {
New-NetIPAddress -InterfaceIndex $hotspotAdapter.ifIndex -IPAddress '192.168.4.1' -PrefixLength 24 -SkipAsSource $true | Out-Null
$addedAddress = $true
}
$testExecutable = Join-Path $PSScriptRoot 'DCT_test.exe'
if (-not (Test-Path -LiteralPath $testExecutable)) { throw 'DCT_test.exe 옆의 스크립트로 실행하세요.' }
$ruleName = 'DCT-Test-Hotspot-AP-5000'
$existingRule = Get-NetFirewallRule -Name $ruleName -ErrorAction SilentlyContinue
if ($existingRule) {
$portFilter = $existingRule | Get-NetFirewallPortFilter
$addressFilter = $existingRule | Get-NetFirewallAddressFilter
$appFilter = $existingRule | Get-NetFirewallApplicationFilter
if ($existingRule.Direction -ne 'Inbound' -or $existingRule.Action -ne 'Allow' -or $existingRule.Enabled -ne 'True' -or
$portFilter.Protocol -ne 'TCP' -or $portFilter.LocalPort -ne '5000' -or
$addressFilter.LocalAddress -ne '192.168.4.1' -or $addressFilter.RemoteAddress -notin @('192.168.4.0/255.255.255.0', '192.168.4.0/24') -or
$appFilter.Program -ne $testExecutable) {
throw '기존 DCT AP 방화벽 규칙이 현재 실행 파일/주소와 다릅니다. 기존 규칙을 변경하지 않았습니다.'
}
} else {
New-NetFirewallRule -Name $ruleName -DisplayName 'DCT test hotspot AP TCP 5000' -Direction Inbound -Action Allow `
-Protocol TCP -LocalAddress '192.168.4.1' -LocalPort 5000 -RemoteAddress '192.168.4.0/24' `
-Program $testExecutable -Profile Any | Out-Null
}
} catch {
if ($addedAddress) {
Remove-NetIPAddress -InterfaceIndex $hotspotAdapter.ifIndex -IPAddress '192.168.4.1' -Confirm:$false
}
throw
}
[void][System.Windows.MessageBox]::Show("AP IP 설정 완료: 192.168.4.1/24`n태블릿 Wi-Fi를 수동 IP 192.168.4.2 / 접두사 24 / 게이트웨이·DNS 192.168.4.1로 설정하세요.`n현재 앱에서 '현재 AP로 통신 연결'을 누르세요.`n핫스팟을 다시 켠 뒤 주소가 사라지면 이 도구를 다시 실행하세요.", 'DCT AP 설정')
} catch {
if ($ValidateOnly) { throw }
[void][System.Windows.MessageBox]::Show($_.Exception.Message, 'DCT AP 설정 실패', 'OK', 'Error')
exit 1
}