更新工作区配置,删除不再使用的Cloudflare相关文件,优化日志输出格式,增强主机信息收集功能,调整代码结构以提高可维护性。

This commit is contained in:
zeaslity
2025-03-28 00:15:08 +08:00
parent c2ca7eb6d7
commit 6816638267
19 changed files with 847 additions and 155 deletions

View File

@@ -21,10 +21,11 @@ const (
// CloudflareClient represents a client for interacting with Cloudflare API
type CloudflareClient struct {
apiToken string
baseURL string
client *http.Client
userAgent string
apiToken string
baseURL string
client *http.Client
userAgent string
dnsNameToIDMap map[string]string // 域名到ID的映射
}
var (
@@ -151,3 +152,14 @@ func (c *CloudflareClient) doRequest(method, url string, body interface{}) (*Res
log.Debug("Request completed successfully")
return &cfResp, nil
}
// GetDNSNameToIDMap 获取域名到ID的映射
func (c *CloudflareClient) GetDNSNameToIDMap(zoneID string) map[string]string {
// 如果dnsNameToIDMap为空则获取所有域名到ID的映射
if c.dnsNameToIDMap == nil {
c.ListDNSRecords(zoneID, nil)
}
return c.dnsNameToIDMap
}

View File

@@ -110,6 +110,12 @@ func (c *CloudflareClient) ListDNSRecords(zoneID string, filter *DNSRecordFilter
return nil, fmt.Errorf("failed to unmarshal DNS records: %w", err)
}
// 将域名和ID的映射关系存储到dnsNameToIDMap中
c.dnsNameToIDMap = make(map[string]string, len(records))
for _, record := range records {
c.dnsNameToIDMap[record.Name] = record.ID
}
log.Info("Successfully retrieved %d DNS records for zone ID: %s", len(records), zoneID)
return records, nil
}

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"net/url"
"strings"
"agent-wdd/log"
)
@@ -179,6 +180,23 @@ func (c *CloudflareClient) GetZone(identifier string) (*Zone, error) {
if isID {
endpoint = fmt.Sprintf("%s/zones/%s", c.baseURL, identifier)
} else {
// tc.hk.107421.xyz ipv6.t2.107421.xyz 转换为 107421.xyz
// 寻找.的个数 如果大于 从后往前找到 第二个 . 的位置
dotCount := strings.Count(identifier, ".")
if dotCount > 1 {
// 从后往前找到 第二个 . 的位置
lastDotIndex := strings.LastIndex(identifier, ".")
secondLastDotIndex := strings.LastIndex(identifier[:lastDotIndex], ".")
identifier = identifier[secondLastDotIndex+1:]
} else if dotCount == 0 {
// 没有. 报错
log.Error("未找到域名 %s 的ZoneID", identifier)
return nil, fmt.Errorf("no zone found with name: %s", identifier)
}
log.Debug("转换后的域名: %s", identifier)
// List zones with name filter
zones, err := c.ListZones(&ZoneFilter{Name: identifier})
if err != nil {