静看光阴荏苒
不管不顾不问不说也不念

利用WireGuard建立IPv6隧道

WireGuard本身就是一个可以组网的东西,之前我自己还写了个一键安装脚本来着,后来发现这货还支持IPv6,正好我本地没有IPv6,就想着看能不能用这个搞个隧道让本机也能用上IPv6。

能用是能用,但也有问题,全部搞完才发现TunSafe这玩意强制你必须配置一个IPv4才能正常连接,这样一来的话IPv6隧道的意义就不大了。。我是想本地的IPv4还是走本地网络,只有IPv6才走WireGuard,但是现在貌似不能这样。。。

蛋疼的一批,还是把过程记录一下吧。。其实和普通的配置没多大区别。。原理无非就是先通过WireGuard建一个虚拟局域网,然后在这个局域网内WireGuard会分配给我们一个内网的v4和v6。连接上之后再把流量转发到外网。

Debian9安装:

echo "deb http://deb.debian.org/debian/ unstable main" > /etc/apt/sources.list.d/unstable.list
apt -y update
apt -y install linux-headers-$(uname -r)
apt -y install openresolv
apt -y install resolvconf
apt -y install wireguard

CentOS7:

curl -Lo /etc/yum.repos.d/wireguard.repo https://copr.fedorainfracloud.org/coprs/jdoss/wireguard/repo/epel-7/jdoss-wireguard-epel-7.repo
yum -y install epel-release
yum -y install kernel-headers-$(uname -r) kernel-devel-$(uname -r)
yum -y install wireguard-dkms wireguard-tools

开IPv4和v6的转发:

echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
echo "net.ipv6.conf.all.forwarding = 1" >> /etc/sysctl.conf
sysctl -p

CentOS7要自己手动新建一个配置文件存放目录,Debian不需要:

mkdir -p /etc/wireguard

然后生成服务端的私钥/公钥

wg genkey | tee /etc/wireguard/privatekey | wg pubkey > /etc/wireguard/publickey

接着是客户端的私钥/公钥:

wg genkey | tee /etc/wireguard/clientprivatekey | wg pubkey > /etc/wireguard/clientpublickey

然后新建服务端的配置文件:

nano /etc/wireguard/wg0.conf

Debian9的配置如下:

[Interface]
PrivateKey = 服务端私钥
Address = 192.168.0.1/24, 2001:20:2333::1/28
ListenPort = 23333
DNS = 8.8.8.8, 2001:4860:4860::8888
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o ens18 -j MASQUERADE; ip6tables -A FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -A POSTROUTING -o ens18 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o ens18 -j MASQUERADE; ip6tables -D FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -D POSTROUTING -o ens18 -j MASQUERADE
SaveConfig = true

[Peer]
PublicKey = 客户端公钥
AllowedIPs = 192.168.0.0/24, 2001:20:2333::1/28

因为WireGuard在配置文件内支持这种PostUp/PostDown的语法,所以我们可以直接把iptables的转发规则写在配置文件内,这样每次开机只要WireGuard运行都会自动加载iptables配置。

如果是CentOS7,配置文件就这样写:

[Interface]
PrivateKey = 服务端私钥
Address = 192.168.0.1/24, 2001:20:2333::1/28
ListenPort = 23333
DNS = 8.8.8.8, 2001:4860:4860::8888

[Peer]
PublicKey = 客户端公钥
AllowedIPs = 192.168.0.0/24, 2001:20:2333::1/28

然后我们自己用firewall做转发:

firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=192.168.0.0/24 masquerade'
firewall-cmd --permanent --direct --add-rule ipv4 filter FORWARD 0 -i wg0 -o eth0 -j ACCEPT
firewall-cmd --permanent --add-rich-rule='rule family=ipv6 source address=2001:20:2333::1/28 masquerade'
firewall-cmd --permanent --direct --add-rule ipv6 filter FORWARD 0 -i wg0 -o eth0 -j ACCEPT
firewall-cmd --permanent --add-port=23333/udp
firewall-cmd --reload

