一键安装v2ray

 

有时需要自己搭建v2ray服务器, 可通过一键安装脚本进行安装配置

参考v2ray-agent

一键安装脚本

wget -P /root -N --no-check-certificate "https://raw.githubusercontent.com/mack-a/v2ray-agent/master/install.sh" && chmod 700 /root/install.sh && /root/install.sh

安装之后会生成vasma命令, 方便后续使用

支持的安装类型

  • VLESS+TCP+TLS
  • VLESS+TCP+xtls-rprx-direct【推荐
  • VLESS+gRPC+TLS【支持CDN、IPv6、延迟低】
  • VLESS+WS+TLS【支持CDN、IPv6】
  • Trojan+TCP+TLS【推荐
  • Trojan+TCP+xtls-rprx-direct【推荐
  • Trojan+gRPC+TLS【支持CDN、IPv6、延迟低】
  • VMess+WS+TLS【支持CDN、IPv6】

脚本功能介绍

xray开启流量统计

添加/etc/v2ray-agent/xray/conf/20-stats.json

参考多用户和流量统计 配置文件写法

{
    "api": {
        "tag": "api",
        "services": [
            "StatsService"
        ]
    },
    "routing": {
        "settings": {
          "rules": [
            {
              "inboundTag": [
                "api"
              ],
              "outboundTag": "api",
              "type": "field"
            }
          ]
        },
        "strategy": "rules"
      },
    "inbounds": [
        {
            "listen": "127.0.0.1",
            "port": 10085,
            "protocol": "dokodemo-door",
            "settings": {
                "address": "127.0.0.1"
            },
            "tag": "api"
        }
    ],
    "policy": {
        "levels": {
            "0": {
                "statsUserUplink": true,
                "statsUserDownlink": true
            }
        },
        "system": {
            "statsInboundUplink": true,
            "statsInboundDownlink": true,
            "statsOutboundUplink": true,
            "statsOutboundDownlink": true
        }
    },
    "stats": {}
}

统计脚本

traffic.sh

#!/bin/bash

_APISERVER=127.0.0.1:10085
_V2CTL=/etc/v2ray-agent/xray/xray

apidata () {
    local ARGS=
    if [[ $1 == "reset" ]]; then
      ARGS="-reset"
    fi
    $_V2CTL api statsquery --server=$_APISERVER "${ARGS}" \
    | awk '{
        if (match($1, /"name":/)) {
            f=1; gsub(/^"|link"|,$/, "", $2);
            split($2, p,  ">>>");
            printf "%s:%s->%s\t", p[1],p[2],p[4];
        }
        else if (match($1, /"value":/) && f){ f = 0; printf "%.0f\n", $2; }
        else if (match($0, /}/) && f) { f = 0; print 0; }
    }'
}

print_sum() {
    local DATA="$1"
    local PREFIX="$2"
    local SORTED=$(echo "$DATA" | grep "^${PREFIX}" | sort -r)
    local SUM=$(echo "$SORTED" | awk '
        /->up/{us+=$2}
        /->down/{ds+=$2}
        END{
            printf "SUM->up:\t%.0f\nSUM->down:\t%.0f\nSUM->TOTAL:\t%.0f\n", us, ds, us+ds;
        }')
    echo -e "${SORTED}\n${SUM}" \
    | numfmt --field=2 --suffix=B --to=iec \
    | column -t
}

DATA=$(apidata $1)
echo "------------Inbound----------"
print_sum "$DATA" "inbound"
echo "-----------------------------"
echo "------------Outbound----------"
print_sum "$DATA" "outbound"
echo "-----------------------------"
echo
echo "-------------User------------"
print_sum "$DATA" "user"
echo "-----------------------------"

修改文件权限

chmod 777 traffic.sh
# 查看统计
./traffic.sh

参考