xray教程

Xray教程

本文转载自:https://tlanyan.me/xray-tutorial/,如文中内容有错误请到原文查看原始版(最新版)

本以为 V2ray的VLESS协议介绍和使用教程V2ray系列教程 的最后一篇,万万没想到Xray横空出世,并且项目发展高歌猛进,风光无限。鉴于此,有必要为学不动的网友科普一下Xray项目。

本Xray教程重点介绍Xray项目由来、和V2ray的关系,至于服务端部署和客户端使用,目前基本与V2ray一致,因此仅做简要介绍。

Xray介绍

一个Debian包维护者发现XTLS库的LICENSE不是BSD许可,提了一个issue希望作者 @rprx 能修改方便打包,详见 https://github.com/XTLS/Go/issues/9。由这个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项目

XTLS和Xray离不开作者 @rprx 的辛勤付出,因此也简要介绍一下@rprx

1. @rprxVLESS协议的设计者,在介绍VLESS协议时写下了 性能至上、可扩展性空前,目标是全场景终极协议 的宏壮愿景;

2. @rprx 是 XTLS 的作者,在 XTLS库 中写下了 “THE FUTURE” 的霸气描述。将内外两条TLS连接结合,rprx可能不是第一个有这想法的人,但却是第一个将其实现、并成熟应用到实际中的作者。从使用表现上看,XTLS无愧于rprx对其的评价:划时代的革命性概念与技术:XTLS,以及社区给出的“黑科技”称谓;

3. @rprx 是Project X和Xray项目的创始人。由于LICENSE理念之争,rprx创建了对标Project V和V2ray-core的Project X和Xray-core项目,广受欢迎。

Xray-Core自上个月创立以来,短短一个月已经发布了七个版本,足见维护者的诚意。Xray-Core目前发布的各个版本主要介绍如下:

1. Xray-core 1.0.0版本:项目创建,提供完整的VLESS和XTLS支持,性能全面增强,功能上是V2ray-core的超集。主要变动是将v2ray和v2ctl可执行文件合并为xray;

2. Xray-core 1.1.0和1.1.1版本:测试过渡版本,未提供详细发行说明;

3. Xray-core 1.1.2版本:引入了Linux Kernel Splice技术,适用于安卓、路由器等Linux环境。Splice技术减少了数据拷贝次数和内存占用,拥有更强的性能。需要说明的是,只适用于类Linux环境的客户端出栈,服务端入栈仍然是direct;

4. Xray-core 1.1.3版本:重构了透明代理的 REDIRECT 模式,使之同时支持 IPv4 和 IPv6,解决了V2ray和trojan遗留的问题;

5. Xray-core 1.1.4版本:优化内存占用,TLS更多选项配置,使服务端伪装站的TLS设置在SSL Labs能达到A+评分;

6. Xray-core 1.1.5版本:测试过渡版本,支持YAML等配置文件,支持OCSP Stapling等功能,官方安装脚本大更新。

Xray和V2ray的区别

在说明Xray和V2ray区别之前,先说一下三个相近但不同的概念:

  • V2ray:Project V 是用于构建基础通信网络的工具合集,其核心工具称为V2Ray。V2ray主要负责网络协议和功能的实现,既可以单独运行,也可以和其它工具配合。V2ray官网是:https://v2ray.com/,Github项目主页是:https://github.com/v2ray,TG讨论组是:@projectv2ray
  • V2fly:出现一些科学上网作者被喝茶事件后,V2ray原开发者长期不上线,其他维护者没有完整权限,导致V2ray项目维护困难。因此社区在2019年组建了V2fly组织,继续维护V2ray,也是目前V2ray发展的主力。V2fly官网是:https://www.v2fly.org,Github项目主页是:https://github.com/v2fly,TG通知频道:@v2fly,TG交流群为:@v2fly_chat
  • Xray:因协议理念之争,VLESS和XTLS的作者单独创建了Xray项目,目前是V2ray的超集,后续可能有不同的发展路线。Xray文档官网(测试中):https://xtls.github.io/, Github项目主页:https://github.com/XTLS,TG交流群:@projectXray

从上面可以看到,先有V2ray(Project V),然后是V2fly,最后才出来Xray(Project X)。其中V2fly是V2ray的社区,可以认为两者是同一个组织。

