跳转到正文
文章目录

引言

在前文基础上,本文将深入解析各个配置代码段的技术细节、工作原理以及实际部署中的注意事项。通过对每个配置项的详细说明,帮助读者全面理解V2Ray的技术架构和配置逻辑。

第一部分:VLESS + XTLS配置深度解析

1.1 服务端配置详解

</> JSON
{
  "log": {
    "loglevel": "warning",
    "access": "/var/log/v2ray/access.log",
    "error": "/var/log/v2ray/error.log"
  }
}

日志配置说明:

  • loglevel: 日志级别设置为"warning"是一个平衡点

    • debug: 输出最详细信息,会记录每个数据包,文件会迅速膨胀
    • info: 记录连接建立等常规信息
    • warning: 仅记录警告和错误,推荐用于生产环境
    • error: 仅记录错误
    • none: 不记录任何日志(不推荐,无法排查问题)
  • 日志路径选择:

    </> BASH
    # 确保日志目录存在且权限正确
    mkdir -p /var/log/v2ray
    chown nobody:nogroup /var/log/v2ray
    chmod 755 /var/log/v2ray
    
  • 日志监控实践:

    </> BASH
    # 实时监控错误日志
    tail -f /var/log/v2ray/error.log
    
    # 分析访问模式
    cat /var/log/v2ray/access.log | grep "accepted" | wc -l  # 统计连接数
    
    # 查找异常IP
    cat /var/log/v2ray/access.log | grep "rejected" | awk '{print $NF}' | sort | uniq -c | sort -rn
    

1.2 VLESS协议核心配置

</> JSON
{
  "port": 443,
  "protocol": "vless",
  "settings": {
    "clients": [
      {
        "id": "你的UUID",
        "flow": "xtls-rprx-vision",
        "level": 0
      }
    ],
    "decryption": "none"
  }
}

配置项深度解析:

端口443的选择原因:

  • HTTPS标准端口,防火墙很少拦截
  • 与正常网站流量特征一致
  • 某些网络环境只允许80/443端口通信

UUID生成与管理:

</> BASH
# Linux/Mac生成UUID
uuidgen

# 批量生成多个UUID(用于多用户)
for i in {1..10}; do uuidgen; done > uuids.txt

# Windows PowerShell生成
[guid]::NewGuid().ToString()

# Python生成
python3 -c "import uuid; print(uuid.uuid4())"

flow参数详解:

xtls-rprx-vision是XTLS的最新版本,具有以下特点:

  1. Vision技术原理:

    • 智能识别TLS内层加密流量
    • 对已加密的HTTPS流量不进行二次加密(避免特征)
    • 对未加密流量才进行加密处理
  2. 其他可选flow值:

    </> PLAINTEXT
    - "xtls-rprx-direct": 直接模式,性能最高但特征明显
    - "": 空值表示不使用XTLS,回退到普通TLS
    
  3. Vision模式优势:

    </> PLAINTEXT
    普通代理: 明文流量 -> TLS加密 -> 再次加密(代理层) = 双层加密特征
    Vision模式: HTTPS流量 -> 直通不加密 = 与真实HTTPS完全一致
    

level参数说明:

</> JSON
{
  "level": 0  // 用户等级,对应policy中的配置
}

配合policy使用:

</> JSON
{
  "policy": {
    "levels": {
      "0": {
        "handshake": 4,      // 握手超时(分钟)
        "connIdle": 300,     // 空闲连接超时(秒)
        "uplinkOnly": 2,     // 仅上行时的超时
        "downlinkOnly": 5,   // 仅下行时的超时
        "statsUserUplink": true,   // 统计上行流量
        "statsUserDownlink": true, // 统计下行流量
        "bufferSize": 4      // 每个连接的缓冲区大小(KB)
      }
    }
  }
}

decryption设置为none的原因:

  • VLESS协议本身不加密内容
  • 加密由外层TLS/XTLS提供
  • 避免双重加密提升性能和隐蔽性

1.3 Fallback机制详解

</> JSON
{
  "fallbacks": [
    {
      "dest": 8080,
      "xver": 1
    },
    {
      "path": "/websocket",
      "dest": 8443,
      "xver": 1
    }
  ]
}

