From 0ecd60184b52c122d69fd11707cc6c85641a438d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Thu, 19 Oct 2023 03:13:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=A7=BB=E9=99=A4=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E4=BB=A3=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/console/commands/panel.go | 9 +-- app/http/controllers/info_controller.go | 3 +- pkg/tools/tools.go | 90 ++++++++++++++++--------- public/panel/views/index.html | 32 +++------ scripts/install_panel.sh | 21 +++--- 5 files changed, 81 insertions(+), 74 deletions(-) diff --git a/app/console/commands/panel.go b/app/console/commands/panel.go index 532404f762..ff7e93f896 100644 --- a/app/console/commands/panel.go +++ b/app/console/commands/panel.go @@ -86,12 +86,7 @@ func (receiver *Panel) Handle(ctx console.Context) error { return nil } - input := arg1 - proxy := false - if input == "y" || input == "Y" || input == "yes" || input == "Yes" { - proxy = true - } - err = tools.UpdatePanel(cast.ToBool(proxy)) + err = tools.UpdatePanel() if err != nil { color.Redln("更新失败: " + err.Error()) return nil @@ -496,7 +491,7 @@ func (receiver *Panel) Handle(ctx console.Context) error { default: color.Yellowln(facades.Config().GetString("panel.name") + "命令行工具 - " + facades.Config().GetString("panel.version")) color.Greenln("请使用以下命令:") - color.Greenln("panel update {proxy} 更新 / 修复面板到最新版本") + color.Greenln("panel update 更新 / 修复面板到最新版本") color.Greenln("panel getInfo 重新初始化面板账号信息") color.Greenln("panel getPort 获取面板访问端口") color.Greenln("panel getEntrance 获取面板访问入口") diff --git a/app/http/controllers/info_controller.go b/app/http/controllers/info_controller.go index b28ea0e963..aa55ad57d3 100644 --- a/app/http/controllers/info_controller.go +++ b/app/http/controllers/info_controller.go @@ -179,8 +179,7 @@ func (c *InfoController) Update(ctx http.Context) http.Response { return Error(ctx, http.StatusInternalServerError, "当前有任务正在执行,禁止更新") } - proxy := ctx.Request().InputBool("proxy") - err = tools.UpdatePanel(proxy) + err = tools.UpdatePanel() if err != nil { facades.Log().Error("[面板][InfoController] 更新面板失败 ", err.Error()) return Error(ctx, http.StatusInternalServerError, "更新失败: "+err.Error()) diff --git a/pkg/tools/tools.go b/pkg/tools/tools.go index ee25f0c07e..14cd4b97bf 100644 --- a/pkg/tools/tools.go +++ b/pkg/tools/tools.go @@ -4,12 +4,11 @@ package tools import ( "errors" "os" - "os/exec" - "strconv" "strings" "time" "github.com/gookit/color" + "github.com/imroc/req/v3" "github.com/shirou/gopsutil/cpu" "github.com/shirou/gopsutil/disk" "github.com/shirou/gopsutil/host" @@ -68,10 +67,16 @@ type PanelInfo struct { // GetLatestPanelVersion 获取最新版本 func GetLatestPanelVersion() (PanelInfo, error) { var info PanelInfo + var output string + isChina := IsChina() - cmd := exec.Command("/bin/bash", "-c", "curl \"https://api.github.com/repos/haozi-team/panel/releases/latest\"") - output, err := cmd.Output() - if err != nil { + if isChina { + output = Exec(`curl -s "https://jihulab.com/api/v4/projects/haozi-team%2Fpanel/releases"`) + } else { + output = Exec(`curl -s "https://api.github.com/repos/haozi-team/panel/releases/latest"`) + } + + if len(output) == 0 { return info, errors.New("获取最新版本失败") } @@ -80,7 +85,7 @@ func GetLatestPanelVersion() (PanelInfo, error) { return info, errors.New("创建临时文件失败") } defer os.Remove(file.Name()) - _, err = file.Write(output) + _, err = file.Write([]byte(output)) if err != nil { return info, errors.New("写入临时文件失败") } @@ -90,33 +95,40 @@ func GetLatestPanelVersion() (PanelInfo, error) { } fileName := file.Name() - name := exec.Command("/bin/bash", "-c", "jq -r '.name' "+fileName) - version := exec.Command("/bin/bash", "-c", "jq -r '.tag_name' "+fileName) - body := exec.Command("/bin/bash", "-c", "jq -r '.body' "+fileName) - date := exec.Command("/bin/bash", "-c", "jq -r '.published_at' "+fileName) - nameOutput, _ := name.Output() - versionOutput, _ := version.Output() - bodyOutput, _ := body.Output() - dateOutput, _ := date.Output() - info.Name = strings.TrimSpace(string(nameOutput)) - info.Version = strings.TrimSpace(string(versionOutput)) - info.Body = strings.TrimSpace(string(bodyOutput)) - info.Date = strings.TrimSpace(string(dateOutput)) - if IsArm() { - downloadUrl := exec.Command("/bin/bash", "-c", "jq -r '.assets[] | select(.name | contains(\"arm64\")) | .browser_download_url' "+fileName) - downloadUrlOutput, _ := downloadUrl.Output() - info.DownloadUrl = strings.TrimSpace(string(downloadUrlOutput)) + var name, version, body, date, downloadUrl string + if isChina { + name = Exec("jq -r '.[0].name' " + fileName) + version = Exec("jq -r '.[0].tag_name' " + fileName) + body = Exec("jq -r '.[0].description' " + fileName) + date = Exec("jq -r '.[0].created_at' " + fileName) + if IsArm() { + downloadUrl = Exec("jq -r '.[0].assets.links[] | select(.name | contains(\"arm64\")) | .direct_asset_url' " + fileName) + } else { + downloadUrl = Exec("jq -r '.[0].assets.links[] | select(.name | contains(\"amd64v2\")) | .direct_asset_url' " + fileName) + } } else { - downloadUrl := exec.Command("/bin/bash", "-c", "jq -r '.assets[] | select(.name | contains(\"amd64v2\")) | .browser_download_url' "+fileName) - downloadUrlOutput, _ := downloadUrl.Output() - info.DownloadUrl = strings.TrimSpace(string(downloadUrlOutput)) + name = Exec("jq -r '.name' " + fileName) + version = Exec("jq -r '.tag_name' " + fileName) + body = Exec("jq -r '.body' " + fileName) + date = Exec("jq -r '.published_at' " + fileName) + if IsArm() { + downloadUrl = Exec("jq -r '.assets[] | select(.name | contains(\"arm64\")) | .browser_download_url' " + fileName) + } else { + downloadUrl = Exec("jq -r '.assets[] | select(.name | contains(\"amd64v2\")) | .browser_download_url' " + fileName) + } } + info.Name = name + info.Version = version + info.Body = body + info.Date = date + info.DownloadUrl = downloadUrl + return info, nil } // UpdatePanel 更新面板 -func UpdatePanel(proxy bool) error { +func UpdatePanel() error { panelInfo, err := GetLatestPanelVersion() if err != nil { return err @@ -124,7 +136,6 @@ func UpdatePanel(proxy bool) error { color.Greenln("最新版本: " + panelInfo.Version) color.Greenln("下载链接: " + panelInfo.DownloadUrl) - color.Greenln("使用代理: " + strconv.FormatBool(proxy)) color.Greenln("备份面板配置...") Exec("cp -f /www/panel/database/panel.db /tmp/panel.db.bak") @@ -139,11 +150,7 @@ func UpdatePanel(proxy bool) error { color.Greenln("清理完成") color.Greenln("正在下载...") - if proxy { - Exec("wget -T 120 -t 3 -O /www/panel/panel.zip https://ghproxy.com/" + panelInfo.DownloadUrl) - } else { - Exec("wget -T 120 -t 3 -O /www/panel/panel.zip " + panelInfo.DownloadUrl) - } + Exec("wget -T 120 -t 3 -O /www/panel/panel.zip " + panelInfo.DownloadUrl) if !Exists("/www/panel/panel.zip") { return errors.New("下载失败") @@ -180,3 +187,22 @@ func UpdatePanel(proxy bool) error { return nil } + +// IsChina 是否中国大陆 +func IsChina() bool { + client := req.C() + client.SetTimeout(5 * time.Second) + client.SetCommonRetryCount(2) + client.ImpersonateSafari() + + resp, err := client.R().Get("https://www.cloudflare-cn.com/cdn-cgi/trace") + if err != nil || !resp.IsSuccessState() { + return false + } + + if strings.Contains(resp.String(), "loc=CN") { + return true + } + + return false +} diff --git a/public/panel/views/index.html b/public/panel/views/index.html index 9d3a7c6b98..8402ff10e6 100644 --- a/public/panel/views/index.html +++ b/public/panel/views/index.html @@ -326,28 +326,15 @@

{{index}}

title: '最新版本: ' + result.data.version + ' ,是否更新?', btn: ['更新', '取消'] }, function () { - let proxy = false - layer.confirm('对于大陆服务器,建议使用代理进行更新', { - title: '是否使用代理更新?', - btn: ['是', '否'] - }, function () { - proxy = true - index = layer.msg('正在更新,稍后请手动刷新...', {icon: 16, time: 0, shade: 0.3}) - admin.req({ - url: '/api/panel/info/update' - , type: 'post' - , data: {proxy: proxy} - }) - }, function () { - proxy = false - index = layer.msg('正在更新,稍后请手动刷新...', {icon: 16, time: 0, shade: 0.3}) - admin.req({ - url: '/api/panel/info/update' - , type: 'post' - , data: {proxy: proxy} - }) + index = layer.msg('更新中(出现请求错误为正常情况),稍后请手动刷新...', { + icon: 16, + time: 0, + shade: 0.3 + }) + admin.req({ + url: '/api/panel/info/update' + , type: 'post' }) - }) } else { layer.msg('当前已是最新版本!') @@ -361,8 +348,7 @@

{{index}}

layer.confirm('真的要重启吗?', { btn: ['是', '否'] }, function () { - proxy = true - index = layer.msg('正在重启,稍后请手动刷新...', {icon: 16, time: 0, shade: 0.3}) + index = layer.msg('重启中(出现请求错误为正常情况),稍后请手动刷新...', {icon: 16, time: 0, shade: 0.3}) admin.req({ url: '/api/panel/info/restart' , type: 'post' diff --git a/scripts/install_panel.sh b/scripts/install_panel.sh index 0529f413ba..4f95e9da94 100644 --- a/scripts/install_panel.sh +++ b/scripts/install_panel.sh @@ -19,7 +19,6 @@ limitations under the License. LOGO="+----------------------------------------------------\n| 耗子Linux面板安装脚本\n+----------------------------------------------------\n| Copyright © 2022-"$(date +%Y)" 耗子科技 All rights reserved.\n+----------------------------------------------------" HR="+----------------------------------------------------" -download_Url="" setup_Path="/www" sshPort=$(cat /etc/ssh/sshd_config | grep 'Port ' | awk '{print $2}') inChina=$(curl --retry 2 -m 10 -L https://www.cloudflare-cn.com/cdn-cgi/trace 2> /dev/null | grep -qx 'loc=CN' && echo "true" || echo "false") @@ -181,9 +180,17 @@ Init_Panel() { rm -rf ${setup_Path}/panel/* # 下载面板zip包并解压 if [ "${ARCH}" == "x86_64" ]; then - panelZip=$(curl "https://api.github.com/repos/haozi-team/panel/releases/latest" | jq -r '.assets[] | select(.name | contains("amd64v2")) | .browser_download_url') + if ${inChina}; then + panelZip=$(curl -s "https://jihulab.com/api/v4/projects/haozi-team%2Fpanel/releases" | jq -r '.[0].assets.links[] | select(.name | contains("amd64v2")) | .direct_asset_url') + else + panelZip=$(curl -s "https://api.github.com/repos/haozi-team/panel/releases/latest" | jq -r '.assets[] | select(.name | contains("amd64v2")) | .browser_download_url') + fi elif [ "${ARCH}" == "aarch64" ]; then - panelZip=$(curl "https://api.github.com/repos/haozi-team/panel/releases/latest" | jq -r '.assets[] | select(.name | contains("arm64")) | .browser_download_url') + if ${inChina}; then + panelZip=$(curl -s "https://jihulab.com/api/v4/projects/haozi-team%2Fpanel/releases" | jq -r '.[0].assets.links[] | select(.name | contains("arm64")) | .direct_asset_url') + else + panelZip=$(curl -s "https://api.github.com/repos/haozi-team/panel/releases/latest" | jq -r '.assets[] | select(.name | contains("arm64")) | .browser_download_url') + fi else echo -e $HR echo "错误:该系统架构不支持安装耗子Linux面板,请更换x86_64/aarch64架构安装。" @@ -194,7 +201,7 @@ Init_Panel() { echo "错误:获取面板下载链接失败,请截图错误信息寻求帮助。" exit 1 fi - wget -T 120 -t 3 -O ${setup_Path}/panel/panel.zip "${download_Url}${panelZip}" + wget -T 120 -t 3 -O ${setup_Path}/panel/panel.zip "${panelZip}" if [ "$?" != "0" ]; then echo -e $HR echo "错误:下载面板失败,请截图错误信息寻求帮助。" @@ -288,12 +295,6 @@ if [ "$install" != 'y' ]; then exit fi -#代理设置 -read -p "是否使用GitHub代理安装(建议大陆机器使用)?(y/n)" proxy -if [ "$proxy" == 'y' ]; then - download_Url="https://ghproxy.com/" -fi - echo -e $LOGO echo '安装面板依赖软件(如报错请检查 APT/Yum 源是否正常)' echo -e $HR