中三A105论坛

注册 登录
查看: 5|回复: 0

V2Ray+WebSocket+TLS+Web+Cloudflare教程

[复制链接]

130

主题

130

帖子

436

积分

中级会员

Rank: 3Rank: 3

积分
436
发表于 2024-5-4 21:59:52 | 显示全部楼层 |阅读模式
本帖最后由 gnn 于 2024-5-4 22:14 编辑

背景

一般来说,装好V2Ray之后就可以科学上网了,但是IP经常被封,固有此方案。
WebSocket+TLS+Web的组合怎么来的?这个组合意思是,在机器上部署一个web站点,使用 Nginx/Caddy/Apache 可以将 V2Ray 稍作隐藏。使用 WebSocket 是因为搭配 Nginx/Caddy/Apache 只能用 WebSocket,使用 TLS 是因为可以流量加密,看起来更像 HTTPS。也就是说,我们的机器看起来是个正常的网站,当访问特定的path,可以把流量转发给V2Ray。
Cloudflare是一个CDN,可以代理所有的流量,同时隐藏真实的IP,这样IP被封的几率就小了。

准备
  • 用来科学上网的VPS机器,可以SSH登录
  • 一个域名,我使用的是阿里云的,4块一年,到期了再换,买便宜的就行。本文以mydomain.com​域名作为演示。

一、 安装v2ray
如果已经装好了v2ray且能通过ip正常使用,跳过这一步
官方文档:fhs-install-v2ray/README.zh-Hans-CN.md at master · v2fly/fhs-install-v2ray (github.com)
sudo -i​切换到root用户,执行:
  1. bash <(curl -L https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh)
复制代码


1.1 配置服务端
位于/usr/local/etc/v2ray/config.json
  1. {
  2.   "inbounds": [
  3.     {
  4.       "port": 10000, // 服务器监听端口
  5.       "protocol": "vmess",    // 主传入协议
  6.       "settings": {
  7.         "clients": [
  8.           {
  9.             "id": "faef4e76-6e0e-4d6c-aea0-f8dfaf9456d9",  // 用户 ID
  10.             "alterId": 0 // 新版本不能设置为64

  11.           }
  12.         ]
  13.       }
  14.     }
  15.   ],
  16.   "outbounds": [
  17.     {
  18.       "protocol": "freedom",  // 主传出协议
  19.       "settings": {}
  20.     }
  21.   ]
  22. }
复制代码
上面的用户ID是UUID,可以理解为密码,请修改为自己的,这里可以在线生成:Online UUID Generator Tool
1.2 启动V2Ray
sudo systemctl restart v2ray.service​
1.3 配置Windows客户端
在这里下载:2dust/v2rayN: A V2Ray client for Windows, support Xray core and v2fly core (github.com)。安装后[添加VMess服务器],核心字段就是VPS的ip、端口、用户ID:(这里也能生成ID,与服务端配置保持一致即可)
image-20230318153757-vg1pitu.png
然后试用一下能不能上谷歌,可以的话说明V2Ray安装正确,进入下一步。


二、 部署一个Web站点
安装Nginx:
  1. sudo apt install nginx
复制代码
新建站点目录:
  1. cd /var/www
  2. sudo mkdir mydomain.com
复制代码
然后在该目录下放一个静态页面,内容随便(可以去github找些静态网站放在这个目录):
  1. cd mydomain.com
  2. sudo echo “hello” > index.html
复制代码
配置virtual host:
  1. cd /etc/nginx/sites-available
  2. touch mydomain.com
复制代码
vim mydomain.com,内容如下:
  1. server {
  2.        listen 80; # 本站监听80端口
  3.        server_name mydomain.com; # 虚拟主机名

  4.        root /var/www/mydomain.com; # 本网站的根目录
  5.        index index.html; # 主页

  6.        location / {
  7.                try_files $uri $uri/ =404;
  8.        }
  9. }
复制代码
然后将默认配置文件改为刚刚创建的站点配置文件:
  1. cd /etc/nginx/sites-enabled
  2. sudo ln -s /etc/nginx/sites-available/mydomain.com default