Fallback工作原理:

</> PLAINTEXT
客户端请求 -> V2Ray监听443端口
           |
           ├─ 有效VLESS流量 -> 代理处理
           |
           └─ 非VLESS流量 -> 回落(Fallback)
                          |
                          ├─ 匹配path="/websocket" -> 转发到8443(VMess)
                          |
                          └─ 其他所有流量 -> 转发到8080(真实网站)

Fallback配置高级选项:

</> JSON
{
  "fallbacks": [
    {
      "name": "fallback-to-website",
      "dest": 8080,
      "xver": 1
    },
    {
      "name": "fallback-to-ws",
      "path": "/websocket",
      "dest": 8443,
      "xver": 1
    },
    {
      "name": "fallback-to-grpc",
      "path": "/grpcservice",
      "dest": 8444,
      "xver": 1
    },
    {
      "name": "fallback-by-alpn",
      "alpn": "h2",          // HTTP/2流量特殊处理
      "dest": 8081,
      "xver": 1
    }
  ]
}

xver参数详解:

</> PLAINTEXT
xver = 0: 不发送原始源地址
xver = 1: 发送PROXY protocol v1格式的源地址
xver = 2: 发送PROXY protocol v2格式的源地址

Nginx接收xver信息的配置:

</> NGINX
server {
    listen 8080 proxy_protocol;  # 启用proxy protocol
    server_name yourdomain.com;
    
    # 信任的代理地址
    set_real_ip_from 127.0.0.1;
    real_ip_header proxy_protocol;
    
    location / {
        # 现在$remote_addr是真实客户端IP
        access_log /var/log/nginx/access.log combined;
    }
}

1.4 TLS配置深度解析

</> JSON
{
  "streamSettings": {
    "network": "tcp",
    "security": "tls",
    "tlsSettings": {
      "serverName": "yourdomain.com",
      "alpn": ["http/1.1", "h2"],
      "certificates": [
        {
          "certificateFile": "/path/to/fullchain.crt",
          "keyFile": "/path/to/private.key"
        }
      ],
      "minVersion": "1.2",
      "maxVersion": "1.3",
      "cipherSuites": "TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256"
    }
  }
}

ALPN协议协商详解:

ALPN (Application-Layer Protocol Negotiation) 在TLS握手时协商应用层协议:

</> PLAINTEXT
配置: "alpn": ["http/1.1", "h2"]
含义: 优先使用HTTP/1.1,如果客户端支持则使用HTTP/2

为什么这样配置?
- 模拟真实浏览器行为
- Chrome/Firefox默认支持这两个协议
- 顺序很重要,体现浏览器特征

浏览器真实ALPN顺序示例:

</> PLAINTEXT
Chrome: h2, http/1.1
Firefox: h2, http/1.1
Safari: h2, http/1.1, spdy/3.1

TLS版本限制说明:

</> JSON
{
  "minVersion": "1.2",   // 最低TLS 1.2
  "maxVersion": "1.3"    // 最高TLS 1.3
}

原因分析:

  • TLS 1.0/1.1已被弃用,现代浏览器不再支持
  • TLS 1.2是当前最广泛支持的版本
  • TLS 1.3是最新标准,性能更好、更安全
  • 不支持过时版本避免兼容性漏洞

密码套件(Cipher Suites)详解:

</> PLAINTEXT
TLS_AES_128_GCM_SHA256
|   |   |   |   └─ 哈希算法: SHA256
|   |   |   └───── 认证加密模式: GCM
|   |   └───────── 对称加密: AES-128
|   └───────────── 密钥交换: (TLS 1.3中集成)
└───────────────── 协议版本: TLS 1.3

推荐的密码套件配置:

</> JSON
{
  // TLS 1.3套件(性能优先)
  "cipherSuites": "TLS_AES_128_GCM_SHA256:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_256_GCM_SHA384"
  
  // 兼容性优先(同时支持TLS 1.2)
  "cipherSuites": "TLS_AES_128_GCM_SHA256:TLS_CHACHA20_POLY1305_SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256"
}

各算法特点:

  • AES-128-GCM: 硬件加速支持好,速度快
  • ChaCha20-Poly1305: 移动设备上性能更好,无硬件加速时优于AES
  • AES-256-GCM: 安全性最高但性能稍低

