feat: 添加通知
This commit is contained in:
parent
a1a65e8928
commit
3ed3047e35
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/codfrm/dnspod-watch/pkg/pushcat"
|
||||
"log"
|
||||
|
||||
"github.com/codfrm/cago"
|
||||
@ -19,6 +20,7 @@ func main() {
|
||||
|
||||
err = cago.New(ctx, cfg).
|
||||
Registry(cago.FuncComponent(logger.Logger)).
|
||||
Registry(pushcat.Pushcat()).
|
||||
Registry(watch.Watch()).
|
||||
Start()
|
||||
if err != nil {
|
||||
|
@ -2,6 +2,8 @@ package watch
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/codfrm/dnspod-watch/pkg/pushcat"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
@ -52,13 +54,20 @@ func (r *record) watch(ctx context.Context) {
|
||||
request.RecordId = common.Uint64Ptr(*r.record.RecordId)
|
||||
request.Status = common.StringPtr("DISABLE")
|
||||
_, err := r.w.client.ModifyRecordStatus(request)
|
||||
msg := fmt.Sprintf("域名: %s, 记录: %s, ip无法访问,暂停记录", r.domain, r.value)
|
||||
if err != nil {
|
||||
r.logger.Ctx(ctx).Error("modify record status err", zap.Error(err))
|
||||
msg += "\n记录修改失败: " + err.Error()
|
||||
} else {
|
||||
r.logger.Ctx(ctx).Info("modify record status success",
|
||||
zap.String("status", "DISABLE"))
|
||||
r.isDisable = true
|
||||
}
|
||||
if err := pushcat.Send(ctx, "ip无法访问,暂停记录", msg); err != nil {
|
||||
r.logger.Ctx(ctx).Error("发送通知错误",
|
||||
zap.Error(err),
|
||||
zap.String("msg", msg))
|
||||
}
|
||||
}
|
||||
} else if r.isDisable && count > 3 {
|
||||
// 检查连续成功3次,开启记录
|
||||
@ -69,13 +78,20 @@ func (r *record) watch(ctx context.Context) {
|
||||
request.RecordId = common.Uint64Ptr(*r.record.RecordId)
|
||||
request.Status = common.StringPtr("ENABLE")
|
||||
_, err := r.w.client.ModifyRecordStatus(request)
|
||||
msg := fmt.Sprintf("域名: %s, 记录: %s, ip可以访问,开启记录", r.domain, r.value)
|
||||
if err != nil {
|
||||
r.logger.Ctx(ctx).Error("modify record status err", zap.Error(err))
|
||||
msg += "\n记录修改失败: " + err.Error()
|
||||
} else {
|
||||
r.logger.Ctx(ctx).Info("modify record status success",
|
||||
zap.String("status", "ENABLE"))
|
||||
r.isDisable = false
|
||||
}
|
||||
if err := pushcat.Send(ctx, "ip可以访问,开启记录", msg); err != nil {
|
||||
r.logger.Ctx(ctx).Error("发送通知错误",
|
||||
zap.Error(err),
|
||||
zap.String("msg", msg))
|
||||
}
|
||||
} else {
|
||||
r.logger.Ctx(ctx).Info("ip is ok")
|
||||
}
|
||||
|
70
pkg/pushcat/pushcat.go
Normal file
70
pkg/pushcat/pushcat.go
Normal file
@ -0,0 +1,70 @@
|
||||
package pushcat
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/codfrm/cago"
|
||||
"github.com/codfrm/cago/configs"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
AccessToken []string `yaml:"accessToken"`
|
||||
}
|
||||
|
||||
type Data struct {
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
var defaultConfig *Config
|
||||
|
||||
func Pushcat() cago.FuncComponent {
|
||||
return func(ctx context.Context, cfg *configs.Config) error {
|
||||
defaultConfig = &Config{}
|
||||
if err := cfg.Scan("pushcat", defaultConfig); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func Send(ctx context.Context, title, msg string) error {
|
||||
url := "https://sct.icodef.com/openapi/v1/message/send"
|
||||
|
||||
data := &Data{
|
||||
Title: title,
|
||||
Content: msg,
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, token := range defaultConfig.AccessToken {
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
respData, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return errors.New(string(respData))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user