复制代码
重启Nginx:
  1. sudo systemctl restart nginx.service
复制代码


三、 设置域名解析
登录阿里云控制台,进入域名列表,为自己的域名设置解析记录:添加一条A记录,让该域名指向你的VPS机器的ip。然后验证能否通过域名访问自己的web站点。在浏览器输入http://mydomain.com​,如果不能访问,检查VPS的防火墙是否放开了80端口。
腾讯云截图:
image-20240221152951-n9fifn1.png
(当后面使用CloudFlare的DNS服务器时,就可以不在这里设置解析记录了,而是在CloudFlare那边设置)。


四、 给Web站点安装TLS证书
按照certbot的指南操作:
  1. # 安装 certbot
  2. sudo apt install snapd
  3. sudo apt remove certbot
  4. sudo snap install --classic certbot
  5. sudo ln -s /snap/bin/certbot /usr/bin/certbot

  6. # 下面这一步就是生成证书并安装到Nginx,有一些交互性的操作,按提示操作即可
  7. sudo certbot --nginx
复制代码
贴一下我的安装日志:
  1. ubuntu@instance-3c23g-220418:~$ sudo certbot --nginx
  2. Saving debug log to /var/log/letsencrypt/letsencrypt.log

  3. Which names would you like to activate HTTPS for?
  4. We recommend selecting either all domains, or all domains in a VirtualHost/server block.
  5. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  6. 1: mydomain.com
  7. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  8. Select the appropriate numbers separated by commas and/or spaces, or leave input
  9. blank to select all options shown (Enter 'c' to cancel):
  10. Certificate not yet due for renewal

  11. You have an existing certificate that has exactly the same domains or certificate name you requested and isn't close to expiry.
  12. (ref: /etc/letsencrypt/renewal/mydomain.com.conf)

  13. What would you like to do?
  14. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  15. 1: Attempt to reinstall this existing certificate
  16. 2: Renew & replace the certificate (may be subject to CA rate limits)
  17. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  18. Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
  19. Renewing an existing certificate for mydomain.com

  20. Successfully received certificate.
  21. Certificate is saved at: /etc/letsencrypt/live/mydomain.com/fullchain.pem
  22. Key is saved at:         /etc/letsencrypt/live/mydomain.com/privkey.pem
  23. This certificate expires on 2024-01-22.
  24. These files will be updated when the certificate renews.
  25. Certbot has set up a scheduled task to automatically renew this certificate in the background.

  26. Deploying certificate
  27. Successfully deployed certificate for mydomain.com to /etc/nginx/sites-enabled/default
  28. Your existing certificate has been successfully renewed, and the new certificate has been installed.

  29. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  30. If you like Certbot, please consider supporting our work by:
  31. * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
  32. * Donating to EFF:                    https://eff.org/donate-le
  33. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
复制代码
如上所述,Certbot提到,它设置了定时任务,会定期更新证书。这个定时任务不在crontable里面,在 systemctl list-timers​里:
  1. ubuntu@instance-3c23g-220418:/etc/nginx/sites-available$ systemctl list-timers
  2. NEXT                        LEFT          LAST                        PASSED        UNIT                         ACTIVATES         
  3. Tue 2023-10-24 02:18:55 UTC 28min left    Mon 2023-10-23 12:07:04 UTC 13h ago       apt-daily.timer              apt-daily.service   
  4. Tue 2023-10-24 04:14:00 UTC 2h 24min left Mon 2023-10-23 15:56:01 UTC 9h ago        snap.certbot.renew.timer     snap.certbot.renew.service
