Skip to content

Commit

Permalink
feat: 移除更新代理
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Oct 18, 2023
1 parent 6fcd65d commit 0ecd601
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 74 deletions.
9 changes: 2 additions & 7 deletions app/console/commands/panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 获取面板访问入口")
Expand Down
3 changes: 1 addition & 2 deletions app/http/controllers/info_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
90 changes: 58 additions & 32 deletions pkg/tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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("获取最新版本失败")
}

Expand All @@ -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("写入临时文件失败")
}
Expand All @@ -90,41 +95,47 @@ 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
}

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")
Expand All @@ -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("下载失败")
Expand Down Expand Up @@ -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
}
32 changes: 9 additions & 23 deletions public/panel/views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -326,28 +326,15 @@ <h3>{{index}}</h3>
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('当前已是最新版本!')
Expand All @@ -361,8 +348,7 @@ <h3>{{index}}</h3>
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'
Expand Down
21 changes: 11 additions & 10 deletions scripts/install_panel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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架构安装。"
Expand All @@ -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 "错误:下载面板失败,请截图错误信息寻求帮助。"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0ecd601

Please sign in to comment.