第二部分:Nginx与V2Ray联动配置

2.1 Nginx前置服务器配置

</> NGINX
server {
    listen 8080;
    server_name yourdomain.com;
    root /var/www/html;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }

    location /robots.txt {
        return 200 "User-agent: *\nDisallow: /admin/\n";
    }

    location /favicon.ico {
        access_log off;
        log_not_found off;
    }
}

配置深度解读:

listen 8080原因:

</> PLAINTEXT
V2Ray监听443并做Fallback:
  非代理流量 -> 转发到127.0.0.1:8080 -> Nginx处理 -> 显示真实网站

这种架构的优势:
1. 外部只暴露443端口
2. 8080仅本地访问,更安全
3. V2Ray作为流量分发器,Nginx专注web服务

真实网站内容构建:

</> BASH
# 部署一个简单的静态博客
cd /var/www/html

# 使用Hugo生成静态网站
wget https://github.com/gohugoio/hugo/releases/download/v0.120.0/hugo_extended_0.120.0_linux-amd64.deb
dpkg -i hugo_*.deb
hugo new site myblog
cd myblog
git clone https://github.com/budparr/gohugo-theme-ananke.git themes/ananke
echo 'theme = "ananke"' >> config.toml

# 创建几篇文章
hugo new posts/first-post.md
hugo new posts/second-post.md

# 生成静态文件
hugo

