diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index c69cebd..77a98fe 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -20,7 +20,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v5 with: - go-version: "1.22" + go-version: "1.23" - name: Test run: go test ./... diff --git a/cmd/ddns-watchdog-client/main.go b/cmd/ddns-watchdog-client/main.go index 74813ca..aaf513a 100644 --- a/cmd/ddns-watchdog-client/main.go +++ b/cmd/ddns-watchdog-client/main.go @@ -45,8 +45,7 @@ func main() { } // 加载服务配置 - err = loadConf() - if err != nil { + if err = loadConf(); err != nil { log.Fatal(err) } @@ -79,8 +78,7 @@ func processFlag() (exit bool, err error) { for _, key := range arr { fmt.Printf("%v\n\t%v\n", key, ncr[key]) } - exit = true - return + return true, nil } // 加载自定义配置文件目录 @@ -91,45 +89,31 @@ func processFlag() (exit bool, err error) { // 有选择地初始化配置文件 if *initOption != "" { for _, event := range *initOption { - err = initConf(string(event)) - if err != nil { + if err = initConf(string(event)); err != nil { return } } - exit = true - return + return true, nil } // 加载客户端配置 // 不得不放在这个地方,因为有下面的检查版本和安装 / 卸载服务 - err = client.Client.LoadConf() - if err != nil { + if err = client.Client.LoadConf(); err != nil { return } // 检查版本 if *version { client.Client.CheckLatestVersion() - exit = true - return + return true, nil } // 安装 / 卸载服务 switch { case *installOption: - err = client.Install() - if err != nil { - return - } - exit = true - return + return true, client.Install() case *uninstallOption: - err = client.Uninstall() - if err != nil { - return - } - exit = true - return + return true, client.Uninstall() } return } @@ -151,7 +135,7 @@ func initConf(event string) (err error) { err = errors.New("你初始化了一个寂寞") } if err != nil { - return err + return } log.Println(msg) return @@ -160,26 +144,22 @@ func initConf(event string) (err error) { func loadConf() (err error) { if !client.Client.Center.Enable { if client.Client.Services.DNSPod { - err = client.DP.LoadConf() - if err != nil { + if err = client.DP.LoadConf(); err != nil { return } } if client.Client.Services.AliDNS { - err = client.AD.LoadConf() - if err != nil { + if err = client.AD.LoadConf(); err != nil { return } } if client.Client.Services.Cloudflare { - err = client.Cf.LoadConf() - if err != nil { + if err = client.Cf.LoadConf(); err != nil { return } } if client.Client.Services.HuaweiCloud { - err = client.HC.LoadConf() - if err != nil { + if err = client.HC.LoadConf(); err != nil { return } } @@ -278,8 +258,7 @@ func accessCenter(ipv4, ipv6 string) { return } defer func(Body io.ReadCloser) { - t := Body.Close() - if t != nil { + if t := Body.Close(); t != nil { err = t } }(resp.Body) @@ -295,8 +274,7 @@ func accessCenter(ipv4, ipv6 string) { } if len(respBodyJson) > 0 { var respBody = common.GeneralResp{} - err = json.Unmarshal(respBodyJson, &respBody) - if err != nil { + if err = json.Unmarshal(respBodyJson, &respBody); err != nil { log.Println(err) return } diff --git a/cmd/ddns-watchdog-server/main.go b/cmd/ddns-watchdog-server/main.go index ab81a05..9fcfc97 100644 --- a/cmd/ddns-watchdog-server/main.go +++ b/cmd/ddns-watchdog-server/main.go @@ -50,8 +50,7 @@ func main() { // 加载白名单 if server.Srv.CenterService { - err = server.Services.LoadConf() - if err != nil { + if err = server.Services.LoadConf(); err != nil { log.Fatal(err) } // 路由绑定函数 @@ -94,13 +93,11 @@ func processFlag() (exit bool, err error) { // 初始化配置 if *initOption != "" { for _, event := range *initOption { - err = initConf(string(event)) - if err != nil { + if err = initConf(string(event)); err != nil { return } } - exit = true - return + return true, nil } if *deleteB { @@ -114,8 +111,7 @@ func processFlag() (exit bool, err error) { return } fmt.Print(msg) - exit = true - return + return true, nil } currentToken := "" @@ -155,6 +151,7 @@ func processFlag() (exit bool, err error) { } } exit = true + switch status { case server.InsertSign: fmt.Printf("Added %v(%v) to whitelist.\n", m, currentToken) @@ -169,34 +166,22 @@ func processFlag() (exit bool, err error) { } // 加载配置 - err = server.Srv.LoadConf() - if err != nil { + if err = server.Srv.LoadConf(); err != nil { return } // 版本信息 if *version { server.Srv.CheckLatestVersion() - exit = true - return + return true, nil } // 安装 / 卸载服务 switch { case *installOption: - err = server.Install() - if err != nil { - return - } - exit = true - return + return true, server.Install() case *uninstallOption: - err = server.Uninstall() - if err != nil { - return - } - exit = true - return + return true, server.Uninstall() } return } @@ -214,7 +199,7 @@ func initConf(event string) (err error) { err = errors.New("你初始化了一个寂寞") } if err != nil { - return err + return } log.Println(msg) return diff --git a/go.mod b/go.mod index e19ff78..59e7785 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,11 @@ module ddns-watchdog -go 1.22 +go 1.23 require ( - github.com/aliyun/alibaba-cloud-sdk-go v1.62.758 + github.com/aliyun/alibaba-cloud-sdk-go v1.63.27 github.com/bitly/go-simplejson v0.5.1 - github.com/huaweicloud/huaweicloud-sdk-go-v3 v0.1.99 + github.com/huaweicloud/huaweicloud-sdk-go-v3 v0.1.117 github.com/spf13/pflag v1.0.5 ) @@ -16,8 +16,8 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b // indirect github.com/tjfoc/gmsm v1.4.1 // indirect - go.mongodb.org/mongo-driver v1.15.0 // indirect - golang.org/x/crypto v0.24.0 // indirect + go.mongodb.org/mongo-driver v1.17.1 // indirect + golang.org/x/crypto v0.28.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index ab6552e..cb6bb6f 100644 --- a/go.sum +++ b/go.sum @@ -4,8 +4,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03 github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/HdrHistogram/hdrhistogram-go v1.1.2/go.mod h1:yDgFjdqOqDEKOvasDdhWNXYg9BVp4O+o5f6V/ehm6Oo= github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw= -github.com/aliyun/alibaba-cloud-sdk-go v1.62.758 h1:jAq1MJDZkNZlqSAmzdyu5vauCB2O1Ki8BsJNkcFER00= -github.com/aliyun/alibaba-cloud-sdk-go v1.62.758/go.mod h1:SOSDHfe1kX91v3W5QiBsWSLqeLxImobbMX1mxrFHsVQ= +github.com/aliyun/alibaba-cloud-sdk-go v1.63.27 h1:mDQ6eofwppTbmvlh8q3+76Oq3fAeN+TwpD/csiki5VE= +github.com/aliyun/alibaba-cloud-sdk-go v1.63.27/go.mod h1:SOSDHfe1kX91v3W5QiBsWSLqeLxImobbMX1mxrFHsVQ= github.com/bitly/go-simplejson v0.5.1 h1:xgwPbetQScXt1gh9BmoJ6j9JMr3TElvuIyjR8pgdoow= github.com/bitly/go-simplejson v0.5.1/go.mod h1:YOPVLzCfwK14b4Sff3oP1AmGhI9T9Vsg84etUnlyp+Q= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= @@ -39,11 +39,12 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/huaweicloud/huaweicloud-sdk-go-v3 v0.1.99 h1:8uD68DJk2jmQ/zuU5yF41Mrmur9ltIIGAh+JFT68lIU= -github.com/huaweicloud/huaweicloud-sdk-go-v3 v0.1.99/go.mod h1:lhdEO9Bbb3hZ0wG+JeK9/GqMOp/sgc92mFmVk5tNSCk= +github.com/huaweicloud/huaweicloud-sdk-go-v3 v0.1.117 h1:TUiy5+4+Q7AWNfvKjQQL6lXOylnp7HL47JyYJ+HgN+I= +github.com/huaweicloud/huaweicloud-sdk-go-v3 v0.1.117/go.mod h1:JWz2ujO9X3oU5wb6kXp+DpR2UuDj2SldDbX8T0FSuhI= github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= @@ -94,8 +95,8 @@ github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gi github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= go.mongodb.org/mongo-driver v1.12.0/go.mod h1:AZkxhPnFJUoH7kZlFkVKucV20K387miPfm7oimrSmK0= -go.mongodb.org/mongo-driver v1.15.0 h1:rJCKC8eEliewXjZGf0ddURtl7tTVy1TK3bfl0gkUSLc= -go.mongodb.org/mongo-driver v1.15.0/go.mod h1:Vzb0Mk/pa7e6cWw85R4F/endUC3u0U9jGcNU603k65c= +go.mongodb.org/mongo-driver v1.17.1 h1:Wic5cJIwJgSpBhe3lx3+/RybR5PiYRMpVFgO7cOHyIM= +go.mongodb.org/mongo-driver v1.17.1/go.mod h1:wwWm/+BuOddhcq3n68LKRmgk2wXzmF6s0SFOa0GINL4= go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= @@ -105,9 +106,9 @@ golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPh golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= -golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= -golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI= -golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= +golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw= +golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U= golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= @@ -156,13 +157,13 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= -golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58= +golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= @@ -171,6 +172,7 @@ golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/internal/client/alidns.go b/internal/client/alidns.go index 33848d2..5c16d1d 100644 --- a/internal/client/alidns.go +++ b/internal/client/alidns.go @@ -26,18 +26,16 @@ func (ad *AliDNS) InitConf() (msg string, err error) { } ad.AccessKeySecret = ad.AccessKeyId - err = common.MarshalAndSave(ad, ConfDirectoryName+"/"+AliDNSConfFileName) - msg = "初始化 " + ConfDirectoryName + "/" + AliDNSConfFileName - return + return "初始化 " + ConfDirectoryName + "/" + AliDNSConfFileName, + common.MarshalAndSave(ad, ConfDirectoryName+"/"+AliDNSConfFileName) } func (ad *AliDNS) LoadConf() (err error) { - err = common.LoadAndUnmarshal(ConfDirectoryName+"/"+AliDNSConfFileName, &ad) - if err != nil { + if err = common.LoadAndUnmarshal(ConfDirectoryName+"/"+AliDNSConfFileName, &ad); err != nil { return } if ad.AccessKeyId == "" || ad.AccessKeySecret == "" || ad.Domain == "" || (ad.SubDomain.A == "" && ad.SubDomain.AAAA == "") { - err = errors.New("请打开配置文件 " + ConfDirectoryName + "/" + AliDNSConfFileName + " 检查你的 access_key_id, access_key_secret, domain, sub_domain 并重新启动") + return errors.New("请打开配置文件 " + ConfDirectoryName + "/" + AliDNSConfFileName + " 检查你的 access_key_id, access_key_secret, domain, sub_domain 并重新启动") } return } @@ -50,8 +48,7 @@ func (ad *AliDNS) Run(enabled common.Enable, ipv4, ipv6 string) (msg []string, e errs = append(errs, err) } else if recordIP != ipv4 { // 更新解析记录 - err = ad.updateParseRecord(ipv4, recordId, "A", ad.SubDomain.A) - if err != nil { + if err = ad.updateParseRecord(ipv4, recordId, "A", ad.SubDomain.A); err != nil { errs = append(errs, err) } else { msg = append(msg, "AliDNS: "+ad.SubDomain.A+"."+ad.Domain+" 已更新解析记录 "+ipv4) @@ -65,8 +62,7 @@ func (ad *AliDNS) Run(enabled common.Enable, ipv4, ipv6 string) (msg []string, e errs = append(errs, err) } else if recordIP != ipv6 { // 更新解析记录 - err = ad.updateParseRecord(ipv6, recordId, "AAAA", ad.SubDomain.AAAA) - if err != nil { + if err = ad.updateParseRecord(ipv6, recordId, "AAAA", ad.SubDomain.AAAA); err != nil { errs = append(errs, err) } else { msg = append(msg, "AliDNS: "+ad.SubDomain.AAAA+"."+ad.Domain+" 已更新解析记录 "+ipv6) diff --git a/internal/client/client.go b/internal/client/client.go index 3f56012..92069b9 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -54,20 +54,18 @@ func (conf *client) InitConf() (msg string, err error) { conf.APIUrl.IPv6 = common.DefaultIPv6APIUrl conf.APIUrl.Version = common.DefaultAPIUrl conf.CheckCycleMinutes = 0 - err = common.MarshalAndSave(conf, ConfDirectoryName+"/"+ConfFileName) - msg = "初始化 " + ConfDirectoryName + "/" + ConfFileName - return + + return "初始化 " + ConfDirectoryName + "/" + ConfFileName, + common.MarshalAndSave(conf, ConfDirectoryName+"/"+ConfFileName) } func (conf *client) LoadConf() (err error) { - err = common.LoadAndUnmarshal(ConfDirectoryName+"/"+ConfFileName, &conf) - if err != nil { + if err = common.LoadAndUnmarshal(ConfDirectoryName+"/"+ConfFileName, &conf); err != nil { return } // 检查启用 IP 类型 if !conf.Enable.IPv4 && !conf.Enable.IPv6 { - err = errors.New("请打开客户端配置文件 " + ConfDirectoryName + "/" + ConfFileName + " 启用需要使用的 IP 类型并重新启动") - return + return errors.New("请打开客户端配置文件 " + ConfDirectoryName + "/" + ConfFileName + " 启用需要使用的 IP 类型并重新启动") } // 检查启用服务 if !conf.Center.Enable && @@ -75,8 +73,7 @@ func (conf *client) LoadConf() (err error) { !conf.Services.AliDNS && !conf.Services.Cloudflare && !conf.Services.HuaweiCloud { - err = errors.New("请打开客户端配置文件 " + ConfDirectoryName + "/" + ConfFileName + " 启用需要使用的服务并重新启动") - return + return errors.New("请打开客户端配置文件 " + ConfDirectoryName + "/" + ConfFileName + " 启用需要使用的服务并重新启动") } return } @@ -87,8 +84,7 @@ func (conf *client) GetLatestVersion() (str string) { return "N/A (请检查网络连接)" } defer func(Body io.ReadCloser) { - t := Body.Close() - if t != nil { + if t := Body.Close(); t != nil { str = t.Error() } }(resp.Body) @@ -97,8 +93,7 @@ func (conf *client) GetLatestVersion() (str string) { return "N/A (数据包错误)" } var recv = common.GetIPResp{} - err = json.Unmarshal(recvJson, &recv) - if err != nil { + if err = json.Unmarshal(recvJson, &recv); err != nil { return "N/A (数据包错误)" } if recv.Version == "" { diff --git a/internal/client/cloudflare.go b/internal/client/cloudflare.go index 11819c6..471ab53 100644 --- a/internal/client/cloudflare.go +++ b/internal/client/cloudflare.go @@ -37,18 +37,16 @@ func (cfc *Cloudflare) InitConf() (msg string, err error) { }, } - err = common.MarshalAndSave(cfc, ConfDirectoryName+"/"+CloudflareConfFileName) - msg = "初始化 " + ConfDirectoryName + "/" + CloudflareConfFileName - return + return "初始化 " + ConfDirectoryName + "/" + CloudflareConfFileName, + common.MarshalAndSave(cfc, ConfDirectoryName+"/"+CloudflareConfFileName) } func (cfc *Cloudflare) LoadConf() (err error) { - err = common.LoadAndUnmarshal(ConfDirectoryName+"/"+CloudflareConfFileName, &cfc) - if err != nil { + if err = common.LoadAndUnmarshal(ConfDirectoryName+"/"+CloudflareConfFileName, &cfc); err != nil { return } if cfc.ZoneID == "" || cfc.APIToken == "" || (cfc.Domain.A == "" && cfc.Domain.AAAA == "") { - err = errors.New("请打开配置文件 " + ConfDirectoryName + "/" + CloudflareConfFileName + " 检查你的 zone_id, api_token, domain 并重新启动") + return errors.New("请打开配置文件 " + ConfDirectoryName + "/" + CloudflareConfFileName + " 检查你的 zone_id, api_token, domain 并重新启动") } return } @@ -61,8 +59,7 @@ func (cfc *Cloudflare) Run(enabled common.Enable, ipv4, ipv6 string) (msg []stri errs = append(errs, err) } else if recordIP != ipv4 { // 更新解析记录 - err = cfc.updateParseRecord(ipv4, domainId, "A", cfc.Domain.A) - if err != nil { + if err = cfc.updateParseRecord(ipv4, domainId, "A", cfc.Domain.A); err != nil { errs = append(errs, err) } else { msg = append(msg, "Cloudflare: "+cfc.Domain.A+" 已更新解析记录 "+ipv4) @@ -76,8 +73,7 @@ func (cfc *Cloudflare) Run(enabled common.Enable, ipv4, ipv6 string) (msg []stri errs = append(errs, err) } else if recordIP != ipv6 { // 更新解析记录 - err = cfc.updateParseRecord(ipv6, domainId, "AAAA", cfc.Domain.AAAA) - if err != nil { + if err = cfc.updateParseRecord(ipv6, domainId, "AAAA", cfc.Domain.AAAA); err != nil { errs = append(errs, err) } else { msg = append(msg, "Cloudflare: "+cfc.Domain.AAAA+" 已更新解析记录 "+ipv6) @@ -101,8 +97,7 @@ func (cfc *Cloudflare) getParseRecord(domain, recordType string) (domainId, reco return } defer func(Body io.ReadCloser) { - t := Body.Close() - if t != nil { + if t := Body.Close(); t != nil { err = t } }(resp.Body) @@ -158,8 +153,7 @@ func (cfc *Cloudflare) updateParseRecord(ipAddr, domainId, recordType, domain st return } defer func(Body io.ReadCloser) { - t := Body.Close() - if t != nil { + if t := Body.Close(); t != nil { err = t } }(req.Body) @@ -170,8 +164,7 @@ func (cfc *Cloudflare) updateParseRecord(ipAddr, domainId, recordType, domain st return } defer func(Body io.ReadCloser) { - t := Body.Close() - if t != nil { + if t := Body.Close(); t != nil { err = t } }(resp.Body) diff --git a/internal/client/dnspod.go b/internal/client/dnspod.go index 76027b3..c064f23 100644 --- a/internal/client/dnspod.go +++ b/internal/client/dnspod.go @@ -29,18 +29,16 @@ func (dpc *DNSPod) InitConf() (msg string, err error) { } dpc.Token = dpc.ID - err = common.MarshalAndSave(dpc, ConfDirectoryName+"/"+DNSPodConfFileName) - msg = "初始化 " + ConfDirectoryName + "/" + DNSPodConfFileName - return + return "初始化 " + ConfDirectoryName + "/" + DNSPodConfFileName, + common.MarshalAndSave(dpc, ConfDirectoryName+"/"+DNSPodConfFileName) } func (dpc *DNSPod) LoadConf() (err error) { - err = common.LoadAndUnmarshal(ConfDirectoryName+"/"+DNSPodConfFileName, &dpc) - if err != nil { + if err = common.LoadAndUnmarshal(ConfDirectoryName+"/"+DNSPodConfFileName, &dpc); err != nil { return } if dpc.ID == "" || dpc.Token == "" || dpc.Domain == "" || (dpc.SubDomain.A == "" && dpc.SubDomain.AAAA == "") { - err = errors.New("请打开配置文件 " + ConfDirectoryName + "/" + DNSPodConfFileName + " 检查你的 id, token, domain, sub_domain 并重新启动") + return errors.New("请打开配置文件 " + ConfDirectoryName + "/" + DNSPodConfFileName + " 检查你的 id, token, domain, sub_domain 并重新启动") } return } @@ -53,8 +51,7 @@ func (dpc *DNSPod) Run(enabled common.Enable, ipv4, ipv6 string) (msg []string, errs = append(errs, err) } else if recordIP != ipv4 { // 更新解析记录 - err = dpc.updateParseRecord(ipv4, recordId, recordLineId, "A", dpc.SubDomain.A) - if err != nil { + if err = dpc.updateParseRecord(ipv4, recordId, recordLineId, "A", dpc.SubDomain.A); err != nil { errs = append(errs, err) } else { msg = append(msg, "DNSPod: "+dpc.SubDomain.A+"."+dpc.Domain+" 已更新解析记录 "+ipv4) @@ -68,8 +65,7 @@ func (dpc *DNSPod) Run(enabled common.Enable, ipv4, ipv6 string) (msg []string, errs = append(errs, err) } else if recordIP != ipv6 { // 更新解析记录 - err = dpc.updateParseRecord(ipv6, recordId, recordLineId, "AAAA", dpc.SubDomain.AAAA) - if err != nil { + if err = dpc.updateParseRecord(ipv6, recordId, recordLineId, "AAAA", dpc.SubDomain.AAAA); err != nil { errs = append(errs, err) } else { msg = append(msg, "DNSPod: "+dpc.SubDomain.AAAA+"."+dpc.Domain+" 已更新解析记录 "+ipv6) @@ -82,8 +78,7 @@ func (dpc *DNSPod) Run(enabled common.Enable, ipv4, ipv6 string) (msg []string, func checkRespondStatus(jsonObj *simplejson.Json) (err error) { statusCode := jsonObj.Get("status").Get("code").MustString() if statusCode != "1" { - err = errors.New("DNSPod: " + statusCode + ": " + jsonObj.Get("status").Get("message").MustString()) - return + return errors.New("DNSPod: " + statusCode + ": " + jsonObj.Get("status").Get("message").MustString()) } return } @@ -100,8 +95,7 @@ func (dpc *DNSPod) getParseRecord(subDomain, recordType string) (recordId, recor if err != nil { return } - err = checkRespondStatus(jsonObj) - if err != nil { + if err = checkRespondStatus(jsonObj); err != nil { return } records, err := jsonObj.Get("records").Array() @@ -136,8 +130,7 @@ func (dpc *DNSPod) updateParseRecord(ipAddr, recordId, recordLineId, recordType, if err != nil { return } - err = checkRespondStatus(jsonObj) - if err != nil { + if err = checkRespondStatus(jsonObj); err != nil { return } return @@ -174,8 +167,7 @@ func postman(url, src string) (dst []byte, err error) { return nil, err } defer func(Body io.ReadCloser) { - t := Body.Close() - if t != nil { + if t := Body.Close(); t != nil { err = t } }(req.Body) @@ -186,8 +178,7 @@ func postman(url, src string) (dst []byte, err error) { return nil, err } defer func(Body io.ReadCloser) { - t := Body.Close() - if t != nil { + if t := Body.Close(); t != nil { err = t } }(resp.Body) diff --git a/internal/client/huaweicloud.go b/internal/client/huaweicloud.go index 4eede00..6b18807 100644 --- a/internal/client/huaweicloud.go +++ b/internal/client/huaweicloud.go @@ -30,18 +30,16 @@ func (hc *HuaweiCloud) InitConf() (msg string, err error) { } hc.SecretAccessKey = hc.AccessKeyId - err = common.MarshalAndSave(hc, ConfDirectoryName+"/"+HuaweiCloudConfFileName) - msg = "初始化 " + ConfDirectoryName + "/" + HuaweiCloudConfFileName - return + return "初始化 " + ConfDirectoryName + "/" + HuaweiCloudConfFileName, + common.MarshalAndSave(hc, ConfDirectoryName+"/"+HuaweiCloudConfFileName) } func (hc *HuaweiCloud) LoadConf() (err error) { - err = common.LoadAndUnmarshal(ConfDirectoryName+"/"+HuaweiCloudConfFileName, &hc) - if err != nil { + if err = common.LoadAndUnmarshal(ConfDirectoryName+"/"+HuaweiCloudConfFileName, &hc); err != nil { return } if hc.AccessKeyId == "" || hc.SecretAccessKey == "" || hc.ZoneName == "" || (hc.Domain.A == "" && hc.Domain.AAAA == "") { - err = errors.New("请打开配置文件 " + ConfDirectoryName + "/" + HuaweiCloudConfFileName + " 检查你的 access_key_id, secret_access_key, domain 并重新启动") + return errors.New("请打开配置文件 " + ConfDirectoryName + "/" + HuaweiCloudConfFileName + " 检查你的 access_key_id, secret_access_key, domain 并重新启动") } return } @@ -59,8 +57,7 @@ func (hc *HuaweiCloud) Run(enabled common.Enable, ipv4, ipv6 string) (msg []stri if err != nil { errs = append(errs, err) } else if recordIP != ipv4 { - err = hc.updateParseRecord(ipv4, recordSetId, "A", hc.Domain.A) - if err != nil { + if err = hc.updateParseRecord(ipv4, recordSetId, "A", hc.Domain.A); err != nil { errs = append(errs, err) } else { msg = append(msg, "HuaweiCloud: "+hc.Domain.A+" 已更新解析记录 "+ipv4) @@ -72,8 +69,7 @@ func (hc *HuaweiCloud) Run(enabled common.Enable, ipv4, ipv6 string) (msg []stri if err != nil { errs = append(errs, err) } else if recordIP != ipv6 { - err = hc.updateParseRecord(ipv6, recordSetId, "AAAA", hc.Domain.AAAA) - if err != nil { + if err = hc.updateParseRecord(ipv6, recordSetId, "AAAA", hc.Domain.AAAA); err != nil { errs = append(errs, err) } else { msg = append(msg, "HuaweiCloud: "+hc.Domain.AAAA+" 已更新解析记录 "+ipv6) @@ -194,8 +190,8 @@ func (hc *HuaweiCloud) updateParseRecord(ipAddr, recordSetId, recordType, domain } request.Body = &model.UpdateRecordSetReq{ Records: &listRecordsBody, - Type: recordType, - Name: domain, + Type: &recordType, + Name: &domain, } _, err = dnsClient.UpdateRecordSet(request) return diff --git a/internal/client/public.go b/internal/client/public.go index a4dd48d..88905fb 100644 --- a/internal/client/public.go +++ b/internal/client/public.go @@ -55,8 +55,7 @@ func Install() (err error) { "RestartSec=2\n\n" + "[Install]\n" + "WantedBy=multi-user.target\n") - err = os.WriteFile(installPath, serviceContent, 0600) - if err != nil { + if err = os.WriteFile(installPath, serviceContent, 0600); err != nil { return } log.Println("可以使用 systemctl 管理 " + ProjName + " 服务了") @@ -71,8 +70,7 @@ func Uninstall() (err error) { if err != nil { return } - err = os.Remove(installPath) - if err != nil { + if err = os.Remove(installPath); err != nil { return } log.Println("卸载服务成功") @@ -114,8 +112,7 @@ func GetOwnIP(enabled common.Enable, apiUrl apiUrl, nc networkCard) (ipv4, ipv6 if err != nil { return } - err = common.MarshalAndSave(ncr, ConfDirectoryName+"/"+NetworkCardFileName) - if err != nil { + if err = common.MarshalAndSave(ncr, ConfDirectoryName+"/"+NetworkCardFileName); err != nil { return } err = errors.New("请打开 " + ConfDirectoryName + "/" + NetworkCardFileName + " 选择网卡填入 " + @@ -151,8 +148,7 @@ func GetOwnIP(enabled common.Enable, apiUrl apiUrl, nc networkCard) (ipv4, ipv6 return } defer func(Body io.ReadCloser) { - t := Body.Close() - if t != nil { + if t := Body.Close(); t != nil { err = t } }(resp.Body) @@ -162,8 +158,7 @@ func GetOwnIP(enabled common.Enable, apiUrl apiUrl, nc networkCard) (ipv4, ipv6 return } var ipInfo common.GetIPResp - err = json.Unmarshal(recvJson, &ipInfo) - if err != nil { + if err = json.Unmarshal(recvJson, &ipInfo); err != nil { return } ipv4 = ipInfo.IP @@ -194,8 +189,7 @@ func GetOwnIP(enabled common.Enable, apiUrl apiUrl, nc networkCard) (ipv4, ipv6 return } defer func(Body io.ReadCloser) { - t := Body.Close() - if t != nil { + if t := Body.Close(); t != nil { err = t } }(resp.Body) @@ -205,8 +199,7 @@ func GetOwnIP(enabled common.Enable, apiUrl apiUrl, nc networkCard) (ipv4, ipv6 return } var ipInfo common.GetIPResp - err = json.Unmarshal(recvJson, &ipInfo) - if err != nil { + if err = json.Unmarshal(recvJson, &ipInfo); err != nil { return } ipv6 = ipInfo.IP diff --git a/internal/common/common.go b/internal/common/common.go index 75eb41f..6069cdc 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -7,11 +7,12 @@ import ( "os" "path/filepath" "runtime" + "strconv" "strings" ) const ( - LocalVersion = "1.5.9" + LocalVersion = "1.5.10" DefaultAPIUrl = "https://yzyweb.cn/ddns-watchdog" DefaultIPv6APIUrl = "https://yzyweb.cn/ddns-watchdog6" ProjectUrl = "https://github.com/yzy613/ddns-watchdog" @@ -93,24 +94,21 @@ func LoadAndUnmarshal(filePath string, dst any) (err error) { if err != nil { return } - err = json.Unmarshal(jsonContent, &dst) - if err != nil { + if err = json.Unmarshal(jsonContent, &dst); err != nil { return } return } func MarshalAndSave(content any, filePath string) (err error) { - err = IsDirExistAndCreate(filepath.Dir(filePath)) - if err != nil { + if err = IsDirExistAndCreate(filepath.Dir(filePath)); err != nil { return } jsonContent, err := json.MarshalIndent(content, "", "\t") if err != nil { return } - err = os.WriteFile(filePath, jsonContent, 0600) - if err != nil { + if err = os.WriteFile(filePath, jsonContent, 0600); err != nil { return } return nil @@ -119,16 +117,21 @@ func MarshalAndSave(content any, filePath string) (err error) { func CompareVersionString(remoteVersion, localVersion string) bool { rv := strings.Split(remoteVersion, ".") lv := strings.Split(localVersion, ".") - if len(rv) <= len(lv) { - for key, value := range rv { - switch { - case value > lv[key]: - return true - case value < lv[key]: - return false - } + + for i := 0; i < max(len(rv), len(lv)); i++ { + var r, l int + if i < len(rv) { + r, _ = strconv.Atoi(rv[i]) + } + if i < len(lv) { + l, _ = strconv.Atoi(lv[i]) + } + + if r > l { + return true } } + return false } diff --git a/internal/server/handler.go b/internal/server/handler.go index 0b3114e..af9215c 100644 --- a/internal/server/handler.go +++ b/internal/server/handler.go @@ -52,8 +52,7 @@ func RespCenterReq(w http.ResponseWriter, req *http.Request) { return } var body common.CenterReq - err = json.Unmarshal(bodyJson, &body) - if err != nil { + if err = json.Unmarshal(bodyJson, &body); err != nil { log.Println(err) w.WriteHeader(http.StatusInternalServerError) return diff --git a/internal/server/public.go b/internal/server/public.go index 572a555..ea3db14 100644 --- a/internal/server/public.go +++ b/internal/server/public.go @@ -40,8 +40,7 @@ func GenerateToken(length int) (token string) { } func DelFromWhitelist(token string) (msg string, err error) { - err = common.LoadAndUnmarshal(ConfDirectoryName+"/"+WhitelistFileName, &whitelist) - if err != nil { + if err = common.LoadAndUnmarshal(ConfDirectoryName+"/"+WhitelistFileName, &whitelist); err != nil { return } if _, ok := whitelist[token]; ok { @@ -80,8 +79,7 @@ func AddToWhitelist(token, message, service, domain, a, aaaa string) (status str } // 加载白名单 - err = common.LoadAndUnmarshal(ConfDirectoryName+"/"+WhitelistFileName, &whitelist) - if err != nil { + if err = common.LoadAndUnmarshal(ConfDirectoryName+"/"+WhitelistFileName, &whitelist); err != nil { return } @@ -136,8 +134,7 @@ func AddToWhitelist(token, message, service, domain, a, aaaa string) (status str } // 保存白名单 - err = common.MarshalAndSave(whitelist, ConfDirectoryName+"/"+WhitelistFileName) - if err != nil { + if err = common.MarshalAndSave(whitelist, ConfDirectoryName+"/"+WhitelistFileName); err != nil { return } return @@ -145,8 +142,7 @@ func AddToWhitelist(token, message, service, domain, a, aaaa string) (status str func InitWhitelist() (msg string, err error) { whitelist = make(map[string]whitelistStruct) - err = common.MarshalAndSave(whitelist, ConfDirectoryName+"/"+WhitelistFileName) - if err != nil { + if err = common.MarshalAndSave(whitelist, ConfDirectoryName+"/"+WhitelistFileName); err != nil { return } msg = "初始化 " + ConfDirectoryName + "/" + WhitelistFileName @@ -154,8 +150,7 @@ func InitWhitelist() (msg string, err error) { } func LoadWhitelist() (err error) { - err = common.LoadAndUnmarshal(ConfDirectoryName+"/"+WhitelistFileName, &whitelist) - return + return common.LoadAndUnmarshal(ConfDirectoryName+"/"+WhitelistFileName, &whitelist) } func GetClientIP(req *http.Request) (ipAddr string) { @@ -207,8 +202,7 @@ func Install() (err error) { "RestartSec=2\n\n" + "[Install]\n" + "WantedBy=multi-user.target\n") - err = os.WriteFile(InstallPath, serviceContent, 0600) - if err != nil { + if err = os.WriteFile(InstallPath, serviceContent, 0600); err != nil { return } log.Println("可以使用 systemctl 控制 " + RunningName + " 服务了") @@ -223,8 +217,7 @@ func Uninstall() (err error) { if err != nil { return } - err = os.Remove(InstallPath) - if err != nil { + if err = os.Remove(InstallPath); err != nil { return } log.Println("卸载服务成功") diff --git a/internal/server/server.go b/internal/server/server.go index 5f3f298..82a11d0 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -42,8 +42,7 @@ func (conf *server) InitConf() (msg string, err error) { Center: "/center", }, } - err = common.MarshalAndSave(conf, ConfDirectoryName+"/"+ConfFileName) - if err != nil { + if err = common.MarshalAndSave(conf, ConfDirectoryName+"/"+ConfFileName); err != nil { return } msg = "初始化 " + ConfDirectoryName + "/" + ConfFileName @@ -51,8 +50,7 @@ func (conf *server) InitConf() (msg string, err error) { } func (conf *server) LoadConf() (err error) { - err = common.LoadAndUnmarshal(ConfDirectoryName+"/"+ConfFileName, &conf) - return + return common.LoadAndUnmarshal(ConfDirectoryName+"/"+ConfFileName, &conf) } func (conf *server) GetLatestVersion() (str string) { @@ -62,8 +60,7 @@ func (conf *server) GetLatestVersion() (str string) { return "N/A (请检查网络连接)" } defer func(Body io.ReadCloser) { - err = Body.Close() - if err != nil { + if err = Body.Close(); err != nil { str = err.Error() } }(resp.Body) @@ -72,8 +69,7 @@ func (conf *server) GetLatestVersion() (str string) { return "N/A (数据包错误)" } var recv = common.GetIPResp{} - err = json.Unmarshal(recvJson, &recv) - if err != nil { + if err = json.Unmarshal(recvJson, &recv); err != nil { return "N/A (数据包错误)" } if recv.Version == "" { diff --git a/internal/server/service.go b/internal/server/service.go index 99704e2..884c78c 100644 --- a/internal/server/service.go +++ b/internal/server/service.go @@ -54,8 +54,7 @@ func (conf *service) InitConf() (msg string, err error) { APIToken: "", }, } - err = common.MarshalAndSave(conf, ConfDirectoryName+"/"+ServiceConfFileName) - if err != nil { + if err = common.MarshalAndSave(conf, ConfDirectoryName+"/"+ServiceConfFileName); err != nil { return } msg = "初始化 " + ConfDirectoryName + "/" + ServiceConfFileName @@ -63,8 +62,7 @@ func (conf *service) InitConf() (msg string, err error) { } func (conf *service) LoadConf() (err error) { - err = common.LoadAndUnmarshal(ConfDirectoryName+"/"+ServiceConfFileName, &conf) - if err != nil { + if err = common.LoadAndUnmarshal(ConfDirectoryName+"/"+ServiceConfFileName, &conf); err != nil { return } err = LoadWhitelist()