复制代码
证书装好了之后,再看Nginx的配置文件,发现里面多了一些内容:
cd /etc/nginx/sites-enabled
cat default:(这个default是指向mydomain.com文件的)
  1. server {
  2.   
  3.        server_name mydomain.com;

  4.        root /var/www/mydomain.com;
  5.        index index.html;

  6.        location / {
  7.                try_files $uri $uri/ =404;
  8.        }

  9.     listen 443 ssl; # managed by Certbot
  10.     ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem; # managed by Certbot
  11.     ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem; # managed by Certbot
  12.     include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  13.     ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
  14. }
  15. server {
  16.     if ($host = mydomain.com) {
  17.         return 301 https://$host$request_uri;
  18.     } # managed by Certbot


  19.     listen 80;
  20.     server_name mydomain.com;
  21.     return 404; # managed by Certbot


  22. }
复制代码
可见,以managed by Certbot​注释结尾的就是certbot添加的内容。配置的意思是,监听443端口,如果通过80端口访问,则重定向到443。也就是说,现在在浏览器访问http://mydomain.com​,将会被重定向到https://mydomain.com​。
重启nginx服务,添加防火墙规则放行443端口。若浏览器可以访问https://mydomain.com​,说明证书无问题,进入下一步。


五、 Nginx分流
现在流量全都去自己的Web网站了,科学上网怎么办?设置Nginx的path,将特定path的流量转发到V2Ray服务:
5.1 修改Nginx配置
cd /etc/nginx/sites-enabled
sudo vim default (这个default是指向mydomain.com文件的)
将下面这段内容,添加到监听443端口的server里面:
  1. location /login { # 与 V2Ray 配置中的 path 保持一致
  2.         if ($http_upgrade != "websocket") { # WebSocket协商失败时返回404
  3.             return 404;
  4.         }
  5.         proxy_redirect off;
  6.         proxy_pass http://127.0.0.1:10000; # 假设WebSocket监听在环回地址的10000端口上
  7.         proxy_http_version 1.1;
  8.         proxy_set_header Upgrade $http_upgrade;
  9.         proxy_set_header Connection "upgrade";
  10.         proxy_set_header Host $host;
  11.         # Show real IP in v2ray access.log
  12.         proxy_set_header X-Real-IP $remote_addr;
  13.         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  14.   }
复制代码
上述配置的意思是,当访问https://mydomain.com/login​时,将其转发到10000端口,而10000端口就是V2Ray服务的监听端口。
login可以随便写,使用login是为了让这个网站看起来更加“正常”。虽然TLS会加密URL path。
添加后的效果为:
  1. server {
  2.   
  3.        server_name mydomain.com;

  4.        root /var/www/mydomain.com;
  5.        index index.html;

  6.        location / {
  7.                try_files $uri $uri/ =404;
  8.        }

  9.     listen 443 ssl; # managed by Certbot
  10.     ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem; # managed by Certbot
  11.     ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem; # managed by Certbot
  12.     include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  13.     ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

  14.     location /login { # 与 V2Ray 配置中的 path 保持一致
  15.         if ($http_upgrade != "websocket") { # WebSocket协商失败时返回404
  16.             return 404;
  17.         }
  18.         proxy_redirect off;
  19.         proxy_pass http://127.0.0.1:10000; # 假设WebSocket监听在环回地址的10000端口上
  20.         proxy_http_version 1.1;
  21.         proxy_set_header Upgrade $http_upgrade;
  22.         proxy_set_header Connection "upgrade";
  23.         proxy_set_header Host $host;
  24.         # Show real IP in v2ray access.log
  25.         proxy_set_header X-Real-IP $remote_addr;
  26.         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  27.   }


  28. }
  29. server {
  30.     if ($host = mydomain.com) {
  31.         return 301 https://$host$request_uri;
  32.     } # managed by Certbot


  33.        listen 80;

  34.        server_name mydomain.com;
  35.     return 404; # managed by Certbot


  36. }
复制代码


