Configuration of transparent V2Ray proxy server using Raspberry Pi 3b 树莓派做V2Ray透明代理
[i=s] 本帖最後由 角色 於 2019-1-26 01:53 編輯 [/i]下面的内容主要来自下面的网站:
[1] Official V2Ray website: [url]https://www.v2ray.com/[/url]
[2] 白话文教程: [url]https://toutyrater.github.io/[/url]
[3] kingwilliam帖子:[url]http://www.telecom-cafe.com/forum/redirect.php?goto=findpost&ptid=7212&pid=45887[/url]
请看下图:
[attach]4253[/attach]
在自由世界的V2Ray server的设计,估计很多member都没有问题,大家可以参考本论坛的帖子,在这里就不用多说了。
主要V2Ray client和树莓派(Rapsberry Pi 3b)设定难度比较大。
先说一下网络结构。
Router 1:
在大陆的一般router就可以,没有什么要求。Router 1 管理设备IP:192.168.55.1,network segment:192.168.55.0/24, DNS: 114.114.114.114(填8.8.8.8都会被污染,所以一般解开域名就可以)。
Router 2:
就是一般大陆很便宜的FAST WiFi Router。WAN IP:192.168.55.18 (固定IP),Gateway=192.168.55.20, DNS=192.168.55.20
Raspberry Pi 3b:(RP)
用static IP,device IP=192.168.55.20.
RP安装V2Ray,default设定是server mode,我们要搞成client,目的是连接自由世界的V2Ray server。
Raspbian version: Raspbian GNU/Linux 9 (stretch) (kernel version: 4.14.34-v7+
V2Ray version: 4.13 [i=s] 本帖最後由 角色 於 2019-1-26 00:30 編輯 [/i]
在Raspberry Pi安装V2Ray是没有什么难度,大家可以安装下面每一步去做就可以把Raspberry Pi的V2Ray设成transparent proxy server。
Step 1:
1)安装好Raspbian OS (Debian)
2)更改LAN Static IP address
3)apt-get update, apt-get upgrade 更新最新的软件
Step 2:
1)安装V2Ray
2)更改 V2Ray configuration file: /etc/v2ray/config.json
/etc/v2ray/config.json[code]
{
"inbounds": [
{
"protocol": "socks",
"port": 1081, // SOCKS 代理端口,在浏览器中需配置代理并指向这个端口
"listen": "192.168.55.20",
"settings": {
"udp": true
}
},
{
"protocol": "dokodemo-door",
"port": 12345,
"domainOverride": ["tls","http"],
"settings": {
"network": "tcp,udp",
"followRedirect": true
}
},
{
"protocol": "dokodemo-door",
"port": 53,
"listen": "192.168.55.20",
"settings": {
"address": "8.8.8.8",
"port": 53,
"network": "udp",
"timeout": 0,
"followredirect": false
}
}
],
"outbounds": [{
"protocol": "vmess",
"settings": {
"vnext": [{
"address": "host name or IP address of remote V2Ray server", // 服务器地址,请修改为你自己的服务器 ip 或域名
"port": 10086, // 服务器端口
"users": [{ "id": "UUID" }]
}]
},
"streamSettings": {
"network": "kcp",
"kcpSettings": {
"header": {
"type": "wechat-video"
}
},
"sockopt":{
"mark": 1
}
}
},
{
"protocol": "freedom",
"tag": "direct",
"settings": {}
}],
"routing": {
"domainStrategy": "IPOnDemand",
"rules": [{
"type": "field",
"domain": ["geosite:cn"],
"ip": ["geoip:private"],
"ip": ["geoip:cn"],
"outboundTag": "direct"
}]
}
}
[/code]. Step 3:
Create a bash-shell script for setting the firewall of Raspberry Pi
v2ray-iptable.sh[code]
#!/bin/bash
# TCP
# Create new chain
iptables -t nat -N V2RAY
# Ignore your V2Ray server's addresses
# It's very IMPORTANT, just be careful.
iptables -t nat -A V2RAY -d 1.2.3.4 -j RETURN #你的server ip address,如果是dynamic IP,可以采用kingwilliam的帖子[3]
# Ignore LANs and any other addresses you'd like to bypass the proxy
# See Wikipedia and RFC5735 for full list of reserved networks.
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
# Anything else should be redirected to Dokodemo-door's local port
iptables -t nat -A V2RAY -p tcp -j REDIRECT --to-ports 12345
# Apply the rules
iptables -t nat -A PREROUTING -p tcp -j V2RAY
#iptables -t nat -A OUTPUT -p tcp -j V2RAY
#UDP
# Create new chain
ip route add local 0.0.0.0/0 dev lo table 100
ip rule add fwmark 1 table 100
iptables -t mangle -N V2RAY_MARK
# Ignore your V2Ray server's addresses
# It's very IMPORTANT, just be careful.
iptables -t mangle -A V2RAY_MARK -d 1.2.3.4 -j RETURN #你的server ip address,如果是dynamic IP,可以采用kingwilliam的帖子[3]
# Ignore LANs and any other addresses you'd like to bypass the proxy
# See Wikipedia and RFC5735 for full list of reserved networks.
iptables -t mangle -A V2RAY_MARK -d 0.0.0.0/8 -j RETURN
iptables -t mangle -A V2RAY_MARK -d 10.0.0.0/8 -j RETURN
iptables -t mangle -A V2RAY_MARK -d 127.0.0.0/8 -j RETURN
iptables -t mangle -A V2RAY_MARK -d 169.254.0.0/16 -j RETURN
iptables -t mangle -A V2RAY_MARK -d 172.16.0.0/12 -j RETURN
iptables -t mangle -A V2RAY_MARK -d 192.168.0.0/16 -j RETURN
iptables -t mangle -A V2RAY_MARK -d 224.0.0.0/4 -j RETURN
iptables -t mangle -A V2RAY_MARK -d 240.0.0.0/4 -j RETURN
iptables -t mangle -A V2RAY_MARK -p udp --dport 53 -j RETURN
# Anything else should be redirected to Dokodemo-door's local port
iptables -t mangle -A V2RAY_MARK -p udp -j TPROXY --on-port 12345 --tproxy-mark 1
# Add any UDP rules
iptables -t mangle -A PREROUTING -p udp -j V2RAY_MARK
#iptables -t mangle -A OUTPUT -j V2RAY_MARK
[/code]. [i=s] 本帖最後由 角色 於 2019-1-26 00:49 編輯 [/i]
Step 4:
让Raspbian能做packet forwarding这个功能。
edit /etc/sysctl.conf, uncomment里面一个句子,要变成[code]
# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.ip_forward=1
[/code].
Step 5:
1)把新的/etc/v2ray/config.json configuration启动[code]
service v2ray restart
[/code].
2)看看是否有error message[code]
/usr/bin/v2ray/v2ray
[/code].
它会告诉你那里出问题,然后你再去看看你的config.json file
Step 6:
1) Reboot Raspbian
2) Load firewall script[code]
bash v2ray.iptables.sh
[/code]. [i=s] 本帖最後由 角色 於 2019-1-27 01:38 編輯 [/i]
Step 7: (测试)
1、 用iPad接Router2的WiFi SSID,而Ipad不坐任何setting。结果能正常上YouTube,Facebook和香港网站。上大陆网如CCTVHD看CCTV5、爱奇艺都没有说系统不在大陆,全部都能正常看到。
2、用Android手机接Router2的WiFi SSID,不做任何VPN或proxy的settings,跟上面iPad一样,上什么网都没有问题。
3、用MacBook的LAN直接接入Router2的LAN port,效果跟上面iPad一样。
4、用notebook Win10,测试过,跟iPad一样,大陆CCTV5、爱奇艺、YouTube、Facebook都能同时上,效果非常好!
5、在Raspberry Pi 3b的Desktop,用Chrome都可以像iPad科学上学,但是需要用非root用户运行下面指令:[code]
chromium-browser --proxy-server="socks://192.168.55.20:1081"
[/code]。
上面测试是在深夜进行,所以效果还可以,但是白天测试有点慢,手机的Google Play Store and YouTube不能很快就display完。如果那些devices接其他WiFi SSID,如果用device的socks连接方式就非常好!估计系统还需要调?怎样调?这需要调研一下才能有下一步。
经过调研后,发现DNS没有通过proxy去解开域名,估计部分hostnames的IP收到污染,导致上网不快而有点慢。 [i=s] 本帖最後由 角色 於 2019-1-26 11:20 編輯 [/i]
致谢:
首先非常感谢kingwilliam的帖子,如果没有它的启动帖子,但是看官网和白话文根本不知道什么时候才能实现透明代理的第一步。还有V2Ray usergorup里的members。
后面的工作:
1、计划把上面的scripts是否可以理顺和简化,目的是把目前连接速度提升。
2、最后会用Raspberry Pi + Mikrotik实现透明和直接走,看用家选用哪个SSID和LAN port。
3、考虑有些用家没有Mikrotik router (RouterOS),而斋用Raspberry Pi,是否能充分利用Raspberry Pi 3b (2.4G Wifi) 或者 Raspberry Pi 3b+ (2.4G WiFi and 5G的ac WiFi)。 [i=s] 本帖最後由 角色 於 2019-1-26 10:33 編輯 [/i]
我在实验中遇到的问题和疑惑都会写出来让大家members分享。
问题列表:
在最初失败的原因总结一下 [url=http://www.telecom-cafe.com/forum/redirect.php?goto=findpost&ptid=7300&pid=45948]Link[/url]
如果没有下面的packet forward修改和重启,整个系统都不能工做。[url=http://www.telecom-cafe.com/forum/redirect.php?goto=findpost&ptid=7300&pid=45946]Link[/url]
sockopt and mark需要吗? [url=http://www.telecom-cafe.com/forum/redirect.php?goto=findpost&ptid=7300&pid=45946]Link[/url] [i=s] 本帖最後由 角色 於 2019-1-26 02:11 編輯 [/i]
如果没有下面的packet forward修改和重启,整个系统都不能工作:[code]
# Uncomment the next line to enable packet forwarding for IPv4
# net.ipv4.ip_forward=1
[/code]只有在Raspbian Desktop上面运行Chrome proxy上网说没有问题,因为内部接sock proxy server,不需要packet forwarding。[code]
chromium-browser --proxy-server="socks://192.168.55.20:1081"
[/code]. sockopt and mark需要吗?
如果我们把mark value 改为 255,或者把下面整句删走,service v2ray restart后都能正常科学上网。[code]
"sockopt":{
"mark": 1
}
[/code]. [i=s] 本帖最後由 角色 於 2019-1-26 10:48 編輯 [/i]
在最初失败的原因总结一下:
1、还没有开始做V2Ray transparent proxy server (透明代理)前,根据我之前的知识,只围绕官网的文献【1】和白话文【2】,但是他们具体network diagram是怎样的,没有说,我估计他们之前有一定共识的network diagram。还有由于V2Ray不断改变configuration结构,而白话文有些不一样,还有坊间的example都是用V2Ray的老版本,当然这与本人知识水平也有关系,例如不会iptables指令。
2、在网上都搜过N次,把所有有关V2Ray做透明代理的网页也看了不少,他们也用不同的方法去实现透明代理(以后再整理这部分),但是当中加入了其他的软件如dnsmasq等等作优化。
3、最近看到网友kingwilliam的帖子,还有他的网络结构,再看看他的settings(虽然不是完整,但是已经是主要部分),根据他的文字描述我也把他的network configuration画了出来。那么我有三个主要文献【1、2、3】。
4、因为kingwilliam CHing是用Ubuntu,而我用的Debian-based Raspbian, 我一度怀疑Raspbian (Debian)不支持TPROXY这个iptables的target!但是从【4】的Linux信息,从kernel 2.2打后就有,但是为什么在man iptables里面找不到这个TRPOXY target呢?我用的Raspbian,Linux kernel已经是4.13了,早就应该支持TRPOXY target!哪个时候不知道怎样利用iptables去查阅PROXY target时候加进去。原来那些信息在iptables man page里的iptables-extensions里。大家可以用下面指令就可以看到:[code]
man iptables-extensions
[/code]. 里面有不少的信息在很多标准的iptables手册都没有。
部分man iptables信息:
TARGETS
A firewall rule specifies criteria for a packet and a target. If the packet does not match, the next rule in the chain is examined; if it does match, then the next rule is specified by the value of the target, which can be the name of a user-defined chain, one of the targets described in iptables-extensions(8), or one of the special values ACCEPT, DROP or RETURN.
man iptables-extensions关于TPROXY的信息:
.
.
.
TARGET EXTENSIONS
iptables can use extended target modules: the following are included in
the standard distribution.
.
.
.
TPROXY
This target is only valid in the mangle table, in the PREROUTING chain and user-defined chains which are only called from this chain. It redirects the packet to a local socket without changing the packet header in any way. It can also change the mark value which can then be used in advanced routing rules. It takes three options:
--on-port port
This specifies a destination port to use. It is a required
option, 0 means the new destination port is the same as the
original. This is only valid if the rule also specifies -p tcp
or -p udp.
--on-ip address
This specifies a destination address to use. By default the
address is the IP address of the incoming interface. This is
only valid if the rule also specifies -p tcp or -p udp.
--tproxy-mark value[/mask]
Marks packets with the given value/mask. The fwmark value set
here can be used by advanced routing. (Required for transparent
proxying to work: otherwise these packets will get forwarded,
which is probably not what you want.)
参考文献:
【1】Official V2Ray website: [url]https://www.v2ray.com/[/url]
【2】白话文教程: [url]https://toutyrater.github.io/[/url]
【3】kingwilliam帖子:[url]http://www.telecom-cafe.com/forum/viewthread.php?tid=7212&rpid=45887&fav=yes&ordertype=0&page=2#pid45887[/url]
【4】[url]https://www.kernel.org/doc/Documentation/networking/tproxy.txt[/url] [i=s] 本帖最後由 角色 於 2019-1-26 17:27 編輯 [/i]
什么时候用Raspberry Pi的DNS?什么时候via proxy?
如果remote free world V2Ray is hostname, 那么V2Ray就会用local Raspberry Pi DNS接解开,这个解开非常重要,不然后面不用说了!!!不能科学上网。
如果有了正确的IP,然后local V2Ray client能接上remote的V2Ray server,那么后面就会按照resolve出来的IP,然后决定怎样走。
根据我发现,不管用sock proxy server,或者透明代理,发问local的DNS会少很多。
(具体是怎样走,要进一步查证)
参考文献:
【1】[url]https://github.com/v2ray/v2ray-core/issues/727[/url]
【2】[url]https://github.com/v2ray/discussion/issues/67[/url] [i=s] 本帖最後由 kingwilliam 於 2019-1-29 06:17 編輯 [/i]
[b]回復 [url=http://www.telecom-cafe.com/forum/redirect.php?goto=findpost&pid=45953&ptid=7300]11#[/url] [i]角色[/i] [/b]
我最初都碰到這個問題,簡單解釋是 一個系統有兩個dns service 互搶port udp 53.
因我是用Ubuntu 18.04 server ,以下都是以這平台做藍本。
那2個互搶 udp53? 是v2ray 透明代理用到的dokodemo 53 和 系統systemd-resolved service.
如何得知那個服務在用udp53? 首先service v2ray stop, 停用v2ray, 再可用 netstat -plntu 找找udp 53 就得知
[attach]4257[/attach]
如何解決? 方法有2 。(一) 就是停用系統 systemd-resolved service. (二) 就是停用 v2ray dokodemo53 如上圖 就在router2 Wan dns 由192.168.55.20 改8.8.8.8.
如何停用systemd-resolved service? 可參考 [url=https://www.admintome.com/blog/turning-off-systemd-resolvd/]Turn off systemd-resolved[/url]
==========
如不看網頁, 做下面4個動作就可以.
# systemctl disable systemd-resolved
# systemctl stop systemd-resolved
# rm /etc/resolv.conf
最後就是建立新的 /etc/resolv.conf
nameservice 192.168.55.1
==========
如選停用系統 systemd-resolved service. 再建入 netstat -plntu, 就會發現沒了udp53
[attach]4258[/attach]
最後就 service v2ray start, 再一次建入 netstat -plntu, 就會看見v2ray 在用 udp 53, 這就是 v2ray dokodemo
[attach]4261[/attach] [b]回復 [url=http://www.telecom-cafe.com/forum/redirect.php?goto=findpost&pid=45959&ptid=7300]12#[/url] [i]kingwilliam[/i] [/b]
谢谢CHing的信息,那么你最后采用哪一个方案呢?
经过Ching的信息,我慢慢开始明白为什么我们在建立V2Ray transparent proxy经常出问题。 我最後用了 "停systemd-resolved service"
2個方法我都試過 個人感覺用dokodemo udp53 反應比較快 [i=s] 本帖最後由 角色 於 2019-1-29 13:49 編輯 [/i]
刚才简单试过,好像traffic先走local,然后再走remote v2Ray server。
如果我在/etc/v2ray/config.json, 加上DNS后连外面网站就非常快!!!
但是这样的改动,看local大陆的网站,因为先用proxy的DNS查,回来的都在大陆以外的IP,但是local大陆的网站知道你在大陆以前地区,所有有些因为不在大陆的IP,所以不让开。但是这个可能与V2Ray的DNS和V2Ray的Routing有关系。同一个大陆域名,在大陆和墙外的DNS解出来的IP都不一样,问题就是,系统怎样知道hostname要往哪个DNS server看呢?
(估计需要再看看V2Ray DNS和Routing)。[code]
{
"dns": {
"servers": [
"8.8.8.8",
"1.0.0.1",
"localhost"
]
},
"inbounds": [
{
"protocol": "socks",
"port": 1081, // SOCKS 代理端口,在浏览器中需配置代理并指向这个端口
"listen": "192.168.1.22",
"settings": {
"udp": true
}
},
.
.
.
[/code]. 有了DNS,系统会先看DNS的排序,因为先看8.8.8.8,而这个8.8.8.8先往V2Ray的vmess走,所以回来的IP是没有污染的,那么到网页(Chrome)就不会出现问题,再通过V2Ray去有关网站。如果污染过的IP,去要去的网站非常慢的。
用上面的DNS,一输入网址,如[url]www.facebook.com[/url],很快整个页面就弹出来,非常快!!!
看YouTube能有1440k(2k)效果!!! [b]回復 [url=http://www.telecom-cafe.com/forum/redirect.php?goto=findpost&pid=45959&ptid=7300]12#[/url] [i]kingwilliam[/i] [/b]
Ching, 我发现我的Raspberry Pi里Raspbian,没有port 53,只有V2Ray自己一个用。估计你的用了port 53,估计Ubuntu里设了DNS server,而我的Raspbian就没有启动DNS server,那么V2Ray在Raspbian就变得简单直接。
頁:
[1]