纯手工打造机场 VLESS+HTTP+TLS

纯手工打造机场 VLESS+HTTP+TLS

大自然的搬运工 357 2022-07-11

背景

之前搭建过ss-libev,但后面炸鸡了,软路由上也用不了了。所以寻思着换一种新技术重新来过,在了解了V2Ray、Trojan后随便选择了V2Ray的 VLESS+HTTP+TLS 搭建了新机场区别参考。后面有机会再试试Trojan^^。
最开始网上找了一键搭建脚本,确实挺方便,但后面了解到可能里面放了一些不为人知的脚本,所以后面决定根据官网新白话文指南手动搭建。

环境

  • 某云24元便(qiong)宜(bi)轻量应用服务器一台(境外机房)+ Centos8
  • 某云5元/年域名一个(注意有的域名续费很贵)

安装配置

生成证书

#安装acme.sh,目前默认使用ZeroSSL.com CA,多一个邮箱参数
curl https://get.acme.sh | sh -s email=my@example.com

#确保生成acme.sh的别名,方便使用
source ~/.bashrc
  • 生成证书
#生成证书
acme.sh --issue -d www.mydomain.com --standalone --keylength ec-256 --force

#acme.sh脚本自动更新证书。也可手动强制更新证书
acme.sh --renew -d www.mydomain.com --force --ecc
  • 安装到指定目录(生成证书时已在~/.acme.sh/www.mydomain.com_ecc/下)
