diff --git a/core/init.go b/core/init.go index 339235a..39e76fb 100644 --- a/core/init.go +++ b/core/init.go @@ -1,6 +1,7 @@ package core import ( + "errors" "fmt" "io" "io/ioutil" @@ -19,6 +20,18 @@ import ( var DataHome = utils.GetDataHome() +func GetVersion() (string, error) { + v, e := httplib.Get("http://172.96.255.172:8765/api/version").String() + if len(v) == 13 { + sillyGirl.Set("version", v) + if v != compiled_at { + console.Log("发现到更新,版本号:", v) + } + return v, e + } + return v, errors.New("版本获取失败") +} + func Init() { initLoc() sillyGirl = MakeBucket("sillyGirl") @@ -64,22 +77,31 @@ func Init() { var body io.Reader var data []byte var latest_version = "" + var resp *http.Response + var req *http.Request console.Debug("正在从 cdle/binary 获取版本号...") proxy := false qurl := "https://raw.githubusercontent.com/cdle/binary/main/compile_time.go" - req, _ := http.NewRequest("GET", qurl, strings.NewReader("")) - resp, err := client.Do(req) - if err != nil { - console.Error("获取版本号错误:%s", err) - // return &storage.Final{ - // Error: fmt.Errorf("貌似网络不太行啊:%s", err), - // } - goto PROXY + + version, _ := GetVersion() + if version != "" { + latest_version = version + } else { + req, _ = http.NewRequest("GET", qurl, strings.NewReader("")) + resp, err = client.Do(req) + if err != nil { + console.Error("获取版本号错误:%s", err) + // return &storage.Final{ + // Error: fmt.Errorf("貌似网络不太行啊:%s", err), + // } + goto PROXY + } + defer resp.Body.Close() + data, _ = ioutil.ReadAll(resp.Body) + latest_version = regexp.MustCompile(`\d{13}`).FindString(string(data)) } - defer resp.Body.Close() - data, _ = ioutil.ReadAll(resp.Body) - latest_version = regexp.MustCompile(`\d{13}`).FindString(string(data)) + if latest_version <= compiled_at { console.Debug("当前版本 %s 已是最新,无需升级", compiled_at) return &storage.Final{ diff --git a/main.go b/main.go index d655e69..a41187e 100644 --- a/main.go +++ b/main.go @@ -23,6 +23,7 @@ func main() { if err == nil { app.Set("ip", ip) } + core.GetVersion() }() core.Init() if app.GetBool("anti_kasi") {