# 复制到web根目录
cp -r public/* /var/www/html/

或使用更简单的方式:

</> BASH
# 克隆一个开源项目文档作为伪装
git clone https://github.com/microsoft/vscode-docs.git /var/www/html

robots.txt的重要性:

</> NGINX
location /robots.txt {
    return 200 "User-agent: *\nDisallow: /admin/\n";
}

真实网站通常都有robots.txt,其作用:

  1. 告诉搜索引擎爬虫哪些页面可以抓取
  2. 增加网站真实性
  3. 可以故意设置一些"禁止访问"的路径,更像真实站点

高级robots.txt配置:

</> NGINX
location = /robots.txt {
    return 200 "User-agent: *
Disallow: /admin/
Disallow: /private/
Disallow: /api/
Allow: /
Sitemap: https://yourdomain.com/sitemap.xml
Crawl-delay: 10
";
    add_header Content-Type text/plain;
}

其他增强真实性的配置:

</> NGINX
server {
    listen 8080;
    server_name yourdomain.com;
    root /var/www/html;
    index index.html;

    # 添加安全头(真实网站标配)
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header Referrer-Policy "no-referrer-when-downgrade" always;

    # Gzip压缩(提升性能和真实性)
    gzip on;
    gzip_vary on;
    gzip_min_length 1024;
    gzip_types text/plain text/css text/xml text/javascript 
               application/x-javascript application/xml+rss 
               application/javascript application/json;

    # 缓存静态资源
    location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg|woff|woff2|ttf|eot)$ {
        expires 1y;
        add_header Cache-Control "public, immutable";
    }

    # sitemap.xml
    location = /sitemap.xml {
        root /var/www/html;
        default_type application/xml;
    }

    # 404页面
    error_page 404 /404.html;
    location = /404.html {
        internal;
    }

    # 500错误页面
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        internal;
    }

    # 访问日志
    access_log /var/log/nginx/yourdomain.access.log combined;
    error_log /var/log/nginx/yourdomain.error.log warn;
}

2.2 WebSocket配置详解

</> NGINX
location /ray {
    proxy_redirect off;
    proxy_pass http://127.0.0.1:10000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

WebSocket代理关键参数:

proxy_http_version 1.1:

</> PLAINTEXT
WebSocket协议基于HTTP/1.1
必须明确指定,否则Nginx默认使用HTTP/1.0
HTTP/1.0不支持WebSocket升级

Upgrade和Connection头:

</> PLAINTEXT
WebSocket握手过程:
1. 客户端发送HTTP请求,包含:
   Upgrade: websocket
   Connection: Upgrade

2. 服务器响应101 Switching Protocols

3. 连接升级为WebSocket

Nginx必须正确传递这些头才能完成握手

完整的WebSocket代理配置:

</> NGINX
location /ray {
    # 基础代理设置
    proxy_pass http://127.0.0.1:10000;
    proxy_redirect off;
    
    # HTTP版本
    proxy_http_version 1.1;
    
    # WebSocket升级头
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    
    # 主机和IP信息
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    
    # 超时设置(WebSocket长连接)
    proxy_connect_timeout 60s;
    proxy_send_timeout 3600s;  # 1小时
    proxy_read_timeout 3600s;  # 1小时
    
    # 缓冲设置
    proxy_buffering off;  # 禁用缓冲,减少延迟
    proxy_buffer_size 4k;
    
    # 错误处理
    proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
}

路径选择策略:

</> NGINX
# 不好的路径(容易被识别)
location /ws { ... }
location /v2ray { ... }
location /proxy { ... }

# 较好的路径(模拟真实API)
location /api/v1/chat { ... }
location /ws/notifications { ... }
location /stream/events { ... }

# 最佳实践(完全随机或有意义)
location /8f3b2a1c { ... }  # 随机字符
location /resources/fonts { ... }  # 伪装成资源路径

第三部分:高级路由与分流配置

3.1 智能分流规则详解

</> JSON
{
  "routing": {
    "domainStrategy": "IPIfNonMatch",
    "rules": [
      {
        "type": "field",
        "domain": ["geosite:cn"],
        "outboundTag": "direct"
      },
      {
        "type": "field",
        "ip": ["geoip:cn", "geoip:private"],
        "outboundTag": "direct"
      }
    ]
  }
}

domainStrategy参数详解:

</> PLAINTEXT
可选值:
1. AsIs: 使用域名原样匹配
   - 快速但可能DNS泄漏
   
2. IPIfNonMatch: 域名匹配失败时解析IP再匹配
   - 推荐选项,平衡性能和准确性
   
3. IPOnDemand: 优先尝试域名匹配,必要时解析IP
   - 类似IPIfNonMatch但更智能
   
4. UseIP: 始终解析域名为IP再匹配
   - 最准确但DNS开销大

工作流程示例:

</> PLAINTEXT
用户访问: example.com

AsIs模式:
  检查域名规则 -> 匹配/不匹配 -> 决定出站

IPIfNonMatch模式:
  检查域名规则 -> 不匹配 -> 解析IP -> 检查IP规则 -> 决定出站

UseIP模式:
  解析域名为IP -> 检查IP规则 -> 决定出站

geosite和geoip数据库:

</> BASH
# 查看数据库信息
/usr/local/bin/v2ray run -test -config /usr/local/etc/v2ray/config.json

# 数据库文件位置
/usr/local/share/v2ray/geoip.dat     # IP地址数据库
/usr/local/share/v2ray/geosite.dat   # 域名数据库

# 手动更新数据库
cd /usr/local/share/v2ray
wget -O geoip.dat https://github.com/v2fly/geoip/releases/latest/download/geoip.dat
wget -O geosite.dat https://github.com/v2fly/domain-list-community/releases/latest/download/dlc.dat

3.2 复杂路由规则示例

按应用分流:

</> JSON
{
  "routing": {
    "domainStrategy": "IPIfNonMatch",
    "rules": [
      {
        "type": "field",
        "domain": [
          "geosite:category-ads-all"  // 广告域名
        ],
        "outboundTag": "block"
      },
      {
        "type": "field",
        "domain": [
          "geosite:cn",               // 国内网站
          "geosite:apple-cn",         // Apple中国服务
          "geosite:google-cn",        // Google中国服务
          "geosite:steam@cn"          // Steam国内CDN
        ],
        "outboundTag": "direct"
      },
      {
        "type": "field",
        "domain": [
          "geosite:netflix",          // Netflix
          "geosite:disney",           // Disney+
          "geosite:hbo",              // HBO
          "geosite:youtube"           // YouTube
        ],
        "outboundTag": "proxy-streaming"  // 专用流媒体节点
      },
      {
        "type": "field",
        "domain": [
          "geosite:openai",           // ChatGPT
          "geosite:anthropic",        // Claude
          "geosite:github"            // GitHub
        ],
        "outboundTag": "proxy-fast"       // 快速节点
      },
      {
        "type": "field",
        "ip": [
          "geoip:cn",
          "geoip:private"
        ],
        "outboundTag": "direct"
      },
      {
        "type": "field",
        "port": "0-65535",
        "outboundTag": "proxy"        // 默认走代理
      }
    ]
  },
  "outbounds": [
    {
      "tag": "proxy",
      "protocol": "vless",
      "settings": { /* 通用代理配置 */ }
    },
    {
      "tag": "proxy-streaming",
      "protocol": "vless",
      "settings": { /* 流媒体专用节点 */ }
    },
    {
      "tag": "proxy-fast",
      "protocol": "vless",
      "settings": { /* 低延迟节点 */ }
    },
    {
      "tag": "direct",
      "protocol": "freedom"
    },
    {
      "tag": "block",
      "protocol": "blackhole"
    }
  ]
}

