2023-06-02 08:32:10 +08:00
|
|
|
//----------------------------------------
|
|
|
|
//
|
|
|
|
// Copyright © yanghy. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under Apache License Version 2.0, January 2004
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
//----------------------------------------
|
|
|
|
|
2023-06-05 15:14:36 +08:00
|
|
|
// Package autoupdate Energy lib-lcl check auto update
|
2023-06-02 08:32:10 +08:00
|
|
|
package autoupdate
|
|
|
|
|
2023-06-03 19:30:20 +08:00
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
2023-06-04 01:25:32 +08:00
|
|
|
"github.com/energye/energy/v2/common/imports"
|
|
|
|
"github.com/energye/golcl/lcl/api"
|
2023-06-03 19:30:20 +08:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2023-06-04 01:25:32 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-06-03 19:30:20 +08:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-06-05 15:14:36 +08:00
|
|
|
const checkURL = "https://energy.yanghy.cn/autoconfig/update.json"
|
|
|
|
|
2023-06-07 11:31:41 +08:00
|
|
|
type LiblclCallback func(model *Model, level int, canUpdate bool)
|
2023-06-04 01:25:32 +08:00
|
|
|
|
2023-06-02 08:32:10 +08:00
|
|
|
var (
|
2023-06-02 14:49:23 +08:00
|
|
|
// 检查更新开关, 默认关闭
|
2023-06-04 01:25:32 +08:00
|
|
|
isCheckUpdate = false
|
2023-06-07 11:31:41 +08:00
|
|
|
CanUpdateLiblcl LiblclCallback // 参数 model: 更新模块, Level: 更新版本级别, canUpdate: 是否有更新
|
2023-06-02 08:32:10 +08:00
|
|
|
)
|
|
|
|
|
2023-06-04 01:25:32 +08:00
|
|
|
// Update 更新模块
|
|
|
|
type Update struct {
|
|
|
|
Liblcl Model `json:"liblcl"`
|
|
|
|
Energy Model `json:"energy"`
|
|
|
|
CEF Model `json:"cef"`
|
2023-06-03 19:30:20 +08:00
|
|
|
}
|
|
|
|
|
2023-06-04 01:25:32 +08:00
|
|
|
// Model 模块
|
|
|
|
type Model struct {
|
2023-06-07 11:31:41 +08:00
|
|
|
CurrentVersion string `json:"-"`
|
|
|
|
Latest string `json:"latest"` // 最新版本
|
|
|
|
Download Download `json:"download"` // 下载源
|
|
|
|
Enable bool `json:"enable"` // 是否开启该模块更新
|
|
|
|
Versions map[string]VersionInfo `json:"versions"` // 当前模块所有版本集合 key=版本, value=版本信息
|
2023-06-03 19:30:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 更新下载源
|
2023-06-04 01:25:32 +08:00
|
|
|
type Download struct {
|
2023-06-03 19:30:20 +08:00
|
|
|
Url string `json:"url"` // 下载地址模板 https://{url}/energye/energy/releases/download/{version}/{OSARCH}.zip
|
2023-06-04 01:25:32 +08:00
|
|
|
Source []string `json:"source"` // 下载地址源 ["gitee.com", "github.com"]
|
2023-06-03 19:30:20 +08:00
|
|
|
SourceSelect uint8 `json:"sourceSelect"` // 下载地址源选择, 根据下标(index)选择源(Source), 替换到(Url)模板
|
|
|
|
}
|
|
|
|
|
|
|
|
// 版本信息
|
2023-06-04 01:25:32 +08:00
|
|
|
type VersionInfo struct {
|
2023-06-05 19:01:30 +08:00
|
|
|
Content []string `json:"content"` // 更新内容
|
|
|
|
Forced bool `json:"forced"` // 是否强制更新, 适用当前版本时才启作用
|
|
|
|
EnergyVersion string `json:"energyVersion"` // 所属 energy 版本
|
2023-06-03 19:30:20 +08:00
|
|
|
}
|
|
|
|
|
2023-06-02 08:32:10 +08:00
|
|
|
// CheckUpdate
|
2023-06-04 01:25:32 +08:00
|
|
|
// Check for updates, when isCheckUpdate is true
|
2023-06-02 08:32:10 +08:00
|
|
|
func CheckUpdate() {
|
|
|
|
if isCheckUpdate {
|
2023-06-04 01:25:32 +08:00
|
|
|
check()
|
2023-06-02 08:32:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsCheckUpdate
|
2023-06-04 01:25:32 +08:00
|
|
|
// Set whether to check for updates
|
2023-06-02 08:32:10 +08:00
|
|
|
func IsCheckUpdate(v bool) {
|
|
|
|
isCheckUpdate = v
|
|
|
|
}
|
|
|
|
|
2023-06-02 14:49:23 +08:00
|
|
|
func check() {
|
2023-06-03 19:30:20 +08:00
|
|
|
client := &http.Client{Timeout: 5 * time.Second, Transport: &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
}}
|
2023-06-05 15:14:36 +08:00
|
|
|
request, err := http.NewRequest("GET", checkURL, nil)
|
2023-06-03 19:30:20 +08:00
|
|
|
if err != nil {
|
|
|
|
println("energy check update http.NewRequest error:", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
response, err := client.Do(request)
|
|
|
|
if err != nil {
|
|
|
|
println("energy check update client.Do error:", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
if data, err := ioutil.ReadAll(response.Body); err == nil {
|
2023-06-04 01:25:32 +08:00
|
|
|
var v = Update{}
|
2023-06-03 19:30:20 +08:00
|
|
|
if err = json.Unmarshal(data, &v); err == nil {
|
2023-06-04 01:25:32 +08:00
|
|
|
liblcl(&v.Liblcl)
|
2023-06-03 19:30:20 +08:00
|
|
|
} else {
|
|
|
|
println("energy check update json.Unmarshal error", err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
println("energy check update ioutil.ReadAl(response.Body) error", err.Error())
|
|
|
|
}
|
2023-06-02 14:49:23 +08:00
|
|
|
}
|
|
|
|
|
2023-06-04 01:25:32 +08:00
|
|
|
// liblcl Model
|
|
|
|
func liblcl(model *Model) {
|
|
|
|
if model.Enable {
|
|
|
|
r1, _, _ := imports.Proc(1).Call()
|
|
|
|
currentLib := api.GoStr(r1)
|
|
|
|
originLib := model.Latest
|
2023-06-07 11:31:41 +08:00
|
|
|
if currentLib == "" {
|
|
|
|
currentLib = "0.0.0"
|
|
|
|
}
|
|
|
|
if originLib == "" {
|
|
|
|
originLib = "0.0.0"
|
|
|
|
}
|
|
|
|
model.CurrentVersion = currentLib
|
|
|
|
can, level := compare(currentLib, originLib)
|
|
|
|
if CanUpdateLiblcl != nil {
|
|
|
|
CanUpdateLiblcl(model, level, can)
|
2023-06-04 01:25:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version comparison, returns true if the current version is smaller than the remote version
|
|
|
|
func compare(current, origin string) (bool, int) {
|
|
|
|
cmajor, cminor, crevision := versionConvert(current)
|
|
|
|
omajor, ominor, orevision := versionConvert(origin)
|
|
|
|
if omajor > cmajor {
|
|
|
|
return true, 1 // major
|
|
|
|
} else if ominor > cminor {
|
|
|
|
return true, 2 // minor
|
|
|
|
} else if orevision > crevision {
|
|
|
|
return true, 3 // revision
|
|
|
|
}
|
|
|
|
return false, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version number conversion=>Major version, minor version, revised version
|
|
|
|
func versionConvert(ver string) (major, minor, revision int) {
|
2023-07-10 17:12:40 +08:00
|
|
|
ver = strings.ToLower(ver)
|
|
|
|
lastv := strings.LastIndex(ver, "v")
|
|
|
|
if lastv != -1 {
|
|
|
|
ver = ver[lastv+1:]
|
|
|
|
}
|
2023-06-04 01:25:32 +08:00
|
|
|
vers := strings.Split(strings.Split(ver, "-")[0], ".")
|
|
|
|
if len(vers) >= 3 {
|
|
|
|
major, _ = strconv.Atoi(vers[0])
|
|
|
|
minor, _ = strconv.Atoi(vers[1])
|
|
|
|
revision, _ = strconv.Atoi(vers[2])
|
|
|
|
} else if len(vers) >= 2 {
|
|
|
|
major, _ = strconv.Atoi(vers[0])
|
|
|
|
minor, _ = strconv.Atoi(vers[1])
|
|
|
|
} else if len(vers) >= 1 {
|
|
|
|
major, _ = strconv.Atoi(vers[0])
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|