v2设置透明代理的要点

所谓透明代理就是局域网的流量直接通过网关,局域网内的客户端(电脑或者智能终端等)不需要再进行单独的代理设置。在网关上根据域名或者IP地址匹配,到底是走国内还是国外。这个过程对于客户端来说是完全透明的。

要点:
0. 在v2.json上存在配置
“inbounds”: [
{
“protocol”: “dokodemo-door”, #任意门协议,实现透明代理
“port”: 1234, #从1234端口进来的直接转发
“settings”: {
“network”: “tcp,udp”,
“followRedirect”: true
},
“sniffing”: {
“enabled”: false,
“destOverride”: [
“http”,
“tls”
]
}
}
]

1.在v2.json配置文件上,outbounds增加如下段落:
{
“sendThrough”: “0.0.0.0”,
“protocol”: “freedom”,
“settings”: { },
“tag”: “direct”, #针对tag为direct进行处理
“streamSettings”: {
“sockopt”: {“mark”: 255} #做标记,从防火墙上直连
}
}

2.在v2.json配置文件上,增加如下段落routing:
“routing”: {
“domainStrategy”: “IPIfNonMatch”, #当域名没有匹配任何规则时,将域名解析成IP(A 记录或AAAA 记录)再次进行匹配
“rules”: [
{
“type”: “field”,
“outboundTag”: “direct”,
“domain”: [“geosite:cn”] #如果是国内的域名匹配outbound中的direct标记
},
{
“type”: “field”,
“outboundTag”: “direct”,
“ip”:[“geoip:cn”] #如果是国内的IP匹配outbound中的direct标记
},
{
“type”: “field”,
“outboundTag”: “direct”,
“ip”:[“geoip:private”] #如果是私有IP匹配outbound中的direct标记
}
]
}

3.在iptables防火墙增加以下规则:
iptables -t nat -N V2RAY
iptables -t nat -A V2RAY -d x.x.x.x -j RETURN #x.x.x.x是v2服务器的IP,所有到服务器的流量直连
iptables -t nat -A V2RAY -d 0.0.0.0/8 -j RETURN #局域网、特殊IP直连
iptables -t nat -A V2RAY -d 10.0.0.0/8 -j RETURN #局域网、特殊IP直连
iptables -t nat -A V2RAY -d 127.0.0.0/8 -j RETURN #局域网、特殊IP直连
iptables -t nat -A V2RAY -d 169.254.0.0/16 -j RETURN #局域网、特殊IP直连
iptables -t nat -A V2RAY -d 172.16.0.0/12 -j RETURN #局域网、特殊IP直连
iptables -t nat -A V2RAY -d 192.168.0.0/16 -j RETURN #局域网、特殊IP直连
iptables -t nat -A V2RAY -d 224.0.0.0/4 -j RETURN #局域网、特殊IP直连
iptables -t nat -A V2RAY -d 240.0.0.0/4 -j RETURN #局域网、特殊IP直连
iptables -t nat -A V2RAY -p tcp -j RETURN -m mark –mark 0xff #0xff就是255,所有标记为255的流量直连,这样就跟刚才v2的outbounds标记对应上了
iptables -t nat -A V2RAY -p tcp -j REDIRECT –to-ports 1234 #其他的流量都转发到1234端口,这里的1234是inbounds段落中定义的转发端口
iptables -t nat -A PREROUTING -p tcp -j V2RAY