Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: The webhook is only triggered once after the third failure #879

Merged
merged 2 commits into from
Oct 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion config/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ const (
UpdatedSuccess = "成功"
)

// 更新失败次数
var updatedFailedTimes = 0

// hasJSONPrefix returns true if the string starts with a JSON open brace.
func hasJSONPrefix(s string) bool {
return strings.HasPrefix(s, "{") || strings.HasPrefix(s, "[")
Expand All @@ -41,6 +44,17 @@ func ExecWebhook(domains *Domains, conf *Config) (v4Status updateStatusType, v6S
v6Status = getDomainsStatus(domains.Ipv6Domains)

if conf.WebhookURL != "" && (v4Status != UpdatedNothing || v6Status != UpdatedNothing) {
// 第3次失败才触发一次webhook
if v4Status == UpdatedFailed || v6Status == UpdatedFailed {
updatedFailedTimes++
if updatedFailedTimes != 3 {
log.Println("将不会触发Webhook,仅在第 3 次失败时触发一次Webhook,当前失败次数:", updatedFailedTimes)
return
}
} else {
updatedFailedTimes = 0
}

// 成功和失败都要触发webhook
method := "GET"
postPara := ""
Expand All @@ -50,8 +64,8 @@ func ExecWebhook(domains *Domains, conf *Config) (v4Status updateStatusType, v6S
postPara = replacePara(domains, conf.WebhookRequestBody, v4Status, v6Status)
if json.Valid([]byte(postPara)) {
contentType = "application/json"
// 如果 RequestBody 的 JSON 无效但前缀为 JSON 括号则为 JSON
} else if hasJSONPrefix(postPara) {
// 如果 RequestBody 的 JSON 无效但前缀为 JSON,提示无效
log.Println("RequestBody 的 JSON 无效!")
}
}
Expand Down