V2Ray透明代理/路由器翻墙客户端配置

  • A+
所属分类:软件
摘要

局域网内的其他机器,修改网关IP为这台虚拟机的IP即可(修改网络属性中的默认网关)或者直接在上级路由(主路由)修改DHCP分配的网关IP。现在只要接入你这个网络的设备,无论是电脑还是手机等,都可以实现自动翻墙功能。

发现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

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: