Skip to content

Commit df0be40

Browse files
committed
优化检查更新功能的逻辑
1 parent 325fc9d commit df0be40

File tree

4 files changed

+29
-23
lines changed

4 files changed

+29
-23
lines changed

client/client.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func GetOwnIP(webAddr string) (ipAddr string, isIPv6 bool, err error) {
3737
return
3838
}
3939

40-
func GetLatestVersion(conf ClientConf) string {
40+
func (conf ClientConf) GetLatestVersion() string {
4141
res, err := http.Get(conf.WebAddr)
4242
if err != nil {
4343
return "N/A (请检查网络连接)"
@@ -58,8 +58,8 @@ func GetLatestVersion(conf ClientConf) string {
5858
return recv.Version
5959
}
6060

61-
func CheckLatestVersion(conf ClientConf) {
62-
LatestVersion := GetLatestVersion(conf)
61+
func (conf ClientConf) CheckLatestVersion() {
62+
LatestVersion := conf.GetLatestVersion()
6363
fmt.Println("当前版本 ", common.LocalVersion)
6464
fmt.Println("最新版本 ", LatestVersion)
6565
switch {

main-code/client/ddns-client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func main() {
5151
}
5252
}
5353
if *version {
54-
client.CheckLatestVersion(conf)
54+
conf.CheckLatestVersion()
5555
return
5656
}
5757
if !conf.EnableDdns {

main-code/server/ddns-server.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ func main() {
7171
}
7272
}
7373
if *version {
74-
server.CheckLatestVersion(conf)
74+
conf.CheckLatestVersion()
7575
return
7676
}
7777

7878
ddnsServerHandler := func(w http.ResponseWriter, req *http.Request) {
7979
info := common.PublicInfo{
8080
IP: server.GetClientIP(req),
81-
Version: server.GetLatestVersion(conf),
81+
Version: conf.GetLatestVersion(),
8282
}
8383
sendJson, getErr := json.Marshal(info)
8484
if getErr != nil {

server/server.go

+23-17
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,40 @@ var (
1616
ConfPath = WorkPath + "conf/"
1717
)
1818

19-
func GetLatestVersion(conf ServerConf) string {
19+
func (conf ServerConf) GetLatestVersion() string {
2020
if !conf.IsRoot {
21-
res, getErr := http.Get(conf.RootServerAddr)
22-
if getErr != nil {
23-
return common.LocalVersion
21+
res, err := http.Get(conf.RootServerAddr)
22+
if err != nil {
23+
return "N/A (请检查网络连接)"
2424
}
2525
defer res.Body.Close()
26-
recvJson, getErr := ioutil.ReadAll(res.Body)
27-
if getErr != nil {
28-
return common.LocalVersion
26+
recvJson, err := ioutil.ReadAll(res.Body)
27+
if err != nil {
28+
return "N/A (数据包错误)"
2929
}
3030
recv := common.PublicInfo{}
31-
getErr = json.Unmarshal(recvJson, &recv)
32-
if getErr != nil {
33-
return common.LocalVersion
31+
err = json.Unmarshal(recvJson, &recv)
32+
if err != nil {
33+
return "N/A (数据包错误)"
34+
}
35+
if recv.Version == "" {
36+
return "N/A (没有获取到版本信息)"
3437
}
3538
return recv.Version
3639
}
3740
return common.LocalVersion
3841
}
3942

40-
func CheckLatestVersion(conf ServerConf) {
43+
func (conf ServerConf) CheckLatestVersion() {
4144
if !conf.IsRoot {
42-
LatestVersion := GetLatestVersion(conf)
45+
LatestVersion := conf.GetLatestVersion()
4346
fmt.Println("当前版本 ", common.LocalVersion)
4447
fmt.Println("最新版本 ", LatestVersion)
45-
if common.CompareVersionString(LatestVersion, common.LocalVersion) {
46-
fmt.Println("\n发现新版本,请前往 https://github.com/yzy613/ddns/releases 下载")
48+
switch {
49+
case strings.Contains(LatestVersion, "N/A"):
50+
fmt.Println("\n需要手动检查更新,请前往 " + common.ProjectAddr + " 查看")
51+
case common.CompareVersionString(LatestVersion, common.LocalVersion):
52+
fmt.Println("\n发现新版本,请前往 " + common.ProjectAddr + " 下载")
4753
}
4854
} else {
4955
fmt.Println("本机是根服务器")
@@ -113,9 +119,9 @@ func Uninstall() {
113119
if IsWindows() {
114120
fmt.Println("Windows 暂不支持安装到系统")
115121
} else {
116-
getErr := os.Remove("/etc/systemd/system/ddns-server.service")
117-
if getErr != nil {
118-
fmt.Println(getErr)
122+
err := os.Remove("/etc/systemd/system/ddns-server.service")
123+
if err != nil {
124+
fmt.Println(err)
119125
return
120126
}
121127
fmt.Println("卸载服务成功")

0 commit comments

Comments
 (0)