静看光阴荏苒
不管不顾不问不说也不念

V2Ray透明代理/透明网关/广告屏蔽/路由器翻墙

这两天认认真真把V2Ray的白话文教程给啃了一遍。。发现V2Ray拿来做透明代理也挺不错的,我用了2天基本都OK。

我目前遇到的唯一问题是对BT这块支持不好,没有一个特别好的办法能够让BT流量直连就很蛋疼。。

这篇文章主要记录V2Ray当作透明代理时客户端的配置,有关服务端的配置可以参考:

一个全能的梯子鸡搭建过程

首先我在PVE里面开了一台虚拟机,安装了Debian9,使用ROOT权限登录进去开启IPv4转发:

echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf && sysctl -p

安装系统的时候使用了DHCP,这里因为要当成网关(旁路由)使用,所以编辑网卡配置文件,改为静态IP:

nano /etc/network/interfaces

我的具体配置:

allow-hotplug ens18
iface ens18 inet static
 address 192.168.0.11 # 这台虚拟机的IP
 gateway 192.168.0.1 # 上级路由的IP
 netmask 255.255.255.0

重启网络服务,因为是在PVE内的虚拟机,可能重启网络服务会失败,那么直接重启虚拟机:

systemctl restart networking.service
reboot

重启登录上来之后,开始安装V2Ray,因为是国内的网络环境,官方的install.direct一键安装脚本无法正常使用,所以这里改为手动安装,首先下载最新版本的V2Ray/解压:

mkdir -p /opt/v2ray && cd /opt/v2ray
wget https://github.com/v2ray/v2ray-core/releases/download/v4.20.0/v2ray-linux-64.zip
unzip v2ray-linux-64.zip

创建需要的目录:

mkdir -p /usr/bin/v2ray /etc/v2ray

移动文件到对应的目录:

cp v2ctl /usr/bin/v2ray
cp v2ray /usr/bin/v2ray
cp geoip.dat /usr/bin/v2ray
cp geosite.dat /usr/bin/v2ray
cp vpoint_vmess_freedom.json /etc/v2ray/config.json
cp systemd/v2ray.service /etc/systemd/system/v2ray.service

编辑systemd服务文件:

nano /etc/systemd/system/v2ray.service

在[Service]下面加一行,解决too many open files的问题:

LimitNOFILE=1048576

如图所示:

启动V2Ray服务:

systemctl daemon-reload
systemctl start v2ray.service 
systemctl enable v2ray.service

现在编辑V2Ray的配置文件,清空里面的所有配置:

nano /etc/v2ray/config.json

这里给出我目前正在用的两份配置文件,第一种就相当于是全局代理,所有流量都走代理:

{
  "inbounds": [
    {
      "port": 1080,
      "protocol": "socks",
      "settings": {
        "auth": "noauth",
        "udp": true
      }
    },
    {
      "port": 12315, // 透明代理开放的端口号
      "protocol": "dokodemo-door",
      "settings": {
        "network": "tcp,udp",
        "followRedirect": true // 这里要为true才能接受来自iptables的流量
      },
      "sniffing": {
        "enabled": true,
        "destOverride": ["http", "tls"]
      }
    }
  ],
  "outbounds": [
    {
      "protocol": "vmess",
      "settings": {
        "vnext": [
          {
            "address": "lala.im", // 服务器地址,请修改为你自己的服务器IP或域名。
            "port": 50000,  // 服务器端口,与服务器上的配置文件要相同
            "users": [
              {
                "id": "你的UUID",  // 用户的UUID必须与服务器端配置相同
                "alterId": 64 // 此处的值也应当与服务器相同
              }
            ]
          }
        ]
      }
    }
  ]
}

第二种对流量进行智能路由,需要的域名走代理,国内的域名/IP则直连,因为这套配置还使用了外部GEO文件,所以要想正常使用,还需要先下载外部GEO文件到V2Ray的运行目录:

wget https://github.com/ToutyRater/V2Ray-SiteDAT/raw/master/geofiles/h2y.dat -O /usr/bin/v2ray/h2y.dat

配置如下:

{
  "inbounds": [
    {
      "port": 1080,
      "protocol": "socks",
      "settings": {
        "auth": "noauth",
        "udp": true
      }
    },
    {
      "port": 12315, // 透明代理开放的端口号
      "protocol": "dokodemo-door",
      "settings": {
        "network": "tcp,udp",
        "followRedirect": true // 这里要为true才能接受来自iptables的流量
      },
      "sniffing": {
        "enabled": true,
        "destOverride": ["http", "tls"]
      }
    }
  ],
  "outbounds": [
    {
      "tag": "proxy", // 打一个TAG,让外部GeoFile使用此TAG处理被GFW屏蔽的域名
      "protocol": "vmess",
      "settings": {
        "vnext": [
          {
            "address": "lala.im", // 服务器地址,请修改为你自己的服务器IP或域名。
            "port": 50000,  // 服务器端口,与服务器上的配置文件要相同
            "users": [
              {
                "id": "你的UUID",  // 用户的UUID必须与服务器端配置相同
                "alterId": 64 // 此处的值也应当与服务器相同
              }
            ]
          }
        ]
      }
    },
    {
      "tag": "block", // 黑洞TAG,让外部GeoFile使用此TAG屏蔽广告域名
      "protocol": "blackhole",
      "settings": {}
    },
    {
      "tag": "direct", // 直连TAG,处理国内域名和IP使其直连
      "protocol": "freedom",
      "settings": {}
    }
  ],
  "routing": {
    "domainStrategy": "IPOnDemand",
    "rules": [
      {
        "type": "field",
        "outboundTag": "proxy",
        "domain": ["ext:h2y.dat:gfw"] // GFWList
      },
      {
        "type": "field",
        "outboundTag": "block",
        "domain": ["ext:h2y.dat:ad"] // 广告域名屏蔽
      },
      {
        "type": "field",
        "outboundTag": "direct",
        "domain": ["geosite:cn"] // 中国大陆主流网站的域名
      },
      {
        "type": "field",
        "outboundTag": "direct",
        "ip": [
          "geoip:cn", // 中国大陆的IP
          "geoip:private" // 私有地址IP,如路由器等
        ]
      }
    ]
  }
}

