diff --git a/cmd/ddns/main.go b/cmd/ddns/main.go index 16c4e49..281a03c 100644 --- a/cmd/ddns/main.go +++ b/cmd/ddns/main.go @@ -46,7 +46,7 @@ func Init() error { return err } default: - dnsProvider, err = dns.NewCloudFlare(config.CF_API_KEY, config.CF_API_EMAIL, log.GetDefault()) + dnsProvider, err = dns.NewCloudFlare() if err != nil { return err } diff --git a/dns/cloudflare.go b/dns/cloudflare.go index b4e5d74..3c46bff 100644 --- a/dns/cloudflare.go +++ b/dns/cloudflare.go @@ -10,32 +10,32 @@ import ( "github.com/cloudflare/cloudflare-go" "github.com/orvice/ddns/config" "github.com/orvice/ddns/notify" - "github.com/weeon/contract" ) type CloudFlare struct { client *cloudflare.API - logger contract.Logger } -func NewCloudFlare(key, email string, logger contract.Logger) (*CloudFlare, error) { - client, err := cloudflare.New(key, email) - if err != nil { - return nil, err - } - +func NewCloudFlare() (*CloudFlare, error) { + key := os.Getenv("CF_KEY") + email := os.Getenv("CF_EMAIL") token := os.Getenv("CF_TOKEN") + + var client *cloudflare.API + var err error + if token != "" { slog.Info("cf use token") client, err = cloudflare.NewWithAPIToken(token) - if err != nil { - return nil, err - } + } else { + client, err = cloudflare.New(key, email) + } + if err != nil { + return nil, err } return &CloudFlare{ client: client, - logger: logger, }, nil } @@ -104,7 +104,7 @@ func (c *CloudFlare) UpdateIP(ctx context.Context, domain, ip string) error { for _, r := range rs { if r.Type == "A" { if r.Content == ip { - c.logger.Infof("ip not change...") + slog.Info("ip not change...") continue } oldIP := r.Content diff --git a/go.mod b/go.mod index 1fd1bb4..8b5bc10 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ require ( github.com/catpie/musdk-go v0.0.0-20230815021801-7d27a00f9c60 github.com/cloudflare/cloudflare-go v0.75.0 github.com/orvice/utils v0.0.0-20180715141935-5b71615d9c36 - github.com/weeon/contract v0.0.0-20190520152601-a4ee53bdb563 github.com/weeon/log v0.0.0-20210217051817-63fe9a730962 github.com/weeon/utils v0.0.0-20190524044050-159b91233ff4 github.com/weppos/publicsuffix-go v0.4.0 @@ -21,6 +20,7 @@ require ( github.com/hashicorp/go-cleanhttp v0.5.2 // indirect github.com/hashicorp/go-retryablehttp v0.7.4 // indirect github.com/technoweenie/multipartstreamer v1.0.1 // indirect + github.com/weeon/contract v0.0.0-20190520152601-a4ee53bdb563 // indirect go.opentelemetry.io/otel v0.16.0 // indirect go.uber.org/atomic v1.6.0 // indirect go.uber.org/multierr v1.5.0 // indirect diff --git a/internal/config/config.go b/internal/config/config.go index ef8255f..c1c8471 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -14,9 +14,6 @@ const ( var ( DNS_MODE string - CF_API_KEY string - CF_API_EMAIL string - NODE_ID int API_URI string @@ -30,9 +27,6 @@ func GetConfigFromEnv() { DOMAIN = env.Get("DOMAIN") UPDATE_TIME = env.GetInt("UPDATE_TIME", 300) - CF_API_KEY = env.Get("CF_API_KEY") - CF_API_EMAIL = env.Get("CF_API_EMAIL") - NODE_ID = env.GetInt("MU_NODE_ID") API_URI = env.Get("API_URI")