月度归档:2022年03月

Xray 教程

什么是 Xray ?

简单粗暴来说,Xray 是用来魔法上网的。

它与大名鼎鼎的 V2ray 有什么区别呢?

介绍个故事。一个 Debian 包维护者发现 XTLS库 的 LICENSE 不是 BSD 许可,提了一个 issue 希望作者 @rprx 能修改方便打包,详见点击 这里。由这个 issue 引发了广泛讨论,rprx 认为目前许可不是问题,也有不少人认为协议是立场的体现,各执一词。

最终 V2ray (V2fly 社区) 维护者经过投票确认 XTLS 不符合 V2ray 的 MIT 协议,并在 V2ray-core 4.33.0版本移除了XTLS。rprx 和其拥护者行动起来,很快就创建了 Project X项目Xray子项目(Xray 取名来自 XTLS和 V2ray 的结合),并发布了 Xray-core 的多个版本,这便是 Xray 的大致由来。

为什么使用 Xray ?

Xray 项目创建以来,V2ray 没再发布新版本,反而 Xray 热火朝天,不断出新版和新功能。

Xray + XTLS 组合号称是黑科技,在性能不佳的小鸡身上,有着明显的速度提升。【我也是听说的,没测过】

喜欢折腾。

安装 Xray 服务

实验环境如下

操作系统CentOS 7.9
服务器位置Dallas, Texas, U.S
公网 IP192.227.193.111

通过 官方脚本 安装,首次安装,需要安装 Xray-core 和 geodata。打开 Xshell,使用 root 用户登陆服务器,执行以下命令。

# bash -c "$(curl -L https://github.com/XTLS/Xray-install/raw/main/install-release.sh)" @ install -u root

配置文件

官方脚本安装的配置文件(路径:/usr/local/etc/xray/config.json)内容为空,可参考 Xray-examples 中提供的模板编辑配置文件。例如使用 VLESS+TCP+XTLS 的配置文件为:

{
  "inbounds": [{
    "port": 443,
    "protocol": "vless",
    "settings": {
      "clients": [
        {
          "id": "28b01209-da52-48e1-ba69-23b6b156046e",
          "flow": "xtls-rprx-direct",
          "level": 0
        }
      ],
      "decryption": "none",
      "fallbacks": [
          {
              "alpn": "http/1.1",
              "dest": 80
          },
          {
              "alpn": "h2",
              "dest": 81
          }
      ]
    },
    "streamSettings": {
        "network": "tcp",
        "security": "xtls",
        "xtlsSettings": {
            "serverName": "xray.domain.com",
            "alpn": ["http/1.1", "h2"],
            "certificates": [
                {
                    "certificateFile": "/usr/local/etc/xray/xray.domain.com.pem",
                    "keyFile": "/usr/local/etc/xray/xray.domain.com.key"
                }
            ]
        }
    }
  }],
  "outbounds": [{
    "protocol": "freedom",
    "settings": {}
  },{
    "protocol": "blackhole",
    "settings": {},
    "tag": "blocked"
  }]
}

上述配置文件中,certificateFile 和 keyFile 涉及到 https 域名证书注册问题,点击 这里 参考教程。

守护进程脚本

vim /etc/systemd/system/xray.service

[Unit]
Description=Xray Service
Documentation=https://github.com/xtls
After=network.target nss-lookup.target

[Service]
User=root
#User=nobody
#CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
#AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
NoNewPrivileges=true
ExecStart=/usr/local/bin/xray run -config /usr/local/etc/xray/config.json
Restart=on-failure
RestartPreventExitStatus=23

[Install]
WantedBy=multi-user.target

重载 Xray 服务并将其加入开机自启

systemctl daemon-reload && systemctl enable xray

启动 Xray 服务

systemctl start xray

客户端配置

服务端配置好后,接下来是配置客户端。目前有如下客户端支持Xray:

v2rayN – 适用于 Windows 平台

请从它的 GitHub 仓库 Release 页面open in new window 获取最新版

请根据该客户端的说明进行设置

v2rayNG – 适用于 Android 平台

请从它的 GitHub 仓库 Release 页面open in new window 获取最新版

请根据该客户端的说明进行设置

Shadowrocket – 适用于 iOS, 基于苹果 M 芯片的 macOS

你需要注册一个【非中国区】的 iCloud 账户

你需要通过 App Store 搜索并购买

请根据该客户端的说明进行设置

V2RayXS – 基于 V2RayX 开发的一款使用 xray-core 的 macOS 客户端

请从它的 GitHub 仓库 Release 页面open in new window 获取最新版