配置完成之后测试:

/usr/bin/v2ray/v2ray -config /etc/v2ray/config.json -test

有错排错没问题的话重启V2Ray:

systemctl restart v2ray.service

最后创建iptables规则,对流量进行处理:

iptables -t nat -N V2RAY
iptables -t nat -A V2RAY -d 192.168.0.0/16 -j RETURN
iptables -t nat -A V2RAY -p tcp -j REDIRECT --to-ports 12315
iptables -t nat -A PREROUTING -p tcp -j V2RAY

局域网内的其他机器,修改网关IP为这台虚拟机的IP即可:

或者直接在上级路由(主路由)修改DHCP分配的网关IP:

现在只要接入你这个网络的设备,无论是电脑还是手机等,都可以实现自动翻墙/广告屏蔽等功能。

一点补充:

如果你使用第一套配置(全局代理)其实也可以实现对流量进行路由,并且据说这种路由方法比V2Ray自带的原生GeoIP方法效率更高,更适合跑在CPU性能不咋地的路由器上。

先重启机器清空所有iptables规则:

reboot

安装ipset:

apt -y install ipset

下载中国IP地址列表:

wget https://raw.githubusercontent.com/17mon/china_ip_list/master/china_ip_list.txt

创建一个ipset链:

ipset -N cn hash:net

将中国的IP都加入到ipset链:

for i in $(cat china_ip_list.txt); do ipset -A cn $i; done

执行下面的命令处理流量:

iptables -t nat -N V2RAY
iptables -t nat -A V2RAY -d 192.168.0.0/16 -j RETURN
iptables -t nat -A V2RAY -p tcp -m set --match-set cn dst -j RETURN
iptables -t nat -A V2RAY -p tcp -j REDIRECT --to-ports 12315
iptables -t nat -A PREROUTING -p tcp -j V2RAY
赞(13)
未经允许不得转载:荒岛 » V2Ray透明代理/透明网关/广告屏蔽/路由器翻墙
分享到: 更多 (0)

评论 9

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #1

    哦~~原来是这种办法,lala好聪敏 :oops: 那么,你可以上门服务嘛 :wink:

    橘子5年前 (2019-10-02) Google Chrome 77.0.3833.93 Google Chrome 77.0.3833.93 Windows 8.1 x64 Edition Windows 8.1 x64 Edition回复
    • 上哪个门?派出所吗 :lol: :lol: :lol:

      咸鱼5年前 (2019-10-04) Google Chrome 77.0.3865.90 Google Chrome 77.0.3865.90 Windows 7 x64 Edition Windows 7 x64 Edition回复
  2. #2

    ipset的方式不错。不过iptables规则里面没有RETURN服务器地址不知道会不会上不去网

    wudixiaozihsq4年前 (2020-02-17) Google Chrome 80.0.3987.106 Google Chrome 80.0.3987.106 Windows 10 x64 Edition Windows 10 x64 Edition回复
  3. #3

    谢谢 教程很有用 已经成功应用在我的设备上

    想问一下udp似乎被忽略了?即使v2ray这里设定了”network”: “tcp,udp”

    cherijfowler4年前 (2020-04-08) Google Chrome 80.0.3987.163 Google Chrome 80.0.3987.163 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • udp转发需要iptables设置mangle表,但这里似乎只有设置nat表?

      lalala4年前 (2020-05-16) Opera 68.0.3618.63 Opera 68.0.3618.63 Windows 10 x64 Edition Windows 10 x64 Edition回复
  4. #4

    lala可以出一期cgproxy透明代理的教程吗,我这小白菜配置是成功了,也能正常用,就是不知道哪里的问题,V2ray报了很多Warning不知所措。
    2020/12/20 22:27:01 [Warning] v2ray.com/core/app/proxyman/outbound: failed to process outbound traffic > v2ray.com/core/proxy/vmess/outbound: failed to find an available destination > v2ray.com/core/common/retry: [dial tcp: operation was canceled] > v2ray.com/core/common/retry: all retry attempts failed

    Distro3年前 (2020-12-20) Google Chrome 87.0.4280.88 Google Chrome 87.0.4280.88 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • 这个我也没折腾过可能要研究一下了。。

      LALA3年前 (2020-12-24) Google Chrome 86.0.4240.198 Google Chrome 86.0.4240.198 Windows 10 x64 Edition Windows 10 x64 Edition回复

分享创造快乐

广告合作资源投稿