diff --git a/core/init.go b/core/init.go index bb6ce6d..93f9708 100644 --- a/core/init.go +++ b/core/init.go @@ -61,18 +61,24 @@ func Init() { if transport != nil { client.Transport = transport } + var body io.Reader + var data []byte + var latest_version = "" + console.Debug("正在从 cdle/binary 获取版本号...") - req, _ := http.NewRequest("GET", "https://raw.githubusercontent.com/cdle/binary/main/compile_time.go", strings.NewReader("")) + 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), - } + // return &storage.Final{ + // Error: fmt.Errorf("貌似网络不太行啊:%s", err), + // } + goto PROXY } defer resp.Body.Close() - var data, _ = ioutil.ReadAll(resp.Body) - latest_version := regexp.MustCompile(`\d{13}`).FindString(string(data)) + 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{ @@ -84,7 +90,7 @@ func Init() { client.Transport = transport } console.Debug("正在从 cdle/binary 获取最新版本 %s 编译文件...", latest_version) - qurl := "https://raw.githubusercontent.com/cdle/binary/master/sillyGirl_" + runtime.GOOS + "_" + runtime.GOARCH + "_" + latest_version + qurl = "https://raw.githubusercontent.com/cdle/binary/master/sillyGirl_" + runtime.GOOS + "_" + runtime.GOARCH + "_" + latest_version if runtime.GOOS == "windows" { qurl += ".exe" } @@ -92,11 +98,40 @@ func Init() { resp, err = client.Do(req) if err != nil { console.Error("获取最新编译文件错误:%s", err) + // return &storage.Final{ + // Error: fmt.Errorf("升级时貌似网络不太行啊:%v", err), + // } + goto PROXY + } + defer resp.Body.Close() + body = resp.Body + goto CREATE + + PROXY: + //使用免费代理下载 + console.Info("正在重新尝试下载...") + qurl = "http://172.96.255.172:8765/api/download?version=" + compiled_at + "&goos=" + runtime.GOOS + "&goarch=" + runtime.GOARCH + resp, err = http.Get(qurl) + if err != nil { return &storage.Final{ - Error: fmt.Errorf("升级时貌似网络不太行啊:%v", err), + Error: fmt.Errorf("升级时貌似网络不太行啊"), } } defer resp.Body.Close() + body = resp.Body + switch resp.Header.Get("Result") { + case "newest": + return &storage.Final{ + Message: fmt.Sprintf("当前版本 %s 已是最新,无需升级", compiled_at), + } + case "fail": + return &storage.Final{ + Error: fmt.Errorf("升级失败"), + } + case "ok": + } + + CREATE: console.Debug("正在创建编译文件...") filename := utils.ExecPath + "/" + utils.ProcessName ready := "" @@ -113,7 +148,7 @@ func Init() { } } defer f.Close() - i, err := io.Copy(f, resp.Body) + i, err := io.Copy(f, body) if i < 2646140 || err != nil { console.Error("创建编译文件错误:%v", i) return &storage.Final{ diff --git a/others/download/main.go b/others/download/main.go new file mode 100644 index 0000000..e9bf6fe --- /dev/null +++ b/others/download/main.go @@ -0,0 +1,124 @@ +package main + +import ( + "fmt" + "io/ioutil" + "net/http" + "regexp" + "strings" + "sync" + + "github.com/gin-gonic/gin" +) + +var version = "" + +func init() { + resp, err := http.Get("https://raw.githubusercontent.com/cdle/binary/main/compile_time.go") + if err != nil { + panic(fmt.Errorf("获取版本号错误:%s", err)) + } + defer resp.Body.Close() + data, err := ioutil.ReadAll(resp.Body) + if err != nil { + panic(fmt.Errorf("获取版本号错误:%s", err)) + } + version = regexp.MustCompile(`\d{13}`).FindString(string(data)) + + if version == "" { + panic(fmt.Errorf("获取版本号错误:%v", version)) + } + fmt.Println("获取版本号:", version) +} + +var Result = "Result" + +type Class struct { + Os string + Arch string + Data []byte + sync.RWMutex +} + +var classes = []*Class{ + { + Os: "linux", + Arch: "amd64", + }, + { + Os: "linux", + Arch: "arm64", + }, + { + Os: "windows", + Arch: "amd64", + }, +} + +func main() { + router := gin.Default() + router.GET("/api/download", func(c *gin.Context) { + if version <= c.Query("version") { + c.Header(Result, "newest") + c.String(200, "版本已是最新,无需升级!") + return + } + var goos = c.Query("goos") + var goarch = c.Query("goarch") + for _, class := range classes { + if goos == class.Os && goarch == class.Arch { + func() { + class.Lock() + defer class.Unlock() + if class.Data == nil { + fmt.Println("没有就下载", version) + qurl := "https://raw.githubusercontent.com/cdle/binary/master/sillyGirl_" + goos + "_" + goarch + "_" + version + if goos == "windows" { + qurl += ".exe" + } + fmt.Println(qurl) + req, _ := http.NewRequest("GET", qurl, strings.NewReader("")) + resp, err := (&http.Client{}).Do(req) + if err != nil { + c.Header(Result, "fail") + c.String(200, err.Error()) + return + } + if resp.StatusCode != 200 { + c.Header(Result, "fail") + c.String(200, "下载失败") + return + } + data, err := ioutil.ReadAll(resp.Body) + if err != nil { + c.Header(Result, "fail") + c.String(200, err.Error()) + return + } + class.Data = data + } + }() + c.Header(Result, "ok") + c.Data(200, "application/octet-stream", class.Data) + } + } + c.Header(Result, "fail") + c.String(200, "") + }) + //http://172.96.255.172:8765/api/version?version= + router.GET("/api/version", func(c *gin.Context) { + v := c.Query("version") + if v != "" { + for _, class := range classes { + class.Lock() + class.Data = nil + class.Unlock() + } + version = v + fmt.Println("更新版本号:", version) + } + c.String(200, version) + }) + + router.Run(":8765") +}