Kaze1027 / overwall-examples

A note about proxy. GFW
4 stars 0 forks source link

使用NaïveProxy搭建梯子服务并运用多用户或者多主机名嵌套xray进行分流

一、准备工作

Ⅰ.参考资料

NaïveProxy服务是基于caady的一个代理插件forwardproxy所实现的,如需参阅文档,请访问以下链接:

注意:forwardproxy是第三方插件,caddy本身是不带的,如果要自己构建带forwardproxy的caddy,请参阅官方文档进行编译,本文所使用的caddy来自github“lxhao61”的“integrated-examples”所编译版本

Xray-core是v2ray-core的超集,含更好的整体性能和 XTLS 等一系列增强,且完全兼容 v2ray-core 的功能及配置。

Ⅱ.硬件环境

Ⅲ.其他条件

二、搭建NaïveProxy服务(caddy)[用于过墙]

  1. 源安装:

    sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
    curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
    curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
    sudo apt update
    sudo apt install caddy

    完成后输入caddy就可以查看caddy相关命令,该过程会自动创建caddy用户组和用户。

  2. 修改caddy.service

    vim /lib/systemd/system/caddy.service

    替换成以下内容(本service仅供参考),主要是为了指定运行程序的用户为caddy:

    # caddy.service
    #
    # For using Caddy with a config file.
    #
    # Make sure the ExecStart and ExecReload commands are correct
    # for your installation.
    #
    # See https://caddyserver.com/docs/install for instructions.
    #
    # WARNING: This service does not use the --resume flag, so if you
    # use the API to make changes, they will be overwritten by the
    # Caddyfile next time the service is restarted. If you intend to
    # use Caddy's API to configure it, add the --resume flag to the
    # `caddy run` command or use the caddy-api.service file instead.
    
    [Unit]
    Description=Caddy
    Documentation=https://caddyserver.com/docs/
    After=network.target network-online.target
    Requires=network-online.target
    
    [Service]
    Type=notify
    User=caddy
    Group=caddy
    ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/caddy.json
    ExecReload=/usr/bin/caddy reload --config /etc/caddy/caddy.json --force
    TimeoutStopSec=5s
    LimitNOFILE=1048576
    LimitNPROC=512
    PrivateTmp=true
    ProtectSystem=full
    AmbientCapabilities=CAP_NET_BIND_SERVICE
    
    [Install]
    WantedBy=multi-user.target

    执行命令重载caddy.service

    systemctl daemon-reload
  3. 替换二进制caddy文件为带forwardproxy的版本:

    https://github.com/lxhao61/integrated-examples/releases下载带forwardproxycaddy,然后将其上传至服务器用户目录,然后进行替换:

    curl -L https://github.com/lxhao61/integrated-examples/releases/latest/download/caddy-$(uname -s)-$(dpkg --print-architecture).tar.gz -o caddy-$(uname -s)-$(dpkg --print-architecture).tar.gz

    (本文服务器为amd64处理器,如果使用arm之类其他类型处理器,请将其更改)

    sudo tar -xvpf caddy-Linux-amd64.tar.gz caddy -C ~
    sudo mv caddy /usr/bin/

    执行命令获取caddy所带modules是否带有forwardproxy

    caddy list-modules | grep forward_proxy

    返回结果应如下:

    http.handlers.forward_proxy

  4. 安装ca-certificates

    sudo apt install ca-certificates
  5. 创建用于存放证书的目录/etc/ssl/private/,然后修改证书目录权限:

    chown -R caddy:caddy /etc/ssl/private/
  6. 创建伪装页面

    mkdir -p /var/www/html/
    cd /var/www/html/

    放入一个网页(yourwebfolder)到上述目录,然后修改目录权限

    chown -R caddy:caddy /var/www/html/
  7. 创建并写入caddy配置文件caddy.json,文件默认位于/etc/caddy/caddy.json

    vim /etc/caddy/caddy.json

    caddy.json配置文件见文末。

  8. 启动caddy并观察是否正常运行:

    systemctl restart caddy && systemctl status caddy

三、搭建Xray服务[用于分流]

  1. 安装xray:

    bash -c "$(curl -L https://github.com/XTLS/Xray-install/raw/main/install-release.sh)" @ install --version 1.6.5
  2. 创建并写入Xray的配置文件,文件默认位于/usr/local/etc/xray/config.json

    vim /usr/local/etc/xray/config.json

    config.json配置文件见文末。

  3. 下载geosite和geoip

    curl -Lo /usr/local/share/xray/geosite.dat https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat && curl -Lo /usr/local/share/xray/geoip.dat https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat

    添加crontab计划任务每日6:10更新geo数据(sleep时间请根据自己服务器运行速度与文件下载速度来设定):

    echo -e "10 6 * * * systemctl stop xray && sleep 10s && curl -Lo /usr/local/share/xray/geosite.dat https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat && curl -Lo /usr/local/share/xray/geoip.dat https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat && sleep 10s && systemctl start xray" >/var/spool/cron/crontabs/root
    /etc/init.d/cron restart
  4. 启动Xray并观察是否正常运行:

    systemctl restart xray && systemctl status xray

*.其他设置及参考配置文件