{"id":5162,"date":"2026-04-07T17:05:16","date_gmt":"2026-04-07T09:05:16","guid":{"rendered":"https:\/\/www.liangliang.org.cn\/?p=5162"},"modified":"2026-04-07T17:38:41","modified_gmt":"2026-04-07T09:38:41","slug":"wsl2-nat-%e7%ab%af%e5%8f%a3%e8%bd%ac%e5%8f%91%e7%ae%a1%e7%90%86%e5%b7%a5%e5%85%b7","status":"publish","type":"post","link":"https:\/\/www.liangliang.org.cn\/?p=5162","title":{"rendered":"WSL2 NAT \u7aef\u53e3\u8f6c\u53d1\u7ba1\u7406\u5de5\u5177"},"content":{"rendered":"<h1>WSL2 NAT \u7aef\u53e3\u8f6c\u53d1\u7ba1\u7406\u5de5\u5177 \u4f7f\u7528\u8bf4\u660e<\/h1>\n<h2>1. \u5de5\u5177\u7b80\u4ecb<\/h2>\n<p>\u672c\u5de5\u5177\u662f\u4e00\u4e2a PowerShell \u811a\u672c\uff0c\u7528\u4e8e\u5728 <strong>WSL2 NAT \u7f51\u7edc\u6a21\u5f0f<\/strong> \u4e0b\u7ba1\u7406 Windows \u5230 WSL2 \u7684\u7aef\u53e3\u8f6c\u53d1\u89c4\u5219\u3002\u5b83\u53ef\u4ee5\u5e2e\u52a9\u60a8\uff1a<\/p>\n<ul>\n<li><strong>\u67e5\u770b<\/strong>\u5f53\u524d\u5df2\u914d\u7f6e\u7684 TCP \u7aef\u53e3\u8f6c\u53d1\u89c4\u5219\u548c UDP \u9632\u706b\u5899\u89c4\u5219<\/li>\n<li><strong>\u6dfb\u52a0<\/strong>\u65b0\u7684 TCP \u7aef\u53e3\u8f6c\u53d1\uff08\u81ea\u52a8\u914d\u7f6e Windows \u9632\u706b\u5899\uff09<\/li>\n<li><strong>\u6dfb\u52a0<\/strong> UDP \u7aef\u53e3\u9632\u706b\u5899\u89c4\u5219\uff08\u63d0\u793a netsh \u4e0d\u652f\u6301 UDP \u8f6c\u53d1\uff09<\/li>\n<li><strong>\u5220\u9664<\/strong>\u4e0d\u518d\u9700\u8981\u7684 TCP\/UDP \u89c4\u5219\uff08\u57fa\u4e8e\u5e8f\u53f7\u64cd\u4f5c\uff09<\/li>\n<\/ul>\n<blockquote>\n<p><strong>\u6ce8\u610f<\/strong>\uff1a\u7531\u4e8e Windows \u7684 <code>netsh interface portproxy<\/code> \u547d\u4ee4<strong>\u4e0d\u652f\u6301 UDP \u8f6c\u53d1<\/strong>\uff0c\u672c\u5de5\u5177\u5bf9\u4e8e UDP \u534f\u8bae\u4ec5\u80fd\u6dfb\u52a0\/\u5220\u9664 Windows \u9632\u706b\u5899\u5165\u7ad9\u89c4\u5219\uff0c\u5b9e\u9645\u7684 UDP \u7aef\u53e3\u8f6c\u53d1\u9700\u8981\u91c7\u7528\u5176\u4ed6\u65b9\u6848\uff08\u5982 WSL2 \u955c\u50cf\u7f51\u7edc\u6a21\u5f0f\u6216\u5728 WSL2 \u5185\u90e8\u4f7f\u7528 socat\uff09\u3002<\/p>\n<\/blockquote>\n<h2>2. \u4f7f\u7528\u524d\u63d0<\/h2>\n<ul>\n<li><strong>\u64cd\u4f5c\u7cfb\u7edf<\/strong>\uff1aWindows 10\/11 \u4e14\u5df2\u5b89\u88c5 WSL2<\/li>\n<li><strong>\u6743\u9650<\/strong>\uff1a\u9700\u8981\u4ee5<strong>\u7ba1\u7406\u5458\u8eab\u4efd<\/strong>\u8fd0\u884c\u811a\u672c\uff08\u811a\u672c\u4f1a\u81ea\u52a8\u8bf7\u6c42\u63d0\u6743\uff09<\/li>\n<li><strong>PowerShell<\/strong>\uff1a\u5efa\u8bae\u4f7f\u7528 PowerShell 5.1 \u6216\u66f4\u9ad8\u7248\u672c<\/li>\n<li><strong>WSL2<\/strong>\uff1a\u76ee\u6807 WSL2 \u53d1\u884c\u7248\u5fc5\u987b\u5904\u4e8e<strong>\u8fd0\u884c\u72b6\u6001<\/strong>\uff08\u53ef\u901a\u8fc7 <code>wsl<\/code> \u547d\u4ee4\u542f\u52a8\uff09<\/li>\n<\/ul>\n<h2>3. \u5b89\u88c5\u4e0e\u9996\u6b21\u8fd0\u884c<\/h2>\n<h3>3.1 \u4e0b\u8f7d\u811a\u672c<\/h3>\n<p>\u5c06\u4ee5\u4e0b\u811a\u672c\u5185\u5bb9\u4fdd\u5b58\u4e3a <code>wsl_nat_port_manager.ps1<\/code> \u6587\u4ef6\uff08\u63a8\u8350\u653e\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u6587\u4ef6\u5939\uff0c\u4f8b\u5982 <code>C:\\Users\\\u4f60\u7684\u7528\u6237\u540d\\Documents\\WSL_tools\\<\/code>\uff09\uff1a<\/p>\n<pre><code class=\"lang-powershell language-powershell powershell\"># \u6b64\u5904\u7c98\u8d34\u5b8c\u6574\u811a\u672c\u4ee3\u7801\uff08\u89c1\u524d\u6587\u6700\u7ec8\u7248\uff09<\/code><\/pre>\n<h3>3.2 \u9996\u6b21\u8fd0\u884c<\/h3>\n<p>\u7531\u4e8e Windows \u9ed8\u8ba4\u7981\u6b62\u8fd0\u884c PowerShell \u811a\u672c\uff0c\u60a8\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u4efb\u4e00\u65b9\u5f0f\u8fd0\u884c\uff1a<\/p>\n<h4>\u65b9\u6cd5\u4e00\uff1a\u4f7f\u7528\u547d\u4ee4\u884c\u7ed5\u8fc7\u6267\u884c\u7b56\u7565\uff08\u63a8\u8350\uff09<\/h4>\n<ul>\n<li>\u4ee5<strong>\u7ba1\u7406\u5458\u8eab\u4efd<\/strong>\u6253\u5f00 PowerShell<\/li>\n<li>\u5207\u6362\u5230\u811a\u672c\u6240\u5728\u76ee\u5f55\uff0c\u6267\u884c\uff1a\n<pre><code class=\"lang-powershell language-powershell powershell\">powershell -ExecutionPolicy Bypass -File .\\wsl_nat_port_manager.ps1<\/code><\/pre><\/li>\n<\/ul>\n<h4>\u65b9\u6cd5\u4e8c\uff1a\u521b\u5efa\u6279\u5904\u7406\u542f\u52a8\u5668<\/h4>\n<ul>\n<li>\u5728\u811a\u672c\u540c\u76ee\u5f55\u4e0b\u65b0\u5efa\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\uff0c\u547d\u540d\u4e3a <code>\u542f\u52a8\u7aef\u53e3\u7ba1\u7406.bat<\/code><\/li>\n<li>\u5199\u5165\u4ee5\u4e0b\u5185\u5bb9\uff1a\n<pre><code class=\"lang-batch language-batch batch\">@echo off\npowershell -ExecutionPolicy Bypass -File &quot;%~dp0wsl_nat_port_manager.ps1&quot;\npause<\/code><\/pre><\/li>\n<li>\u4ee5\u540e<strong>\u53f3\u952e<\/strong>\u8be5 <code>.bat<\/code> \u6587\u4ef6 \u2192 <strong>\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c<\/strong><\/li>\n<\/ul>\n<h4>\u65b9\u6cd5\u4e09\uff1a\u6c38\u4e45\u4fee\u6539\u6267\u884c\u7b56\u7565\uff08\u4ec5\u9650\u4e2a\u4eba\u5f00\u53d1\u673a\uff09<\/h4>\n<ul>\n<li>\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u6253\u5f00 PowerShell\uff0c\u6267\u884c\uff1a\n<pre><code class=\"lang-powershell language-powershell powershell\">Set-ExecutionPolicy RemoteSigned -Scope CurrentUser<\/code><\/pre><\/li>\n<li>\u8f93\u5165 <code>Y<\/code> \u786e\u8ba4\u3002\u4e4b\u540e\u53ef\u4ee5\u76f4\u63a5\u53cc\u51fb <code>.ps1<\/code> \u6587\u4ef6\u8fd0\u884c\u3002<\/li>\n<\/ul>\n<h2>4. \u754c\u9762\u4e0e\u64cd\u4f5c\u6307\u5357<\/h2>\n<p>\u8fd0\u884c\u811a\u672c\u540e\uff0c\u4f1a\u663e\u793a\u4e3b\u83dc\u5355\uff1a<\/p>\n<pre><code>======================================\n   WSL2 NAT \u7aef\u53e3\u8f6c\u53d1\u7ba1\u7406\u5de5\u5177\n======================================\n\u5f53\u524d WSL2 IP: 172.18.87.79\n\n\u5df2\u8f6c\u53d1\u7684 TCP \u7aef\u53e3:\n   1996 (BT-Panel)  3390 (xrdp)  9659 (\u81ea\u5b9a\u4e49)\n\n\u5df2\u6dfb\u52a0\u7684 UDP \u7aef\u53e3 (\u4ec5\u9632\u706b\u5899):\n   \u65e0\n\n\u8bf7\u9009\u62e9\u64cd\u4f5c:\n  1. \u67e5\u770b\u8be6\u7ec6\u89c4\u5219\n  2. \u6dfb\u52a0\u7aef\u53e3 (TCP\/UDP)\n  3. \u5220\u9664\u7aef\u53e3 (\u8f93\u5165\u5e8f\u53f7)\n  0. \u9000\u51fa<\/code><\/pre>\n<h3>4.1 \u67e5\u770b\u8be6\u7ec6\u89c4\u5219 (\u9009\u9879 1)<\/h3>\n<p>\u663e\u793a\u6240\u6709 TCP \u8f6c\u53d1\u89c4\u5219\uff08\u5305\u542b\u76ee\u6807 IP \u548c\u6ce8\u91ca\uff09\u4ee5\u53ca UDP \u9632\u706b\u5899\u89c4\u5219\u5217\u8868\u3002<\/p>\n<h3>4.2 \u6dfb\u52a0\u7aef\u53e3 (\u9009\u9879 2)<\/h3>\n<ul>\n<li>\u8f93\u5165\u8981\u6dfb\u52a0\u7684\u7aef\u53e3\u53f7\uff08\u652f\u6301\u591a\u4e2a\uff0c\u7528\u82f1\u6587\u9017\u53f7\u5206\u9694\uff0c\u4f8b\u5982 <code>1996,3390,8080<\/code>\uff09\u3002<\/li>\n<li>\u9009\u62e9\u534f\u8bae\uff1a<code>1<\/code> = TCP\uff0c<code>2<\/code> = UDP\u3002\n<ul>\n<li>\u82e5\u9009\u62e9 UDP\uff0c\u5de5\u5177\u4f1a\u63d0\u793a <code>netsh<\/code> \u4e0d\u652f\u6301 UDP \u8f6c\u53d1\uff0c\u8be2\u95ee\u662f\u5426\u4ec5\u6dfb\u52a0\u9632\u706b\u5899\u89c4\u5219\u3002\u9009\u62e9 <code>y<\/code> \u540e\uff0c\u4f1a\u521b\u5efa Windows \u9632\u706b\u5899\u5165\u7ad9\u89c4\u5219\uff08\u5141\u8bb8 UDP\uff09\uff0c\u4f46\u4e0d\u4f1a\u6267\u884c\u7aef\u53e3\u8f6c\u53d1\u3002<\/li>\n<\/ul><\/li>\n<li>\u786e\u8ba4\u540e\uff0c\u5de5\u5177\u4f1a\u81ea\u52a8\uff1a\n<ul>\n<li>\u5220\u9664\u53ef\u80fd\u5b58\u5728\u7684\u65e7 TCP \u8f6c\u53d1\u89c4\u5219\uff08\u907f\u514d\u51b2\u7a81\uff09<\/li>\n<li>\u6dfb\u52a0\u65b0\u7684 TCP \u8f6c\u53d1\u89c4\u5219\uff08\u6307\u5411\u5f53\u524d WSL2 IP\uff09<\/li>\n<li>\u521b\u5efa\/\u786e\u8ba4\u9632\u706b\u5899\u89c4\u5219\uff08TCP\/UDP \u5206\u522b\u547d\u540d\uff1a<code>WSL TCP Port xxxx<\/code> \/ <code>WSL UDP Port xxxx<\/code>\uff09<\/li>\n<\/ul><\/li>\n<\/ul>\n<h3>4.3 \u5220\u9664\u7aef\u53e3 (\u9009\u9879 3)<\/h3>\n<ul>\n<li>\u5de5\u5177\u4f1a\u5217\u51fa<strong>\u6240\u6709\u5df2\u5b58\u5728\u7684\u89c4\u5219<\/strong>\uff08TCP \u8f6c\u53d1 + UDP \u9632\u706b\u5899\uff09\uff0c\u5e76\u7edf\u4e00\u7f16\u53f7\u3002<\/li>\n<li>\u8f93\u5165\u8981\u5220\u9664\u7684<strong>\u5e8f\u53f7<\/strong>\uff08\u4f8b\u5982 <code>1<\/code>\u3001<code>2,3<\/code>\u3001<code>1-3<\/code> \u7b49\uff09\u3002<\/li>\n<li>\u786e\u8ba4\u540e\uff0c\u5de5\u5177\u4f1a\uff1a\n<ul>\n<li>\u5220\u9664 TCP \u7aef\u53e3\u8f6c\u53d1\u89c4\u5219\uff08\u6267\u884c <code>netsh interface portproxy delete<\/code>\uff09<\/li>\n<li>\u5220\u9664 UDP \u9632\u706b\u5899\u89c4\u5219\uff08\u6267\u884c <code>Remove-NetFirewallRule<\/code>\uff09<\/li>\n<\/ul><\/li>\n<\/ul>\n<blockquote>\n<p>\u63d0\u793a\uff1a\u5220\u9664\u64cd\u4f5c<strong>\u4e0d\u4f1a\u81ea\u52a8\u5220\u9664\u5bf9\u5e94\u7684\u9632\u706b\u5899\u89c4\u5219<\/strong>\uff08TCP \u9632\u706b\u5899\u89c4\u5219\u4f1a\u4fdd\u7559\uff0c\u56e0\u4e3a\u53ef\u80fd\u88ab\u5176\u4ed6\u7a0b\u5e8f\u4f7f\u7528\uff09\u3002\u5982\u9700\u5f7b\u5e95\u6e05\u7406\uff0c\u53ef\u624b\u52a8\u8fd0\u884c <code>Remove-NetFirewallRule -DisplayName &quot;WSL TCP Port xxxx&quot;<\/code>\u3002<\/p>\n<\/blockquote>\n<h3>4.4 \u9000\u51fa (\u9009\u9879 0)<\/h3>\n<p>\u9000\u51fa\u811a\u672c\u3002<\/p>\n<h2>5. \u6ce8\u610f\u4e8b\u9879<\/h2>\n<ol>\n<li>\n<p><strong>WSL2 IP \u4f1a\u53d8\u5316<\/strong><br \/>\n\u6bcf\u6b21\u91cd\u542f WSL2 \u6216 Windows \u540e\uff0cWSL2 \u7684\u865a\u62df IP \u5730\u5740\u53ef\u80fd\u4f1a\u6539\u53d8\u3002<strong>\u5efa\u8bae\u6bcf\u6b21 WSL2 \u91cd\u542f\u540e\u91cd\u65b0\u8fd0\u884c\u672c\u5de5\u5177<\/strong>\uff0c\u66f4\u65b0\u7aef\u53e3\u8f6c\u53d1\u89c4\u5219\uff08\u6dfb\u52a0\u65f6\u4f1a\u81ea\u52a8\u4f7f\u7528\u6700\u65b0 IP\uff09\u3002<\/p>\n<\/li>\n<li>\n<p><strong>UDP \u8f6c\u53d1\u7684\u9650\u5236<\/strong><br \/>\nWindows \u7684 <code>netsh interface portproxy<\/code> \u4e0d\u652f\u6301 UDP\u3002\u5982\u679c\u60a8\u7684\u5e94\u7528\u786e\u5b9e\u9700\u8981 UDP \u8f6c\u53d1\uff0c\u8bf7\u8003\u8651\u4ee5\u4e0b\u66ff\u4ee3\u65b9\u6848\uff1a<\/p>\n<ul>\n<li><strong>\u65b9\u6848\u4e00\uff08\u63a8\u8350\uff09<\/strong>\uff1a\u4f7f\u7528 WSL2 \u7684\u955c\u50cf\u7f51\u7edc\u6a21\u5f0f\u3002\u7f16\u8f91 <code>%UserProfile%\\.wslconfig<\/code>\uff0c\u8bbe\u7f6e <code>networkingMode=Mirrored<\/code>\uff0c\u7136\u540e\u6267\u884c <code>wsl --shutdown<\/code> \u91cd\u542f WSL\u3002\u4e4b\u540e WSL \u4e0e\u4e3b\u673a\u5171\u4eab\u7f51\u7edc\uff0cUDP \u7aef\u53e3\u81ea\u52a8\u53ef\u7528\u3002<\/li>\n<li><strong>\u65b9\u6848\u4e8c<\/strong>\uff1a\u5728 WSL2 \u5185\u90e8\u5b89\u88c5 <code>socat<\/code>\uff0c\u624b\u52a8\u8fdb\u884c UDP \u8f6c\u53d1\u3002\u4f8b\u5982\uff1a\n<pre><code class=\"lang-bash language-bash bash\">sudo apt install socat\nsocat UDP-LISTEN:1996,fork UDP:172.18.87.79:1996<\/code><\/pre>\n<p>\uff08\u5c06 <code>172.18.87.79<\/code> \u66ff\u6362\u4e3a\u4f60\u7684 WSL2 IP\uff09<\/p><\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u9632\u706b\u5899\u89c4\u5219\u6e05\u7406<\/strong><br \/>\n\u672c\u5de5\u5177\u6dfb\u52a0\u7684\u9632\u706b\u5899\u89c4\u5219\u540d\u79f0\u683c\u5f0f\u4e3a <code>WSL TCP Port xxxx<\/code> \u548c <code>WSL UDP Port xxxx<\/code>\u3002\u5982\u679c\u9700\u8981\u624b\u52a8\u5220\u9664\uff0c\u53ef\u4ee5\u5728<strong>\u7ba1\u7406\u5458 PowerShell<\/strong> \u4e2d\u6267\u884c\uff1a<\/p>\n<pre><code class=\"lang-powershell language-powershell powershell\">Remove-NetFirewallRule -DisplayName &quot;WSL TCP Port 1996&quot;\nRemove-NetFirewallRule -DisplayName &quot;WSL UDP Port 1996&quot;<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u811a\u672c\u81ea\u52a8\u63d0\u6743<\/strong><br \/>\n\u811a\u672c\u5f00\u5934\u5305\u542b\u4e86\u81ea\u52a8\u8bf7\u6c42\u7ba1\u7406\u5458\u6743\u9650\u7684\u4ee3\u7801\u3002\u5982\u679c\u60a8\u53cc\u51fb\u8fd0\u884c\uff0c\u4f1a\u5f39\u51fa UAC \u63d0\u793a\uff0c\u8bf7\u70b9\u51fb\u201c\u662f\u201d\u3002<\/p>\n<\/li>\n<li>\n<p><strong>WSL2 \u672a\u542f\u52a8\u65f6\u7684\u5904\u7406<\/strong><br \/>\n\u5982\u679c WSL2 \u672a\u8fd0\u884c\uff0c\u811a\u672c\u4f1a\u63d0\u793a\u201c\u65e0\u6cd5\u83b7\u53d6 WSL IP\u201d\u5e76\u9000\u51fa\u3002\u8bf7\u5148\u542f\u52a8 WSL\uff08\u4f8b\u5982\u5728\u7ec8\u7aef\u4e2d\u6267\u884c <code>wsl<\/code> \u6216\u6253\u5f00\u4e00\u4e2a WSL \u7a97\u53e3\uff09\u3002<\/p>\n<\/li>\n<\/ol>\n<h2>6. \u5e38\u89c1\u95ee\u9898 (FAQ)<\/h2>\n<p><strong>Q1\uff1a\u8fd0\u884c\u811a\u672c\u65f6\u63d0\u793a\u201c\u65e0\u6cd5\u52a0\u8f7d\u6587\u4ef6\uff0c\u56e0\u4e3a\u5728\u6b64\u7cfb\u7edf\u4e0a\u7981\u6b62\u8fd0\u884c\u811a\u672c\u201d<\/strong><br \/>\nA\uff1a\u8bf7\u53c2\u8003\u7b2c 3.2 \u8282\u201c\u9996\u6b21\u8fd0\u884c\u201d\u4e2d\u7684\u65b9\u6cd5\uff0c\u4f7f\u7528 <code>-ExecutionPolicy Bypass<\/code> \u53c2\u6570\u8fd0\u884c\uff0c\u6216\u4fee\u6539\u6267\u884c\u7b56\u7565\u3002<\/p>\n<p><strong>Q2\uff1a\u6dfb\u52a0 TCP \u7aef\u53e3\u540e\uff0c\u4ece Windows \u8bbf\u95ee <code>localhost:\u7aef\u53e3<\/code> \u4ecd\u7136\u5931\u8d25<\/strong><br \/>\nA\uff1a\u8bf7\u6309\u4ee5\u4e0b\u6b65\u9aa4\u6392\u67e5\uff1a<\/p>\n<ol>\n<li>\u786e\u8ba4 WSL2 \u5185\u90e8\u7684\u670d\u52a1\u786e\u5b9e\u5728\u76d1\u542c <code>0.0.0.0<\/code> \u800c\u4e0d\u662f <code>127.0.0.1<\/code>\uff08\u5728 WSL2 \u4e2d\u8fd0\u884c <code>ss -tlnp | grep \u7aef\u53e3<\/code> \u68c0\u67e5\uff09\u3002<\/li>\n<li>\u68c0\u67e5 Windows \u9632\u706b\u5899\u662f\u5426\u5141\u8bb8\u8be5\u7aef\u53e3\uff08\u811a\u672c\u4f1a\u81ea\u52a8\u6dfb\u52a0\u89c4\u5219\uff0c\u4f46\u67d0\u4e9b\u5b89\u5168\u8f6f\u4ef6\u53ef\u80fd\u963b\u6b62\uff09\u3002<\/li>\n<li>\u786e\u8ba4 WSL2 IP \u662f\u5426\u53d1\u751f\u53d8\u5316\uff08\u8fd0\u884c <code>wsl hostname -I<\/code> \u83b7\u53d6\u6700\u65b0 IP\uff0c\u5e76\u4e0e\u8f6c\u53d1\u89c4\u5219\u4e2d\u7684 IP \u6bd4\u5bf9\uff09\u3002<\/li>\n<\/ol>\n<p><strong>Q3\uff1a\u5220\u9664\u7aef\u53e3\u540e\uff0c\u7aef\u53e3\u4ecd\u7136\u53ef\u4ee5\u8bbf\u95ee\uff1f<\/strong><br \/>\nA\uff1a\u53ef\u80fd\u662f\u9632\u706b\u5899\u89c4\u5219\u672a\u5220\u9664\u3002\u811a\u672c\u76ee\u524d\u53ea\u5220\u9664 TCP \u8f6c\u53d1\u89c4\u5219\u548c UDP \u9632\u706b\u5899\u89c4\u5219\uff0c\u4f46\u4fdd\u7559\u4e86 TCP \u9632\u706b\u5899\u89c4\u5219\uff08\u907f\u514d\u8bef\u5220\uff09\u3002\u60a8\u53ef\u4ee5\u624b\u52a8\u5220\u9664\u5bf9\u5e94\u7684\u9632\u706b\u5899\u89c4\u5219\uff08\u89c1\u7b2c 5 \u8282\uff09\u3002<\/p>\n<p><strong>Q4\uff1a\u5982\u4f55\u6279\u91cf\u6dfb\u52a0\u7aef\u53e3\uff1f<\/strong><br \/>\nA\uff1a\u5728\u6dfb\u52a0\u7aef\u53e3\u65f6\uff0c\u8f93\u5165\u7528\u9017\u53f7\u5206\u9694\u7684\u7aef\u53e3\u53f7\u5373\u53ef\uff0c\u4f8b\u5982 <code>1996,3390,8080<\/code>\u3002<\/p>\n<p><strong>Q5\uff1a\u4e3a\u4ec0\u4e48 UDP \u7aef\u53e3\u4e0d\u80fd\u8f6c\u53d1\uff1f<\/strong><br \/>\nA\uff1a\u8fd9\u662f Windows <code>netsh portproxy<\/code> \u7684\u9650\u5236\u3002\u5efa\u8bae\u6539\u7528 WSL2 \u955c\u50cf\u7f51\u7edc\u6a21\u5f0f\uff08\u89c1\u7b2c 5 \u8282\uff09\u3002<\/p>\n<h2>7. \u5378\u8f7d<\/h2>\n<p>\u672c\u5de5\u5177\u662f\u4e00\u4e2a\u72ec\u7acb\u7684 PowerShell \u811a\u672c\uff0c\u4e0d\u4f1a\u5728\u7cfb\u7edf\u4e2d\u5b89\u88c5\u4efb\u4f55\u6587\u4ef6\u3002\u5982\u9700\u201c\u5378\u8f7d\u201d\uff0c\u76f4\u63a5\u5220\u9664\u811a\u672c\u6587\u4ef6\u5373\u53ef\u3002\u9632\u706b\u5899\u89c4\u5219\u548c\u7aef\u53e3\u8f6c\u53d1\u89c4\u5219\u53ef\u901a\u8fc7\u811a\u672c\u7684\u5220\u9664\u529f\u80fd\u6e05\u9664\uff0c\u4e5f\u53ef\u624b\u52a8\u6e05\u7406\uff08\u53c2\u89c1\u7b2c 5 \u8282\uff09\u3002<\/p>\n<h2>8. \u66f4\u65b0\u65e5\u5fd7<\/h2>\n<ul>\n<li><strong>v1.0<\/strong>\uff082026-04-07\uff09  \n<ul>\n<li>\u521d\u59cb\u7248\u672c  <\/li>\n<li>\u652f\u6301 TCP \u7aef\u53e3\u8f6c\u53d1\u6dfb\u52a0\/\u5220\u9664\/\u67e5\u770b  <\/li>\n<li>\u652f\u6301 UDP \u9632\u706b\u5899\u89c4\u5219\u6dfb\u52a0\/\u5220\u9664  <\/li>\n<li>\u81ea\u52a8\u83b7\u53d6 WSL2 IP \u5e76\u914d\u7f6e\u8f6c\u53d1  <\/li>\n<li>\u4ea4\u4e92\u5f0f\u83dc\u5355\uff0c\u5b9e\u65f6\u663e\u793a\u5f53\u524d\u7aef\u53e3<\/li>\n<\/ul><\/li>\n<\/ul>\n<hr \/>\n<p><strong>\u5982\u6709\u4efb\u4f55\u95ee\u9898\u6216\u5efa\u8bae\uff0c\u6b22\u8fce\u53cd\u9988\u3002<\/strong><\/p>\n<h3>\u6e90\u7801<\/h3>\n<pre><code># ============================================\n# WSL2 NAT \u7aef\u53e3\u8f6c\u53d1\u7ba1\u7406\u5de5\u5177 (\u652f\u6301 TCP\/UDP \u5b8c\u6574\u7ba1\u7406)\n# \u529f\u80fd\uff1a\u67e5\u770b\/\u6dfb\u52a0\/\u5220\u9664 TCP \u8f6c\u53d1 + UDP \u9632\u706b\u5899\u89c4\u5219\n# ============================================\n\n# ---------- \u81ea\u52a8\u8bf7\u6c42\u7ba1\u7406\u5458\u6743\u9650 ----------\nif (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] &quot;Administrator&quot;)) {\n    $arguments = &quot;-ExecutionPolicy Bypass -File `&quot;&quot; + $MyInvocation.MyCommand.Path + &quot;`&quot;&quot;\n    Start-Process powershell -Verb RunAs -ArgumentList $arguments\n    exit\n}\n\n# ---------- \u5e38\u7528\u7aef\u53e3\u6ce8\u91ca ----------\n$portComments = @{\n    22   = &quot;SSH&quot;\n    80   = &quot;HTTP&quot;\n    443  = &quot;HTTPS&quot;\n    3306 = &quot;MySQL&quot;\n    5432 = &quot;PostgreSQL&quot;\n    6379 = &quot;Redis&quot;\n    27017 = &quot;MongoDB&quot;\n    8080 = &quot;Web\u5907\u7528&quot;\n    8888 = &quot;Jupyter&quot;\n    3389 = &quot;RDP&quot;\n    5900 = &quot;VNC&quot;\n}\n\n# ---------- \u83b7\u53d6 WSL IP ----------\nfunction Get-WslIp {\n    $ip = (wsl hostname -I).Trim()\n    if (-not $ip) {\n        Write-Error &quot;\u65e0\u6cd5\u83b7\u53d6 WSL IP\uff0c\u8bf7\u786e\u4fdd WSL \u6b63\u5728\u8fd0\u884c&quot;\n        pause\n        exit 1\n    }\n    return $ip\n}\n\n# ---------- \u83b7\u53d6\u5f53\u524d TCP \u8f6c\u53d1\u89c4\u5219\uff08\u7aef\u53e3 -&gt; \u76ee\u6807IP\uff09----------\nfunction Get-TcpRules {\n    $output = netsh interface portproxy show v4tov4 2&gt;$null\n    $rules = @{}\n    if ([string]::IsNullOrWhiteSpace($output)) { return $rules }\n    $lines = $output -split &quot;`r`n&quot;\n    foreach ($line in $lines) {\n        if ($line -match &#039;0\\.0\\.0\\.0\\s+(\\d+)\\s+(\\d+\\.\\d+\\.\\d+\\.\\d+)\\s+\\d+&#039;) {\n            $port = [int]$Matches[1]\n            $target = $Matches[2]\n            $rules[$port] = $target\n        }\n    }\n    return $rules\n}\n\n# ---------- \u83b7\u53d6\u6240\u6709\u5df2\u6dfb\u52a0\u7684 UDP \u9632\u706b\u5899\u89c4\u5219\uff08\u7aef\u53e3\u5217\u8868\uff09----------\nfunction Get-UdpFirewallPorts {\n    $rules = Get-NetFirewallRule -DisplayName &quot;WSL UDP Port *&quot; -ErrorAction SilentlyContinue\n    $ports = @()\n    foreach ($rule in $rules) {\n        # \u4ece\u89c4\u5219\u540d\u79f0\u4e2d\u63d0\u53d6\u7aef\u53e3\u53f7\uff0c\u4f8b\u5982 &quot;WSL UDP Port 1996&quot;\n        if ($rule.DisplayName -match &#039;WSL UDP Port (\\d+)&#039;) {\n            $ports += [int]$Matches[1]\n        }\n    }\n    return ($ports | Sort-Object -Unique)\n}\n\n# ---------- \u663e\u793a\u5f53\u524d TCP \u7aef\u53e3\u5217\u8868\uff08\u4e3b\u83dc\u5355\u7528\uff09----------\nfunction Show-TcpPortList {\n    $rules = Get-TcpRules\n    if ($rules.Count -eq 0) {\n        Write-Host &quot;   (\u65e0)&quot; -ForegroundColor DarkGray\n        return\n    }\n    $sorted = $rules.Keys | Sort-Object\n    $output = @()\n    foreach ($port in $sorted) {\n        $comment = if ($portComments.ContainsKey($port)) { &quot;($($portComments[$port]))&quot; } else { &quot;&quot; }\n        $output += &quot;$port $comment&quot;\n    }\n    Write-Host (&quot;   &quot; + ($output -join &quot;  &quot;)) -ForegroundColor Green\n}\n\n# ---------- \u663e\u793a\u5f53\u524d UDP \u9632\u706b\u5899\u7aef\u53e3\u5217\u8868\uff08\u4e3b\u83dc\u5355\u7528\uff09----------\nfunction Show-UdpPortList {\n    $udpPorts = Get-UdpFirewallPorts\n    if ($udpPorts.Count -eq 0) {\n        Write-Host &quot;   (\u65e0)&quot; -ForegroundColor DarkGray\n        return\n    }\n    $output = @()\n    foreach ($port in $udpPorts) {\n        $comment = if ($portComments.ContainsKey($port)) { &quot;($($portComments[$port]))&quot; } else { &quot;&quot; }\n        $output += &quot;$port $comment&quot;\n    }\n    Write-Host (&quot;   &quot; + ($output -join &quot;  &quot;)) -ForegroundColor Green\n}\n\n# ---------- \u663e\u793a\u8be6\u7ec6\u89c4\u5219\u8868\u683c\uff08TCP\uff09----------\nfunction Show-DetailRules {\n    $rules = Get-TcpRules\n    if ($rules.Count -eq 0) {\n        Write-Host &quot;`n\u5f53\u524d\u6ca1\u6709\u4efb\u4f55 TCP \u7aef\u53e3\u8f6c\u53d1\u89c4\u5219\u3002&quot; -ForegroundColor Yellow\n    } else {\n        Write-Host &quot;`n\u5f53\u524d TCP \u7aef\u53e3\u8f6c\u53d1\u89c4\u5219:&quot; -ForegroundColor Cyan\n        Write-Host (&quot;{0,-8} {1,-20} {2,-15}&quot; -f &quot;\u7aef\u53e3&quot;, &quot;\u76ee\u6807\u5730\u5740&quot;, &quot;\u6ce8\u91ca&quot;)\n        Write-Host (&quot;{0,-8} {1,-20} {2,-15}&quot; -f &quot;----&quot;, &quot;--------&quot;, &quot;----&quot;)\n        foreach ($port in ($rules.Keys | Sort-Object)) {\n            $target = $rules[$port]\n            $comment = if ($portComments.ContainsKey($port)) { $portComments[$port] } else { &quot;&quot; }\n            Write-Host (&quot;{0,-8} {1,-20} {2,-15}&quot; -f $port, $target, $comment)\n        }\n    }\n\n    $udpPorts = Get-UdpFirewallPorts\n    if ($udpPorts.Count -eq 0) {\n        Write-Host &quot;`n\u5f53\u524d\u6ca1\u6709\u4efb\u4f55 UDP \u9632\u706b\u5899\u89c4\u5219\u3002&quot; -ForegroundColor Yellow\n    } else {\n        Write-Host &quot;`n\u5f53\u524d UDP \u9632\u706b\u5899\u89c4\u5219\uff08\u4ec5\u5165\u7ad9\u5141\u8bb8\uff0c\u65e0\u8f6c\u53d1\uff09:&quot; -ForegroundColor Cyan\n        foreach ($port in $udpPorts) {\n            $comment = if ($portComments.ContainsKey($port)) { &quot;($($portComments[$port]))&quot; } else { &quot;&quot; }\n            Write-Host &quot;  $port $comment&quot; -ForegroundColor Green\n        }\n    }\n    Write-Host &quot;&quot;\n}\n\n# ---------- \u6dfb\u52a0\u7aef\u53e3\uff08TCP\/UDP\uff09----------\nfunction Add-Ports {\n    param($wslIp)\n    # \u663e\u793a\u5f53\u524d\u5df2\u6709\u7aef\u53e3\n    $existingTcp = Get-TcpRules\n    $existingUdp = Get-UdpFirewallPorts\n    Write-Host &quot;`n\u5f53\u524d\u5df2\u8f6c\u53d1\u7684 TCP \u7aef\u53e3: &quot; -ForegroundColor Cyan -NoNewline\n    if ($existingTcp.Count -gt 0) { Write-Host ($existingTcp.Keys | Sort-Object) -ForegroundColor Green } else { Write-Host &quot;\u65e0&quot; -ForegroundColor DarkGray }\n    Write-Host &quot;\u5f53\u524d UDP \u9632\u706b\u5899\u7aef\u53e3: &quot; -ForegroundColor Cyan -NoNewline\n    if ($existingUdp.Count -gt 0) { Write-Host $existingUdp -ForegroundColor Green } else { Write-Host &quot;\u65e0&quot; -ForegroundColor DarkGray }\n\n    Write-Host &quot;`n\u8bf7\u8f93\u5165\u8981\u6dfb\u52a0\u7684\u7aef\u53e3\u53f7\uff08\u591a\u4e2a\u7528\u9017\u53f7\u5206\u9694\uff0c\u4f8b\u5982 1996,3390,8080\uff09:&quot; -ForegroundColor Cyan\n    $input = Read-Host &quot;\u7aef\u53e3\u5217\u8868&quot;\n    if ([string]::IsNullOrWhiteSpace($input)) {\n        Write-Host &quot;\u672a\u8f93\u5165\u7aef\u53e3\uff0c\u8fd4\u56de\u4e3b\u83dc\u5355\u3002&quot; -ForegroundColor Yellow\n        return\n    }\n    $ports = $input -split &#039;,&#039; | ForEach-Object { $_.Trim() } | Where-Object { $_ -match &#039;^\\d+$&#039; } | ForEach-Object { [int]$_ }\n    if ($ports.Count -eq 0) {\n        Write-Host &quot;\u6ca1\u6709\u6709\u6548\u7684\u7aef\u53e3\u53f7\u3002&quot; -ForegroundColor Red\n        return\n    }\n\n    # \u9009\u62e9\u534f\u8bae\n    Write-Host &quot;\u8bf7\u9009\u62e9\u534f\u8bae: 1 = TCP, 2 = UDP&quot; -ForegroundColor Cyan\n    $protoChoice = Read-Host &quot;\u8bf7\u8f93\u5165\u6570\u5b57&quot;\n    $protocol = if ($protoChoice -eq &#039;1&#039;) { &quot;TCP&quot; } elseif ($protoChoice -eq &#039;2&#039;) { &quot;UDP&quot; } else { &quot;TCP&quot; }\n\n    if ($protocol -eq &quot;UDP&quot;) {\n        Write-Host &quot;\u26a0  netsh portproxy \u4e0d\u652f\u6301 UDP \u8f6c\u53d1\u3002&quot; -ForegroundColor Red\n        Write-Host &quot;   \u5c06\u4ec5\u4e3a\u8fd9\u4e9b\u7aef\u53e3\u6dfb\u52a0 Windows \u9632\u706b\u5899\u89c4\u5219\uff08\u5141\u8bb8\u5165\u7ad9 UDP\uff09\u3002&quot; -ForegroundColor Yellow\n        Write-Host &quot;   \u5b9e\u9645\u7684 UDP \u7aef\u53e3\u8f6c\u53d1\u9700\u8981\u5728 WSL2 \u5185\u81ea\u884c\u914d\u7f6e\uff0c\u4f8b\u5982\uff1a&quot; -ForegroundColor Gray\n        Write-Host &quot;     - \u4f7f\u7528\u955c\u50cf\u7f51\u7edc\u6a21\u5f0f (networkingMode=Mirrored)&quot; -ForegroundColor Gray\n        Write-Host &quot;     - \u5728 WSL2 \u5185\u8fd0\u884c socat UDP-LISTEN:\u7aef\u53e3,fork UDP:\u76ee\u6807IP:\u7aef\u53e3&quot; -ForegroundColor Gray\n        $continue = Read-Host &quot;\u662f\u5426\u7ee7\u7eed\u6dfb\u52a0 UDP \u9632\u706b\u5899\u89c4\u5219\uff1f(y\/N)&quot;\n        if ($continue -ne &#039;y&#039; -and $continue -ne &#039;Y&#039;) { return }\n    }\n\n    # \u786e\u8ba4\u6dfb\u52a0\n    Write-Host &quot;\u5c06\u6dfb\u52a0\u7aef\u53e3: $($ports -join &#039;, &#039;) \u534f\u8bae: $protocol&quot; -ForegroundColor Yellow\n    $confirm = Read-Host &quot;\u786e\u8ba4\u64cd\u4f5c\uff1f(Y\/N)&quot;\n    if ($confirm -ne &#039;Y&#039; -and $confirm -ne &#039;y&#039;) { return }\n\n    foreach ($port in $ports) {\n        if ($protocol -eq &quot;TCP&quot;) {\n            # \u5220\u9664\u65e7\u89c4\u5219\uff08\u907f\u514d\u51b2\u7a81\uff09\n            netsh interface portproxy delete v4tov4 listenport=$port listenaddress=0.0.0.0 2&gt;$null\n            # \u6dfb\u52a0\u65b0\u89c4\u5219\n            netsh interface portproxy add v4tov4 listenport=$port listenaddress=0.0.0.0 connectport=$port connectaddress=$wslIp 2&gt;&amp;1 | Out-Null\n            if ($LASTEXITCODE -eq 0) {\n                Write-Host &quot;\u2713 TCP \u7aef\u53e3 $port \u5df2\u8f6c\u53d1\u81f3 $wslIp&quot; -ForegroundColor Green\n            } else {\n                Write-Host &quot;\u2717 TCP \u7aef\u53e3 $port \u8f6c\u53d1\u5931\u8d25&quot; -ForegroundColor Red\n            }\n        }\n        # \u9632\u706b\u5899\u89c4\u5219\uff08TCP \u548c UDP \u90fd\u6dfb\u52a0\uff09\n        $ruleName = &quot;WSL $protocol Port $port&quot;\n        $existingRule = Get-NetFirewallRule -DisplayName $ruleName -ErrorAction SilentlyContinue\n        if (-not $existingRule) {\n            New-NetFirewallRule -DisplayName $ruleName -Direction Inbound -Action Allow -Protocol $protocol -LocalPort $port | Out-Null\n            Write-Host &quot;\u2713 \u9632\u706b\u5899\u89c4\u5219 ($protocol) $ruleName \u5df2\u521b\u5efa&quot; -ForegroundColor Green\n        } else {\n            Write-Host &quot;\u2713 \u9632\u706b\u5899\u89c4\u5219 ($protocol) $ruleName \u5df2\u5b58\u5728&quot; -ForegroundColor Gray\n        }\n    }\n    Write-Host &quot;\u6dfb\u52a0\u5b8c\u6210\u3002&quot; -ForegroundColor Green\n    pause &quot;\u6309 Enter \u7ee7\u7eed...&quot;\n}\n\n# ---------- \u5220\u9664\u7aef\u53e3\uff08\u652f\u6301 TCP \u89c4\u5219\u548c UDP \u9632\u706b\u5899\u89c4\u5219\uff09----------\nfunction Remove-Ports {\n    $tcpRules = Get-TcpRules\n    $udpPorts = Get-UdpFirewallPorts\n    if ($tcpRules.Count -eq 0 -and $udpPorts.Count -eq 0) {\n        Write-Host &quot;\u6ca1\u6709\u53ef\u5220\u9664\u7684\u89c4\u5219\u3002&quot; -ForegroundColor Yellow\n        pause\n        return\n    }\n\n    # \u663e\u793a TCP \u89c4\u5219\n    if ($tcpRules.Count -gt 0) {\n        Write-Host &quot;`n\u5f53\u524d TCP \u7aef\u53e3\u8f6c\u53d1\u89c4\u5219:&quot; -ForegroundColor Cyan\n        $tcpList = @($tcpRules.Keys | Sort-Object)\n        for ($i = 0; $i -lt $tcpList.Count; $i++) {\n            $port = $tcpList[$i]\n            $comment = if ($portComments.ContainsKey($port)) { &quot; ($($portComments[$port]))&quot; } else { &quot;&quot; }\n            Write-Host &quot;$($i+1). TCP $port$comment&quot;\n        }\n    }\n    # \u663e\u793a UDP \u89c4\u5219\n    if ($udpPorts.Count -gt 0) {\n        Write-Host &quot;`n\u5f53\u524d UDP \u9632\u706b\u5899\u89c4\u5219 (\u4ec5\u5141\u8bb8\u5165\u7ad9\uff0c\u65e0\u8f6c\u53d1):&quot; -ForegroundColor Cyan\n        $udpList = $udpPorts | Sort-Object\n        $startIdx = $tcpRules.Count\n        for ($i = 0; $i -lt $udpList.Count; $i++) {\n            $port = $udpList[$i]\n            $comment = if ($portComments.ContainsKey($port)) { &quot; ($($portComments[$port]))&quot; } else { &quot;&quot; }\n            Write-Host &quot;$($startIdx + $i + 1). UDP $port$comment&quot;\n        }\n    }\n\n    Write-Host &quot;`n\u8bf7\u8f93\u5165\u8981\u5220\u9664\u7684\u89c4\u5219\u5e8f\u53f7\uff08\u4f8b\u5982 1,3\uff09\u6216\u76f4\u63a5\u8f93\u5165\u7aef\u53e3\u53f7\uff08\u4f8b\u5982 1996,3390\uff09:&quot; -ForegroundColor Cyan\n    $input = Read-Host &quot;\u9009\u62e9&quot;\n    if ([string]::IsNullOrWhiteSpace($input)) { return }\n\n    $toDeleteTcp = @()\n    $toDeleteUdp = @()\n    $items = $input -split &#039;,&#039;\n    foreach ($item in $items) {\n        $item = $item.Trim()\n        if ($item -match &#039;^\\d+$&#039;) {\n            $num = [int]$item\n            if ($num -le $tcpRules.Count) {\n                $port = ($tcpRules.Keys | Sort-Object)[$num-1]\n                $toDeleteTcp += $port\n            } elseif ($num -le ($tcpRules.Count + $udpPorts.Count)) {\n                $udpIndex = $num - $tcpRules.Count - 1\n                if ($udpIndex -ge 0 -and $udpIndex -lt $udpPorts.Count) {\n                    $port = ($udpPorts | Sort-Object)[$udpIndex]\n                    $toDeleteUdp += $port\n                } else {\n                    Write-Host &quot;\u65e0\u6548\u5e8f\u53f7: $num&quot; -ForegroundColor Red\n                }\n            } else {\n                Write-Host &quot;\u65e0\u6548\u5e8f\u53f7: $num&quot; -ForegroundColor Red\n            }\n        } elseif ($item -match &#039;^\\d+$&#039;) { # \u76f4\u63a5\u7aef\u53e3\u53f7\n            $port = [int]$item\n            if ($tcpRules.ContainsKey($port)) {\n                $toDeleteTcp += $port\n            } elseif ($udpPorts -contains $port) {\n                $toDeleteUdp += $port\n            } else {\n                Write-Host &quot;\u7aef\u53e3 $port \u672a\u627e\u5230\u4efb\u4f55\u89c4\u5219\u3002&quot; -ForegroundColor Red\n            }\n        } else {\n            Write-Host &quot;\u65e0\u6548\u8f93\u5165: $item&quot; -ForegroundColor Red\n        }\n    }\n\n    if ($toDeleteTcp.Count -eq 0 -and $toDeleteUdp.Count -eq 0) { return }\n\n    Write-Host &quot;\u5c06\u5220\u9664 TCP \u7aef\u53e3: $($toDeleteTcp -join &#039;, &#039;)&quot; -ForegroundColor Yellow\n    Write-Host &quot;\u5c06\u5220\u9664 UDP \u7aef\u53e3: $($toDeleteUdp -join &#039;, &#039;)&quot; -ForegroundColor Yellow\n    $confirm = Read-Host &quot;\u786e\u8ba4\u5220\u9664\uff1f(Y\/N)&quot;\n    if ($confirm -ne &#039;Y&#039; -and $confirm -ne &#039;y&#039;) { return }\n\n    foreach ($port in $toDeleteTcp) {\n        netsh interface portproxy delete v4tov4 listenport=$port listenaddress=0.0.0.0\n        Write-Host &quot;\u2713 \u5df2\u5220\u9664 TCP \u7aef\u53e3 $port \u7684\u8f6c\u53d1\u89c4\u5219&quot; -ForegroundColor Green\n        # \u53ef\u9009\uff1a\u540c\u65f6\u5220\u9664\u5bf9\u5e94\u7684\u9632\u706b\u5899\u89c4\u5219\uff08\u4f46\u4fdd\u7559\u4ee5\u514d\u5f71\u54cd\u5176\u4ed6\uff09\n        # Remove-NetFirewallRule -DisplayName &quot;WSL TCP Port $port&quot; -ErrorAction SilentlyContinue\n    }\n    foreach ($port in $toDeleteUdp) {\n        Remove-NetFirewallRule -DisplayName &quot;WSL UDP Port $port&quot; -ErrorAction SilentlyContinue\n        Write-Host &quot;\u2713 \u5df2\u5220\u9664 UDP \u7aef\u53e3 $port \u7684\u9632\u706b\u5899\u89c4\u5219&quot; -ForegroundColor Green\n    }\n    Write-Host &quot;\u5220\u9664\u5b8c\u6210\u3002&quot; -ForegroundColor Green\n    pause &quot;\u6309 Enter \u7ee7\u7eed...&quot;\n}\n\n# ---------- \u4e3b\u83dc\u5355\uff08\u5b9e\u65f6\u663e\u793a\u7aef\u53e3\u5217\u8868\uff09----------\nfunction MainMenu {\n    Clear-Host\n    $wslIp = Get-WslIp\n    Write-Host &quot;======================================&quot; -ForegroundColor Cyan\n    Write-Host &quot;   WSL2 NAT \u7aef\u53e3\u8f6c\u53d1\u7ba1\u7406\u5de5\u5177&quot; -ForegroundColor White\n    Write-Host &quot;======================================&quot; -ForegroundColor Cyan\n    Write-Host &quot;\u5f53\u524d WSL2 IP \u5730\u5740: $wslIp&quot; -ForegroundColor Green\n    Write-Host &quot;&quot;\n    Write-Host &quot;\u5df2\u8f6c\u53d1\u7684 TCP \u7aef\u53e3 (\u8f6c\u53d1\u89c4\u5219):&quot; -ForegroundColor Yellow\n    Show-TcpPortList\n    Write-Host &quot;&quot;\n    Write-Host &quot;\u5df2\u6dfb\u52a0\u7684 UDP \u7aef\u53e3 (\u4ec5\u9632\u706b\u5899):&quot; -ForegroundColor Yellow\n    Show-UdpPortList\n    Write-Host &quot;&quot;\n    Write-Host &quot;\u8bf7\u9009\u62e9\u64cd\u4f5c:&quot; -ForegroundColor Yellow\n    Write-Host &quot;  1. \u67e5\u770b\u8be6\u7ec6\u89c4\u5219 (TCP + UDP)&quot;\n    Write-Host &quot;  2. \u6dfb\u52a0\u7aef\u53e3\u8f6c\u53d1 (TCP\/UDP)&quot;\n    Write-Host &quot;  3. \u5220\u9664\u7aef\u53e3\u8f6c\u53d1\/\u9632\u706b\u5899\u89c4\u5219 (TCP\/UDP)&quot;\n    Write-Host &quot;  0. \u9000\u51fa&quot;\n    Write-Host &quot;&quot;\n    $choice = Read-Host &quot;\u8bf7\u8f93\u5165\u6570\u5b57&quot;\n    switch ($choice) {\n        &#039;1&#039; { Show-DetailRules; pause &quot;\u6309 Enter \u8fd4\u56de\u83dc\u5355...&quot;; MainMenu }\n        &#039;2&#039; { Add-Ports -wslIp $wslIp; MainMenu }\n        &#039;3&#039; { Remove-Ports; MainMenu }\n        &#039;0&#039; { Write-Host &quot;\u9000\u51fa\u811a\u672c\u3002&quot;; exit 0 }\n        default { Write-Host &quot;\u65e0\u6548\u9009\u9879&quot;; pause &quot;\u6309 Enter \u8fd4\u56de\u83dc\u5355...&quot;; MainMenu }\n    }\n}\n\n# ---------- \u5165\u53e3 ----------\nMainMenu<\/code><\/pre>","protected":false},"excerpt":{"rendered":"<p>WSL2 NAT \u7aef\u53e3\u8f6c\u53d1\u7ba1\u7406\u5de5\u5177 \u4f7f\u7528\u8bf4\u660e 1. \u5de5\u5177\u7b80\u4ecb \u672c\u5de5\u5177\u662f\u4e00\u4e2a PowerShell \u811a\u672c\uff0c\u7528\u4e8e\u5728 WSL2 NAT \u7f51\u7edc\u6a21\u5f0f \u4e0b\u7ba1\u7406 Windows \u5230 WSL2 \u7684\u7aef\u53e3\u8f6c\u53d1\u89c4\u5219\u3002\u5b83\u53ef\u4ee5&#046;&#046;&#046;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,12,29],"tags":[],"class_list":["post-5162","post","type-post","status-publish","format-standard","hentry","category-debian","category-linux-gnu","category-windows"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.liangliang.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/5162","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.liangliang.org.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.liangliang.org.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.liangliang.org.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.liangliang.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5162"}],"version-history":[{"count":8,"href":"https:\/\/www.liangliang.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/5162\/revisions"}],"predecessor-version":[{"id":5173,"href":"https:\/\/www.liangliang.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/5162\/revisions\/5173"}],"wp:attachment":[{"href":"https:\/\/www.liangliang.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.liangliang.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.liangliang.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}