支持一键导入 VMessAEAD / VLESS 分享链接标准提案open in new window 为标准的分享链接

请根据该客户端的说明进行设置

Linux 编译安装 Nginx

引言

在生产环境中,Nginx 通常以源码编译的方式来安装,一则生产环境网络要求苛刻,二则可以自选模块,比如在 复用 443 端口时候,需要用到 stream_ssl_module 模块。

本文以 CentOS 7 和 Debian 11 操作系统为例,文中第四部分是如何新增模块,重新编译已经安装好的 Nginx 服务。

安装

安装依赖包

以下适用于 CentOS 7 | RadHat 7

yum install -y libtool autoconf cmake curl vim \
gcc gcc-c++ \
openssl openssl-devel \
pcre pcre-devel \
zlib zlib-devel \
libxml2 libxml2-devel \
libxslt libxslt-devel \
gd gd-devel \
GeoIP GeoIP-devel GeoIP-data \
gperftools 

以下适用于 Debian 11

apt-get install gcc build-essential manpages-dev \
libpcre3 libpcre3-dev libxslt-dev \
libgeoip-dev google-perftools \
libgoogle-perftools4 libgoogle-perftools-dev \
libtcmalloc-minimal4 libgdchart-gd2-noxpm \
libgdchart-gd2-noxpm-dev -y

下载 Nginx | openssl 包

curl -LO https://nginx.org/download/nginx-1.23.3.tar.gz
curl -LO https://ftp.openssl.org/source/old/1.1.1/openssl-1.1.1k.tar.gz

解压

tar -zxvf nginx-1.23.3.tar.gz -C /usr/local/src
tar -zxvf openssl-1.1.1k.tar.gz -C /usr/local/src

cd /usr/local/src/nginx-1.23.3

useradd www

生成侦测文件

./configure \
--prefix=/usr/local/nginx \
--user=www \
--group=www \
--with-poll_module \
--with-threads \
--with-file-aio \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_xslt_module \
--with-http_image_filter_module \
--with-http_geoip_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-mail --with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-stream_realip_module \
--with-stream_geoip_module \
--with-stream_ssl_preread_module \
--with-google_perftools_module \
--with-pcre \
--with-openssl=/usr/local/src/openssl-1.1.1k \
--with-openssl-opt=enable-weak-ssl-ciphers

编译并安装

make && make install

守护进程脚本

vim /etc/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /usr/local/nginx/logs/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /usr/local/nginx/logs/nginx.pid)"

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start nginx.service

重新编译已存在的 nginx

下载 Nginx

解压

生成侦测文件

在这里添加你想要添加的模块,比如 –with-stream_ssl_module,根据需求来

编译这里千万不要 make install

make

备份并复制

# 备份原有的 nginx 可执行文件
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.default.bak

# 复制全新编译的 nginx
cp /usr/local/src/nginx-1.20.2/objs/nginx /usr/local/nginx/sbin/nginx

重新启动 Nginx

systemctl restart nginx

搭建 v2ray 服務

一、Environment

Operating System: CentOS 7
Server Location: San Jose

二、Let’s Start

1. 安裝和更新 V2Ray (安裝執行檔和 .dat 資料檔)

# bash <(curl -L https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh)

1.1 安裝最新發行的 geoip.dat 和 geosite.dat (只更新 .dat 資料檔)(首次安裝,無需執行)

# bash <(curl -L https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-dat-release.sh)

1.2 移除 V2Ray

# bash <(curl -L https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh) --remove

2. 配置文件 /usr/local/etc/v2ray/config.json

{
    "log": {
        "access": "/var/log/v2ray/access.log",
        "error": "/var/log/v2ray/error.log",
        "loglevel": "warning"
    },
    
    "inbounds": [
    {
        "port": 33689,
        "listen": "127.0.0.1",
        "protocol": "vmess",
        "settings": {
            "clients": [
            {
                "id": "89e2d792-1d03-46ec-9210-23c52d9d4a2f",
                "alterId": 0
            }
            ]
        },
        
        "sniffing": {
            "enabled": true, 
            "destOverride": ["http", "tls"]
        },
        
        "streamSettings": {
            "network": "ws",
            "wsSettings": {
                "path": "/wYof60qQ"
            }
        }
    }
    ],

    "outbounds": [
    {
        "tag": "direct",
        "protocol": "freedom",
        "settings": {}
    },
    
    {
        "tag": "blocked",
        "protocol": "blackhole",
        "settings": {}
    }
    
    ],
    
    "routing": {
        "strategy": "rules",
        "settings": {
            "domainStrategy": "AsIs",
            "rules": [
            {
                "type": "field",
                "ip": ["geoip:private"],
                "outboundTag": "blocked"
            }
            ]
        }
    }
}

