mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-04 04:30:05 +00:00
chore: cleanup some code (#1069)
* chore * modernize loop * ddns: simpify Provider struct
This commit is contained in:
2
go.mod
2
go.mod
@@ -29,6 +29,7 @@ require (
|
|||||||
github.com/robfig/cron/v3 v3.0.1
|
github.com/robfig/cron/v3 v3.0.1
|
||||||
github.com/swaggo/files v1.0.1
|
github.com/swaggo/files v1.0.1
|
||||||
github.com/swaggo/gin-swagger v1.6.0
|
github.com/swaggo/gin-swagger v1.6.0
|
||||||
|
github.com/swaggo/swag v1.16.4
|
||||||
github.com/tidwall/gjson v1.18.0
|
github.com/tidwall/gjson v1.18.0
|
||||||
golang.org/x/crypto v0.36.0
|
golang.org/x/crypto v0.36.0
|
||||||
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394
|
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394
|
||||||
@@ -74,7 +75,6 @@ require (
|
|||||||
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
|
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
|
||||||
github.com/pkg/errors v0.9.1 // indirect
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
|
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
|
||||||
github.com/swaggo/swag v1.16.4 // indirect
|
|
||||||
github.com/tidwall/match v1.1.1 // indirect
|
github.com/tidwall/match v1.1.1 // indirect
|
||||||
github.com/tidwall/pretty v1.2.1 // indirect
|
github.com/tidwall/pretty v1.2.1 // indirect
|
||||||
github.com/tidwall/sjson v1.2.5 // indirect
|
github.com/tidwall/sjson v1.2.5 // indirect
|
||||||
|
|||||||
@@ -116,7 +116,7 @@ func searchByIDPri[S ~[]E, E CommonInterface](seq iter.Seq[string], x S) S {
|
|||||||
var class E
|
var class E
|
||||||
split, ok := any(class).(splitter[S, E])
|
split, ok := any(class).(splitter[S, E])
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
return x
|
||||||
}
|
}
|
||||||
|
|
||||||
plist, list2 := split.SplitList(x)
|
plist, list2 := split.SplitList(x)
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ type DDNSProfile struct {
|
|||||||
DomainsRaw string `json:"-"`
|
DomainsRaw string `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d DDNSProfile) TableName() string {
|
func (d *DDNSProfile) TableName() string {
|
||||||
return "ddns"
|
return "ddns"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,10 +20,8 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Provider struct {
|
type Provider struct {
|
||||||
ipAddr string
|
prefix string
|
||||||
recordType string
|
zone string
|
||||||
prefix string
|
|
||||||
zone string
|
|
||||||
|
|
||||||
DDNSProfile *model.DDNSProfile
|
DDNSProfile *model.DDNSProfile
|
||||||
IPAddrs *model.IP
|
IPAddrs *model.IP
|
||||||
@@ -36,7 +34,7 @@ func (provider *Provider) GetProfileID() uint64 {
|
|||||||
|
|
||||||
func (provider *Provider) UpdateDomain(ctx context.Context, overrideDomains ...string) {
|
func (provider *Provider) UpdateDomain(ctx context.Context, overrideDomains ...string) {
|
||||||
for _, domain := range utils.IfOr(len(overrideDomains) > 0, overrideDomains, provider.DDNSProfile.Domains) {
|
for _, domain := range utils.IfOr(len(overrideDomains) > 0, overrideDomains, provider.DDNSProfile.Domains) {
|
||||||
for retries := 0; retries < int(provider.DDNSProfile.MaxRetries); retries++ {
|
for retries := range int(provider.DDNSProfile.MaxRetries) {
|
||||||
log.Printf("NEZHA>> Updating DNS Record of domain %s: %d/%d", domain, retries+1, provider.DDNSProfile.MaxRetries)
|
log.Printf("NEZHA>> Updating DNS Record of domain %s: %d/%d", domain, retries+1, provider.DDNSProfile.MaxRetries)
|
||||||
if err := provider.updateDomain(ctx, domain); err != nil {
|
if err := provider.updateDomain(ctx, domain); err != nil {
|
||||||
log.Printf("NEZHA>> Failed to update DNS record of domain %s: %v", domain, err)
|
log.Printf("NEZHA>> Failed to update DNS record of domain %s: %v", domain, err)
|
||||||
@@ -57,17 +55,13 @@ func (provider *Provider) updateDomain(ctx context.Context, domain string) error
|
|||||||
|
|
||||||
// 当IPv4和IPv6同时成功才算作成功
|
// 当IPv4和IPv6同时成功才算作成功
|
||||||
if *provider.DDNSProfile.EnableIPv4 {
|
if *provider.DDNSProfile.EnableIPv4 {
|
||||||
provider.recordType = getRecordString(true)
|
if err = provider.addDomainRecord(ctx, "A", provider.IPAddrs.IPv4Addr); err != nil {
|
||||||
provider.ipAddr = provider.IPAddrs.IPv4Addr
|
|
||||||
if err = provider.addDomainRecord(ctx); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if *provider.DDNSProfile.EnableIPv6 {
|
if *provider.DDNSProfile.EnableIPv6 {
|
||||||
provider.recordType = getRecordString(false)
|
if err = provider.addDomainRecord(ctx, "AAAA", provider.IPAddrs.IPv6Addr); err != nil {
|
||||||
provider.ipAddr = provider.IPAddrs.IPv6Addr
|
|
||||||
if err = provider.addDomainRecord(ctx); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -75,13 +69,13 @@ func (provider *Provider) updateDomain(ctx context.Context, domain string) error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (provider *Provider) addDomainRecord(ctx context.Context) error {
|
func (provider *Provider) addDomainRecord(ctx context.Context, recType, addr string) error {
|
||||||
_, err := provider.Setter.SetRecords(ctx, provider.zone,
|
_, err := provider.Setter.SetRecords(ctx, provider.zone,
|
||||||
[]libdns.Record{
|
[]libdns.Record{
|
||||||
{
|
{
|
||||||
Type: provider.recordType,
|
Type: recType,
|
||||||
Name: provider.prefix,
|
Name: provider.prefix,
|
||||||
Value: provider.ipAddr,
|
Value: addr,
|
||||||
TTL: time.Minute,
|
TTL: time.Minute,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -123,10 +117,3 @@ func (provider *Provider) splitDomainSOA(ctx context.Context, domain string) (pr
|
|||||||
|
|
||||||
return "", "", fmt.Errorf("SOA record not found for domain: %s", domain)
|
return "", "", fmt.Errorf("SOA record not found for domain: %s", domain)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getRecordString(isIpv4 bool) string {
|
|
||||||
if isIpv4 {
|
|
||||||
return "A"
|
|
||||||
}
|
|
||||||
return "AAAA"
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -7,8 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Internal use
|
// Internal use
|
||||||
type Provider struct {
|
type Provider struct{}
|
||||||
}
|
|
||||||
|
|
||||||
func (provider *Provider) SetRecords(ctx context.Context, zone string,
|
func (provider *Provider) SetRecords(ctx context.Context, zone string,
|
||||||
recs []libdns.Record) ([]libdns.Record, error) {
|
recs []libdns.Record) ([]libdns.Record, error) {
|
||||||
|
|||||||
@@ -8,21 +8,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrGjsonNotFound = errors.New("specified path does not exist")
|
|
||||||
ErrGjsonWrongType = errors.New("wrong type")
|
ErrGjsonWrongType = errors.New("wrong type")
|
||||||
)
|
)
|
||||||
|
|
||||||
var emptyIterator = func(yield func(string, string) bool) {}
|
var emptyIterator = func(yield func(string, string) bool) {}
|
||||||
|
|
||||||
func GjsonGet(json []byte, path string) (gjson.Result, error) {
|
|
||||||
result := gjson.GetBytes(json, path)
|
|
||||||
if !result.Exists() {
|
|
||||||
return result, ErrGjsonNotFound
|
|
||||||
}
|
|
||||||
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GjsonIter(json string) (iter.Seq2[string, string], error) {
|
func GjsonIter(json string) (iter.Seq2[string, string], error) {
|
||||||
if json == "" {
|
if json == "" {
|
||||||
return emptyIterator, nil
|
return emptyIterator, nil
|
||||||
|
|||||||
@@ -187,7 +187,6 @@ func checkStatus() {
|
|||||||
// 清理旧数据
|
// 清理旧数据
|
||||||
if max > 0 && max < len(alertsStore[alert.ID][server.ID]) {
|
if max > 0 && max < len(alertsStore[alert.ID][server.ID]) {
|
||||||
index := len(alertsStore[alert.ID][server.ID]) - max
|
index := len(alertsStore[alert.ID][server.ID]) - max
|
||||||
clear(alertsStore[alert.ID][server.ID][:index]) // for GC
|
|
||||||
alertsStore[alert.ID][server.ID] = alertsStore[alert.ID][server.ID][index:]
|
alertsStore[alert.ID][server.ID] = alertsStore[alert.ID][server.ID][index:]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -496,7 +496,6 @@ func (ss *ServiceSentinel) worker() {
|
|||||||
log.Printf("NEZHA>> Failed to save service monitor metrics: %v", err)
|
log.Printf("NEZHA>> Failed to save service monitor metrics: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
clear(ss.serviceCurrentStatusData[mh.GetId()].result)
|
|
||||||
ss.serviceCurrentStatusData[mh.GetId()].result = ss.serviceCurrentStatusData[mh.GetId()].result[:0]
|
ss.serviceCurrentStatusData[mh.GetId()].result = ss.serviceCurrentStatusData[mh.GetId()].result[:0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user