Skip to content

Commit

Permalink
feat: proxy support for cloudflare
Browse files Browse the repository at this point in the history
  • Loading branch information
we11adam committed Dec 26, 2024
1 parent 22d7c95 commit e40bd4e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ updaters:
email: "[email protected]" # Cloudflare account email
apikey: fd25bdc03a4c17450aa4327aa37de4573270f # Cloudflare API key
domain: ddns.yourdomain.com # Domain to update
proxy: http://127.0.0.1:2080 # Optional
# lightdns: # Use LightDNS as the updater
# key: bgw99xiio5ewbphb
# domain: uddns.dyn.la
Expand Down
27 changes: 23 additions & 4 deletions updater/cloudflare/cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"log/slog"
"net"
"net/http"
"net/url"
"strings"

"github.com/cloudflare/cloudflare-go"
Expand All @@ -24,6 +26,7 @@ type Config struct {
APIKey string `mapstructure:"apikey"`
APIToken string `mapstructure:"apitoken"`
Domain string `mapstructure:"domain"`
Proxy string `mapstructure:"proxy"`
}

type Cloudflare struct {
Expand All @@ -49,16 +52,32 @@ func New(config *Config) (*Cloudflare, error) {
}

var (
api *cloudflare.API
err error
api *cloudflare.API
httpClient *http.Client
err error
)

if config.Proxy == "" {
httpClient = &http.Client{}
} else {
proxy, err := url.Parse(config.Proxy)
if err != nil {
return nil, fmt.Errorf("failed to parse Cloudflare proxy URL: %w", err)
}
slog.Info("[Cloudflare] Using proxy", "proxy", config.Proxy)
httpClient = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxy),
},
}
}

if config.APIToken != "" {
slog.Debug("[Cloudflare] Using API Token for authentication")
api, err = cloudflare.NewWithAPIToken(config.APIToken)
api, err = cloudflare.NewWithAPIToken(config.APIToken, cloudflare.HTTPClient(httpClient))
} else if config.APIKey != "" && config.Email != "" {
slog.Debug("[Cloudflare] Using API Key and Email for authentication")
api, err = cloudflare.New(config.APIKey, config.Email)
api, err = cloudflare.New(config.APIKey, config.Email, cloudflare.HTTPClient(httpClient))
} else {
return nil, fmt.Errorf("Cloudflare configuration error: either APIToken or both APIKey and Email must be provided")
}
Expand Down

0 comments on commit e40bd4e

Please sign in to comment.