按时间分流(工作时间和非工作时间使用不同节点):

这需要结合外部脚本实现:

</> BASH
#!/bin/bash
# /usr/local/bin/switch-v2ray-config.sh

HOUR=$(date +%H)
WORK_CONFIG="/usr/local/etc/v2ray/config-work.json"
HOME_CONFIG="/usr/local/etc/v2ray/config-home.json"
ACTIVE_CONFIG="/usr/local/etc/v2ray/config.json"

if [ $HOUR -ge 9 ] && [ $HOUR -lt 18 ]; then
    # 工作时间(9:00-18:00)使用工作配置
    cp $WORK_CONFIG $ACTIVE_CONFIG
else
    # 非工作时间使用家用配置
    cp $HOME_CONFIG $ACTIVE_CONFIG
fi

systemctl reload v2ray

添加定时任务:

</> BASH
crontab -e
# 每小时检查一次
0 * * * * /usr/local/bin/switch-v2ray-config.sh

3.3 负载均衡配置

多出站负载均衡:

</> JSON
{
  "outbounds": [
    {
      "tag": "balancer",
      "protocol": "freedom",
      "settings": {}
    },
    {
      "tag": "proxy1",
      "protocol": "vless",
      "settings": {
        "vnext": [{
          "address": "server1.example.com",
          "port": 443,
          "users": [{ "id": "uuid1", "flow": "xtls-rprx-vision" }]
        }]
      }
    },
    {
      "tag": "proxy2",
      "protocol": "vless",
      "settings": {
        "vnext": [{
          "address": "server2.example.com",
          "port": 443,
          "users": [{ "id": "uuid2", "flow": "xtls-rprx-vision" }]
        }]
      }
    }
  ],
  "balancers": [
    {
      "tag": "proxy-balancer",
      "selector": ["proxy1", "proxy2"],
      "strategy": {
        "type": "random"  // 或 "leastPing"
      }
    }
  ],
  "routing": {
    "balancers": [
      {
        "tag": "proxy-balancer",
        "selector": ["proxy1", "proxy2"]
      }
    ],
    "rules": [
      {
        "type": "field",
        "network": "tcp,udp",
        "balancerTag": "proxy-balancer"
      }
    ]
  }
}

负载均衡策略:

  • random: 随机选择
  • leastPing: 选择延迟最低的(需要observatory)
  • roundRobin: 轮询

配置Observatory实现智能选择:

</> JSON
{
  "observatory": {
    "subjectSelector": ["proxy1", "proxy2"],
    "probeURL": "https://www.google.com/generate_204",
    "probeInterval": "1m"
  },
  "balancers": [
    {
      "tag": "proxy-balancer",
      "selector": ["proxy1", "proxy2"],
      "strategy": {
        "type": "leastPing"
      }
    }
  ]
}

工作原理:

  1. Observatory每分钟探测所有出站节点
  2. 记录响应时间和成功率
  3. leastPing策略自动选择最快的节点
  4. 节点故障时自动切换

第四部分:安全加固与监控

4.1 防火墙配置详解

</> BASH
ufw default deny incoming
ufw default allow outgoing
ufw allow 22/tcp
ufw allow 443/tcp
ufw enable

高级UFW规则:

</> BASH
# 限制SSH连接速率(防暴力破解)
ufw limit 22/tcp

