正文索引 [隐藏]
前言
虽然在电脑上有V2RayN,但无法实现全局代理,最好的办法是在网关上进行透明代理。我之前使用的是软路由,后来我觉得软路由某些功能不好用就直接刷了个debian系统玩,所以我们要在debian上手动部署v2ray和配置iptables规则
之前在debian上想弄ss-redir透明代理的,但是需要自己弄路由规则来分流,没弄成功。但还好v2ray是有内置的路由规则来分流的,我们只需要修改好v2ray的配置文件和配置好iptables规则
先在debian上安装v2ray
#下载压缩包 wget https://github.com/v2ray/v2ray-core/releases/download/v4.19.1/v2ray-linux-64.zip #解压 unzip v2ray-linux-64.zip #进入文件夹 cd v2ray-linux-64.zip #创建v2ray程序文件夹 mkdir /usr/bin/v2ray #赋予v2ray执行权限 chmod +x ./v2ctl ./v2ray #复制下面的文件到v2ray程序文件夹 cp ./geoip.dat ./geosite.dat ./v2ctl ./v2ray /usr/bin/v2ray #复制v2ray.service文件到/etc/systemd/system/ cp ./systemd/v2ray.service /etc/systemd/system/ #设置v2ray开机自启 systemctl enable v2ray.service
准备配置文件

