mirror of
https://github.com/anaganisk/digitalocean-dynamic-dns-ip.git
synced 2024-08-30 17:42:10 +00:00
Merge pull request #7 from cdzombak/master
Allow setting custom IP check URLs
This commit is contained in:
commit
9eed6ba6d8
3
.gitignore
vendored
3
.gitignore
vendored
@ -2,3 +2,6 @@ config.json
|
||||
digitalocean-dynamic-ip
|
||||
digitalocean-dynamic-dns-ip
|
||||
digitalocean-dynamic-ip.json
|
||||
|
||||
# GoLand IDE
|
||||
.idea/
|
23
README.md
23
README.md
@ -1,7 +1,7 @@
|
||||
# DIGITAL OCEAN DYNAMIC IP API CLIENT
|
||||
A simple script in Go language to automatically update Digital ocean DNS records if you have a dynamic IP. Since it can be compiled on any platform, you can use it along with raspberrypi etc.
|
||||
|
||||
To find your Dynamic IP, this program will call out to https://ipv4bot.whatismyipaddress.com for ipv4 addresses and https://ipv6bot.whatismyipaddress.com for ipv6 addresses. This is to support dual-stack environments.
|
||||
To find your Dynamic IP, this program will call out to https://ipv4bot.whatismyipaddress.com for ipv4 addresses and https://ipv6bot.whatismyipaddress.com for ipv6 addresses. This is to support dual-stack environments. (These URLs can be customized; see Usage, below.)
|
||||
|
||||
## Requirements
|
||||
Requires Git, Go 1.8+, and https://github.com/mitchellh/go-homedir for building.
|
||||
@ -18,17 +18,20 @@ Once the module is fetched, you should be able to compile the program using `go
|
||||
|
||||
## Usage
|
||||
```bash
|
||||
# clone the repo in ~/go/src/github.com/anaganisk:
|
||||
git clone https://github.com/anaganisk/digitalocean-dynamic-dns-ip.git
|
||||
```
|
||||
create a file ".digitalocean-dynamic-ip.json"(dot prefix to hide the file) and place it user home directory and add the following json
|
||||
|
||||
Create a file `.digitalocean-dynamic-ip.json` (dot prefix to hide the file) and place it in your home directory. Add the following JSON (or refer to the sample configuration file, `digitalocean-dynamic-ip.sample.json`):
|
||||
|
||||
```json
|
||||
{
|
||||
"apikey": "samplekeydasjkdhaskjdhrwofihsamplekey",
|
||||
"doPageSize" : 20,
|
||||
"doPageSize": 20,
|
||||
"useIPv4": true,
|
||||
"useIPv6": false,
|
||||
"allowIPv4InIPv6": false,
|
||||
"ipv4CheckUrl": "https://ipv4bot.whatismyipaddress.com",
|
||||
"domains": [
|
||||
{
|
||||
"domain": "example.com",
|
||||
@ -52,6 +55,7 @@ create a file ".digitalocean-dynamic-ip.json"(dot prefix to hide the file) and p
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
The TTL can optionally be updated if passed in the configuration. Digital Ocean has a minimum TTL of 30 seconds. The `type` and the `name` must match existing records in the Digital Ocean DNS configuration. Only `types` of `A` and `AAAA` allowed at the moment.
|
||||
|
||||
If you want to reduce the number of calls made to the digital ocean API and have more than 20 DNS records in your domain, you can adjust the `doPageSize` parameter. By default, Digital Ocean returns 20 records per page. Digital Ocean has a max page size of 200 items.
|
||||
@ -60,22 +64,25 @@ By default, the configuration checks both IPv4 and IPv6 addresses assuming your
|
||||
|
||||
The `allowIPv4InIPv6` configuration option will allow adding an IPv4 address to be used in a AAAA record for IPv6 lookups.
|
||||
|
||||
The `ipv4CheckUrl` and `ipv6CheckUrl` configuration settings are optional. If set, they must be URLs which respond to a GET request, with a plaintext response containing only your IP address. If unset, they default to `https://ipv_bot.whatismyipaddress.com`.
|
||||
|
||||
```bash
|
||||
#run
|
||||
go build digitalocean-dynamic-ip.go
|
||||
# after running `go build digitalocean-dynamic-ip.go`, run:
|
||||
./digitalocean-dynamic-ip
|
||||
```
|
||||
|
||||
Optionally, you can create the configuration file with any name wherever you want, and pass it as a command line argument:
|
||||
|
||||
```bash
|
||||
#run
|
||||
#run:
|
||||
./digitalocean-dynamic-ip /path/to/my/config.json
|
||||
```
|
||||
|
||||
You can either set this to run periodically with a cronjob or use your own method.
|
||||
|
||||
```bash
|
||||
# run crontab -e
|
||||
# sample cron job task
|
||||
# run `crontab -e` to edit your crontab
|
||||
# sample cron job task
|
||||
|
||||
# m h dom mon dow command
|
||||
*/5 * * * * /home/user/digitalocean-dynamic-dns-ip/digitalocean-dynamic-ip
|
||||
|
@ -31,6 +31,8 @@ type ClientConfig struct {
|
||||
DOPageSize int `json:"doPageSize"`
|
||||
UseIPv4 *bool `json:"useIPv4"`
|
||||
UseIPv6 *bool `json:"useIPv6"`
|
||||
IPv4CheckURL string `json:"ipv4CheckUrl"`
|
||||
IPv6CheckURL string `json:"ipv6CheckUrl"`
|
||||
AllowIPv4InIPv6 bool `json:"allowIPv4InIPv6"`
|
||||
Domains []Domain `json:"domains"`
|
||||
}
|
||||
@ -118,9 +120,17 @@ func usage() {
|
||||
//CheckLocalIPs : get current IP of server. checks both IPv4 and Ipv6 to support dual stack environments
|
||||
func CheckLocalIPs() (ipv4, ipv6 net.IP) {
|
||||
var ipv4String, ipv6String string
|
||||
ipv4CheckUrl := "https://ipv4bot.whatismyipaddress.com"
|
||||
ipv6CheckUrl := "https://ipv6bot.whatismyipaddress.com"
|
||||
if len(config.IPv4CheckURL) > 0 {
|
||||
ipv4CheckUrl = config.IPv4CheckURL
|
||||
}
|
||||
if len(config.IPv6CheckURL) > 0 {
|
||||
ipv6CheckUrl = config.IPv6CheckURL
|
||||
}
|
||||
|
||||
if config.UseIPv4 == nil || *(config.UseIPv4) {
|
||||
ipv4String, _ = getURLBody("https://ipv4bot.whatismyipaddress.com")
|
||||
ipv4String, _ = getURLBody(ipv4CheckUrl)
|
||||
if ipv4String == "" {
|
||||
log.Println("No IPv4 address found. Consider disabling IPv4 checks in the config `\"useIPv4\": false`")
|
||||
} else {
|
||||
@ -128,6 +138,7 @@ func CheckLocalIPs() (ipv4, ipv6 net.IP) {
|
||||
if ipv4 != nil {
|
||||
// make sure we got back an actual ipv4 address
|
||||
ipv4 = ipv4.To4()
|
||||
log.Printf("Discovered IPv4 address `%s`", ipv4.String())
|
||||
}
|
||||
if ipv4 == nil {
|
||||
log.Printf("Unable to parse `%s` as an IPv4 address", ipv4String)
|
||||
@ -136,13 +147,15 @@ func CheckLocalIPs() (ipv4, ipv6 net.IP) {
|
||||
}
|
||||
|
||||
if config.UseIPv6 == nil || *(config.UseIPv6) {
|
||||
ipv6String, _ = getURLBody("https://ipv6bot.whatismyipaddress.com")
|
||||
ipv6String, _ = getURLBody(ipv6CheckUrl)
|
||||
if ipv6String == "" {
|
||||
log.Println("No IPv6 address found. Consider disabling IPv6 checks in the config `\"useIPv6\": false`")
|
||||
} else {
|
||||
ipv6 = net.ParseIP(ipv6String)
|
||||
if ipv6 == nil {
|
||||
log.Printf("Unable to parse `%s` as an IPv6 address", ipv6String)
|
||||
} else {
|
||||
log.Printf("Discovered IPv6 address `%s`", ipv6.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -151,9 +164,7 @@ func CheckLocalIPs() (ipv4, ipv6 net.IP) {
|
||||
|
||||
func getURLBody(url string) (string, error) {
|
||||
request, err := http.Get(url)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
checkError(err)
|
||||
defer request.Body.Close()
|
||||
body, err := ioutil.ReadAll(request.Body)
|
||||
checkError(err)
|
||||
@ -206,10 +217,10 @@ func UpdateRecords(domain Domain, ipv4, ipv6 net.IP) {
|
||||
doRecords := GetDomainRecords(domain.Domain)
|
||||
// look for the item to update
|
||||
if len(doRecords) < 1 {
|
||||
log.Printf("%s: No DNS records found in Digital Ocean", domain.Domain)
|
||||
log.Printf("%s: No DNS records found in DigitalOcean", domain.Domain)
|
||||
return
|
||||
}
|
||||
log.Printf("%s: %d DNS records found in Digital Ocean", domain.Domain, len(doRecords))
|
||||
log.Printf("%s: %d DNS records found in DigitalOcean", domain.Domain, len(doRecords))
|
||||
for _, toUpdateRecord := range domain.Records {
|
||||
if toUpdateRecord.Type != "A" && toUpdateRecord.Type != "AAAA" {
|
||||
log.Printf("%s: Unsupported type (Only A and AAAA records supported) for updates %+v", domain.Domain, toUpdateRecord)
|
||||
@ -327,7 +338,7 @@ func main() {
|
||||
config = GetConfig()
|
||||
currentIPv4, currentIPv6 := CheckLocalIPs()
|
||||
if currentIPv4 == nil && currentIPv6 == nil {
|
||||
log.Fatal("current IP addresses are not a valid, or both are disabled in the config. Check you configuration and internet connection")
|
||||
log.Fatal("Current IP addresses are not valid, or both are disabled in the config. Check your configuration and internet connection.")
|
||||
}
|
||||
for _, domain := range config.Domains {
|
||||
log.Printf("%s: START", domain.Domain)
|
||||
|
@ -3,6 +3,8 @@
|
||||
"doPageSize": 20,
|
||||
"useIPv4": true,
|
||||
"useIPv6": true,
|
||||
"ipv4CheckUrl": "https://ipv4bot.whatismyipaddress.com",
|
||||
"ipv6CheckUrl": "https://ipv6bot.whatismyipaddress.com",
|
||||
"allowIPv4InIPv6": false,
|
||||
"domains": [
|
||||
{
|
||||
@ -11,6 +13,10 @@
|
||||
{
|
||||
"name": "subdomainOrRecord",
|
||||
"type": "A"
|
||||
},
|
||||
{
|
||||
"name": "subdomainOrRecord",
|
||||
"type": "AAAA"
|
||||
}
|
||||
]
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user