WireGuard+udp2raw异地组网配置过程

WireGuard+udp2raw异地组网配置过程

WireGuard设置过程

安装wireguard

1
2
3

sudo apt update
sudo apt install wireguard 复制

生成本地公私钥

1
2
3

wg genkey | sudo tee /etc/wireguard/privatekey | wg pubkey | sudo tee /etc/wireguard/publickey
复制

编辑连接wg0的配置

1
2
3

sudo nano /etc/wireguard/wg0.conf
复制

VPS主机-配置文件内容:

1
2
3
4
5
6
7
8
9
10
11
12

[Interface]
Address = 10.10.5.2/32
ListenPort = 5632
PrivateKey = PrivateKey

[Peer]
PublicKey = PublicKey
AllowedIPs = 10.10.5.2/32,192.168.1.0/24
#Endpoint = ip or domain:51820
PersistentKeepalive = 10
复制

内网主机-配置文件内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

[Interface]
Address = 10.10.5.1/32
ListenPort = 51820
PrivateKey = PrivateKey
PostUp = iptables -A FORWARD -i %i -j ACCEPT
PostUp = iptables -A FORWARD -o %i -j ACCEPT
PostUp = iptables -t nat -A POSTROUTING -s 10.10.5.0/24 -j SNAT --to-source 192.168.1.194
PostDown = iptables -D FORWARD -i %i -j ACCEPT
PostDown = iptables -D FORWARD -o %i -j ACCEPT
PostDown = iptables -t nat -D POSTROUTING -s 10.10.5.0/24 -j SNAT --to-source 192.168.1.194
#MTU = 1200

[Peer]
PublicKey = PublicKey
AllowedIPs = 10.10.5.2/32
Endpoint = 127.0.0.1:5632
PersistentKeepalive = 10
复制

内网主机开启数据转发

1
2
3

echo 'net.ipv4.ip_forward=1' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p 复制

修改全部文件权限

1
2
3

sudo chmod 600 /etc/wireguard/*
复制

随启动运行

1
2
3
4
5

sudo systemctl enable wg-quick@wg0

sudo systemctl start wg-quick@wg0
复制

VPS端 注意防火墙要打开5632

重启wg0接口

1
2
3

wg-quick down wg0 && wg-quick up wg0
复制

udp2raw部分

下载udp2raw

1
2
3
4
5
6

mkdir udp2raw
cd udp2raw
wget https://github.com/wangyu-/udp2raw/releases/download/20230206.0/udp2raw_binaries.tar.gz

tar -xzf udp2raw_binaries.tar.gz 复制

server.sh 服务端脚本

1
2
3
4
5

#!/bin/bash

nohup /root/udp2raw/udp2raw_amd64 -s -l0.0.0.0:30005 -r127.0.0.1:5632 -k "pass" --raw-mode faketcp --cipher-mode xor  -a > /root/udp2raw/udp_16400.log 2>&1 &
复制

client.sh 客户端脚本

1
2
3
4
5

#!/bin/bash

nohup /root/udp2raw/udp2raw_amd64 -c -l0.0.0.0:5632 -r141.11.175.39:30005 -k "pass" --raw-mode faketcp --cipher-mode xor  -a > /root/udp2raw/udp_16400.log 2>&1 &
复制

修改本地主机的wg0配置文件

1
2
1.添加MTU = 1200
2.修改接入点地址 复制

stop.sh 停止后台运行脚本

1
2
3
4
5
6
7
8
9
10

#!/bin/bash
echo "Stopping udp2raw instances..."
pids=$(ps aux | grep udp2raw_amd64 | grep -v grep | awk '{print $2}')
for pid in $pids; do
    kill -9 $pid
    echo "Stopped process with PID $pid"
done
echo "All udp2raw instances stopped."
复制

udp2raw各参数释义

选项 注释
–raw-mode 伪装模式,支持 faketcp/udp/icmp,用于绕过防火墙
-k,–key 密码设置
--cipher-mode 加密模式(如aes128cbc
--auth-mode 认证模式(如md5/crc32
-a,–auto-rule 自动管理iptables规则
-g,–gen-rule 自动生成 iptables 规则
–disable-anti-replay 禁用反重放保护功能
–source-ip 服务端IP地址
–source-port 服务端端口号
–conf-file 指定配置文件
–fifo 向运行中的程序发送命令
–log-level 日志级别(0静默,1错误,2警告,3信息)
–log-position 设置日志文件名
–disable-color 禁用日志颜色输出
–disable-bpf
–sock-buf 设置 socket 缓冲区大小
–force-sock-buf
–seq-mode
–lower-level MAK地址
–gen-add
–keep-rule 定期主动检查 iptables 规则
–clear 清除iptables规则

原文连接:

WireGuard+udp2raw异地组网配置过程


WireGuard+udp2raw异地组网配置过程
https://www.qxz5637.top/2025/05/30/WireGuard+udp2raw异地组网配置过程/
作者
文心
发布于
2025年5月30日
许可协议