详细一点说,Xray和V2ray区别如下:

  1. Xray是V2ray的一个分支(Fork)。Xray项目基于V2ray而来,其支持并且兼容V2ray的配置;
  2. Xray是V2ray的超集。虽然最新版V2ray删除了XTLS,但仍保留VLESS协议。Xray提供完整的VLESS和XTLS支持,目前是V2ray的超集,但后续Xray可能会有会有自己的发展方向;
  3. 如果使用XTLS,推荐Xray,否则请安装V2ray-Core 4.29.0 ~ 4.32.1版本;不使用XTLS的情况下,使用V2ray和Xray均可。

一个小提示是,Xray项目创建以来,V2ray没再发布新版本,反而Xray热火朝天,不断出新版和新功能。此外Xray的TG群也非常热闹,每天至少七八K的消息。如果你喜欢尝试新东西和折腾,Xray适合你,否则V2ray也挺好用。

如今Xray和V2ray分家,后续有没有可能Xray再合并回V2ray呢?这个问题没有答案,也许会合并回去结束分裂,也可能就此分道扬镳。io.js从Node.js分出后来又合并回去,C++源自C但完全是一门新语言独立发展,因此一切皆有可能。

Xray安装和使用教程

和其他自行部署的技术相同,使用Xray分为安装服务端和配置客户端两部分,接下来分别做介绍。

安装Xray

1. 准备一台境外的VPS,购买可参考 一些VPS商家整理;

2. SSH连接到境外服务器,windows请参考:Bitvise连接Linux服务器教程,mac系统请参考:Mac电脑连接Linux教程

3. 自行部署Xray服务端需要你有基本linux技巧,能使用vim/nano等编辑器。官方提供了大多数Linux系统的一键脚本,可以直接使用:

bash <(curl -L https://github.com/XTLS/Xray-install/raw/main/install-release.sh) install

新手推荐使用 Xray一键脚本

官方脚本安装的文件符合FHS规范,可执行文件xray在 /usr/local/bin 目录下,配置文件位于 /etc/local/etc/xray目录内。

4. 官方脚本安装的配置文件内容为空,可参考 Xray-examples 中提供的模板编辑配置文件。例如使用VLESS+TCP+XTLS的配置文件为:

{
    "log": {
        "loglevel": "info"
    },
    "inbounds": [
        {
            "port": 443, # 可以换成其他端口
            "protocol": "vless",
            "settings": {
                "clients": [
                    {
                        "id": "", // 填写UUID,可以使用xray uuid生成
                        "flow": "xtls-rprx-direct",
                        "level": 0
                    }
                ],
                "decryption": "none",
                "fallbacks": [
                    {
                        "dest": 80 // 回落配置,可以直接转到其他网站,例如"www.baidu.com:80"
                    }
                ]
            },
            "streamSettings": {
                "network": "tcp",
                "security": "xtls",
                "xtlsSettings": {
                    "alpn": [
                        "http/1.1"
                    ],
                    "certificates": [
                        {
                            "certificateFile": "/path/to/fullchain.crt", // 换成你的证书,绝对路径
                            "keyFile": "/path/to/private.key" // 换成你的私钥,绝对路径
                        }
                    ]
                }
            }
        }
    ],
    "outbounds": [
        {
            "protocol": "freedom"
        }
    ]
}

XTLS需要证书,因此需要一个域名并申请证书。域名不需要备案,国内和国外买的都可以。域名购买可参考:Namesilo域名注册和使用教程 或从 适合国人的域名注册商推荐 选购。域名申请证书可参考从Let’s Encrypt获取免费证书 或 从阿里云获取免费SSL证书

fallback选项以及ALPN等设置请参考:VLESS协议的fallback参数介绍

5. 配置完毕后,可通过 systemctl start xray 运行 xray,systemctl stop xray 停止xray,systemctl restart xray 重启,journalctl -xe --no-pager -u xray 查看运行日志。

最后,记得放行防火墙。如果是阿里云、腾讯云、AWS/GCP等大厂的服务器,还需要到网页后台的安全组放行端口。

配置Xray客户端

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

Xray Windows客户端

  • V2rayN:3.28版本起支持xray,只需要下载Xray-core,将解压的文件放到V2rayN-Core文件夹下即可。需要注意V2rayN 4.0版本开启移除PAC,改用路由规则,会给习惯了PAC的用户带来困扰。习惯Qv2ray的网友应该乐于接受这个改变;
  • winXray:winXray是Windows系统上简洁稳定的Xray/V2RayShadowsocksTrojan 通用客户端,可自动检测并连接访问速度最快的 代理服务器。该项目原作者删库后出现了一些同名库,安全性未知,因此本站托管的依然是旧版;
  • Qv2ray:Qv2ray是一个基于Qt框架开发的v2ray客户端,可通过插件支持SS、SSR、VMESS、VLESS、trojan等多种协议。

Xray安卓客户端

  • V2rayNG:V2rayNG可以说是最跟随Xray步伐的V2ray客户端了,Xray发布新版本后会在第一时间更新,推荐使用。

Xray Mac客户端

  • Qv2ray:Qv2ray是一个基于Qt框架开发的跨平台v2ray客户端,因此支持MacOS系统。实际上,自V2rayU作者删库不更新后,Qv2ray可以说是Mac系统上支持VLESS协议的独苗了,但可能会有设置系统代理无效的bug。

Xray苹果客户端:

  • Shadowrocket/小火箭:小火箭目前是ios系统上更新最频繁的V2ray客户端,价格也不贵,支持多种协议,推荐使用。

以上客户端均可以在 V2ray客户端 下载,请参考其中的配置教程配置,本文不再赘述。

总结

不管V2ray和Xray今后发展如何,本人都真心感谢为这两个项目付出时间和贡献的开发人员,他们为自由获取互联网信息作出了重要贡献。

本文到此结束,欢迎批评指正!

参考

  1. V2ray教程
  2. V2ray高级技巧:流量伪装
  3. V2ray的VLESS协议介绍和使用教程
  4. VLESS协议的fallback参数介绍

《Xray教程》上有43条评论

  1. 不能同时使用V2ray和Xray是吗?我先安装了V2ray-Vmess,再安装Xray的时候输入的不同的端口,会显示“其他进程占用了80或443端口,请先关闭再运行一键脚本”,所以只能选一个吗?多谢!

  2. trojan
    Welcome to trojan 1.16.0
    [2021-02-08 23:32:42] [FATAL] fatal: config.json: cannot open file
    [2021-02-08 23:32:42] [FATAL] exiting. . .

    xray run /usr/local/xray/config.json
    Xray 1.2.4 (Xray, Penetrates Everything.) Custom (go1.15.7 linux/amd64)
    A unified platform for anti-censorship.
    2021/02/08 23:34:08 Using config from STDIN
    2021/02/08 23:34:08 [Info] main/json: Reading config: stdin:

      1. trojan /usr/local/etc/trojan/config.json
        Welcome to trojan 1.16.0
        [2021-02-09 00:22:49] [FATAL] fatal: bind: Address already in use
        [2021-02-09 00:22:49] [FATAL] exiting. . .

  3. 手段安装完nginx+php+mysql
    然后通过脚本安装trojan和xray
    利用stream实现443分流,
    再修改trojan和xray配置
    因为不知道如何关闭trojan和xray
    所以就导致了端口冲突

      1. 我是之前用v2ray和trojan共存2用了近半年,现在想尝鲜xray,因为相关文档不全面所以配置有些错误(由之前的centos7也换到了centos8【注:nginx1.14不支持SSL转发模块,而centos8默认的源nginx版本是1.14

  4. 都设成开机启动,端口冲突直接导致ssh连接不上
    安装完后忘记先执行关掉处理(sudo systemctl stop)
    我重装好几次了:)

  5. CentOS8
    dnf install langpacks-en glibc-all-langpacks -y
    sudo dnf update
    sudo dnf install tar
    sudo dnf install vim
    sudo dnf install wget
    setenforce 0

    yum install -y python3 python3-pip
    pip3 install certbot
    sudo dnf install yum-utils
    vim /etc/yum.repos.d/nginx.repo
    “`
    [nginx-stable]
    name=nginx stable repo
    baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    gpgcheck=1
    enabled=1
    gpgkey=https://nginx.org/keys/nginx_signing.key
    module_hotfixes=true

    [nginx-mainline]
    name=nginx mainline repo
    baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
    gpgcheck=1
    enabled=0
    gpgkey=https://nginx.org/keys/nginx_signing.key
    module_hotfixes=true
    “`
    sudo dnf install nginx
    sudo systemctl enable nginx
    /usr/local/bin/certbot –server https://acme-v02.api.letsencrypt.org/directory -d “*.planet520.com” -d “planet520.com” –manual –preferred-challenges dns-01 certonly
    sudo systemctl stop nginx

    sudo dnf install mariadb mariadb-server
    sudo systemctl enable mariadb
    sudo systemctl start mariadb
    mysql_secure_installation
    sudo systemctl stop mariadb

    sudo dnf install https://dl.Fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
    sudo dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm
    sudo dnf module list php
    sudo dnf install php php-fpm php-opcache php-gd php-curl php-mysqlnd
    sudo systemctl enable php-fpm
    sudo systemctl stop php-fpm

    sudo bash -c “$(curl -fsSL https://raw.githubusercontent.com/trojan-gfw/trojan-quickstart/master/trojan-quickstart.sh)”
    sudo systemctl enable trojan
    sudo systemctl stop trojan

    sudo bash -c “$(curl -fsSL https://github.com/XTLS/Xray-install/raw/main/install-release.sh)”
    sudo systemctl enable xray
    sudo systemctl stop xray

    sudo dnf install net-tools
    netstat -lnpt

    修改相关配置文件
    firewall-cmd –add-port=80/tcp –permanent
    firewall-cmd –add-port=443/tcp –permanent

    sudo reboot

    按照以上步骤安装,xray和trojan没法起用,甚至nginx访问的也是502

          1. stream {
            map $ssl_preread_server_name $backend_name {
            x.com web;
            a.x.com trojan;
            b.x.com xray;

            default web;
            }

            upstream web {
            server 0.0.0.0:xxxx0;
            }

            upstream trojan {
            server 0.0.0.0:xxxx1;
            }

            upstream xray {
            server 0.0.0.0:xxxx2;
            }

            server {
            listen 443 reuseport;
            proxy_pass $backend_name;
            ssl_preread on;
            }
            }

  6. server {
    listen 80;
    server_name localhost;
    return 301 https://x.com$request_uri;
    }

    server {
    listen 20070 ssl http2;
    server_name x.com;

    root /e;
    index index.php index.html index.htm default.php default.htm default.html;

    if (!-e $request_filename) {
    rewrite ^(.*)$ /index.php$1 last;
    }

    ssl_certificate “/etc/letsencrypt/live/x.com/fullchain.pem”;
    ssl_certificate_key “/etc/letsencrypt/live/x.com/privkey.pem”;
    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout 10m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    location ~ .*\.php(\/.*)*$ {
    fastcgi_pass unix:/run/php-fpm/www.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /www/$fastcgi_script_name;
    include fastcgi_params;
    }

    location = /config.php{
    deny all;
    }

    location / {
    if (-f $request_filename/index.html){
    rewrite (.*) $1/index.html break;
    }
    if (-f $request_filename/index.php){
    rewrite (.*) $1/index.php;
    }
    if (!-f $request_filename){
    rewrite (.*) /index.php;
    }
    }
    }

  7. trojan的json
    {
    “run_type”: “server”,
    “local_addr”: “127.0.0.1”,
    “local_port”: xxxx2,
    “remote_addr”: “127.0.0.1”,
    “remote_port”: 80,
    “password”: [
    “password”
    ],
    “log_level”: 1,
    “ssl”: {
    “cert”: “/etc/letsencrypt/live/x.com/fullchain.pem”,
    “key”: “/etc/letsencrypt/live/x.com/privkey.pem”,
    “key_password”: “”,
    “cipher”: “ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384”,
    “cipher_tls13”: “TLS_AES_128_GCM_SHA256:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_256_GCM_SHA384”,
    “prefer_server_cipher”: true,
    “alpn”: [
    “http/1.1”
    ],
    “alpn_port_override”: {
    “h2”: 81
    },
    “reuse_session”: true,
    “session_ticket”: false,
    “session_timeout”: 600,
    “plain_http_response”: “”,
    “curves”: “”,
    “dhparam”: “”
    },
    “tcp”: {
    “prefer_ipv4”: false,
    “no_delay”: true,
    “keep_alive”: true,
    “reuse_port”: false,
    “fast_open”: false,
    “fast_open_qlen”: 20
    }
    }

  8. xray的json配置
    {
    “log”: {
    “loglevel”: “info”
    },
    “inbounds”: [
    {
    “listen”: “127.0.0.1”,
    “port”: xxxx2,
    “protocol”: “vless”,
    “settings”: {
    “clients”: [
    {
    “id”: “d76b072e-3bd7-428d-8782-bb59af3401b2”,
    “flow”: “xtls-rprx-direct”,
    “level”: 0
    }
    ],
    “decryption”: “none”,
    “fallbacks”: [
    {
    “dest”: “127.0.0.1:80”
    }
    ]
    },
    “streamSettings”: {
    “network”: “tcp”,
    “security”: “xtls”,
    “xtlsSettings”: {
    “alpn”: [
    “http/1.1”
    ],
    “certificates”: [
    {
    “certificateFile”: “/etc/letsencrypt/live/x.com/fullchain.pem”,
    “keyFile”: “/etc/letsencrypt/live/x.com/privkey.pem”
    }
    ]
    }
    }
    }
    ],
    “outbounds”: [
    {
    “protocol”: “freedom”
    }
    ]
    }

    trojan的端口是xxxx1,扣错了

  9. 我觉得可能是我服务器供应商的镜像问题,centos8用(94ish.me)的BBR脚本切换内核都会导致系统崩溃ssh连不上,重装后才恢复正常

  10. — Logs begin at Tue 2021-02-09 05:55:18 EST, end at Tue 2021-02-09 07:41:37 EST. —
    Feb 09 07:36:03 moerxsun xray[3737]: Failed to start: main: failed to load config files: [/usr/local/etc/xray/config.json] > infra/conf: Failed to build XTLS config. > infra/conf: failed to parse certificate > open /etc/letsencrypt/live/x.com/fullchain.pem: permission denied
    Feb 09 07:36:03 admin systemd[1]: xray.service: Main process exited, code=exited, status=23/n/a
    Feb 09 07:36:03 admin systemd[1]: xray.service: Failed with result ‘exit-code’.
    — Subject: Unit failed
    — Defined-By: systemd
    — Support: https://access.redhat.com/support

    — The unit xray.service has entered the ‘failed’ state with result ‘exit-code’.
    Feb 09 07:41:28 admin systemd[1]: Started Xray Service.
    — Subject: Unit xray.service has finished start-up
    — Defined-By: systemd
    — Support: https://access.redhat.com/support

    — Unit xray.service has finished starting up.

    — The start-up result is done.
    Feb 09 07:41:28 admin xray[3995]: Xray 1.2.4 (Xray, Penetrates Everything.) Custom (go1.15.7 linux/amd64)
    Feb 09 07:41:28 admin xray[3995]: A unified platform for anti-censorship.
    Feb 09 07:41:28 admin xray[3995]: 2021/02/09 07:41:28 [Info] main/json: Reading config: /usr/local/etc/xray/config.json
    Feb 09 07:41:28 admin xray[3995]: Failed to start: main: failed to load config files: [/usr/local/etc/xray/config.json] > infra/conf: Failed to build XTLS config. > infra/conf: failed to parse certificate > open /etc/letsencrypt/live/x.com/x.pem: permission denied
    Feb 09 07:41:28 admin systemd[1]: xray.service: Main process exited, code=exited, status=23/n/a
    Feb 09 07:41:28 admin systemd[1]: xray.service: Failed with result ‘exit-code’.
    — Subject: Unit failed
    — Defined-By: systemd
    — Support: https://access.redhat.com/support

    — The unit xray.service has entered the ‘failed’ state with result ‘exit-code’.

  11. 对xray不柿很熟,但对v2ray的ws+tls很熟(用了半年),现在就是想折腾下新鲜玩意

  12. 用一键脚本装好后,再修改nginx配置完全没任何问题,估计这个xray脚本有问题

  13. ● xray.service – Xray Service
    Loaded: loaded (/etc/systemd/system/xray.service; enabled; vendor preset: disabled)
    Drop-In: /etc/systemd/system/xray.service.d
    └─10-donot_touch_single_conf.conf
    Active: failed (Result: exit-code) since Sat 2021-05-22 14:53:27 GMT; 6s ago
    Docs: https://github.com/xtls
    Process: 14964 ExecStart=/usr/local/bin/xray run -config /usr/local/etc/xray/config.json (code=exited, status=20>
    Main PID: 14964 (code=exited, status=203/EXEC)

    May 22 14:53:27 instance-20210509-0724 systemd[1]: xray.service: Service RestartSec=100ms expired, scheduling rest>
    May 22 14:53:27 instance-20210509-0724 systemd[1]: xray.service: Scheduled restart job, restart counter is at 5.
    May 22 14:53:27 instance-20210509-0724 systemd[1]: Stopped Xray Service.
    May 22 14:53:27 instance-20210509-0724 systemd[1]: xray.service: Start request repeated too quickly.
    May 22 14:53:27 instance-20210509-0724 systemd[1]: xray.service: Failed with result ‘exit-code’.
    May 22 14:53:27 instance-20210509-0724 systemd[1]: Failed to start Xray Service.

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注