micahmo/WgServerforWindows

Use New-NetNat instead of SharedAccess to make wi-fi hotspot work

kenvix opened this issue · 3 comments

Recently I found that once wireguard server's NAT is enable, I will not able to create a wi-fi hotspot with Internet access.

Why should not use SharedAccess ?

This service is made for hotspot and can only support one NAT instance, meaning that, you will not able to set up a Wi-Fi hotspot with Internet access because SharedAccess services has been occupied by wireguard server's NAT

However, with New-NetNat command, you'll able to set up many NAT instances and keep wi-fi hotspot work.

How to set up NAT with New-NetNat?

Requirements:

  • Windows 10 Anniversary Update or later
  • Hyper-V is enabled (But you can set hypervisorlaunchtype to off in bcdedit if you dont want to use hyper-v!) There no is need for hyper-v installation

Disable Internet sharing first.
Snipaste_2022-01-05_11-07-48

Then, open powershell with admin permission

# First, get your interface index of wg_server interface
Get-NetAdapter
# Give a IP to wg_server interface
New-NetIPAddress -IPAddress 172.22.0.1 -PrefixLength 24 -InterfaceIndex 68 # Your interface index here
New-NetNat -Name wgservernat -InternalIPInterfaceAddressPrefix 172.22.0.0/24 # Your NAT name and interface CIDR here
Get-NetNat # 

Snipaste_2022-01-05_11-25-42

For test purpose, you can disable SharedAccess service and will find that NAT is working still .
Snipaste_2022-01-05_11-37-26

For more information, check this article

Please note New-NetIPAddress should be executed every time you reboot

Hey @kenvix, thanks for sharing this! There have been numerous issues with SharedAccess, including the limitation of only one shared network at a time, so I like the idea of New-NetNat. SharedAccess should still be supported for older versions of Windows.

I can't promise that this will be added any time soon, but I appreciate the suggestion.

Update: There no is need for hyper-v installation (Win10 10.0.19044)

Additionally, a powershell script for someone who want to execute New-NetIPAddress automatically at startup:

#Requires -RunAsAdministrator
$ifName = "wg_server" 
function Get-IfIndex() {
    return (Get-NetAdapter | Where -Property Status -eq 'Up' | Where -Property Name -Like $ifName | select -ExpandProperty ifIndex -first 1)
}
$ifIndex = Get-IfIndex
while ( $ifIndex -eq $null -Or $ifIndex  -eq "" ) {
    echo "Waiting interface $ifName up ..."
    sleep 3s   
    $ifIndex = Get-IfIndex
}
echo "$ifName Interface index is $ifIndex"
echo "Setting up IpAddr"
New-NetIPAddress -IPAddress 172.22.0.1 -PrefixLength 24 -InterfaceIndex $ifIndex
exit 0

Thanks @kenvix!