属实无论是iptables还是firewalld这些防火墙真的难用的一批,我一般情况下,不是必须用到它的功能,我都是直接关闭的,或者自己装一个ufw。

还有我他妈前段时间在Proxmox上配防火墙把全端口封了(我把我自己强了),还好那机器有各种救援系统,登进去搞了几个小时算是恢复了,不然要出事。。。算了不扯这些没用的了。。

现在启动WireGuard:

wg-quick up wg0

没问题的话设置开机启动:

systemctl enable wg-quick@wg0

接下来就是客户端配置文件了,这个通用:

[Interface]
PrivateKey = 客户端私钥
Address = 192.168.0.2/24, 2001:20:2333::666/28
DNS = 8.8.8.8, 2001:4860:4860::8888

[Peer]
PublicKey = 服务端公钥
Endpoint = 服务端公网IP:23333
AllowedIPs = 0.0.0.0/0, ::0/0
PersistentKeepalive = 25

配置到TunSafe之后连接,你本地应该就可以用上服务器内的IPv6了,而且我发现走IPv6的流量没干扰,比如访问Google,用WireGuard走IPv4各种阻断,IPv6就畅通无阻。。。

烦躁的一批,想用个IPv6就这么难!还有那些公共的免费隧道,比如HE的那个,延迟太高了,没什么卵用。

赞(14)
未经允许不得转载:荒岛 » 利用WireGuard建立IPv6隧道
分享到: 更多 (0)

评论 13

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #1

    高产大佬 :idea:

    瞎折腾5年前 (2019-02-13) Google Chrome 71.0.3578.99 Google Chrome 71.0.3578.99 Android 6.0.1 Android 6.0.1回复
  2. #2

    然而并不能用 :shock:
    没有检测到 IPv6 地址
    当内容商同时提供 IPv4 和 IPv6 时,你的浏览器会欣然通过 IPv4 连接。
    连接纯 IPv6 网站超时,你会发现那些网站都打不开。

    海马5年前 (2019-03-06) Google Chrome 74.0.3725.3 Google Chrome 74.0.3725.3 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • 我弄这个并不是拿来访问网站的,但是你这连IPv6都没检测到就说明是配置有问题了。。

      LALA5年前 (2019-03-07) Google Chrome 71.0.3578.98 Google Chrome 71.0.3578.98 Windows 10 x64 Edition Windows 10 x64 Edition回复
      • 我本意也不是拿来访问某些网站的….我想拿来挂PT,搞半天也没研究出来怎么只让V6走WireGuard

        海马5年前 (2019-03-24) Google Chrome 73.0.3683.75 Google Chrome 73.0.3683.75 Windows 7 Windows 7回复
        • ZeroTier可以:https://lala.im/5015.html

          LALA5年前 (2019-03-24) Google Chrome 72.0.3626.121 Google Chrome 72.0.3626.121 Windows 10 x64 Edition Windows 10 x64 Edition回复
  3. #3

    请问下如何设置运用多台服务器,实现多跳?

    15年前 (2019-03-12) Google Chrome 71.0.3578.98 Google Chrome 71.0.3578.98 Windows 10 x64 Edition Windows 10 x64 Edition回复
  4. #4

    感觉其实并不完善。WireGuard是Tier3的,只能实现客户机获取内网地址,并访问IPv6网络。虽然地址形式上像是公网,但其实还是内网。

    newcoderlife5年前 (2019-08-08) Google Chrome 76.0.3809.100 Google Chrome 76.0.3809.100 Mac OS X  10.14.6 Mac OS X 10.14.6回复
    • 是的,也就是随便玩玩,别当真。。

      LALA5年前 (2019-08-09) Google Chrome 74.0.3729.169 Google Chrome 74.0.3729.169 Windows 10 x64 Edition Windows 10 x64 Edition回复
  5. #5

    :grin: :grin:

    嘿哈3年前 (2021-07-14) Google Chrome 91.0.4472.114 Google Chrome 91.0.4472.114 Mac OS X  10.15.7 Mac OS X 10.15.7回复

分享创造快乐

广告合作资源投稿