返回列表 發帖

Configuration of transparent V2Ray proxy server using Raspberry Pi 3b 树莓派做V2Ray透明代理 (第二种组合)

本帖最後由 角色 於 2019-1-29 11:20 編輯

第一种连接方式【1】,需要两个路由器,而这个只需要一个路由器和一个树莓派就可以。而下面的iptables script是copy白话文【2】。在第一种连接【1】,主路由不需要修改什么,而下面的连接方式,主路由的LAN的DHCP需要修改。

Reference:
【1】http://www.telecom-cafe.com/forum/viewthread.php?tid=7300
【2】https://toutyrater.github.io/app/transparent_proxy.html


上面的network configuration,WAN取互联网IP(不管是公网IP或者私网IP),能上网就可以。

我的Raspberry Pi设static IP,192.168.1.22,gateway=Router IP = 192.168.1.1,而里面的DHCP server,派发IP从192.168.1.100-200,DNS=192.168.1.20.

Raspberry Pi
Static IP: 192.168.1.22
Gateway: 192.168.1.1
Nameserver: 8.8.8.8

Router
WAN :自动取IP(static or dynamic IP),能上网就可以。
Router IP: 192.168.1.1
LAN Side: DHCP server , gateway=192.168.1.22, DNS=192.168.1.22, 派发IP=192.168.1.100 - 192.168.1.200 (避开192.168.1.22)就可以。
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

V2Ray /etc/v2ray/config.json:
  1. {
  2.   "inbounds": [
  3.     {
  4.       "protocol": "socks",
  5.       "port": 1081,  // SOCKS 代理端口,在浏览器中需配置代理并指向这个端口
  6.       "listen": "192.168.1.22",
  7.       "settings": {
  8.         "udp": true
  9.       }
  10.     },
  11.     {
  12.       "protocol": "dokodemo-door",
  13.       "port": 12345,
  14.       "domainOverride": ["tls","http"],
  15.       "settings": {
  16.         "network": "tcp,udp",
  17.         "followRedirect": true
  18.       },
  19.       "sniffing": {
  20.         "enabled": true,
  21.         "destOverride": ["http","tls"]
  22.       }
  23.     },
  24.     {
  25.       "protocol": "dokodemo-door",
  26.       "port": 53,
  27.       "listen": "192.168.1.22",
  28.       "settings": {
  29.         "address": "8.8.8.8",
  30.         "port": 53,
  31.         "network": "udp",
  32.         "timeout": 0,
  33.         "followredirect": false
  34.       }
  35.     }
  36.   ],
  37.   "outbounds": [{
  38.     "protocol": "vmess",
  39.     "settings": {
  40.       "vnext": [{
  41.         "address": "服务器 ip 或域名", // 服务器地址,请修改为你自己的服务器 ip 或域名
  42.         "port": 10086,  // 服务器端口
  43.         "users": [{ "id": "UUID" }]
  44.       }]
  45.     },
  46.     "streamSettings": {
  47.       "network": "kcp",
  48.       "kcpSettings": {
  49.         "header": {
  50.           "type": "wechat-video"
  51.         }
  52.       },
  53.       "sockopt":{
  54.         "mark":255
  55.       }
  56.     }
  57.   },
  58.   {
  59.     "protocol": "freedom",
  60.     "tag": "direct",
  61.     "settings": {}
  62.   }],
  63.   "routing": {
  64.     "domainStrategy": "IPOnDemand",
  65.     "rules": [{
  66.       "type": "field",
  67. #      "domain": ["geosite:cn"],
  68.       "ip": ["geoip:private"],
  69.       "ip": ["geoip:cn"],
  70.       "outboundTag": "direct"
  71.     }]
  72.   }
  73. }
複製代碼
.

TOP

iptables script
  1. #!/bin/bash

  2. #TCP
  3. iptables -t nat -N V2RAY # 新建一个名为 V2RAY 的链
  4. iptables -t nat -A V2RAY -d 192.168.0.0/16 -j RETURN # 直连 192.168.0.0/16
  5. iptables -t nat -A V2RAY -p tcp -j RETURN -m mark --mark 0xff # 直连 SO_MARK为 0xff 的流量(0xff 是 16 进制数,数值上等同与上面的 255),此规则目的是避免代理本机(网关)流量出现回环问题
  6. iptables -t nat -A V2RAY -p tcp -j REDIRECT --to-ports 12345 # 其余流量转发到 12345 端口(即 V2Ray)
  7. iptables -t nat -A PREROUTING -p tcp -j V2RAY # 对局域网其他设备进行透明代理
  8. iptables -t nat -A OUTPUT -p tcp -j V2RAY # 对本机进行透明代理, 然后设定 UDP 流量透明代理的 iptables 规则,命令如下

  9. #UDP
  10. ip rule add fwmark 1 table 100
  11. ip route add local 0.0.0.0/0 dev lo table 100
  12. iptables -t mangle -N V2RAY_MASK
  13. iptables -t mangle -A V2RAY_MASK -d 192.168.0.0/16 -j RETURN
  14. iptables -t mangle -A V2RAY_MASK -p udp -j TPROXY --on-port 12345 --tproxy-mark 1
  15. iptables -t mangle -A PREROUTING -p udp -j V2RAY_MASK
