Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support ipv4 #1

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# CF-DDNS
ddns service for Cloudflare. Currently only supports A and AAAA records.

## Usage

This is a WIP project, so no install script is provided yet. You can use the systemd files as a reference to setup your own service.

### Configuration
cf-ddns is configured using a json file. The default location is `/etc/cf-ddns/config.json`. You can specify a different location using the `-c` flag.

```json
{
"cloudflare": {
"token": "...", // Cloudflare API token, requires Zone.DNS.Edit permissions
"zoneId": "..."
},
"records": [
{
"name": "ddns.toledompm.xyz",
"proxy": true
},
],
"ipv6": {
"enabled": true,
"fetchAddress": "https://myexternalip.com/json" // URL to fetch IPv6 address from, must return a json object with an "ip" field.
},
"ipv4": {
"enabled": true,
"fetchAddress": "https://myexternalip.com/json"
}
}
```
33 changes: 22 additions & 11 deletions cmd/ddns.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,33 @@ var (
Short: "Cloudflare DDNS Updater",
Long: `Cloudflare DDNS Updater is a simple tool to update Cloudflare DNS records with your current IP address.`,
Run: func(cmd *cobra.Command, args []string) {
ipv6, err := nw.GetIPV6()
if err != nil {
fmt.Printf("Error getting IP: %s\n", err)
os.Exit(1)
}

for _, record := range cfg.Records {
if cfg.IPV6.Enabled {
ipv6, err := nw.GetIPV6()
if err != nil {
fmt.Printf("Error getting IP: %s\n", err)
os.Exit(1)
}

err = cf.UpdateRecord(ipv6, record.Name, cfg.Cloudflare.ZoneID, record.Proxy, "AAAA")
if err != nil {
fmt.Printf("Error updating record: %s\n", err)
os.Exit(1)
}
} else {
fmt.Println("Only ipv6 is supported at this time")
os.Exit(1)
}

if cfg.IPV4.Enabled {
ipv4, err := nw.GetIPV4()
if err != nil {
fmt.Printf("Error getting IP: %s\n", err)
os.Exit(1)
}

err = cf.UpdateRecord(ipv4, record.Name, cfg.Cloudflare.ZoneID, record.Proxy, "A")
if err != nil {
fmt.Printf("Error updating record: %s\n", err)
os.Exit(1)
}
}
}
},
Expand All @@ -50,7 +61,7 @@ func Execute() {
func init() {
cobra.OnInitialize(initConfig)

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "(required) path to config file")
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "/etc/cf-ddns/config.json", "(required) path to config file")
}

func initConfig() {
Expand All @@ -69,5 +80,5 @@ func initConfig() {
cfg = config.MustParseConfig(cfgFileBytes, cfg)

cf = handlers.NewCloudflare(cfg.Cloudflare.Token)
nw = handlers.NewNetwork(cfg.IPV6.FetchAddress)
nw = handlers.NewNetwork(cfg.IPV6.FetchAddress, cfg.IPV4.FetchAddress)
}
7 changes: 7 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ type Config struct {
// FetchAddress is the URL to fetch the current IPV6 address
FetchAddress string `json:"fetchAddress"`
} `json:"ipv6"`

IPV4 struct {
// Enabled is a flag to enable or disable IPV4
Enabled bool `json:"enabled"`
// FetchAddress is the URL to fetch the current IPV4 address
FetchAddress string `json:"fetchAddress"`
} `json:"ipv4"`
}

func New() *Config {
Expand Down
15 changes: 13 additions & 2 deletions handlers/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,35 @@ import (

type Network interface {
GetIPV6() (string, error)
GetIPV4() (string, error)
}

type ipResponse struct {
IP string `json:"ip"`
}

func NewNetwork(ipv6FetchAddress string) Network {
func NewNetwork(ipv6FetchAddress string, ipv4FetchAddress string) Network {
return &NetworkHandler{
ipv6FetchAddress: ipv6FetchAddress,
ipv4FetchAddress: ipv4FetchAddress,
}
}

type NetworkHandler struct {
ipv6FetchAddress string
ipv4FetchAddress string
}

func (n *NetworkHandler) GetIPV4() (string, error) {
return getIP(n.ipv4FetchAddress)
}

func (n *NetworkHandler) GetIPV6() (string, error) {
httpResponse, err := http.Get(n.ipv6FetchAddress)
return getIP(n.ipv6FetchAddress)
}

func getIP(apiAddr string) (string, error) {
httpResponse, err := http.Get(apiAddr)
if err != nil {
return "", err
}
Expand Down