{"id":130,"date":"2022-07-02T23:05:40","date_gmt":"2022-07-02T15:05:40","guid":{"rendered":"https:\/\/blog.messyghost.net\/?p=130"},"modified":"2022-12-09T13:15:05","modified_gmt":"2022-12-09T05:15:05","slug":"%e5%88%a9%e7%94%a8wireguard%e8%bf%9b%e8%a1%8c%e7%ab%af%e5%8f%a3%e8%bd%ac%e5%8f%91","status":"publish","type":"post","link":"https:\/\/blog.messyghost.net\/?p=130","title":{"rendered":"\u5229\u7528WireGuard\u8fdb\u884c\u7aef\u53e3\u8f6c\u53d1"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u524d\u8a00<\/h2>\n\n\n\n<p>\u4ee5\u5f80\u90fd\u662f\u7528FRP\u6765\u8fdb\u884c\u8f6c\u53d1\u7684\uff0c\u4e0d\u8fc7\u8fd9\u79cd\u65b9\u6848\u53ea\u80fd\u8ba9\u5916\u7f51\u901a\u8fc7\u516c\u7f51IP\u7279\u5b9a\u7aef\u53e3\u8bbf\u95ee\u5185\u7f51\uff0c\u4e0d\u80fd\u8ba9\u5185\u7f51\u7684\u8bbe\u5907\u7684\u7aef\u53e3\u6620\u5c04\u5230\u5916\u7f51IP\u7684\u76f8\u5e94\u7aef\u53e3\u4e0a\uff0c\u4f7f\u4e3b\u52a8\u53d1\u51fa\u53bb\u7684\u6570\u636e\u5305\u4e5f\u662f\u7ecf\u8fc7\u516c\u7f51IP\u7684\u3002\u6700\u8fd1\u60f3\u5f00\u4e00\u4e2a\u6e38\u620f\u670d\u5c31\u53d7\u5230\u8fd9\u4e2a\u7279\u6027\u7684\u6298\u78e8\uff0c\u4e8e\u662f\u60f3\u7740\u770b\u770b\u80fd\u4e0d\u80fd\u7ec4\u4e2a\u5185\u7f51\uff0c\u7136\u540e\u5efa\u7acbNAT\u5173\u7cfb\u3002<\/p>\n\n\n\n<p>\u4e00\u5f00\u59cb\u662f\u7528PPTP\u7684\uff0c\u7136\u540e\u5728\u53d7\u9650\u673a\u4e0a\u7528iptables SNAT\u5176IP\u5230\u5efa\u7acb\u7684\u865a\u62df\u7f51\u5361\u7684IP\u4e0a\uff0c\u6b64\u65b9\u6848\u5e76\u4e0d\u53ef\u884c\uff0c\u8c8c\u4f3c\u662f\u4e0d\u80fd\u628a\u672c\u5730\u5730\u5740NAT\u5230\u672c\u5730\u5730\u5740\u4e0a\u3002\u540e\u6765\u67e5\u4e86\u5f88\u591a\u5730\u65b9\uff0c\u8fd8\u6362\u7528\u4e86WireGuard\uff0c\u7ec8\u4e8e\u627e\u5230\u4e86\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n\n\n\n<p>\u8bbe\u5907\uff1a\u90fd\u662fUbuntu Server 20.04<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u547d\u4ee4\u5047\u8bbe\u5728root\u6743\u9650\u4e0b\u6267\u884c\uff0c\u6ce8\u610f\u6743\u9650\u7ba1\u7406\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5efa\u7acb\u5c40\u57df\u7f51<\/h2>\n\n\n\n<p>\u9996\u5148\u8981\u7528WireGuard\u5efa\u7acb\u4e00\u4e2a\u5c40\u57df\u7f51\uff08\u5b50\u7f51\uff1a10.0.0.0\/24\uff09\uff0c\u53c2\u8003\u914d\u7f6e\u5982\u4e0b\uff08\u4e0a\u4e3a\u670d\u52a1\u5668\uff0c\u4e0b\u4e3a\u5ba2\u6237\u7aef\uff0c\u5747\u4fdd\u5b58\u5230\/etc\/wireguard\/wg0.conf\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Interface]\nAddress = 10.0.0.1\/24\nPrivateKey = <strong><em>&lt;Key><\/em><\/strong>\nListenPort = 51820\n&#91;Peer]\nPublicKey = <strong><em>&lt;Key><\/em><\/strong>\nAllowedIPs = 10.0.0.31<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Interface]\nPrivateKey = <strong><em>&lt;Key><\/em><\/strong>\nListenPort = 21841\nAddress = 10.0.0.31\/24\n\n&#91;Peer]\nPublicKey = <strong><em>&lt;Key><\/em><\/strong>\nAllowedIPs = 0.0.0.0\/0\nEndpoint = <strong><em>&lt;IP><\/em><\/strong>:51820\nPersistentKeepalive = 25<\/code><\/pre>\n\n\n\n<p>\u9700\u8981\u6267\u884c\u7684\u5176\u4ed6\u547d\u4ee4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>\u751f\u6210\u5bc6\u94a5\uff08\u7528\u4e8e\u52a0\u5bc6\u94fe\u63a5\uff09<\/em>\n$ <strong>wg keygen &gt; private_key<\/strong>\n$ <strong>wg pubkey &lt; private_key &gt; public_key<\/strong>\n\n<em>\u542f\u7528\u8bbe\u5907<\/em>\n# <strong>wg-quick up wg0<\/strong><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u8f6c\u53d1\u670d\u52a1\u5668\u6d41\u91cf<\/h2>\n\n\n\n<p>\u6211\u4eec\u5047\u8bbe\u670d\u52a1\u5668\u516c\u7f51IP\u4e3aA.B.C.D\uff0c\u9700\u8981\u8f6c\u53d1\u7684\u7aef\u53e3\u662ftcp\/E\uff08A.B.C.E:E-&gt;\u53d7\u9650\u8bbe\u5907:E\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>\u628a\u670d\u52a1\u5668\u63a5\u6536\u5230\u7684\u6d41\u91cf\u8f6c\u53d1\u7ed9\u53d7\u9650\u8bbe\u5907<\/em>\n# <strong>iptables -t nat -A PREROUTING -p tcp -d A.B.C.D --dport E -j DNAT --to-destination 10.0.0.31:E<\/strong>\n\n<em>\u4ece\u53d7\u9650\u8bbe\u5907\u53d1\u51fa\u7684\u6d41\u91cf\u8981\u6539\u5199\u4e3a\u670d\u52a1\u5668IP\uff08\u5982\u679c\u53ea\u9700\u8981\u8ba9\u5916\u7f51\u80fd\u8bbf\u95ee\u5185\u7f51\uff0c\u5219\u4e0d\u9700\u8981\u8fd9\u4e00\u6b65\uff09<\/em>\n# <strong>iptables -t nat -A POSTROUTING -p tcp -s 10.0.0.31 --sport E -j SNAT --to-source A.B.C.D:E<\/strong>\n\n<em>\u5f00\u542f\u5185\u6838\u529f\u80fd\uff1a\u7aef\u53e3\u8f6c\u53d1<\/em>\n# <strong>sysctl -w net.ipv4.ip_forward=1<\/strong><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u8f6c\u53d1\u53d7\u9650\u673a\u6d41\u91cf<\/h2>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u8bf7\u6ce8\u610f\uff1aiptables -t nat -A POSTROUTING -p tcp &#8211;sport E -j SNAT &#8211;to-source 10.0.0.31:E \u4e0d\u53ef\u53d6\u3002<\/p>\n<\/blockquote>\n<\/div>\n<\/div>\n\n\n\n<p>\u8981\u8ba9\u53d7\u9650\u673a\u7684\u6d41\u91cf\u7ecf\u8fc7\u670d\u52a1\u5668\u624d\u80fd\u8fdb\u884c\u8f6c\u53d1\uff0c\u4f46\u662f\u6211\u4eec\u4e0d\u5e0c\u671b\u53d7\u9650\u673a\u8bbf\u95ee\u5916\u7f51\u7684\u6d41\u91cf\u5168\u90e8\u8d70\u670d\u52a1\u5668\uff08\u670d\u52a1\u5668\u6d41\u91cf\u6709\u9650\u3001\u5bbd\u5e26\u4e0d\u5982\u5bb6\u7528\u5bbd\u5e26\u9ad8\uff09\uff0c\u6240\u4ee5\u6211\u4eec\u7684\u601d\u8def\u662f\uff1a\u7ed9\u9700\u8981\u8f6c\u53d1\u7684\u5305\u6253\u4e0a\u6807\u8bb0\uff0c\u8d70\u8def\u7531\u8868\u3002\u4e0b\u9762\u547d\u4ee4\u8d70\u8def\u7531\u8868100\uff0c\u6253\u4e0a\u6807\u8bb00x64\uff0c\u6ce8\u610f\u9632\u6b62\u51b2\u7a81\u3002\u5047\u8bbe\u53d7\u9650\u673a\u6240\u5728\u5b50\u7f51\u4e3a192.168.1.0\/24\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>\u67090x64\u6807\u8bb0\u7684\u6570\u636e\u5305\u8d70100\u53f7\u8def\u7531\u8868<\/em>\n# <strong>ip rule add fwmark 0x64 table 100<\/strong>\n<em>100\u53f7\u8def\u7531\u8868\u7684\u9ed8\u8ba4\u7f51\u5173\u662f\u670d\u52a1\u5668<\/em>\n# <strong>ip route add table 100 default via 10.0.0.1<\/strong>\n<em>\u7ed9E\u7aef\u53e3\u53d1\u5f80\u5b50\u7f51\u5916\u7684\u5305\u6253\u4e0a\u6807\u8bb0\u3002\u7531\u4e8e\u6ca1\u6709\u7528\u65b0\u7684\u94fe\uff0c\u6240\u4ee5\u4f7f\u7528\u53d6\u53cd<\/em>\n# <strong>iptables -t mangle -A OUTPUT -p tcp ! -d 192.168.1.0\/24 --sport <em>E<\/em> -j MARK --set-mark 0x64<\/strong>\n<em>\u7ed9\u9001\u5f80\u670d\u52a1\u5668\u7684\u5305\u5199\u4e0a\u6b63\u786e\u7684IP<\/em>\n# <strong>iptables -t nat -A POSTROUTING -o wg0 -p tcp --sport <em>E<\/em> -j SNAT --to-source 10.0.0.31:<em>E<\/em><\/strong><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5c3e\u58f0<\/h2>\n\n\n\n<p>\u81f3\u6b64\uff0c\u7aef\u53e3\u8f6c\u53d1\u5df2\u914d\u7f6e\u5b8c\u6bd5\u3002\u6709\u5173\u914d\u7f6e\u6301\u4e45\u5316\u7684\u95ee\u9898\u672c\u6587\u4e0d\u63a2\u8ba8\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528UDP\u534f\u8bae\u7684\u8bdd\uff0c\u5728\u56de\u5e94\u9636\u6bb5\u53ef\u80fd\u4f1a\u5bf9\u5e94\u9519\u8bef\u7aef\u53e3\uff0c\u53ef\u4ee5\u9488\u5bf9\u5176\u5173\u95edCONNTRACK\u89e3\u51b3\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u756a\u5916<\/h2>\n\n\n\n<p>\u4ec0\u4e48\uff1f\uff01\u8fd8\u6709nftables\u8fd9\u79cd\u597d\u4e1c\u897f\uff08ufw\u548cfirewalld\u4e0d\u5982\u5c31\u4e0d\u5199\u4e86\u5427\u2026\u2026\uff09\uff1f\uff01<\/p>\n\n\n\n<p>\u53c2\u8003\u914d\u7f6e\uff1a<\/p>\n\n\n\n<p>\u670d\u52a1\u5668\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\ntable ip proxy {\n    define lip = <em><strong>A.B.C.D<\/strong><\/em>\n    define cip = 10.0.0.31\n\n    # \u53d7\u9650\u673a\u548c\u670d\u52a1\u5668\u540c\u7aef\u53e3\n    define tcpports = {<em><strong>E<\/strong><\/em>, ...}\n    define udpports = {<em><strong>E<\/strong><\/em>, ...}\n\n    # \u7aef\u53e3\u6620\u5c04\n    chain prerouting {\n        type filter hook prerouting priority 0; policy accept;\n        # type nat\u7136\u540ednat to $cip\u4e5f\u884c\n        tcp dport $tcpports ip daddr set $cip\n        udp dport $udpports ip daddr set $cip\n    }\n\n    chain postrouting {\n        type nat hook postrouting priority 0; policy accept;\n        tcp sport $tcpports ip saddr set $lip\n        udp sport $udpports ip saddr set $lip\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u5ba2\u6237\u7aef\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>table ip proxy {\n    define pip = 10.0.0.31\n    define lnet = 192.168.1.0\/24\n    define rmark = 0x64\n    \n    define odev = wg0\n    \n    define tcpports = {<em><strong>E<\/strong><\/em>, ...}\n    define udpports = {<em><strong>E<\/strong><\/em>, ...}\n\n    # \u6253\u4e0a\u6807\u8bb0\uff0c\u8d70\u8def\u7531\u8868\n    chain output {\n        type route hook output priority 0; policy accept;\n        ip daddr $lnet return\n        tcp sport $tcpports mark set $rmark\n        udp sport $udpports mark set $rmark\n    }\n\n    # \u51fa\u7f51\u5361\u65f6\u6539\u5199\u4e3a\u6b63\u786e\u7684IP\n    chain postrouting {\n        type filter hook postrouting priority 0; policy accept;\n        meta oifname $odev ip saddr set $pip\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u4ee5\u5f80\u90fd\u662f\u7528FRP\u6765\u8fdb\u884c\u8f6c\u53d1\u7684\uff0c\u4e0d\u8fc7\u8fd9\u79cd\u65b9\u6848\u53ea&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/blog.messyghost.net\/?p=130\">Continue reading<span class=\"screen-reader-text\">\u5229\u7528WireGuard\u8fdb\u884c\u7aef\u53e3\u8f6c\u53d1<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,8],"tags":[9,15,14,10,11,12],"class_list":["post-130","post","type-post","status-publish","format-standard","hentry","category-3","category-8","tag-iptables","tag-netfilter","tag-nftables","tag-wireguard","tag-11","tag-12","entry"],"_links":{"self":[{"href":"https:\/\/blog.messyghost.net\/index.php?rest_route=\/wp\/v2\/posts\/130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.messyghost.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.messyghost.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.messyghost.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.messyghost.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=130"}],"version-history":[{"count":11,"href":"https:\/\/blog.messyghost.net\/index.php?rest_route=\/wp\/v2\/posts\/130\/revisions"}],"predecessor-version":[{"id":285,"href":"https:\/\/blog.messyghost.net\/index.php?rest_route=\/wp\/v2\/posts\/130\/revisions\/285"}],"wp:attachment":[{"href":"https:\/\/blog.messyghost.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.messyghost.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.messyghost.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}