From b67a47f5fe3f3842b51ca98ee59d036724976912 Mon Sep 17 00:00:00 2001 From: chunyu Date: Wed, 21 May 2025 12:44:42 +0800 Subject: [PATCH] =?UTF-8?q?refactor(vps=5Fmanager):=20=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E7=9A=84=E7=BB=93=E7=AE=97=E8=B4=A6=E5=8F=B7?= =?UTF-8?q?=E6=9F=A5=E7=9C=8B=E9=80=BB=E8=BE=91=E5=B9=B6=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E8=84=9A=E6=9C=AC=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 移除vps_manager.sh中重复的结算账号查看逻辑,简化代码结构。优化启动脚本处理逻辑,改为使用本地/opt/gcloud/startup-script.sh文件,避免远程下载失败的问题。 --- enablesshandcreatesocks5.sh | 15 ++ vps_manager.sh | 279 +++++++----------------------------- 2 files changed, 70 insertions(+), 224 deletions(-) diff --git a/enablesshandcreatesocks5.sh b/enablesshandcreatesocks5.sh index cf57e2a..615bb72 100644 --- a/enablesshandcreatesocks5.sh +++ b/enablesshandcreatesocks5.sh @@ -37,6 +37,21 @@ mkdir -p /root/.ssh chmod 700 /root/.ssh touch /root/.ssh/authorized_keys chmod 600 /root/.ssh/authorized_keys + +# 添加公钥到authorized_keys +PUBLIC_KEYS=( + "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCk1UYH6SmDtRKwnEt2iJiTC/Si3HlMYwzDG9FlMNQNLQ9g8AOK1ZLDQgUjM+eugMVugLPz8aFT8waSV9QDudU+epRAsczIfd7pHKaApWSWo55oTHwzjt8kb7JY3XvcnqVb55wbwQWQiMpIyj4q8fBmJCCeMWLtIS4c68KhSg4ihz6YOQpuDtDclWXEByr1C1i0MQ7ymwhjJazrN3LThTATTqoP5Ho3b2FEuZcBaSRIQrDBWJYVzl15Fbq0RfQaleudl18j7BUN/1/SHUcyUbTb5H4XkHiLQhOutf+mMqX0wZPSOy6q+GRP8Fi3bKHFXR/6+/HIyz0ocx9FQY5ir46v chunyu.he20@tendcloud.com" + "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDVw/Lamb8wHXeLgCKGbumrocMvq+a6goVFBAuhYk/TVUoislrO1SrrH5YMFc7aQMZNP/mbubirIck8h0wT8hiU070OHO7HuaAyIGgFh4icIX/m7znhvWteG/evxJUN95ZWm4bk+UmGUbbAO4BkSEGub/ENJ3RGR9eJuDabgMha5fyzl9J9sm6jDeXVyGtLOy9NkYYHo/J0kUAwK1YOQ88rAXIhsJ04qsH7256VAdo6enO39Y0RG4NhK3hlRYP46f8NWyCaJFbcz4tpbHNdG9Xbqg7j/RSn7tO5bpB283m/wsZR7kM28x9dzyojJJYycEn9CTUzBjxcBBuKNa57Y+eoBo7q2KXx13ziMvO5k7Bl8GXknl0uguf49hjbPS95CThns+sqz7G3Px8a79BJ1rHEewlmMMJUa/kRY+NAcum0nVkWzZIpR6I2KWMP+8OaJLj97vIHgGydRP8y4I6IiiZBlOlshNJ3iI/XxsSWjWjdWxSHUZtHj4L1IaxclrX+QtU= root@gc-hk.asia-east2-c.c.annular-bucksaw-448504-h3.internal" +) + +# 将公钥添加到authorized_keys文件 +for key in "${PUBLIC_KEYS[@]}"; do + if ! grep -q "$key" /root/.ssh/authorized_keys; then + echo "$key" >> /root/.ssh/authorized_keys + fi +done + + # 重启 SSH 服务 systemctl restart sshd if [ $? -eq 0 ]; then diff --git a/vps_manager.sh b/vps_manager.sh index 2c87b4d..8284352 100644 --- a/vps_manager.sh +++ b/vps_manager.sh @@ -488,19 +488,6 @@ while true; do echo "按任意键返回菜单..." read -e -r -n 1 ;; - 16) - echo "正在查看所有可用的结算账号..." - if get_billing_accounts; then - echo "可用的结算账号列表:" - for item in "${billing_account_array_display[@]}"; do - echo "$item" - done - else - echo "无法获取结算账号列表。" - fi - echo "按任意键返回菜单..." - read -e -r -n 1 - ;; 2) echo "正在删除账号..." account_data=$(get_account_list) @@ -587,19 +574,6 @@ while true; do echo "按任意键返回菜单..." read -e -r -n 1 ;; - 16) - echo "正在查看所有可用的结算账号..." - if get_billing_accounts; then - echo "可用的结算账号列表:" - for item in "${billing_account_array_display[@]}"; do - echo "$item" - done - else - echo "无法获取结算账号列表。" - fi - echo "按任意键返回菜单..." - read -e -r -n 1 - ;; 3) echo "正在查看所有账号..." account_data=$(get_account_list) @@ -612,19 +586,6 @@ while true; do echo "按任意键返回菜单..." read -e -r -n 1 ;; - 16) - echo "正在查看所有可用的结算账号..." - if get_billing_accounts; then - echo "可用的结算账号列表:" - for item in "${billing_account_array_display[@]}"; do - echo "$item" - done - else - echo "无法获取结算账号列表。" - fi - echo "按任意键返回菜单..." - read -e -r -n 1 - ;; 4) echo "正在切换默认账号..." account_data=$(get_account_list) @@ -663,19 +624,6 @@ while true; do echo "按任意键返回菜单..." read -e -r -n 1 ;; - 16) - echo "正在查看所有可用的结算账号..." - if get_billing_accounts; then - echo "可用的结算账号列表:" - for item in "${billing_account_array_display[@]}"; do - echo "$item" - done - else - echo "无法获取结算账号列表。" - fi - echo "按任意键返回菜单..." - read -e -r -n 1 - ;; 5) echo "正在添加项目 (并关联结算账号)..." echo "请输入项目 ID(6-30 个字符,小写字母开头,可包含小写字母、数字或连字符,例如 my-project-123):" @@ -812,19 +760,6 @@ while true; do echo "按任意键返回菜单..." read -e -r -n 1 ;; - 16) - echo "正在查看所有可用的结算账号..." - if get_billing_accounts; then - echo "可用的结算账号列表:" - for item in "${billing_account_array_display[@]}"; do - echo "$item" - done - else - echo "无法获取结算账号列表。" - fi - echo "按任意键返回菜单..." - read -e -r -n 1 - ;; 6) echo "正在删除项目..." project_data=$(get_project_list) @@ -911,19 +846,6 @@ while true; do echo "按任意键返回菜单..." read -e -r -n 1 ;; - 16) - echo "正在查看所有可用的结算账号..." - if get_billing_accounts; then - echo "可用的结算账号列表:" - for item in "${billing_account_array_display[@]}"; do - echo "$item" - done - else - echo "无法获取结算账号列表。" - fi - echo "按任意键返回菜单..." - read -e -r -n 1 - ;; 7) echo "正在查看所有项目..." project_data=$(get_project_list) @@ -936,19 +858,6 @@ while true; do echo "按任意键返回菜单..." read -e -r -n 1 ;; - 16) - echo "正在查看所有可用的结算账号..." - if get_billing_accounts; then - echo "可用的结算账号列表:" - for item in "${billing_account_array_display[@]}"; do - echo "$item" - done - else - echo "无法获取结算账号列表。" - fi - echo "按任意键返回菜单..." - read -e -r -n 1 - ;; 8) echo "正在切换默认项目..." project_data=$(get_project_list) @@ -987,19 +896,6 @@ while true; do echo "按任意键返回菜单..." read -e -r -n 1 ;; - 16) - echo "正在查看所有可用的结算账号..." - if get_billing_accounts; then - echo "可用的结算账号列表:" - for item in "${billing_account_array_display[@]}"; do - echo "$item" - done - else - echo "无法获取结算账号列表。" - fi - echo "按任意键返回菜单..." - read -e -r -n 1 - ;; 9) echo "正在查看虚拟机列表..." current_project=$(gcloud config get-value project) @@ -1020,19 +916,6 @@ while true; do echo "按任意键返回菜单..." read -e -r -n 1 ;; - 16) - echo "正在查看所有可用的结算账号..." - if get_billing_accounts; then - echo "可用的结算账号列表:" - for item in "${billing_account_array_display[@]}"; do - echo "$item" - done - else - echo "无法获取结算账号列表。" - fi - echo "按任意键返回菜单..." - read -e -r -n 1 - ;; 10) echo "正在创建虚拟机..." current_project=$(gcloud config get-value project) @@ -1054,46 +937,61 @@ while true; do continue fi # 下载启动脚本 - echo "正在从远程地址下载启动脚本..." - script_url="https://github.com/Lsmoisu/Toolbox/raw/refs/heads/main/enablesshandcreatesocks5.sh" - if ! command -v curl &> /dev/null; then - echo "curl 未安装,尝试安装..." - if command -v apt-get &> /dev/null; then - sudo apt-get update && sudo apt-get install -y curl - elif command -v yum &> /dev/null; then - sudo yum install -y curl - else - echo "无法安装 curl,请手动安装后重试。" - echo "按任意键返回菜单..." - read -e -r -n 1 - continue - fi - fi - - # 使用 -L 参数跟随重定向,-sS 减少输出,-f 失败时返回错误码 - if curl -L -sS -f -o startup-script.sh "$script_url"; then - echo "启动脚本下载成功!" - # 检查文件是否为空 - if [ -s startup-script.sh ]; then - echo "脚本内容非空,下载验证通过。" - chmod +x startup-script.sh - else - echo "错误:下载的脚本文件为空,请检查URL内容或网络连接。" - rm -f startup-script.sh - echo "按任意键返回菜单..." - read -e -r -n 1 - continue - fi - else - echo "启动脚本下载失败,请检查网络连接或URL是否正确。" - echo "错误信息:" - curl -L -sS "$script_url" -o /dev/null -w "%{http_code}\n" - rm -f startup-script.sh - echo "按任意键返回菜单..." - read -e -r -n 1 - continue - fi +# echo "正在从远程地址下载启动脚本..." +# script_url="https://github.com/Lsmoisu/Toolbox/raw/refs/heads/main/#enablesshandcreatesocks5.sh" +# if ! command -v curl &> /dev/null; then +# echo "curl 未安装,尝试安装..." +# if command -v apt-get &> /dev/null; then +# sudo apt-get update && sudo apt-get install -y curl +# elif command -v yum &> /dev/null; then +# sudo yum install -y curl +# else +# echo "无法安装 curl,请手动安装后重试。" +# echo "按任意键返回菜单..." +# read -e -r -n 1 +# continue +# fi +# fi +# +# # 使用 -L 参数跟随重定向,-sS 减少输出,-f 失败时返回错误码 +# if curl -L -sS -f -o startup-script.sh "$script_url"; then +# echo "启动脚本下载成功!" +# # 检查文件是否为空 +# if [ -s startup-script.sh ]; then +# echo "脚本内容非空,下载验证通过。" +# chmod +x startup-script.sh +# else +# echo "错误:下载的脚本文件为空,请检查URL内容或网络连接。" +# rm -f startup-script.sh +# echo "按任意键返回菜单..." +# read -e -r -n 1 +# continue +# fi +# else +# echo "启动脚本下载失败,请检查网络连接或URL是否正确。" +# echo "错误信息:" +# curl -L -sS "$script_url" -o /dev/null -w "%{http_code}\n" +# rm -f startup-script.sh +# echo "按任意键返回菜单..." +# read -e -r -n 1 +# continue +# fi + #检测初始化脚本 + if [ ! -f "/opt/gcloud/startup-script.sh" ]; then + echo "未找到初始化脚本 startup-script.sh,请确保脚本位于当前目录。" + echo "按任意键返回菜单..." + read -e -r -n 1 + fi + if [ -s /opt/gcloud/startup-script.sh ]; then + echo "脚本内容非空,验证通过。" + chmod +x /opt/gcloud/startup-script.sh + else + echo "错误:初始化脚本检查失败,请检查/opt/gcloud/startup-script.sh" + echo "按任意键返回菜单..." + read -e -r -n 1 + continue + fi region_location_map["us-east7"]="美国弗吉尼亚州" region_location_map["us-west1"]="美国俄勒冈州" region_location_map["us-west2"]="美国加利福尼亚州洛杉矶" @@ -1320,8 +1218,7 @@ while true; do --zone="$zone" \ --machine-type=e2-micro \ --network-interface=network-tier=PREMIUM,stack-type=IPV4_ONLY,subnet=default \ - --metadata ssh-keys=root:ssh-rsa\ AAAAB3NzaC1yc2EAAAADAQABAAABgQDVw/Lamb8wHXeLgCKGbumrocMvq+a6goVFBAuhYk/TVUoislrO1SrrH5YMFc7aQMZNP/mbubirIck8h0wT8hiU070OHO7HuaAyIGgFh4icIX/m7znhvWteG/evxJUN95ZWm4bk+UmGUbbAO4BkSEGub/ENJ3RGR9eJuDabgMha5fyzl9J9sm6jDeXVyGtLOy9NkYYHo/J0kUAwK1YOQ88rAXIhsJ04qsH7256VAdo6enO39Y0RG4NhK3hlRYP46f8NWyCaJFbcz4tpbHNdG9Xbqg7j/RSn7tO5bpB283m/wsZR7kM28x9dzyojJJYycEn9CTUzBjxcBBuKNa57Y+eoBo7q2KXx13ziMvO5k7Bl8GXknl0uguf49hjbPS95CThns+sqz7G3Px8a79BJ1rHEewlmMMJUa/kRY+NAcum0nVkWzZIpR6I2KWMP+8OaJLj97vIHgGydRP8y4I6IiiZBlOlshNJ3iI/XxsSWjWjdWxSHUZtHj4L1IaxclrX+QtU=\ root@gc-hk.asia-east2-c.c.annular-bucksaw-448504-h3.internal \ - --metadata-from-file startup-script=startup-script.sh \ + --metadata-from-file startup-script=/opt/gcloud/startup-script.sh \ --maintenance-policy=MIGRATE \ --provisioning-model=STANDARD \ --scopes=https://www.googleapis.com/auth/devstorage.read_only,https://www.googleapis.com/auth/logging.write,https://www.googleapis.com/auth/monitoring.write,https://www.googleapis.com/auth/service.management.readonly,https://www.googleapis.com/auth/servicecontrol,https://www.googleapis.com/auth/trace.append \ @@ -1341,19 +1238,6 @@ while true; do echo "按任意键返回菜单..." read -e -r -n 1 ;; - 16) - echo "正在查看所有可用的结算账号..." - if get_billing_accounts; then - echo "可用的结算账号列表:" - for item in "${billing_account_array_display[@]}"; do - echo "$item" - done - else - echo "无法获取结算账号列表。" - fi - echo "按任意键返回菜单..." - read -e -r -n 1 - ;; 11) echo "正在删除虚拟机..." current_project=$(gcloud config get-value project) @@ -1459,19 +1343,6 @@ while true; do echo "按任意键返回菜单..." read -e -r -n 1 ;; - 16) - echo "正在查看所有可用的结算账号..." - if get_billing_accounts; then - echo "可用的结算账号列表:" - for item in "${billing_account_array_display[@]}"; do - echo "$item" - done - else - echo "无法获取结算账号列表。" - fi - echo "按任意键返回菜单..." - read -e -r -n 1 - ;; 12) echo "正在查看当前项目下所有实例的 socks5 配置..." current_project=$(gcloud config get-value project) @@ -1567,19 +1438,6 @@ while true; do echo "按任意键返回菜单..." read -e -r -n 1 ;; - 16) - echo "正在查看所有可用的结算账号..." - if get_billing_accounts; then - echo "可用的结算账号列表:" - for item in "${billing_account_array_display[@]}"; do - echo "$item" - done - else - echo "无法获取结算账号列表。" - fi - echo "按任意键返回菜单..." - read -e -r -n 1 - ;; 13) echo "正在配置防火墙规则..." current_project=$(gcloud config get-value project) @@ -1784,19 +1642,6 @@ while true; do echo "按任意键返回菜单..." read -e -r -n 1 ;; - 16) - echo "正在查看所有可用的结算账号..." - if get_billing_accounts; then - echo "可用的结算账号列表:" - for item in "${billing_account_array_display[@]}"; do - echo "$item" - done - else - echo "无法获取结算账号列表。" - fi - echo "按任意键返回菜单..." - read -e -r -n 1 - ;; 15) echo "正在准备删除防火墙规则..." current_project=$(gcloud config get-value project) @@ -1850,7 +1695,6 @@ while true; do read -e -r -n 1 ;; 16) - echo "正在查看所有可用的结算账号..." if get_billing_accounts; then echo "可用的结算账号列表:" for item in "${billing_account_array_display[@]}"; do @@ -1872,18 +1716,5 @@ while true; do echo "按任意键返回菜单..." read -e -r -n 1 ;; - 16) - echo "正在查看所有可用的结算账号..." - if get_billing_accounts; then - echo "可用的结算账号列表:" - for item in "${billing_account_array_display[@]}"; do - echo "$item" - done - else - echo "无法获取结算账号列表。" - fi - echo "按任意键返回菜单..." - read -e -r -n 1 - ;; esac done