diff --git a/README.md b/README.md index 82a71f9..fc941a9 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,7 @@ Usage of ./godns: * provider: The providers that GoDNS supports, available values are: `Cloudflare`, `Google`, `DNSPod`, `AliDNS`, `HE`, `DuckDNS`. * email: Email or account name of your DNS provider. * password: Password of your account. -* login_token: Login token of your account. +* login_token: API token of your account. * domains: Domains list, with your sub domains. * ip_url: A site helps you to get your public IP address. * interval: The interval `seconds` that GoDNS check your public IP. @@ -112,7 +112,9 @@ Usage of ./godns: ### Config example for Cloudflare -For Cloudflare, you need to provide email & Global API Key as password, and config all the domains & subdomains. +For Cloudflare, you need to provide the email & Global API Key as password (or to use the API token) and config all the domains & subdomains. + +* Using email & Global API Key ```json { @@ -133,6 +135,26 @@ For Cloudflare, you need to provide email & Global API Key as password, and conf } ``` +* Using the API Token + +```json +{ + "provider": "Cloudflare", + "login_token": "API Token", + "domains": [{ + "domain_name": "example.com", + "sub_domains": ["www","test"] + },{ + "domain_name": "example2.com", + "sub_domains": ["www","test"] + } + ], + "ip_url": "https://myip.biturl.top", + "interval": 300, + "socks5_proxy": "" +} +``` + ### Config example for DNSPod For DNSPod, you need to provide your API Token(you can create it [here](https://www.dnspod.cn/console/user/security)), and config all the domains & subdomains. diff --git a/handler/cloudflare/cloudflare_handler.go b/handler/cloudflare/cloudflare_handler.go index 46e4b05..76bd552 100644 --- a/handler/cloudflare/cloudflare_handler.go +++ b/handler/cloudflare/cloudflare_handler.go @@ -40,7 +40,7 @@ type DNSRecord struct { Proxied bool `json:"proxied"` Type string `json:"type"` ZoneID string `json:"zone_id"` - TTL int32 `json:"ttl"` + TTL int32 `json:"ttl"` } // SetIP updates DNSRecord.IP @@ -146,8 +146,14 @@ func (handler *Handler) newRequest(method, url string, body io.Reader) (*http.Re req, _ := http.NewRequest(method, handler.API+url, body) req.Header.Set("Content-Type", "application/json") - req.Header.Set("X-Auth-Email", handler.Configuration.Email) - req.Header.Set("X-Auth-Key", handler.Configuration.Password) + + if handler.Configuration.Email != "" && handler.Configuration.Password != "" { + req.Header.Set("X-Auth-Email", handler.Configuration.Email) + req.Header.Set("X-Auth-Key", handler.Configuration.Password) + } else if handler.Configuration.LoginToken != "" { + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", handler.Configuration.LoginToken)) + } + return req, client } diff --git a/utils.go b/utils.go index 5bb312d..d16af15 100644 --- a/utils.go +++ b/utils.go @@ -207,11 +207,13 @@ func CheckSettings(config *Settings) error { return errors.New("password cannot be empty") } } else if config.Provider == CLOUDFLARE { - if config.Email == "" { - return errors.New("email cannot be empty") - } - if config.Password == "" { - return errors.New("password cannot be empty") + if config.LoginToken == "" { + if config.Email == "" { + return errors.New("email cannot be empty") + } + if config.Password == "" { + return errors.New("password cannot be empty") + } } } else if config.Provider == ALIDNS { if config.Email == "" {