# 允许特定IP访问SSH
ufw allow from 1.2.3.4 to any port 22

# 允许特定IP段
ufw allow from 192.168.1.0/24 to any port 22

# 记录被拒绝的连接
ufw logging on
ufw logging medium

# 查看规则
ufw status numbered

# 删除规则
ufw delete 3

# 重置所有规则
ufw reset

使用iptables实现更精细控制:

</> BASH
# 限制单个IP的连接数
iptables -A INPUT -p tcp --dport 443 -m connlimit --connlimit-above 10 -j REJECT

# 防止SYN flood攻击
iptables -A INPUT -p tcp --syn -m limit --limit 1/s --limit-burst 3 -j ACCEPT
iptables -A INPUT -p tcp --syn -j DROP

# 记录可疑连接
iptables -A INPUT -p tcp --dport 443 -m recent --name portscan --rcheck --seconds 60 --hitcount 20 -j LOG --log-prefix "Port Scan: "
iptables -A INPUT -p tcp --dport 443 -m recent --name portscan --rcheck --seconds 60 --hitcount 20 -j DROP

# 保存规则(Ubuntu/Debian)
iptables-save > /etc/iptables/rules.v4

# 设置开机自动加载
apt install iptables-persistent

4.2 Fail2ban配置

</> BASH
# 安装
apt install fail2ban

# V2Ray专用配置
cat > /etc/fail2ban/filter.d/v2ray.conf << 'EOF'
[Definition]
failregex = rejected.* from <HOST>:\d+
ignoreregex =
EOF

# Jail配置
cat > /etc/fail2ban/jail.d/v2ray.conf << 'EOF'
[v2ray]
enabled = true
port = 443
filter = v2ray
logpath = /var/log/v2ray/access.log
maxretry = 5
findtime = 600
bantime = 3600
action = iptables-allports[name=v2ray, protocol=all]
EOF

# 重启服务
systemctl restart fail2ban

# 查看状态
fail2ban-client status v2ray

4.3 系统监控脚本

完整监控脚本:

</> BASH
#!/bin/bash
# /usr/local/bin/v2ray-monitor.sh

# 配置
LOG_FILE="/var/log/v2ray-monitor.log"
ALERT_EMAIL="your@email.com"
MAX_CPU=80
MAX_MEM=80
MAX_CONN=1000

# 记录日志函数
log_message() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> $LOG_FILE
}

# 发送告警
send_alert() {
    echo "$1" | mail -s "V2Ray Alert" $ALERT_EMAIL
    log_message "ALERT: $1"
}

# 检查V2Ray服务状态
check_service() {
    if ! systemctl is-active --quiet v2ray; then
        send_alert "V2Ray service is down, attempting restart"
        systemctl restart v2ray
        sleep 5
        if systemctl is-active --quiet v2ray; then
            log_message "V2Ray service restarted successfully"
        else
            send_alert "Failed to restart V2Ray service"
        fi
    fi
}

# 检查资源使用
check_resources() {
    CPU=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | cut -d'%' -f1)
    MEM=$(free | grep Mem | awk '{printf("%.0f", $3/$2 * 100.0)}')
    
    if (( $(echo "$CPU > $MAX_CPU" | bc -l) )); then
        send_alert "High CPU usage: ${CPU}%"
    fi
    
    if [ $MEM -gt $MAX_MEM ]; then
        send_alert "High memory usage: ${MEM}%"
    fi
}

# 检查连接数
check_connections() {
    CONN_COUNT=$(ss -tn | grep :443 | wc -l)
    
    if [ $CONN_COUNT -gt $MAX_CONN ]; then
        send_alert "High connection count: $CONN_COUNT"
    fi
    
    log_message "Current connections: $CONN_COUNT"
}

# 检查证书有效期
check_cert() {
    CERT_FILE="/path/to/fullchain.crt"
    DAYS_LEFT=$(( ($(date -d "$(openssl x509 -enddate -noout -in $CERT_FILE | cut -d= -f2)" +%s) - $(date +%s)) / 86400 ))
    
    if [ $DAYS_LEFT -lt 30 ]; then
        send_alert "SSL certificate expires in $DAYS_LEFT days"
    fi
    
    log_message "Certificate valid for $DAYS_LEFT more days"
}