3. 守護進程脚本 /etc/systemd/system/v2ray.service

[Unit]
Description=V2Ray Service
Documentation=https://www.v2fly.org/
After=network.target nss-lookup.target

[Service]
User=nobody
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
NoNewPrivileges=true
ExecStart=/usr/bin/env v2ray.vmess.aead.forced=false /usr/local/bin/v2ray -config /usr/local/etc/v2ray/config.json
Restart=on-failure
RestartPreventExitStatus=23

[Install]
WantedBy=multi-user.targe

4. 重載、啓動、添加開機自啓、查詢狀態

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

三、參考文章

  • https://github.com/v2fly/fhs-install-v2ray
  • https://github.com/v2fly/fhs-install-v2ray/wiki/Migrate-from-the-old-script-to-this
  • https://www.v2ray.com/


搭建 Trojan-Go 服务

一、简介

熟悉的筒子们可以直接跳过第一部分

Trojan-Go 使用 Go 实现的完整 Trojan 代理,与 Trojan 协议以及 Trojan 版本的配置文件格式兼容。安全,高效,轻巧,易用。

支持使用多路复用提升并发性能,使用路由模块实现国内直连。

支持 CDN 流量中转(基于 WebSocket over TLS/SSL)

支持使用 AEAD 对 Trojan 流量二次加密(基于Shadowsocks AEAD)。

支持可插拔的传输层插件,允许替换 TLS,使用其他加密隧道传输 Trojan 协议流量。

完整配置教程和配置介绍参见 Trojan-Go文档

Trojan-Go 官方发行包

下载 Trojan-Go 二进制包,以 CentOS 为例

二、安装环境

  • 服务器一台(本文以 CentOS 7 为例)
  • 域名一个(本文以 trojan-go.domain.com 为例)
  • 域名对应的 SSL 证书(如何获取 SSL 证书,请点击这里
  • Trojan-Go 官方发行包 (本文以 v0.10.6 为例)

三、开始吧

# 下载依赖包
yum install vim wget curl unzip -y

# 创建安装目录
mkdir -p /usr/local/trojan-go && cd /usr/local/trojan-go

# 下载二进制包
curl -LO https://github.com/p4gefau1t/trojan-go/releases/download/v0.10.6/trojan-go-linux-amd64.zip

# 解压安装包
unzip trojan-go-linux-amd64.zip

编辑 Trojan-go 服务端主配置文件
vim /usr/local/trojan-go/config.json

{
    "run_type": "server",
    "local_addr": "::",
    "local_port": 443,
    "remote_addr": "127.0.0.1",
    "remote_port": 80,
    "password": [
        "rfha9342@#30ds",
        "-adA5^s23ej",
        "sc8)-1e%ER.PscU"
    ],
    "ssl": {
        "cert": "/root/.acme.sh/trojan-go.domain.com/fullchain.cer",
        "key": "/root/.acme.sh/trojan-go.domain.com/trojan-go.domain.com.key",
        "sni": "trojan-go.domain.com",
        "alpn": [
            "http/1.1"
        ],
        "session_ticket": true,
        "reuse_session": true,
        "fallback_addr": "127.0.0.1",
        "fallback_port": 80
    },
    "tcp": {
        "no_delay": true,
        "keep_alive": true,
        "prefer_ipv4": false
    },
    "mux": {
        "enabled": false,
        "concurrency": 8,
        "idle_timeout": 60
    },
    "websocket": {
        "enabled": true,
        "path": "/dHs7P",
        "host": "trojan-go.domain.com"
    },
    "mysql": {
        "enabled": false,
        "server_addr": "localhost",
        "server_port": 3306,
        "database": "",
        "username": "",
        "password": "",
        "check_rate": 60
    }
}

守护进程脚本 /etc/systemd/system/trojan-go.service

[Unit]
Description=Trojan-Go - An unidentifiable mechanism that helps you bypass GFW
Documentation=https://p4gefau1t.github.io/trojan-go/
After=network.target nss-lookup.target

[Service]
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
NoNewPrivileges=true
ExecStart=/usr/local/trojan-go/trojan-go -config /usr/local/trojan-go/config.json
Restart=on-failure
RestartSec=10s
LimitNOFILE=infinity

[Install]
WantedBy=multi-user.target

重载脚本,并将脚本添加至开机启动

systemctl daemon-reload
systemctl start trojan-go.service
systemctl enable trojan-go.service
systemctl status trojan-go.service