参考透明代理/路由器翻墙进行修改
#导出的文件 { "log": { "access": "", "error": "", "loglevel": "warning" }, "inbounds": [ { "port": 10808, "listen": "127.0.0.1", "protocol": "socks", "sniffing": { "enabled": true, "destOverride": [ "http", "tls" ] }, "settings": { "auth": "noauth", "udp": true, "ip": null, "clients": null }, "streamSettings": null } ], "outbounds": [ { "tag": "proxy", "protocol": "vmess", "settings": { "vnext": [ { "address": "yourdomain.com", "port": port, "users": [ { "id": "用户ID", "alterId": 额外ID, "email": "[email protected]", "security": "auto" } ] } ], "servers": null, "response": null }, "streamSettings": { "network": "ws", "security": "tls", "tlsSettings": { "allowInsecure": true, "serverName": null }, "tcpSettings": null, "kcpSettings": null, "wsSettings": { "connectionReuse": true, "path": "/路径", "headers": null }, "httpSettings": null, "quicSettings": null }, "mux": { "enabled": true } }, { "tag": "direct", "protocol": "freedom", "settings": { "vnext": null, "servers": null, "response": null }, "streamSettings": null, "mux": null }, { "tag": "block", "protocol": "blackhole", "settings": { "vnext": null, "servers": null, "response": { "type": "http" } }, "streamSettings": null, "mux": null } ], "dns": null, "routing": { "domainStrategy": "IPIfNonMatch", "rules": [ { "type": "field", "port": null, "outboundTag": "direct", "ip": [ "geoip:private" ], "domain": null }, { "type": "field", "port": null, "outboundTag": "direct", "ip": [ "geoip:cn" ], "domain": null }, { "type": "field", "port": null, "outboundTag": "direct", "ip": null, "domain": [ "geosite:cn" ] } ] } } #修改后的文件 { "log": { "access": "", "error": "", "loglevel": "warning" }, "inbounds": [ { "port": 12345, //开放的端口号 "protocol": "dokodemo-door", "settings": { "network": "tcp,udp", "followRedirect": true // 这里要为 true 才能接受来自 iptables 的流量 }, "sniffing": { "enabled": true, "destOverride": ["http", "tls"] }, "streamSettings": { "sockopt": { "tproxy": "redirect" } } ], "outbounds": [ { "tag": "proxy", "protocol": "vmess", "settings": { "vnext": [ { "address": "yourdomain.com", "port": port, "users": [ { "id": "用户ID", "alterId": 额外ID, "email": "[email protected]", "security": "auto" } ] } ], "servers": null, "response": null }, "streamSettings": { "network": "ws", "security": "tls", "tlsSettings": { "allowInsecure": true, "serverName": null }, "tcpSettings": null, "kcpSettings": null, "wsSettings": { "connectionReuse": true, "path": "/路径", "headers": null }, "httpSettings": null, "quicSettings": null, "sockopt": { "mark": 255 //这里是 SO_MARK,用于 iptables 识别,每个 outbound 都要配置;255可以改成其他数值,但要与下面的 iptables 规则对应;如果有多个 outbound,最好奖所有 outbound 的 SO_MARK 都设置成一样的数值 } }, "mux": { "enabled": true } }, { "tag": "direct", "protocol": "freedom", "settings": { "vnext": null, "servers": null, "response": null }, "streamSettings": { "sockopt": { "mark": 255 //这里是 SO_MARK,用于 iptables 识别,每个 outbound 都要配置;255可以改成其他数值,但要与下面的 iptables 规则对应;如果有多个 outbound,最好奖所有 outbound 的 SO_MARK 都设置成一样的数值 } }, "mux": null }, { "tag": "block", "protocol": "blackhole", "settings": { "vnext": null, "servers": null, "response": { "type": "http" } }, "streamSettings": { "sockopt": { "mark": 255 //这里是 SO_MARK,用于 iptables 识别,每个 outbound 都要配置;255可以改成其他数值,但要与下面的 iptables 规则对应;如果有多个 outbound,最好奖所有 outbound 的 SO_MARK 都设置成一样的数值 } }, "mux": null } ], "dns": null, "routing": { "domainStrategy": "IPIfNonMatch", "rules": [ { "type": "field", "port": null, "outboundTag": "direct", "ip": [ "geoip:private" ], "domain": null }, { "type": "field", "port": null, "outboundTag": "direct", "ip": [ "geoip:cn" ], "domain": null }, { "type": "field", "port": null, "outboundTag": "direct", "ip": null, "domain": [ "geosite:cn" ] } ] } }
#主要就是把inbounds里面的内容替换成下面的代码 { "port": 12345, //开放的端口号 "protocol": "dokodemo-door", "settings": { "network": "tcp,udp", "followRedirect": true // 这里要为 true 才能接受来自 iptables 的流量 }, "sniffing": { "enabled": true, "destOverride": ["http", "tls"] }, "streamSettings": { "sockopt": { "tproxy": "redirect" } } #streamSettings里面要添加下面的代码 "sockopt": { "mark": 255 //这里是 SO_MARK,用于 iptables 识别,每个 outbound 都要配置;255可以改成其他数值,但要与下面的 iptables 规则对应;如果有多个 outbound,最好奖所有 outbound 的 SO_MARK 都设置成一样的数值 }
#创建v2ray配置文件夹 mkdir /etc/v2ray cd /etc/v2ray #把配置文件上传 rz #测试下配置文件有没有问题 /usr/bin/v2ray/v2ray -test -config=/etc/v2ray/config.json #没问题就运行v2ray systemctl start v2ray.servive
配置iptables规则
#创建rule文件 vi /etc/v2ray/rule #添加下面命令 iptables -t nat -N V2RAY # 新建一个名为 V2RAY 的链 iptables -t nat -A V2RAY -d 服务器ip -j RETURN iptables -t nat -A V2RAY -d 0.0.0.0/8 -j RETURN iptables -t nat -A V2RAY -d 10.0.0.0/8 -j RETURN iptables -t nat -A V2RAY -d 127.0.0.0/8 -j RETURN iptables -t nat -A V2RAY -d 169.254.0.0/16 -j RETURN iptables -t nat -A V2RAY -d 172.16.0.0/12 -j RETURN iptables -t nat -A V2RAY -d 192.168.0.0/16 -j RETURN iptables -t nat -A V2RAY -d 224.0.0.0/4 -j RETURN iptables -t nat -A V2RAY -d 240.0.0.0/4 -j RETURN iptables -t nat -A V2RAY -p tcp -j RETURN -m mark --mark 0xff # 直连 SO_MARK 为 0xff 的流量(0xff 是 16 进制数,数值上等同与上面配置的 255),此规则目的是避免代理本机(网关)流量出现回环问题 iptables -t nat -A V2RAY -p tcp -j REDIRECT --to-ports 12345 # 其余流量转发到 12345 端口(即 V2Ray) iptables -t nat -A PREROUTING -p tcp -j V2RAY # 对局域网其他设备进行透明代理 iptables -t nat -A OUTPUT -p tcp -j V2RAY # 对本机进行透明代理 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_MASK iptables -t mangle -A V2RAY_MASK -d 服务器ip -j RETURN iptables -t mangle -A V2RAY_MASK -d 0.0.0.0/8 -j RETURN iptables -t mangle -A V2RAY_MASK -d 10.0.0.0/8 -j RETURN iptables -t mangle -A V2RAY_MASK -d 127.0.0.0/8 -j RETURN iptables -t mangle -A V2RAY_MASK -d 169.254.0.0/16 -j RETURN iptables -t mangle -A V2RAY_MASK -d 172.16.0.0/12 -j RETURN iptables -t mangle -A V2RAY_MASK -d 192.168.0.0/16 -j RETURN iptables -t mangle -A V2RAY_MASK -d 224.0.0.0/4 -j RETURN iptables -t mangle -A V2RAY_MASK -d 240.0.0.0/4 -j RETURN iptables -t mangle -A V2RAY_MASK -p udp -j TPROXY --on-port 12345 --tproxy-mark 1 iptables -t mangle -A PREROUTING -p udp -j V2RAY_MASK #添加执行权限 chmod +x /etc/v2ray/rule #开机自动添加规则 vi /etc/rc.local #添加命令 /etc/v2ray/rule
参考文章
评论
还没有任何评论,你来说两句吧!