複製代碼
.

TOP

本帖最後由 角色 於 2019-1-29 02:32 編輯

测试结果:

1、iPad and MacBook接上router的WiFi SSID就能科学上网。

2、MacBook用LAN cable接上router的LAN能科学上网。

3、手机连上Router WiFi SSID也能科学上网。

但是浏览大陆网站就出问题!!!(估计可能要采用kingwilliam的iptables方法)

调试中的发现:

1、在白话文中【1】,DHCP只说gateway,但是没有提及过DNS怎样设。于是建好后,加上firewall rules (iptables),依然不能科学上网。因为有了上一次经验【2】,于是Router的DHCP server DNS设为192.168.1.22, 而在config.json里也加上 port 53 Dokodemo。经过这些改动,整个系统都能科学上网。但是我发现系统不畅顺,估计可以从DNS回来的外国IP有污染。

现在我有了两套settings的network configurations,那么下一个目标可能就是一个Raspberry Pi,LAN接router的LAN port,而router不需要改动什么,而Raspberry的WiFi改为WiFi access point, 那么mobile device可以不需要安装什么都能科学上网。


Reference:
【1】https://toutyrater.github.io/app/transparent_proxy.html
【2】http://www.telecom-cafe.com/forum/viewthread.php?tid=7300

TOP

TOP

本帖最後由 角色 於 2019-1-29 02:29 編輯

V2Ray是怎样解域名?

在解hostname,在V2Ray有几个入口,1)localhost(linux machine), 2)Dokodemo,3)V2Ray DNS

一般我们用V2Ray,如果没有 Dokodemo和V2Ray DNS,一般会用到machine的localhost去解域名。由于我现在用的方法跟传统接法不一样(一般主要用socks protocol),所以他们之间是怎样协调呢?

如果没有2)Dokodemo,只有1)local machine and 3)V2Ray DNS,这个V2Ray的透明代理部分都不能工作。

如果没有1)localhost, 系统只能ping,而不能解域名,所以必须要有。

如果没有3)V2Ray DNS,那么有的时候Dokodemo起作业,有的时候localhost起作用。

最好的配置是三者都有。那么有DNS request进入Raspberry,要求先看DNS的nameserver service排序。一般是8.8.8.8, 1.0.0.1,localhost。而第二个8.8.4.4是必须的,触发localhost会经过出现。

怎样判定,DNS name是否通过V2Ray走过去?我们可以在remote V2Ray那里用Linux “tcpdump port 53 -nn”就知道。
  1.   "dns": {
  2.     "servers": [
  3.       "8.8.8.8",
  4.       "1.0.0.1",
  5.       "localhost"
  6.     ]
  7.   },
  8.   "inbounds": [
  9.     {
  10.       "protocol": "socks",
  11.       "port": 1081,  // SOCKS 代理端口,在浏览器中需配置代理并指向这个端口
  12.       "listen": "192.168.1.22",
  13.       "settings": {
  14.         "udp": true
  15.       }
  16.     },
  17.     {
  18.       "protocol": "dokodemo-door",
  19.       "port": 12345,
  20.       "domainOverride": ["tls","http"],
  21.       "settings": {
  22.         "network": "tcp,udp",
  23.         "followRedirect": true
  24.       },
  25.       "sniffing": {
  26.         "enabled": true,
  27.         "destOverride": ["http","tls"]
  28.       }
  29.     },
  30.     {
  31.       "protocol": "dokodemo-door",
  32.       "port": 53,
  33.       "listen": "192.168.1.22",
  34.       "settings": {
  35.         "address": "8.8.8.8",
  36.         "port": 53,
  37.         "network": "udp",
  38.         "timeout": 0,
  39.         "followredirect": false
  40.       }
  41.     }
  42.   ],
  43. .
  44. .
  45. .
複製代碼
.

TOP

返回列表