5.2 修改V2Ray配置
sudo vim /usr/local/etc/v2ray/config.json
  1. {
  2.     "inbounds": [
  3.         {
  4.             "port": 10000,
  5.             "listen": "127.0.0.1",
  6.             "protocol": "vmess",
  7.             "settings": {
  8.                 "clients": [
  9.                     {
  10.                         "id": "faef4e76-6e0e-4d6c-aea0-f8dfaf9456d9",
  11.                         "alterId": 0
  12.                     }
  13.                 ]
  14.             },
  15.             "streamSettings": {
  16.                 "network": "ws",
  17.                 "wsSettings": {
  18.                     "path": "/login"
  19.                 }
  20.             }
  21.         }
  22.     ],
  23.     "outbounds": [
  24.         {
  25.             "protocol": "freedom",
  26.             "settings": {}
  27.         }
  28.     ]
  29. }
复制代码
可见,是在原来的基础上,添加了如下配置:
  1. "streamSettings": {
  2.     "network": "ws",
  3.     "wsSettings": {
  4.         "path": "/login"
  5.     }
  6. }
复制代码
重启Nginx和v2ray服务。
5.3 修改v2ray客户端配置
image-20230318164550-mix7a1z.png
与最开始的配置相比,有以下几点变化:
  • 地址:之前是VPS机器的IP,变为域名了
  • 端口:之前直接写的v2ray服务端的端口,现在是443端口
  • 传输协议:之前是TCP,现在是WebSocket(ws)
  • 然后有一个路径:login
  • 传输安全性:之前没有,现在是TLS
改了客户端配置后,再试一下能不能访问谷歌,如果一些顺利的话,进入下一步。

六、 使用Cloudflare隐藏IP
到前面为止,虽然我们配置了使用域名来访问v2ray,但是阿里云的DNS解析服务依然受监管,依然知道我们的流量最终去到了哪个IP。如果我们的流量比较异常,还是会被揪出来,IP还是会被封。因此,可以使用Cloudflare的DNS服务器,增加一层保护,稳一点是一点。
cloudflare 是一家国外的 CDN 加速服务商,注册之后,添加站点,输入你的域名mydomain.com。

6.1 更换DNS服务器
然后为域名添加DNS记录: image-20230318171405-l7e92hf.png

然后登录阿里云控制台,域名列表-管理,将DNS服务器改为cloudflare的DNS服务器:
这是阿里云默认的
image-20230318171535-uaglot6.png
image-20230318171627-8kfpeys.png

6.2 配置加密模式
然后进入cloudflare-SSL/TLS,将加密模式设置为完全(严格):
image-20230318171840-hw31hkz.png

6.3 速度优化
然后在速度-优化中,把这些都勾上:
image-20230318172150-oevb6f6.png

七、 更新域名域名都是几块钱一年的便宜域名,到期后如何修改?以腾讯云为例



7.1 腾讯云-域名注册-我的域名,修改DNS服务器,改为CloudFlare提供的名称服务器:
image-20240221151836-2n6enrb.png
7.2 按照前文第6节,在CloudFlare设置一遍


7.3 登陆vps,修改nginx配置和证书
编辑/etc/nginx/sites-enabled/default,全局替换,将里面的域名改为新的域名。注意不要存放web文件的目录不要改了,也就是这一行:
  1. root /var/www/mydomain.top;
复制代码
如果你改了mydomain.top,请同步去/var/www/目录下修改目录名。
然后删除所有证书相关的配置,也就是以managed by Certbot​结尾的行,保存。
然后运行以下命令再次生成证书,参考前文第4节:
  1. sudo certbot --nginx
复制代码
然后编辑/etc/nginx/sites-enabled/default,删除里面一些多余的行。
7.4 最后重启nginx即可。

八、 总结
image-20220308230038-enct8t4.png


https://whuwangyong.github.io/20 ... b-cloudflare-guide/
https://ericclose.github.io/V2Ra ... ith-Cloudflare.htmlhttps://github.com/XIU2/CloudflareSpeedTest
https://www.hicairo.com/post/24.html
https://github.com/233boy/v2ray/ ... y%E6%B5%81%E9%87%8F
https://privacymelon.com/how-to-setup-v2ray-ws-tls-cdn/
https://github.com/233boy/v2ray/ ... 7%E6%95%99%E7%A8%8B

image-20230318171405-l7e92hf.png
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回列表 返回顶部