# 检查磁盘空间
check_disk() {
    DISK_USAGE=$(df / | tail -1 | awk '{print $5}' | sed 's/%//')
    
    if [ $DISK_USAGE -gt 80 ]; then
        send_alert "Disk usage is ${DISK_USAGE}%"
    fi
}

# 执行所有检查
main() {
    log_message "Starting monitoring check"
    check_service
    check_resources
    check_connections
    check_cert
    check_disk
    log_message "Monitoring check completed"
}

main

设置定时任务:

</> BASH
crontab -e
# 每5分钟检查一次
*/5 * * * * /usr/local/bin/v2ray-monitor.sh

4.4 流量分析与异常检测

</> BASH
#!/bin/bash
# /usr/local/bin/traffic-analyzer.sh

# 分析连接时长
analyze_duration() {
    echo "=== Connection Duration Analysis ==="
    awk '/accepted/ {
        start=$1" "$2
        id=$NF
    }
    /closed/ && $NF==id {
        cmd="date -d \""start"\" +%s"
        cmd | getline start_ts
        close(cmd)
        
        cmd="date -d \""$1" "$2"\" +%s"
        cmd | getline end_ts
        close(cmd)
        
        duration = end_ts - start_ts
        print duration
    }' /var/log/v2ray/access.log | awk '{
        sum+=$1
        count++
        if($1 > max) max=$1
        if(min=="" || $1 < min) min=$1
    }
    END {
        print "Average:", sum/count, "seconds"
        print "Max:", max, "seconds"
        print "Min:", min, "seconds"
    }'
}

# 分析访问的域名Top 10
analyze_domains() {
    echo "=== Top 10 Accessed Domains ==="
    grep "accepted" /var/log/v2ray/access.log | \
    awk -F'domain:' '{print $2}' | \
    awk '{print $1}' | \
    sort | uniq -c | \
    sort -rn | \
    head -10
}

# 分析客户端IP
analyze_clients() {
    echo "=== Client IP Statistics ==="
    grep "accepted" /var/log/v2ray/access.log | \
    awk '{print $NF}' | \
    cut -d':' -f1 | \
    sort | uniq -c | \
    sort -rn
}

# 检测异常流量
detect_anomalies() {
    echo "=== Anomaly Detection ==="
    
    # 检测高频连接(可能是扫描)
    THRESHOLD=100
    HIGH_FREQ=$(grep "accepted" /var/log/v2ray/access.log | \
                awk '{print $NF}' | cut -d':' -f1 | \
                sort | uniq -c | sort -rn | \
                awk -v t=$THRESHOLD '$1 > t {print}')
    
    if [ ! -z "$HIGH_FREQ" ]; then
        echo "High frequency connections detected:"
        echo "$HIGH_FREQ"
    fi
    
    # 检测异常大的传输
    LARGE_TRANSFER=$(grep "traffic" /var/log/v2ray/access.log | \
                     awk '{if($NF > 1073741824) print}')  # >1GB
    
    if [ ! -z "$LARGE_TRANSFER" ]; then
        echo "Large data transfers detected:"
        echo "$LARGE_TRANSFER"
    fi
}

# 生成日报告
generate_report() {
    REPORT_FILE="/var/log/v2ray/daily-report-$(date +%Y%m%d).txt"
    
    {
        echo "V2Ray Daily Report - $(date)"
        echo "=================================="
        echo ""
        analyze_duration
        echo ""
        analyze_domains
        echo ""
        analyze_clients
        echo ""
        detect_anomalies
    } > $REPORT_FILE
    
    echo "Report generated: $REPORT_FILE"
}

case "$1" in
    duration)
        analyze_duration
        ;;
    domains)
        analyze_domains
        ;;
    clients)
        analyze_clients
        ;;
    anomalies)
        detect_anomalies
        ;;
    report)
        generate_report
        ;;
    *)
        echo "Usage: $0 {duration|domains|clients|anomalies|report}"
        exit 1
        ;;
esac

第五部分:CDN集成与优化

5.1 Cloudflare完整配置

DNS设置:

</> PLAINTEXT
类型  名称           内容                  代理状态  TTL
A     yourdomain    1.2.3.4(你的服务器IP)  已代理    自动
A     www           1.2.3.4                已代理    自动
AAAA  yourdomain    2001:db8::1(IPv6)      已代理    自动

SSL/TLS设置:

</> PLAINTEXT
SSL/TLS加密模式: 完全(严格)

为什么选择"完全(严格)":
- 关闭: 不安全,不推荐
- 灵活: Cloudflare到用户加密,Cloudflare到源服务器不加密
- 完全: 全程加密,但接受自签名证书
- 完全(严格): 全程加密,必须是有效的证书 ✓

最低TLS版本: TLS 1.2
TLS 1.3: 启用
自动HTTPS重写: 启用
始终使用HTTPS: 关闭(避免回环)

Page Rules优化:

</> PLAINTEXT
规则1: 绕过缓存
URL: yourdomain.com/ray*
设置:
- 缓存级别: 绕过
- 安全级别: 中
- 浏览器完整性检查: 关闭

规则2: 静态资源缓存
URL: yourdomain.com/*.(jpg|jpeg|png|gif|css|js|ico|svg|woff|woff2)
设置:
- 缓存级别: 缓存所有内容
- 边缘缓存TTL: 1个月
- 浏览器缓存TTL: 1天

规则3: 强制HTTPS(主站)
URL: http://yourdomain.com/*
设置:
- 始终使用HTTPS: 开

防火墙规则:

</> PLAINTEXT
规则1: 阻止已知恶意IP
(cf.threat_score gt 14)
动作: 质询

规则2: 速率限制
(http.request.uri.path contains "/ray")
速率: 60请求/分钟
动作: 超过时阻止

规则3: 地理位置限制(可选)
(ip.geoip.country in {"CN" "RU"})
动作: JS质询或阻止

5.2 自建CDN节点

对于需要更高控制权的场景:

使用多台服务器构建CDN:

</> PLAINTEXT
架构:
用户 -> 就近接入点 -> 中转节点 -> 出口节点

接入点配置(多个地理位置):
</> JSON
{
  "inbounds": [{
    "port": 443,
    "protocol": "vless",
    "settings": {
      "clients": [{"id": "uuid", "flow": "xtls-rprx-vision"}],
      "decryption": "none"
    }
  }],
  "outbounds": [{
    "protocol": "vmess",
    "settings": {
      "vnext": [{
        "address": "relay.example.com",  // 中转节点
        "port": 10000,
        "users": [{"id": "relay-uuid"}]
      }]
    }
  }]
}

使用GeoDNS实现就近接入:

</> PLAINTEXT
安装PowerDNS Recursor:
apt install pdns-recursor

配置lua脚本实现GeoDNS:
</> LUA
-- /etc/powerdns/recursor.lua
function preresolve(dq)
    if dq.qname:equal("proxy.example.com") then
        local clientIP = dq.remoteaddr:toString()
        
        -- 简单的地理判断(实际应使用GeoIP库)
        if clientIP:match("^1%.") or clientIP:match("^2%.") then
            -- 亚洲IP
            dq:addAnswer(pdns.A, "asia-node.example.com")
        elseif clientIP:match("^3%.") then
            -- 欧洲IP
            dq:addAnswer(pdns.A, "eu-node.example.com")
        else
            -- 默认美洲节点
            dq:addAnswer(pdns.A, "us-node.example.com")
        end
        return true
    end
    return false
end

结语

通过以上详细配置和解析,我们涵盖了V2Ray节点构建的完整技术栈:

  1. 协议层: VLESS、VMess、Trojan的深度配置
  2. 传输层: WebSocket、gRPC、TCP等传输方式
  3. 安全层: TLS/XTLS配置、证书管理
  4. 路由层: 智能分流、负载均衡
  5. 运维层: 监控、日志、安全加固

这些技术的掌握需要理论和实践相结合。建议从简单配置开始,逐步增加复杂度,在实际使用中不断优化和调整。

重要提醒: 这些技术知识应当用于合法目的,如保护个人隐私、企业安全通信等。使用时务必遵守当地法律法规,不得用于非法用途。技术是中立的,但使用者需要对自己的行为负责。

评论

搜索站内内容

输入关键词开始搜索