openwrt软路由使用v2ray做透明代理简单教程

文章正文
发布时间:2024-06-25 21:35

openwrt软路由使用v2ray做透明代理简单教程

01 May 2021

1. 自己的云服务器的设置

根据服务器上运行的系统版本下载v2raycore,欧博abg我用的是64位的centos7,直接下载的编译好的zip文件,由于系统不自带zip解压软件,用yum先下载了zip,下面上指令:

yum install -y zip unzip curl -LO https://github.com/v2ray/v2ray-core/releases/download/v4.28.2/v2ray-linux-64.zip unzip v2ray-linux-64.zip -d v2ray cd v2ray setsid ./v2ray --config=myconfig/config.json

curl -O 是将获取内容输出为文件,欧博官网且文件名为源文件名称不变

curl -L 是获取重定向后的下载地址,不加此指令无法下载,详情请自行查询manpage

我这里为了简单使用了setsid作为后台运行的方法,实测比较稳定,我的理解是setsid是把程序进程挂载到进程id为1的超级进程上运行,欧博用ps -ef查询可看到v2ray进程的ppid为1,直接启动的话是挂载到当前终端的进程下,终端退出运行程序的进程也会一同销毁,而这个超级进程是系统启动的第一个进程,很多驱动和系统服务也是挂载到这个进程上实现的后台运行

配置文件使用的是myconfig/config.json这个文件,欧博娱乐这是一个v2ray文件夹下的相对路径,启动前请把配置自行写入config.json,官网都有不赘述

2. 路由器设定

为了方便,我使用的是预装好openwrt的路由器

路由器

首先使用lscpu或者查看/proc/cpuinfo文件来确定cpu型号和使用的指令集

我的cpu使用的是mips指令集,查了一下还有big和little两种版本(mips是big-endian的mips架构,mipsle是little-endian的mips架构,他们之间的区别就是内存中存放的数据的字节顺序相反,也就是把低位放在低地址还是高地址),对应预编译的版本中的mips和mipsle,经测试v2ray发布版本中预编译的mipsle这个版本是可以使用的

因为使用环境变量开启代理的方法一旦退出终端就会失效,所以实现透明代理最好的方法是通过iptables实现流量转发到v2ray客户端端口,我的路由器默认是开启ipv4转发的,如果不是请自行修改配置文件,具体步骤见扩展阅读

加入如下规则到防火墙的启动配置文件中,openwrt直接写到custom rules里面就可以

防火墙配置

防火墙配置

# v2ray-tcp iptables -t nat -N V2RAY # Create a new chain called V2RAY iptables -t nat -A V2RAY -d 192.168.0.0/16 -j RETURN # Direct connection 192.168.0.0/16 iptables -t nat -A V2RAY -p tcp -j RETURN -m mark --mark 0xff # Directly connect SO_MARK to 0xff traffic (0xff is a hexadecimal number, numerically equivalent to 255), the purpose of this rule is to avoid proxy loopback with local (gateway) traffic iptables -t nat -A V2RAY -p tcp -j REDIRECT --to-ports 12345 # The rest of the traffic is forwarded to port 12345 (ie V2Ray) iptables -t nat -A PREROUTING -p tcp -j V2RAY # Transparent proxy for other LAN devices iptables -t nat -A OUTPUT -p tcp -j V2RAY # Transparent proxy for this machine # v2ray-udp 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 192.168.0.0/16 -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

还得在v2ray配置文件里加入如下内容,省略部分为常规配置项目,我的配置是用v2rayN这款软件导出的这样比较方便

{"routing":{...},"inbounds":[{...},{"port":12345,//Theopenport"protocol":"dokodemo-door","settings":{"network":"tcp,udp","followRedirect":true//Needtobesetastruetoaccepttrafficfromiptables},"sniffing":{"enabled":true,"destOverride":["http","tls"]}}],"outbounds":[{..."streamSettings":{..."sockopt":{"mark":255//HereisSO_MARK,usedforiptablestorecognise.Eachoutboundneedstoconfigure;youcanuseothervalueotherthan255butitneedstobeconsistantasiniptablesrules;iftherearemultipleoutbounds,itisrecommendedthatyousetallSO_MARKvaluethesameforalloutbounds.}}}...]}

由于路由器没有setsid所以我用了如下指令实现后台运行,为了方便可以写入系统启动配置文件/etc/rc.local

./v2ray --config=config.json &

扩展阅读:

Linux 技巧:让进程在后台可靠运行的几种方法:https://www.linuxprobe.com/process-run-in-background.html

LINUX PID 1 和 SYSTEMD:https://blog.csdn.net/weixin_36841920/article/details/79437754?utm_term=linux%E4%B8%ADPID%E4%B8%BA1%E7%9A%84%E8%BF%9B%E7%A8%8B&utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~sobaiduweb~default-2-79437754&spm=3001.4430

V2Ray Beginner’s Guide - Transparent Proxy:

Linux 让终端走代理的几种方法:https://zhuanlan.zhihu.com/p/46973701

如有侵权联系删除

首页
评论
分享
Top