diff --git a/data.go b/data.go index 4996a58..8a06fc1 100644 --- a/data.go +++ b/data.go @@ -63,7 +63,6 @@ func checkCfw() *cfwInfo { os.Remove(path.Join(ci.rootPath, "test")) } ci.installVersion = true - updateCore = false } break } @@ -133,7 +132,7 @@ func tranSelect() { } func cfwInput() { - fmt.Println("当前cfw版本: " + currentVersion) + fmt.Println("当前cfw版本: " + ci.version) fmt.Println() for k, v := range cfwVersionList { fmt.Printf("%2d. %s\n", k+1, v) @@ -185,7 +184,7 @@ func cfwSelect() { } fmt.Println() if !*forceUpdate { - if strings.Contains(currentVersion, cfwVersion) { + if strings.Contains(ci.version, cfwVersion) { if specialVersion == "" { fmt.Println("当前cfw版本已为最新!") } else { diff --git a/main.go b/main.go index 94e188d..154eb82 100644 --- a/main.go +++ b/main.go @@ -11,13 +11,14 @@ import ( ) var ( - version, buildDate, goVersion, gitVersion string - cfwVersion, currentVersion, tempPath, transWay, specialVersion string - cfwVersionList []string - updateTrans bool - updateCore = true - v = flag.Bool("V", false, "显示版本号") - forceUpdate = flag.Bool("f", false, "强制更新cfw(默认和已存在版本相同则不更新)") + version, buildDate, goVersion, gitVersion string + cfwVersion, tempPath, transWay, specialVersion string + cfwVersionList []string + updateTrans bool + updateCore = true + ci *cfwInfo + v = flag.Bool("V", false, "显示版本号") + forceUpdate = flag.Bool("f", false, "强制更新cfw(默认和已存在版本相同则不更新)") ) func init() { @@ -57,10 +58,18 @@ func updateUpdater() { } func getCfw() *downloadInfo { - cfwUrl := fmt.Sprintf("https://github.com/Fndroid/clash_for_windows_pkg/releases/download/%s/Clash.for.Windows-%s-win.7z", cfwVersion, cfwVersion) + var cfwFileName string + if ci.installVersion { + cfwFileName = fmt.Sprintf("Clash.for.Windows.Setup.%s.exe", cfwVersion) + } else { + cfwFileName = fmt.Sprintf("Clash.for.Windows-%s-win.7z", cfwVersion) + } + cfwUrl := fmt.Sprintf("https://github.com/Fndroid/clash_for_windows_pkg/releases/download/%s/%s", cfwVersion, cfwFileName) downloadFile(cfwUrl, "") di := newDI(cfwUrl) - extract7z(di.fileFullName) + if !ci.installVersion { + extract7z(di.fileFullName) + } fmt.Println() return di } @@ -85,89 +94,104 @@ func downloadPack() []*downloadInfo { return diList } -func updateCfw(cfw *cfwInfo, diList []*downloadInfo) { - var stopCh chan struct{} - closeChan := func() { - close(stopCh) - fmt.Printf("\n\n") - } - if updateCore || updateTrans { - stopCh = make(chan struct{}) - go showProgress("更新cfw中", stopCh) - cfw.process.Kill() - } +func transUpdate(diList []*downloadInfo, stopCh chan struct{}) { if updateTrans { var err error - if updateCore { - err = copy.Copy(fullPath(path.Join(diList[1].fileName, "app.asar")), fullPath(path.Join(diList[0].fileName, "resources/app.asar"))) + if ci.installVersion { + err = copy.Copy(fullPath(path.Join(diList[len(diList)-1].fileName, "app.asar")), path.Join(ci.rootPath, "resources/app.asar")) } else { - err = copy.Copy(fullPath(path.Join(diList[0].fileName, "app.asar")), path.Join(cfw.rootPath, "resources/app.asar")) + err = copy.Copy(fullPath(path.Join(diList[len(diList)-1].fileName, "app.asar")), fullPath(path.Join(diList[0].fileName, "resources/app.asar"))) } if err != nil { - closeChan() - fmt.Println("请尝试以管理员身份运行此程序:") + close(stopCh) + fmt.Printf("\n\n请尝试以管理员身份运行此程序:\n") exit(err.Error()) } } - if updateCore { - if cfw.portableData { - if err := copy.Copy(cfw.rootPath+"/data", fullPath(diList[0].fileName+"/data")); err != nil { - closeChan() - exit(err.Error()) - } +} + +func updateProcess(diList []*downloadInfo, stopCh chan struct{}) { + closeChan := func() { + close(stopCh) + fmt.Printf("\n\n") + } + if ci.installVersion { + if updateCore { + exec.Command(fullPath(diList[0].fileFullName), "/S").Run() } + var checkInfo *cfwInfo for { - if err := os.RemoveAll(cfw.rootPath); err == nil { + if checkInfo = checkCfw(); checkInfo != nil { break } } - if err := copy.Copy(fullPath(diList[0].fileName), cfw.rootPath); err != nil { - closeChan() - exit(err.Error()) + checkInfo.process.Kill() + transUpdate(diList, stopCh) + } else { + transUpdate(diList, stopCh) + if updateCore { + if ci.portableData { + if err := copy.Copy(ci.rootPath+"/data", fullPath(diList[0].fileName+"/data")); err != nil { + closeChan() + exit(err.Error()) + } + } + for { + if err := os.RemoveAll(ci.rootPath); err == nil { + break + } + } + if err := copy.Copy(fullPath(diList[0].fileName), ci.rootPath); err != nil { + closeChan() + exit(err.Error()) + } } } +} + +func updateCfw(diList []*downloadInfo) { + var stopCh chan struct{} + if updateCore || updateTrans { + stopCh = make(chan struct{}) + go showProgress("更新cfw中", stopCh) + ci.process.Kill() + } + updateProcess(diList, stopCh) if updateCore || updateTrans { startBackground() - go exec.Command(path.Join(cfw.rootPath, "Clash for Windows.exe")).Run() + go exec.Command(path.Join(ci.rootPath, "Clash for Windows.exe")).Run() for { if checkCfw() != nil { break } } - closeChan() - fmt.Printf("更新成功!\n\n") + close(stopCh) + fmt.Printf("\n\n更新成功!\n\n") } } -func checkEnv() *cfwInfo { - var cfwInfo *cfwInfo +func checkEnv() { fmt.Println("正在获取本机cfw信息..") - if cfwInfo = checkCfw(); cfwInfo == nil { + if ci = checkCfw(); ci == nil { exit("请先运行Clash for Windows再来更新!") } - currentVersion = cfwInfo.version - proxyUrl := fmt.Sprintf("127.0.0.1:%s", cfwInfo.mixPort) + proxyUrl := fmt.Sprintf("127.0.0.1:%s", ci.mixPort) os.Setenv("HTTP_PROXY", proxyUrl) os.Setenv("HTTPS_PROXY", proxyUrl) - if !cfwInfo.installVersion { - fmt.Println("正在获取cfw最新的版本号..") - cfwVersionList = recentlyTag("https://github.com/Fndroid/clash_for_windows_pkg/tags") - } + fmt.Println("正在获取cfw最新的版本号..") + cfwVersionList = recentlyTag("https://github.com/Fndroid/clash_for_windows_pkg/tags") updateUpdater() - if !cfwInfo.installVersion { - cfwSelect() - } + cfwSelect() tranSelect() - return cfwInfo } func task() { - cfwInfo := checkEnv() + checkEnv() defer timeCost(time.Now()) tempPath = "temp_" + time.Now().Format("200601021504") os.Mkdir(tempPath, os.ModePerm) diList := downloadPack() - updateCfw(cfwInfo, diList) + updateCfw(diList) } func main() {