#创建指定文件夹
mkdir -p /etc/data/ssl
#安装到该文件夹,改命令也会随脚本定时触发
acme.sh --installcert -d mydomain.me --ecc \
        --fullchain-file /etc/data/ssl/ss.crt \
        --key-file /etc/data/ssl/ss.key \
        --reloadcmd "sh /etc/data/ssl/restart.sh"
  • 更新完证书,重启相应服务, restart.sh放入/etc/data/ssl(需要执行权限:chmod a+x restart.sh
#!/bin/bash
cd /etc/data/ssl
#修改权限
chmod a+r ss.key
#重启服务
systemctl reload nginx

安装更新V2Ray

// 安装更新
# bash <(curl -L https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh)
// 移除
# bash <(curl -L https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh) --remove

安装完成后提示

You may need to execute a command to remove dependent software: apt remove curl unzip
Please execute the command: systemctl enable v2ray; systemctl start v2ray

执行systemctl enable v2raysystemctl start v2ray后会在/usr/local/etc/v2ray目录下生成空的配置文件:config.json

服务器端配置

// 最好删除注释
{
	"inbounds": [
		{
            "protocol": "vless",
            "listen": "0.0.0.0",
            #服务器端对外暴露端口,可以不为443,此端口需要关闭防火墙
            "port": 443,
            "settings": {
            	"clients": [
                	{
                		#UUID替换为你生成的uuid,可以理解为客户端连接对应的密码。
            			#命令:v2ctl uuid
                    	"id": "UUID", 
			        	"flow": "xtls-rprx-direct"
                    }
                ],
                #此值不要省略
				"decryption": "none",
                #因为下面alpn分别支持http1.1和http2,所以分别对应2个协议回滚,
                #同时nginx需配置对应监听2种协议端口(若这里未配置了h2,可能不能打开nginx对应web页面)。
                #这里可以理解为HTTP识别到vless就走上面-->outbounds,否则走fallbacks,
                #在fallbacks里(path)匹配决定请求去往目的(dest)。
                "fallbacks": [
                    {
                        #省略时默认http/1.1
                        "alpn": "http/1.1",
                        #该值必填
                        "dest": 9090,
                        #是否传递真实ip信息(需要填1,不需要填0)
                        "xver": 1
                    },
                    {
                        "alpn": "h2",
                        "dest": 8080,
                        "xver": 1
                    }
                ]
        	},
        	"streamSettings": {
            	"network": "tcp",
            	#tls-->tlsSettings,xtls-->xtlsSettings
		    	"security": "tls",
                "tlsSettings": {
                    #换成你的域名,域名需要映射到公网ip
                    "serverName": "www.mydomain.com", 
                    "alpn": [
                        "h2",
                        "http/1.1"
                    ],
		    		"certificates": [
		        		{
                            #对应生成的crt文件绝对路径
                            "certificateFile": "/etc/data/ssl/ss.crt",
                            #对应生成的key文件绝对路径,
                            #在这遇到文件没有读权限的问题:chmod a+r ss.key
                            "keyFile": "/etc/data/ssl/ss.key"
						}
		    		]
				}
            }
		}
    ],
    "outbounds": [
        {
            "protocol": "freedom"
        }
    ]
}

配置完成后,重启服务:systemctl restart v2ray

流量流转大致如下:

sockshttp+sslvless其他协议伪装freedomhttp1.1http2浏览器v2RayN客户端v2Ray服务器监听443正常入站fallbacks目标地址nginx监听9090nginx监听8080

注意:

  • dest决定 TLS 解密后 TCP 流量的去向,目前支持两类地址:(该项必填,否则无法启动)

    1. TCP,格式为 "addr:port",其中 addr 支持 IPv4、域名、IPv6,若填写域名,也将直接发起 TCP 连接(而不走内置的 DNS)。
    2. Unix domain socket,格式为绝对路径,形如 "/dev/shm/domain.socket",可在开头加 "@" 代表 abstract (opens new window)"@@" 则代表带 padding 的 abstract。
      若只填 port,数字或字符串均可,形如 80"80",通常指向一个明文 http 服务(addr 会被补为 "127.0.0.1")。
  • xver发送 PROXY protocol (opens new window),专用于传递请求的真实来源 IP 和端口,填版本 1 或 2,默认为 0,即不发送。若有需要建议填 1。
    目前填 1 或 2,功能完全相同,只是结构不同,且前者可打印,后者为二进制。V2Ray 的 TCP 和 WS 入站均已支持接收 PROXY protocol。
    若你正在 配置Nginx接收 PROXY protocol (opens new window),除了设置 proxy_protocol 外,还需设置 set_real_ip_from,否则可能会出问题。

客户端配置

客户端有很多选择,这里选择v2RayN

单击服务器–>添加[VLESS]服务器;右键系统代理–>自动配置系统代理

添加vless服务器

安装对外服务

#安装nginx
yum install -y nginx

#开机启动
systemctl enable nginx
#修改完重启
systemctl restart nginx

配置文件在:/etc/nginx/nginx.conf

由于V2Ray配置文件中xver: 1,即nginx接收到的为proxy protocol参考

http {
    #这里需要修改,添加proxy_protocol_addr
    log_format  main  '$proxy_protocol_addr - $remote_user [$time_local] '
                        '"$request" $status $body_bytes_sent '
                        '"$http_referer" "$http_user_agent"';

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    server {
        #为proxy协议时,需要增加proxy_protocol
        listen      9090 proxy_protocol;
        listen      8080 http2 proxy_protocol;

        client_max_body_size 50m;
        server_name     www.mydomain.com;
        root         	/usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
            proxy_set_header Host            $host;
            proxy_set_header X-Real-IP       $proxy_protocol_addr;
            proxy_set_header X-Forwarded-For $proxy_protocol_addr;
            #这里映射到本地的一个Halo服务,可以不配置默认映射到nginx静态页面
            proxy_pass http://127.0.0.1:8090;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

静态页面放入:/usr/share/nginx/html

BBR加速

服务器为Centos8,直接开启BBR加速。

echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
sysctl -p

# 重启
reboot

#然后查看BBR是否开启成功
#会返回bbr
sysctl -n net.ipv4.tcp_congestion_control
#会返回tcp_bbr
lsmod | grep bbr