Skip to content

Commit

Permalink
fix: display the error logs first (#973)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeessy2 authored Jan 12, 2024
1 parent 1a443b7 commit 1a3cd4e
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 36 deletions.
21 changes: 16 additions & 5 deletions dns/alidns.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,17 @@ func (ali *Alidns) create(domain *config.Domain, recordType string, ipAddr strin
var result AlidnsResp
err := ali.request(params, &result)

if err == nil && result.RecordID != "" {
if err != nil {
util.Log("新增域名解析 %s 失败! 异常信息: %s", domain, err)
domain.UpdateStatus = config.UpdatedFailed
return
}

if result.RecordID != "" {
util.Log("新增域名解析 %s 成功! IP: %s", domain, ipAddr)
domain.UpdateStatus = config.UpdatedSuccess
} else {
util.Log("新增域名解析 %s 失败! 异常信息: %s", domain, err)
util.Log("新增域名解析 %s 失败! 异常信息: %s", domain, "返回RecordId为空")
domain.UpdateStatus = config.UpdatedFailed
}
}
Expand All @@ -148,11 +154,17 @@ func (ali *Alidns) modify(recordSelected AlidnsRecord, domain *config.Domain, re
var result AlidnsResp
err := ali.request(params, &result)

if err == nil && result.RecordID != "" {
if err != nil {
util.Log("更新域名解析 %s 失败! 异常信息: %s", domain, err)
domain.UpdateStatus = config.UpdatedFailed
return
}

if result.RecordID != "" {
util.Log("更新域名解析 %s 成功! IP: %s", domain, ipAddr)
domain.UpdateStatus = config.UpdatedSuccess
} else {
util.Log("更新域名解析 %s 失败! 异常信息: %s", domain, err)
util.Log("更新域名解析 %s 失败! 异常信息: %s", domain, "返回RecordId为空")
domain.UpdateStatus = config.UpdatedFailed
}
}
Expand All @@ -170,7 +182,6 @@ func (ali *Alidns) request(params url.Values, result interface{}) (err error) {
req.URL.RawQuery = params.Encode()

if err != nil {
util.Log("异常信息: %s", err)
return
}

Expand Down
1 change: 0 additions & 1 deletion dns/baidu.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ func (baidu *BaiduCloud) request(method string, url string, data interface{}, re
)

if err != nil {
util.Log("异常信息: %s", err)
return
}

Expand Down
1 change: 1 addition & 0 deletions dns/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func (cb *Callback) addUpdateDomainRecords(recordType string) {
req, err := http.NewRequest(method, u.String(), strings.NewReader(postPara))
if err != nil {
util.Log("异常信息: %s", err)
domain.UpdateStatus = config.UpdatedFailed
return
}
req.Header.Add("content-type", contentType)
Expand Down
41 changes: 35 additions & 6 deletions dns/cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"strconv"
"strings"

"github.com/jeessy2/ddns-go/v6/config"
"github.com/jeessy2/ddns-go/v6/util"
Expand Down Expand Up @@ -91,11 +92,19 @@ func (cf *Cloudflare) addUpdateDomainRecords(recordType string) {
for _, domain := range domains {
// get zone
result, err := cf.getZones(domain)
if err != nil || len(result.Result) != 1 {

if err != nil {
util.Log("查询域名信息发生异常! %s", err)
domain.UpdateStatus = config.UpdatedFailed
return
}

if len(result.Result) == 0 {
util.Log("在DNS服务商中未找到域名: %s", domain.String())
domain.UpdateStatus = config.UpdatedFailed
return
}

zoneID := result.Result[0].ID

var records CloudflareRecordsResp
Expand All @@ -107,8 +116,15 @@ func (cf *Cloudflare) addUpdateDomainRecords(recordType string) {
&records,
)

if err != nil || !records.Success {
if err != nil {
util.Log("查询域名信息发生异常! %s", err)
domain.UpdateStatus = config.UpdatedFailed
return
}

if !records.Success {
util.Log("查询域名信息发生异常! %s", strings.Join(records.Messages, ", "))
domain.UpdateStatus = config.UpdatedFailed
return
}

Expand Down Expand Up @@ -139,11 +155,18 @@ func (cf *Cloudflare) create(zoneID string, domain *config.Domain, recordType st
record,
&status,
)
if err == nil && status.Success {

if err != nil {
util.Log("新增域名解析 %s 失败! 异常信息: %s", domain, err)
domain.UpdateStatus = config.UpdatedFailed
return
}

if status.Success {
util.Log("新增域名解析 %s 成功! IP: %s", domain, ipAddr)
domain.UpdateStatus = config.UpdatedSuccess
} else {
util.Log("新增域名解析 %s 失败! 异常信息: %s", domain, err)
util.Log("新增域名解析 %s 失败! 异常信息: %s", domain, strings.Join(status.Messages, ", "))
domain.UpdateStatus = config.UpdatedFailed
}
}
Expand All @@ -169,11 +192,18 @@ func (cf *Cloudflare) modify(result CloudflareRecordsResp, zoneID string, domain
record,
&status,
)

if err != nil {
util.Log("更新域名解析 %s 失败! 异常信息: %s", domain, err)
domain.UpdateStatus = config.UpdatedFailed
return
}

if err == nil && status.Success {
util.Log("更新域名解析 %s 成功! IP: %s", domain, ipAddr)
domain.UpdateStatus = config.UpdatedSuccess
} else {
util.Log("更新域名解析 %s 失败! 异常信息: %s", domain, err)
util.Log("更新域名解析 %s 失败! 异常信息: %s", domain, strings.Join(status.Messages, ", "))
domain.UpdateStatus = config.UpdatedFailed
}
}
Expand Down Expand Up @@ -203,7 +233,6 @@ func (cf *Cloudflare) request(method string, url string, data interface{}, resul
bytes.NewBuffer(jsonStr),
)
if err != nil {
util.Log("异常信息: %s", err)
return
}
req.Header.Set("Authorization", "Bearer "+cf.DNS.Secret)
Expand Down
17 changes: 16 additions & 1 deletion dns/dnspod.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ func (dnspod *Dnspod) create(domain *config.Domain, recordType string, ipAddr st
}

status, err := dnspod.commonRequest(recordCreateAPI, params, domain)

if err != nil {
util.Log("新增域名解析 %s 失败! 异常信息: %s", domain, err)
domain.UpdateStatus = config.UpdatedFailed
return
}

if err == nil && status.Status.Code == "1" {
util.Log("新增域名解析 %s 成功! IP: %s", domain, ipAddr)
domain.UpdateStatus = config.UpdatedSuccess
Expand Down Expand Up @@ -147,8 +154,16 @@ func (dnspod *Dnspod) modify(record DnspodRecord, domain *config.Domain, recordT
if !params.Has("record_line") {
params.Set("record_line", "默认")
}

status, err := dnspod.commonRequest(recordModifyURL, params, domain)
if err == nil && status.Status.Code == "1" {

if err != nil {
util.Log("更新域名解析 %s 失败! 异常信息: %s", domain, err)
domain.UpdateStatus = config.UpdatedFailed
return
}

if status.Status.Code == "1" {
util.Log("更新域名解析 %s 成功! IP: %s", domain, ipAddr)
domain.UpdateStatus = config.UpdatedSuccess
} else {
Expand Down
4 changes: 1 addition & 3 deletions dns/google_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (gd *GoogleDomain) modify(domain *config.Domain, recordType string, ipAddr
util.Log("更新域名解析 %s 成功! IP: %s", domain, ipAddr)
domain.UpdateStatus = config.UpdatedSuccess
default:
util.Log("更新域名解析 %s 失败! 异常信息: %s", domain, result)
util.Log("更新域名解析 %s 失败! 异常信息: %s", domain, result.Status)
domain.UpdateStatus = config.UpdatedFailed
}
}
Expand All @@ -106,7 +106,6 @@ func (gd *GoogleDomain) request(params url.Values, result *GoogleDomainResp) (er
)

if err != nil {
util.Log("异常信息: %s", err)
return
}

Expand All @@ -116,7 +115,6 @@ func (gd *GoogleDomain) request(params url.Values, result *GoogleDomainResp) (er
client := util.CreateHTTPClient()
resp, err := client.Do(req)
if err != nil {
util.Log("异常信息: %s", err)
return
}

Expand Down
26 changes: 21 additions & 5 deletions dns/huawei.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,14 @@ func (hw *Huaweicloud) addUpdateDomainRecords(recordType string) {
func (hw *Huaweicloud) create(domain *config.Domain, recordType string, ipAddr string) {
zone, err := hw.getZones(domain)
if err != nil {
util.Log("查询域名信息发生异常! %s", err)
domain.UpdateStatus = config.UpdatedFailed
return
}

if len(zone.Zones) == 0 {
util.Log("在DNS服务商中未找到域名: %s", domain.String())
domain.UpdateStatus = config.UpdatedFailed
return
}

Expand All @@ -149,11 +153,18 @@ func (hw *Huaweicloud) create(domain *config.Domain, recordType string, ipAddr s
record,
&result,
)
if err == nil && (len(result.Records) > 0 && result.Records[0] == ipAddr) {

if err != nil {
util.Log("新增域名解析 %s 失败! 异常信息: %s", domain, err)
domain.UpdateStatus = config.UpdatedFailed
return
}

if len(result.Records) > 0 && result.Records[0] == ipAddr {
util.Log("新增域名解析 %s 成功! IP: %s", domain, ipAddr)
domain.UpdateStatus = config.UpdatedSuccess
} else {
util.Log("新增域名解析 %s 失败! 异常信息: %s", domain, err)
util.Log("新增域名解析 %s 失败! 异常信息: %s", domain, result.Status)
domain.UpdateStatus = config.UpdatedFailed
}
}
Expand All @@ -180,11 +191,17 @@ func (hw *Huaweicloud) modify(record HuaweicloudRecordsets, domain *config.Domai
&result,
)

if err == nil && (len(result.Records) > 0 && result.Records[0] == ipAddr) {
if err != nil {
util.Log("更新域名解析 %s 失败! 异常信息: %s", domain, err)
domain.UpdateStatus = config.UpdatedFailed
return
}

if len(result.Records) > 0 && result.Records[0] == ipAddr {
util.Log("更新域名解析 %s 成功! IP: %s", domain, ipAddr)
domain.UpdateStatus = config.UpdatedSuccess
} else {
util.Log("更新域名解析 %s 失败! 异常信息: %s", domain, err)
util.Log("更新域名解析 %s 失败! 异常信息: %s", domain, result.Status)
domain.UpdateStatus = config.UpdatedFailed
}
}
Expand Down Expand Up @@ -215,7 +232,6 @@ func (hw *Huaweicloud) request(method string, url string, data interface{}, resu
)

if err != nil {
util.Log("异常信息: %s", err)
return
}

Expand Down
7 changes: 2 additions & 5 deletions dns/namecheap.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (nc *NameCheap) modify(domain *config.Domain, recordType string, ipAddr str
err := nc.request(&result, ipAddr, domain)

if err != nil {
util.Log("更新域名解析 %s 失败! 异常信息: %s", domain, result)
util.Log("更新域名解析 %s 失败! 异常信息: %s", domain, err)
domain.UpdateStatus = config.UpdatedFailed
return
}
Expand All @@ -85,7 +85,7 @@ func (nc *NameCheap) modify(domain *config.Domain, recordType string, ipAddr str
util.Log("更新域名解析 %s 成功! IP: %s", domain, ipAddr)
domain.UpdateStatus = config.UpdatedSuccess
default:
util.Log("更新域名解析 %s 失败! 异常信息: %s", domain, result)
util.Log("更新域名解析 %s 失败! 异常信息: %s", domain, result.Status)
domain.UpdateStatus = config.UpdatedFailed
}
}
Expand All @@ -105,21 +105,18 @@ func (nc *NameCheap) request(result *NameCheapResp, ipAddr string, domain *confi
)

if err != nil {
util.Log("异常信息: %s", err)
return
}

client := util.CreateHTTPClient()
resp, err := client.Do(req)
if err != nil {
util.Log("异常信息: %s", err)
return
}

defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
util.Log("异常信息: %s", err)
return err
}

Expand Down
2 changes: 0 additions & 2 deletions dns/namesilo.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,12 @@ func (ns *NameSilo) request(ipAddr string, domain *config.Domain, recordID, reco
)

if err != nil {
util.Log("异常信息: %s", err)
return
}

client := util.CreateHTTPClient()
resp, err := client.Do(req)
if err != nil {
util.Log("异常信息: %s", err)
return
}

Expand Down
21 changes: 16 additions & 5 deletions dns/porkbun.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,17 @@ func (pb *Porkbun) create(domain *config.Domain, recordType string, ipAddr strin
&response,
)

if err == nil && response.Status == "SUCCESS" {
if err != nil {
util.Log("新增域名解析 %s 失败! 异常信息: %s", domain, err)
domain.UpdateStatus = config.UpdatedFailed
return
}

if response.Status == "SUCCESS" {
util.Log("新增域名解析 %s 成功! IP: %s", domain, ipAddr)
domain.UpdateStatus = config.UpdatedSuccess
} else {
util.Log("新增域名解析 %s 失败! 异常信息: %s", domain, err)
util.Log("新增域名解析 %s 失败! 异常信息: %s", domain, response.Status)
domain.UpdateStatus = config.UpdatedFailed
}
}
Expand Down Expand Up @@ -161,11 +167,17 @@ func (pb *Porkbun) modify(record *PorkbunDomainQueryResponse, domain *config.Dom
&response,
)

if err == nil && response.Status == "SUCCESS" {
if err != nil {
util.Log("更新域名解析 %s 失败! 异常信息: %s", domain, err)
domain.UpdateStatus = config.UpdatedFailed
return
}

if response.Status == "SUCCESS" {
util.Log("更新域名解析 %s 成功! IP: %s", domain, ipAddr)
domain.UpdateStatus = config.UpdatedSuccess
} else {
util.Log("更新域名解析 %s 失败! 异常信息: %s", domain, err)
util.Log("更新域名解析 %s 失败! 异常信息: %s", domain, response.Status)
domain.UpdateStatus = config.UpdatedFailed
}
}
Expand All @@ -182,7 +194,6 @@ func (pb *Porkbun) request(url string, data interface{}, result interface{}) (er
bytes.NewBuffer(jsonStr),
)
if err != nil {
util.Log("异常信息: %s", err)
return
}
req.Header.Set("Content-Type", "application/json")
Expand Down
Loading

0 comments on commit 1a3cd4e

Please sign in to comment.