init commit
This commit is contained in:
45
internal/watch/utils.go
Normal file
45
internal/watch/utils.go
Normal file
@ -0,0 +1,45 @@
|
||||
package watch
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/codfrm/cago/pkg/logger"
|
||||
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
|
||||
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
|
||||
dnspod "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod/v20210323"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func (w *watch) init() (*dnspod.RecordListItem, error) {
|
||||
w.credential = common.NewCredential(
|
||||
w.config.SecretID,
|
||||
w.config.SecretKey,
|
||||
)
|
||||
cpf := profile.NewClientProfile()
|
||||
cpf.HttpProfile.Endpoint = "dnspod.tencentcloudapi.com"
|
||||
var err error
|
||||
w.client, err = dnspod.NewClient(w.credential, "", cpf)
|
||||
if err != nil {
|
||||
logger.Default().Error("NewClient error", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 实例化一个请求对象,每个接口都会对应一个request对象
|
||||
request := dnspod.NewDescribeRecordListRequest()
|
||||
|
||||
request.Domain = common.StringPtr(w.config.Domain)
|
||||
|
||||
response, err := w.client.DescribeRecordList(request)
|
||||
if err != nil {
|
||||
logger.Default().Error("DescribeRecordList error", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
for _, v := range response.Response.RecordList {
|
||||
if *v.Name == w.config.Name && *v.Value == w.config.Value {
|
||||
logger.Default().Info("record found", zap.Any("record", v))
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
return nil, errors.New("record not found")
|
||||
|
||||
}
|
115
internal/watch/watch.go
Normal file
115
internal/watch/watch.go
Normal file
@ -0,0 +1,115 @@
|
||||
package watch
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/codfrm/cago"
|
||||
"github.com/codfrm/cago/configs"
|
||||
"github.com/codfrm/cago/pkg/logger"
|
||||
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
|
||||
dnspod "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod/v20210323"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
SecretID string `yaml:"secretID"` // 账号 secret id
|
||||
SecretKey string `yaml:"secretKey"` // 账号 secret key
|
||||
Domain string // 域名
|
||||
Name string // 记录名
|
||||
Value string // 记录值
|
||||
}
|
||||
|
||||
type watch struct {
|
||||
config *Config
|
||||
credential *common.Credential
|
||||
client *dnspod.Client
|
||||
isDisable bool
|
||||
}
|
||||
|
||||
func Watch() cago.Component {
|
||||
return &watch{
|
||||
isDisable: false,
|
||||
}
|
||||
}
|
||||
|
||||
func (w *watch) Start(ctx context.Context, cfg *configs.Config) error {
|
||||
config := &Config{}
|
||||
if err := cfg.Scan("watch", config); err != nil {
|
||||
return err
|
||||
}
|
||||
// 获取记录列表
|
||||
w.config = config
|
||||
record, err := w.init()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
go w.watch(ctx, record)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *watch) CloseHandle() {
|
||||
|
||||
}
|
||||
|
||||
// watch 每分钟检查ip是否可以访问, 无法访问自动暂停记录
|
||||
func (w *watch) watch(ctx context.Context, record *dnspod.RecordListItem) {
|
||||
t := time.NewTicker(time.Second)
|
||||
count := 0
|
||||
for {
|
||||
select {
|
||||
case <-t.C:
|
||||
// 检查ip是否可以访问
|
||||
count++
|
||||
if err := w.checkIP(ctx, w.config.Value); err != nil {
|
||||
// 连续3次无法访问,暂停记录
|
||||
if !w.isDisable && count > 3 {
|
||||
count = 0
|
||||
// 暂停记录
|
||||
request := dnspod.NewModifyRecordStatusRequest()
|
||||
request.SetContext(ctx)
|
||||
request.Domain = common.StringPtr(w.config.Domain)
|
||||
request.RecordId = common.Uint64Ptr(*record.RecordId)
|
||||
request.Status = common.StringPtr("DISABLE")
|
||||
_, err := w.client.ModifyRecordStatus(request)
|
||||
if err != nil {
|
||||
logger.Ctx(ctx).Error("modify record status err", zap.Error(err))
|
||||
} else {
|
||||
logger.Ctx(ctx).Info("modify record status success",
|
||||
zap.String("status", "DISABLE"))
|
||||
w.isDisable = true
|
||||
}
|
||||
}
|
||||
} else if w.isDisable && count > 3 {
|
||||
// 检查连续成功3次,开启记录
|
||||
count = 0
|
||||
request := dnspod.NewModifyRecordStatusRequest()
|
||||
request.SetContext(ctx)
|
||||
request.Domain = common.StringPtr(w.config.Domain)
|
||||
request.RecordId = common.Uint64Ptr(*record.RecordId)
|
||||
request.Status = common.StringPtr("ENABLE")
|
||||
_, err := w.client.ModifyRecordStatus(request)
|
||||
if err != nil {
|
||||
logger.Ctx(ctx).Error("modify record status err", zap.Error(err))
|
||||
} else {
|
||||
logger.Ctx(ctx).Info("modify record status success",
|
||||
zap.String("status", "ENABLE"))
|
||||
w.isDisable = false
|
||||
}
|
||||
} else {
|
||||
logger.Ctx(ctx).Info("ip is ok")
|
||||
}
|
||||
case <-ctx.Done():
|
||||
t.Stop()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (w *watch) checkIP(ctx context.Context, ip string) error {
|
||||
con, err := net.DialTimeout("tcp", ip+":80", time.Second*10)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return con.Close()
|
||||
}
|
Reference in New Issue
Block a user