266 次浏览
通过中转服务器,实现在外网同时访问家庭内网和公司内网,并在内网使用透明代理实现科学上网。
需要一台在可以科学上网的在公网上运行 V2ray 的服务器作为中转服务器,如阿里云服务器。
假设中转服务器为 A,家庭内网设备为 B,公司内网设备为 C,外网设备为 D。
B 和 C 通过V2Ray主动连接中转服务器 A 建立加密隧道,D 作为客户端连接中转服务器 A。
最终实现:
1)在公司时可访问家庭内网设备并通过透明代理进行科学上网;
2)在家庭时可访问公司内网设备并通过透明代理进行科学上网;
3)在外网时可访问家庭内网设备和公司内网设备并进行科学上网。
1、中转服务器配置
1)配置V2Ray
下载并执行官方安装脚本
bash <(curl -L -s https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh)启动并检查V2ray运行状态:
sudo systemctl start v2ray # 启动
sudo systemctl enable v2ray # 设置开机自启
sudo systemctl status v2ray # 检查服务状态配置V2ray服务端:(使用Vmess+WS+TLS协议并通过Nginx反代)
sudo nano /usr/local/etc/v2ray/config.json内容如下:
{
"log": {
"access": "/var/log/v2ray/access.log", // 访问日志路径
"error": "/var/log/v2ray/error.log", // 错误日志路径
"loglevel": "warning" // 日志等级,可选:debug / info / warning / error / none
},
"reverse": {
"portals": [ // 反向代理实现内网穿透
{
"tag": "portal-out-home",
"domain": "reverse.home" // 与家庭内网 bridges 的 domain 对应
},
{
"tag": "portal-out-comp",
"domain": "reverse.comp" // 与公司内网 bridges 的 domain 对应
}
]
},
"inbounds": [
{
"tag": "vmess-in", // vmess + WebSocket + TLS 入站
"listen": "127.0.0.1", // 只监听本地连接,由 Nginx 反向代理
"port": 10000, // 此端口必须与 Nginx 配置中 proxy_pass 的端口一致
"protocol": "vmess",
"settings": {
"clients": [{
"id": "输入UUID", // 使用命令成 UUID cat /proc/sys/kernel/random/uuid
"alterId": 0
}]
},
"streamSettings": {
"network": "ws", // 使用 WebSocket 传输
"wsSettings": {
"path": "/v2ray" // 此路径必须与 Nginx 配置中的 location 路径一致
}
}
}
],
"outbounds": [
{
"tag": "direct-out", // 直接出站
"protocol": "freedom", // 使用freedom直接出站
"settings": {}
}
],
"routing": {
"rules": [
{
"type":"field", // 识别家庭内网主动发起的反向代理连接
"inboundTag": ["vmess-in"],
"domain": ["full:reverse.home"],
"outboundTag": "portal-out-home"
},
{
"type": "field", // 把对家庭内网网段的请求转发给家庭内网
"inboundTag": ["vmess-in"],
"ip": ["192.168.31.0/24"],
"outboundTag": "portal-out-home"
},
{
"type":"field", // 识别公司内网主动发起的反向代理连接
"inboundTag": ["vmess-in"],
"domain": ["full:reverse.comp"],
"outboundTag": "portal-out-comp"
},
{
"type": "field", // 把对公司内网网段的请求转发给公司内网
"inboundTag": ["vmess-in"],
"ip": ["192.168.1.0/24", "192.168.80.0/24"],
"outboundTag": "portal-out-comp"
},
{
"type": "field", // 其他请求直接出站
"inboundTag": ["vmess-in"],
"outboundTag": "direct-out"
}
]
}
}测试并重载V2Ray
# 检查配置语法
sudo /usr/local/bin/v2ray test -config /usr/local/etc/v2ray/config.json
# 重启服务
sudo systemctl restart v2ray
# 检查服务状态
sudo systemctl status v2ray
# 查看V2Ray日志
journalctl -u v2ray -f
sudo tail -f /var/log/v2ray/error.log2)获取SSL证书(使用 Let’s Encrypt)
安装 Certbot
sudo apt install certbot python3-certbot-nginx -y运行以下命令申请证书(注意:在此之前 Nginx 配置中还不能引用证书文件,否则 nginx -t 会失败):
sudo certbot --nginx -d your-domain.com或更明确地指定 Nginx 插件:替换 your-domain.com 为您的域名,替换 your-email@example.com 为您的邮箱(用于紧急通知和恢复)。
sudo certbot --nginx -d your-domain.com --non-interactive --agree-tos -m your-email@example.com验证成功后,自动签发证书并保存到:
/etc/letsencrypt/live/your-domain.com/fullchain.pem
/etc/letsencrypt/live/your-domain.com/privkey.pem
自动续签:
sudo crontab -e手动添加一个cron任务,用于自动续签:
0 3 * * * /usr/bin/certbot renew --quiet --post-hook "nginx -s reload"命令说明:
0 3 * * * 表示每天凌晨3点执行任务
–quiet 让命令以静默方式运行,不输出无关信息。
–post-hook “nginx -s reload” 重新加载Nginx配置,让新证书生效
3)配置 Nginx ,用于反向代理和 TLS
安装 Nginx:
sudo apt install nginx -y创建站点配置文件
sudo nano /etc/nginx/conf.d/v2ray.conf写入以下内容(替换 your-domain.com )
server {
listen 443 ssl http2; # 启用 HTTP/2 可以提供更好的性能
listen [::]:443 ssl http2; # 监听 IPv6 地址
server_name your-domain.com; # 使用域名匹配
# SSL 证书路径(使用Let's Encrypt生成)
ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem;
# 用于 V2Ray WebSocket
location /v2ray { # 此路径与 V2Ray 配置和客户端保持一致
if ($http_upgrade != "websocket") {return 404;} # 外网直接访问时返回404
proxy_pass http://127.0.0.1:10000; # 此端口需要与 V2Ray 配置中的端口一致
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# WebSocket 超时设置
proxy_read_timeout 3600s;
proxy_send_timeout 3600s;
}
# 指向内部web服务
location / {
proxy_pass http://127.0.0.1:80; # 如本地wordpress端口
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
# 重定向所有HTTP(80)请求到HTTPS(443)
server {
listen 80;
listen [::]:80; # 监听 IPv6 地址
server_name your-domain.com; # 使用域名匹配
# 告诉搜索引擎和浏览器这是一个永久重定向
return 301 https://$server_name$request_uri;
}测试并重载Nginx
# 测试Nginx
sudo nginx -t
# 重启服务
sudo systemctl restart nginx
# 检查服务状态
sudo systemctl status nginx2、家庭内网配置
1)配置V2ray服务器
配置V2ray:
sudo nano /usr/local/etc/v2ray/config.json内容如下:
{
"reverse": {
"bridges": [ // 反向代理实现内网穿透
{
"tag": "bridge-in",
"domain": "reverse.home" // 与中转服务器 portals 的 domain 对应
}
]
},
"inbounds": [
{
"tag": "tproxy-in",
"port": 12345, // 透明代理监听端口
"protocol": "dokodemo-door",
"settings": {
"network": "tcp,udp",
"followRedirect": true
},
"sniffing": {
"enabled": true,
"destOverride": ["http","tls"]
},
"streamSettings": {
"sockopt": {
"tproxy": "tproxy", // 透明代理使用 TPROXY 方式
"mark":255 // 设置 mark 标记,防止回环
}
}
}
],
"outbounds": [
{
"tag": "proxy-out", // 代理出站,连接中转服务器
"protocol": "vmess",
"settings": {
"vnext": [{
"address": "输入代理节点地址或域名",
"port": 443,
"users":[{ "id": "输入UUID", "alterId": 0, "security": "auto"}]
}]
},
"streamSettings": {
"network": "ws",
"security": "tls",
"tlsSettings": {
"serverName": "输入代理节点域名",
"allowInsecure": false
},
"wsSettings": {
"path": "/v2ray"
},
"sockopt": {
"mark": 255
}
}
},
{
"tag": "direct-out", // 直接出站
"protocol": "freedom", // 使用 freedom 直接出站
"settings": {
"domainStrategy": "UseIP" // 域名解析策略,UseIP:优先使用IP,可以提高连接成功率
},
"streamSettings": {
"sockopt": {
"mark": 255
}
}
},
{
"tag": "block-out", // 拦截出站
"protocol": "blackhole", // 黑洞协议丢弃所有数据
"settings": {}
},
{
"tag": "dns-out", // DNS 转发
"protocol": "dns",
"streamSettings": {
"sockopt": {
"mark": 255
}
}
}
],
"dns": {
"servers": [
{
"address": "223.5.5.5", //中国大陆域名使用阿里的 DNS
"port": 53,
"domains": [
"geosite:cn",
"ntp.org", // NTP 服务器
"输入代理节点域名" // 中转服务器
]
},
{
"address": "114.114.114.114", //中国大陆域名使用 114 的 DNS (备用)
"port": 53,
"domains": [
"geosite:cn",
"ntp.org", // NTP 服务器
"输入代理节点域名" // 中转服务器
]
},
{
"address": "8.8.8.8", //非中国大陆域名使用 Google 的 DNS
"port": 53,
"domains": ["geosite:geolocation-!cn"]
},
{
"address": "1.1.1.1", //非中国大陆域名使用 Cloudflare 的 DNS (备用)
"port": 53,
"domains": ["geosite:geolocation-!cn"]
}
]
},
"routing": {
"domainStrategy": "IPOnDemand", //域名解析策略,IPOnDemand: 匹配时随时进行域名解析
"rules": [
{
"type": "field", // 识别反向桥接流量
"inboundTag": ["bridge-in"],
"domain": ["full:reverse.home"],
"outboundTag": "proxy-out" // 转发到代理服务器
},
{
"type":"field", // 反向连接内部服务
"inboundTag": ["bridge-in"],
"outboundTag": "direct-out" // 最终连接本地服务
},
{
"type": "field", // 53 端口 UDP 流量,使用 V2Ray 的 DNS
"inboundTag": ["tproxy-in"],
"port": 53,
"network": "udp",
"outboundTag": "dns-out"
},
{
"type": "field", // 123 端口 UDP 流量直出 (NTP 协议)
"inboundTag": ["tproxy-in"],
"port": 123,
"network": "udp",
"outboundTag": "direct-out"
},
{
"type": "field", // 国内 DNS 服务器地址直连,以达到 DNS 分流目的
"ip": ["223.5.5.5", "114.114.114.114"],
"outboundTag": "direct-out"
},
{
"type": "field", // 国外 DNS 服务器地址走代理,以达到 DNS 分流目的
"ip": ["8.8.8.8", "1.1.1.1"],
"outboundTag": "proxy-out"
},
{
"type": "field", // 广告拦截
"domain": ["geosite:category-ads-all"],
"outboundTag": "block-out"
},
{
"type": "field", // 国外流量走代理
"domain": ["geosite:geolocation-!cn"],
"outboundTag": "proxy-out"
},
{
"type": "field", // 对公司内网的访问走代理
"ip": ["192.168.1.0/24", "192.168.80.0/24"],
"outboundTag": "proxy-out"
},
{
"type": "field", // 其他请求直接出站
"inboundTag": ["tproxy-in"],
"outboundTag": "direct-out"
}
]
}
}测试并重载V2Ray
# 检查配置语法
sudo /usr/local/bin/v2ray test -config /usr/local/etc/v2ray/config.json
# 重启服务
sudo systemctl restart v2ray
# 检查服务状态
sudo systemctl status v2ray
# 查看V2Ray日志
journalctl -u v2ray -f
sudo tail -f /var/log/v2ray/error.log2)配置透明代理规则
iptables 规则:
执行下面的命令开启透明代理。由于使用了 TPROXY 方式的透明代理,所以 TCP 流量也是使用 mangle 表。
# 清空旧规则
ip rule del fwmark 1 table 100 2>/dev/null
ip route flush table 100# 设置策略路由
ip rule add fwmark 1 table 100
ip route add local 0.0.0.0/0 dev lo table 100
# 代理局域网设备
iptables -t mangle -N V2RAY
iptables -t mangle -A V2RAY -d 127.0.0.1/32 -j RETURN
iptables -t mangle -A V2RAY -d 224.0.0.0/4 -j RETURN
iptables -t mangle -A V2RAY -d 255.255.255.255/32 -j RETURN
iptables -t mangle -A V2RAY -d 192.168.0.0/16 -p tcp -j RETURN # 直连局域网,避免 V2Ray 无法启动时无法连网关的 SSH,如果你配置的是其他网段(如 10.x.x.x 等),则修改成自己的
iptables -t mangle -A V2RAY -d 192.168.0.0/16 -p udp ! --dport 53 -j RETURN # 直连局域网,53 端口除外(因为要使用 V2Ray 的 DNS)
iptables -t mangle -A V2RAY -j RETURN -m mark --mark 0xff # 直连 SO_MARK 为 0xff 的流量(0xff 是 16 进制数,数值上等同与上面V2Ray 配置的 255),此规则目的是解决v2ray占用大量CPU(https://github.com/v2ray/v2ray-core/issues/2621)
iptables -t mangle -A V2RAY -p udp -j TPROXY --on-ip 127.0.0.1 --on-port 12345 --tproxy-mark 1 # 给 UDP 打标记 1,转发至 12345 端口
iptables -t mangle -A V2RAY -p tcp -j TPROXY --on-ip 127.0.0.1 --on-port 12345 --tproxy-mark 1 # 给 TCP 打标记 1,转发至 12345 端口
iptables -t mangle -A PREROUTING -j V2RAY # 应用规则
# 代理网关本机
iptables -t mangle -N V2RAY_MASK
iptables -t mangle -A V2RAY_MASK -d 224.0.0.0/4 -j RETURN
iptables -t mangle -A V2RAY_MASK -d 255.255.255.255/32 -j RETURN
iptables -t mangle -A V2RAY_MASK -d 192.168.0.0/16 -p tcp -j RETURN # 直连局域网
iptables -t mangle -A V2RAY_MASK -d 192.168.0.0/16 -p udp ! --dport 53 -j RETURN # 直连局域网,53 端口除外(因为要使用 V2Ray 的 DNS)
iptables -t mangle -A V2RAY_MASK -j RETURN -m mark --mark 0xff # 直连 SO_MARK 为 0xff 的流量(0xff 是 16 进制数,数值上等同与上面V2Ray 配置的 255),此规则目的是避免代理本机(网关)流量出现回环问题
iptables -t mangle -A V2RAY_MASK -p udp -j MARK --set-mark 1 # 给 UDP 打标记,重路由
iptables -t mangle -A V2RAY_MASK -p tcp -j MARK --set-mark 1 # 给 TCP 打标记,重路由
iptables -t mangle -A OUTPUT -j V2RAY_MASK # 应用规则
# 新建 DIVERT 规则,避免已有连接的包二次通过 TPROXY,理论上有一定的性能提升
iptables -t mangle -N DIVERT
iptables -t mangle -A DIVERT -j MARK --set-mark 1
iptables -t mangle -A DIVERT -j ACCEPT
iptables -t mangle -I PREROUTING -p tcp -m socket -j DIVERT执行了以上 ip 和 iptables 命令后,局域网同网段的设备以及网关本身就可以直接翻墙了。
3)开机自动运行透明代理规则
由于策略路由以及 iptables/nftables 有重启会失效的特性,所以当测试配置没有问题之后,需要配置服务在开机时自动配置策略路由和 iptables,否则每次开机的时候就要手动执行一遍。
由于 iptables 命令有点多,所以先将 iptables 规则保存到 /etc/iptables/rules.v4 中。
mkdir -p /etc/iptables && iptables-save > /etc/iptables/rules.v4创建服务文件:
sudo nano /etc/systemd/system/v2ray-tproxy.service内容如下:
[Unit]
Description=V2Ray TProxy Setup
After=network.target
Wants=network.target
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=-/sbin/ip rule add fwmark 1 table 100 ; /sbin/ip route add local 0.0.0.0/0 dev lo table 100 ; /sbin/iptables-restore /etc/iptables/rules.v4
ExecStop=-/sbin/ip rule del fwmark 1 table 100 ; /sbin/ip route del local 0.0.0.0/0 dev lo table 100 ; /sbin/iptables -t mangle -F
[Install]
WantedBy=multi-user.target启用和测试服务
# 开机自启
sudo systemctl enable v2ray-tproxy
# 重新加载配置
sudo systemctl daemon-reload
# 清空旧规则
ip rule del fwmark 1 table 100 2>/dev/null
ip route flush table 100
# 启动服务
sudo systemctl start v2ray-tproxy
# 重启服务
sudo systemctl restart v2ray-tproxy
# 检查服务状态
sudo systemctl status v2ray-tproxy
# 检查iptables规则
sudo iptables -t mangle -L -n -v
# 测试网络连接
curl -I http://www.google.com
curl -I http://www.baidu.com4)解决 too many open files 问题
对 UDP 透明代理比较容易出现“卡住”的情况,这个时候会发现日志中出现了非常多 “too many open files” 的语句,这主要是受到最大文件描述符数值的限制,把这个数值往大调就好了。
打开配置文件:
sudo nano /etc/systemd/system/v2ray.service在 [Service] 下加入 LimitNPROC=500 和 LimitNOFILE=1000000,修改后的内容如下:
[Unit]
Description=V2Ray Service
Documentation=https://www.v2fly.org/
After=network.target nss-lookup.target
[Service]
User=nobody
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
NoNewPrivileges=true
ExecStart=/usr/local/bin/v2ray -config /usr/local/etc/v2ray/config.json
Restart=on-failure
RestartPreventExitStatus=23
LimitNPROC=500
LimitNOFILE=1000000
[Install]
WantedBy=multi-user.target5)配置网关
开启 IP 转发:
echo net.ipv4.ip_forward=1 >> /etc/sysctl.conf && sysctl -p配置路由器或局域网内设备:
将默认网关和DNS指向V2ray客户端所在的IP地址。
3、公司内网配置
公司内网的 配置与家庭内网的配置基本相同
1)配置V2ray服务器
配置V2ray:
sudo nano /usr/local/etc/v2ray/config.json内容如下:
{
"reverse": {
"bridges": [ // 反向代理实现内网穿透
{
"tag": "bridge-in",
"domain": "reverse.comp" // 与中转服务器 portals 的 domain 对应
}
]
},
"inbounds": [
{
"tag": "tproxy-in",
"port": 12345, // 透明代理监听端口
"protocol": "dokodemo-door",
"settings": {
"network": "tcp,udp",
"followRedirect": true
},
"sniffing": {
"enabled": true,
"destOverride": ["http","tls"]
},
"streamSettings": {
"sockopt": {
"tproxy": "tproxy", // 透明代理使用 TPROXY 方式
"mark":255 // 设置 mark 标记,防止回环
}
}
}
],
"outbounds": [
{
"tag": "proxy-out", // 代理出站,连接中转服务器
"protocol": "vmess",
"settings": {
"vnext": [{
"address": "输入代理节点地址或域名",
"port": 443,
"users":[{ "id": "输入UUID", "alterId": 0, "security": "auto"}]
}]
},
"streamSettings": {
"network": "ws",
"security": "tls",
"tlsSettings": {
"serverName": "输入代理节点域名",
"allowInsecure": false
},
"wsSettings": {
"path": "/v2ray"
},
"sockopt": {
"mark": 255
}
}
},
{
"tag": "direct-out", // 直接出站
"protocol": "freedom", // 使用 freedom 直接出站
"settings": {
"domainStrategy": "UseIP" // 域名解析策略,UseIP:优先使用IP,可以提高连接成功率
},
"streamSettings": {
"sockopt": {
"mark": 255
}
}
},
{
"tag": "block-out", // 拦截出站
"protocol": "blackhole", // 黑洞协议丢弃所有数据
"settings": {}
},
{
"tag": "dns-out", // DNS 转发
"protocol": "dns",
"streamSettings": {
"sockopt": {
"mark": 255
}
}
}
],
"dns": {
"servers": [
{
"address": "223.5.5.5", //中国大陆域名使用阿里的 DNS
"port": 53,
"domains": [
"geosite:cn",
"ntp.org", // NTP 服务器
"$输入代理节点域名" // 中转服务器
]
},
{
"address": "114.114.114.114", //中国大陆域名使用 114 的 DNS (备用)
"port": 53,
"domains": [
"geosite:cn",
"ntp.org", // NTP 服务器
"$输入代理节点域名" // 中转服务器
]
},
{
"address": "8.8.8.8", //非中国大陆域名使用 Google 的 DNS
"port": 53,
"domains": ["geosite:geolocation-!cn"]
},
{
"address": "1.1.1.1", //非中国大陆域名使用 Cloudflare 的 DNS (备用)
"port": 53,
"domains": ["geosite:geolocation-!cn"]
}
]
},
"routing": {
"domainStrategy": "IPOnDemand", //域名解析策略,IPOnDemand: 匹配时随时进行域名解析
"rules": [
{
"type": "field", // 识别反向桥接流量
"inboundTag": ["bridge-in"],
"domain": ["full:reverse.comp"],
"outboundTag": "proxy-out" // 转发到代理服务器
},
{
"type":"field", // 反向连接内部服务
"inboundTag": ["bridge-in"],
"outboundTag": "direct-out" // 最终连接本地服务
},
{
"type": "field", // 53 端口 UDP 流量,使用 V2Ray 的 DNS
"inboundTag": ["tproxy-in"],
"port": 53,
"network": "udp",
"outboundTag": "dns-out"
},
{
"type": "field", // 123 端口 UDP 流量直出 (NTP 协议)
"inboundTag": ["tproxy-in"],
"port": 123,
"network": "udp",
"outboundTag": "direct-out"
},
{
"type": "field", // 国内 DNS 服务器地址直连,以达到 DNS 分流目的
"ip": ["223.5.5.5", "114.114.114.114"],
"outboundTag": "direct-out"
},
{
"type": "field", // 国外 DNS 服务器地址走代理,以达到 DNS 分流目的
"ip": ["8.8.8.8", "1.1.1.1"],
"outboundTag": "proxy-out"
},
{
"type": "field", // 广告拦截
"domain": ["geosite:category-ads-all"],
"outboundTag": "block-out"
},
{
"type": "field", // 国外流量走代理
"domain": ["geosite:geolocation-!cn"],
"outboundTag": "proxy-out"
},
{
"type": "field", // 对家庭内网的访问走代理
"ip": ["192.168.31.0/24"],
"outboundTag": "proxy-out"
},
{
"type": "field", // 其他请求直接出站
"inboundTag": ["tproxy-in"],
"outboundTag": "direct-out"
}
]
}
}测试并重载V2Ray
# 检查配置语法
sudo /usr/local/bin/v2ray test -config /usr/local/etc/v2ray/config.json
# 重启服务
sudo systemctl restart v2ray
# 检查服务状态
sudo systemctl status v2ray
# 查看V2Ray日志
journalctl -u v2ray -f
sudo tail -f /var/log/v2ray/error.log2)配置透明代理规则
iptables 规则:
执行下面的命令开启透明代理。由于使用了 TPROXY 方式的透明代理,所以 TCP 流量也是使用 mangle 表。
# 清空旧规则
ip rule del fwmark 1 table 100 2>/dev/null
ip route flush table 100# 设置策略路由
ip rule add fwmark 1 table 100
ip route add local 0.0.0.0/0 dev lo table 100
# 代理局域网设备
iptables -t mangle -N V2RAY
iptables -t mangle -A V2RAY -d 127.0.0.1/32 -j RETURN
iptables -t mangle -A V2RAY -d 224.0.0.0/4 -j RETURN
iptables -t mangle -A V2RAY -d 255.255.255.255/32 -j RETURN
iptables -t mangle -A V2RAY -d 192.168.0.0/16 -p tcp -j RETURN # 直连局域网,避免 V2Ray 无法启动时无法连网关的 SSH,如果你配置的是其他网段(如 10.x.x.x 等),则修改成自己的
iptables -t mangle -A V2RAY -d 192.168.0.0/16 -p udp ! --dport 53 -j RETURN # 直连局域网,53 端口除外(因为要使用 V2Ray 的 DNS)
iptables -t mangle -A V2RAY -j RETURN -m mark --mark 0xff # 直连 SO_MARK 为 0xff 的流量(0xff 是 16 进制数,数值上等同与上面V2Ray 配置的 255),此规则目的是解决v2ray占用大量CPU(https://github.com/v2ray/v2ray-core/issues/2621)
iptables -t mangle -A V2RAY -p udp -j TPROXY --on-ip 127.0.0.1 --on-port 12345 --tproxy-mark 1 # 给 UDP 打标记 1,转发至 12345 端口
iptables -t mangle -A V2RAY -p tcp -j TPROXY --on-ip 127.0.0.1 --on-port 12345 --tproxy-mark 1 # 给 TCP 打标记 1,转发至 12345 端口
iptables -t mangle -A PREROUTING -j V2RAY # 应用规则
# 代理网关本机
iptables -t mangle -N V2RAY_MASK
iptables -t mangle -A V2RAY_MASK -d 224.0.0.0/4 -j RETURN
iptables -t mangle -A V2RAY_MASK -d 255.255.255.255/32 -j RETURN
iptables -t mangle -A V2RAY_MASK -d 192.168.0.0/16 -p tcp -j RETURN # 直连局域网
iptables -t mangle -A V2RAY_MASK -d 192.168.0.0/16 -p udp ! --dport 53 -j RETURN # 直连局域网,53 端口除外(因为要使用 V2Ray 的 DNS)
iptables -t mangle -A V2RAY_MASK -j RETURN -m mark --mark 0xff # 直连 SO_MARK 为 0xff 的流量(0xff 是 16 进制数,数值上等同与上面V2Ray 配置的 255),此规则目的是避免代理本机(网关)流量出现回环问题
iptables -t mangle -A V2RAY_MASK -p udp -j MARK --set-mark 1 # 给 UDP 打标记,重路由
iptables -t mangle -A V2RAY_MASK -p tcp -j MARK --set-mark 1 # 给 TCP 打标记,重路由
iptables -t mangle -A OUTPUT -j V2RAY_MASK # 应用规则
# 新建 DIVERT 规则,避免已有连接的包二次通过 TPROXY,理论上有一定的性能提升
iptables -t mangle -N DIVERT
iptables -t mangle -A DIVERT -j MARK --set-mark 1
iptables -t mangle -A DIVERT -j ACCEPT
iptables -t mangle -I PREROUTING -p tcp -m socket -j DIVERT执行了以上 ip 和 iptables 命令后,局域网同网段的设备以及网关本身就可以直接翻墙了。
3)开机自动运行透明代理规则
由于策略路由以及 iptables/nftables 有重启会失效的特性,所以当测试配置没有问题之后,需要配置服务在开机时自动配置策略路由和 iptables,否则每次开机的时候就要手动执行一遍。
由于 iptables 命令有点多,所以先将 iptables 规则保存到 /etc/iptables/rules.v4 中。
mkdir -p /etc/iptables && iptables-save > /etc/iptables/rules.v4创建服务文件:
sudo nano /etc/systemd/system/v2ray-tproxy.service内容如下:
[Unit]
Description=V2Ray TProxy Setup
After=network.target
Wants=network.target
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=-/sbin/ip rule add fwmark 1 table 100 ; /sbin/ip route add local 0.0.0.0/0 dev lo table 100 ; /sbin/iptables-restore /etc/iptables/rules.v4
ExecStop=-/sbin/ip rule del fwmark 1 table 100 ; /sbin/ip route del local 0.0.0.0/0 dev lo table 100 ; /sbin/iptables -t mangle -F
[Install]
WantedBy=multi-user.target启用和测试服务
# 开机自启
sudo systemctl enable v2ray-tproxy
# 重新加载配置
sudo systemctl daemon-reload
# 清空旧规则
ip rule del fwmark 1 table 100 2>/dev/null
ip route flush table 100
# 启动服务
sudo systemctl start v2ray-tproxy
# 重启服务
sudo systemctl restart v2ray-tproxy
# 检查服务状态
sudo systemctl status v2ray-tproxy
# 检查iptables规则
sudo iptables -t mangle -L -n -v
# 测试网络连接
curl -I http://www.google.com
curl -I http://www.baidu.com4)解决 too many open files 问题
对 UDP 透明代理比较容易出现“卡住”的情况,这个时候会发现日志中出现了非常多 “too many open files” 的语句,这主要是受到最大文件描述符数值的限制,把这个数值往大调就好了。
打开配置文件:
sudo nano /etc/systemd/system/v2ray.service在 [Service] 下加入 LimitNPROC=500 和 LimitNOFILE=1000000,修改后的内容如下:
[Unit]
Description=V2Ray Service
Documentation=https://www.v2fly.org/
After=network.target nss-lookup.target
[Service]
User=nobody
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
NoNewPrivileges=true
ExecStart=/usr/local/bin/v2ray -config /usr/local/etc/v2ray/config.json
Restart=on-failure
RestartPreventExitStatus=23
LimitNPROC=500
LimitNOFILE=1000000
[Install]
WantedBy=multi-user.target5)配置网关
开启 IP 转发:
echo net.ipv4.ip_forward=1 >> /etc/sysctl.conf && sysctl -p配置路由器或局域网内设备:
将默认网关和DNS指向V2ray客户端所在的IP地址。
4、外网客户端配置
1)小火箭配置
修改 default.conf 配置:
点击【配置】,选择【本地文件】中的【default.conf】,进入配置界面,
点击【规则】,添加规则:类型IP-CIDR,策略PROXY,不解析域名开启,IP CDIR 192.168.1.0/24(公司内网地址)
点击【规则】,添加规则:类型IP-CIDR,策略PROXY,不解析域名开启,IP CDIR 192.168.31.0/24(家庭内网地址)
修改网络设置(SMB和远程桌面需要):
点击【设置】,点击【隧道】,开启【包含所有网络】。
2)V2RayN配置
修改路由设置:
点击【设置】,选择【路由设置】,双击【绕过大陆】或【黑名单】,
点击【添加规则】,添加规则:别名公司内网,outboundTag proxy,IP或IP CIDR 192.168.1.0/24(公司内网地址)
点击【添加规则】,添加规则:别名家庭内网,outboundTag proxy,IP或IP CIDR 192.168.31.0/24(家庭内网地址)
启用Tun(远程桌面需要)