-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from yliu7949/v0.9.0
V0.9.0
- Loading branch information
Showing
16 changed files
with
266 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
|
||
*.exe | ||
*.old | ||
*.token | ||
*.txt | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package proxy | ||
|
||
import ( | ||
"crypto/tls" | ||
"log" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
var Client = http.Client{} | ||
|
||
func EnableProxy(proxyURL string) { | ||
proxyFunc := http.ProxyFromEnvironment | ||
if proxyURL != "" { | ||
u, err := url.Parse(proxyURL) | ||
if err != nil { | ||
log.Fatal("Parse proxy url error: ", err) | ||
} | ||
proxyFunc = http.ProxyURL(u) | ||
} | ||
|
||
Client = http.Client{ | ||
Transport: &http.Transport{ | ||
TLSClientConfig: &tls.Config{ | ||
InsecureSkipVerify: true, | ||
}, | ||
Proxy: proxyFunc, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
//go:build !windows | ||
|
||
package upgrade | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
func fileReplace() { | ||
_ = os.Remove(ksFilePath + ksFileName) | ||
_ = os.Rename(ksFilePath+ksFileName+".new", ksFilePath+ksFileName) | ||
|
||
cmd := exec.Command(ksFilePath+ksFileName, "version") | ||
output, _ := cmd.Output() | ||
fmt.Println(string(output)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//go:build windows | ||
|
||
package upgrade | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"syscall" | ||
) | ||
|
||
func fileReplace() { | ||
_ = os.Rename(ksFilePath+ksFileName, ksOldFile) | ||
_ = os.Rename(ksFilePath+ksFileName+".new", ksFilePath+ksFileName) | ||
|
||
cmd := exec.Command(ksFilePath+ksFileName, "version") | ||
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} | ||
output, _ := cmd.Output() | ||
fmt.Println(string(output)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package upgrade | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
|
||
"github.com/yliu7949/KouShare-dl/internal/color" | ||
"github.com/yliu7949/KouShare-dl/internal/proxy" | ||
) | ||
|
||
var ( | ||
ksFileName string | ||
ksFilePath string | ||
ksOldFile string | ||
) | ||
|
||
func init() { | ||
if runtime.GOOS == "windows" { | ||
ksFileName = "ks.exe" | ||
} else { | ||
ksFileName = "ks" | ||
} | ||
|
||
binaryFilePath, _ := os.Executable() | ||
ksFilePath = filepath.Dir(binaryFilePath) + string(os.PathSeparator) | ||
ksOldFile = ksFilePath + ksFileName + ".old" | ||
} | ||
|
||
// GetLatestVersion 获取最新的KouShare-dl版本号 | ||
func GetLatestVersion() string { | ||
_ = os.Remove(ksOldFile) | ||
latestVersion, _ := net.LookupTXT("ks-version.gleamoe.com") | ||
return latestVersion[0] | ||
} | ||
|
||
// Upgrade 查询并升级KouShare-dl至最新版本 | ||
func Upgrade() { | ||
_ = os.Remove(ksOldFile) | ||
|
||
fmt.Println("正在更新KouShare-dl ...") | ||
if downloadBinaryFile() != nil { | ||
_ = os.Remove(ksFilePath + ksFileName + ".new") | ||
fmt.Println(color.Error("无法完整下载新版本程序,请访问 https://github.com/yliu7949/KouShare-dl/releases/latest 手动下载最新版本。")) | ||
return | ||
} | ||
fmt.Print(color.Done("新版本程序下载完毕。"), "\n\n") | ||
fileReplace() | ||
} | ||
|
||
func downloadBinaryFile() error { | ||
URL := fmt.Sprintf("https://github.com/yliu7949/KouShare-dl/releases/download/%s/%s", GetLatestVersion(), ksFileName) | ||
resp, err := proxy.Client.Get(URL) | ||
if err != nil { | ||
fmt.Println(err) | ||
return err | ||
} | ||
defer func() { | ||
err = resp.Body.Close() | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
}() | ||
|
||
data, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
return err | ||
} | ||
|
||
if err = os.WriteFile(ksFilePath+ksFileName+".new", data, 0664); err != nil { | ||
fmt.Println(err.Error()) | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.