1
0
added past ip compare with current

removed binary

removed binary

fix relative path of config

change cron

change cron
This commit is contained in:
Sai Kiran Anagani 2017-08-30 16:18:43 +05:30
commit d2cff7ecc2
5 changed files with 152 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
config.json
digitalocean-dynamic-ip

8
LICENSE.md Normal file
View File

@ -0,0 +1,8 @@
# License MIT
### Copyright 2017 Sai Kiran Anagani
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

36
README.md Normal file
View File

@ -0,0 +1,36 @@
# 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.
## requirements
Requires Git and Go for building
## Usage
```bash
git clone https://github.com/anaganisk/digitalocean-dynamic-dns-ip.git
```
create a file config.json and place it same directory as this script and add the following json
```json
{
"apikey": "samplekeydasjkdhaskjdhrwofihsamplekey",
"domain": "example.com",
"records": [
{
"name": "subdomain",
"type": "A"
}
]
}
```
```bash
#run
go build digitalocean-dynamic-ip.go
./digitalocean-dynamic-ip
```
You can either set this to run periodically with a cronjob or use your own method.
```bash
# run crontab -e
# sample cron job task
# m h dom mon dow command
*/5 * * * * /home/user/digitalocean-dynamic-dns-ip/digitalocean-dynamic-ip
```

10
config.sample.json Normal file
View File

@ -0,0 +1,10 @@
{
"apikey": "samplekeydasjkdhaskjdhrwofihsamplekey",
"domain": "example.com",
"records": [
{
"name": "subdomain",
"type": "A"
}
]
}

View File

@ -0,0 +1,96 @@
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"path/filepath"
"strconv"
)
func checkError(err error) {
if err != nil {
panic(err)
}
}
// ClientConfig : configuration json
type ClientConfig struct {
APIKey string `json:"apiKey"`
Domain string `json:"domain"`
Record []DNSRecord `json:"records"`
}
// DNSRecord : Modifyiable DNS record
type DNSRecord struct {
ID int64 `json:"id"`
Name string `json:"name"`
Data string `json:"data"`
Type string `json:"type"`
}
// DOResponse : DigitalOcean DNS Records response.
type DOResponse struct {
DomainRecords []DNSRecord `json:"domain_records"`
}
func main() {
// load config
absPath, err := filepath.Abs("./config.json")
checkError(err)
getfile, err := ioutil.ReadFile(absPath)
checkError(err)
var config ClientConfig
json.Unmarshal(getfile, &config)
checkError(err)
// check current local ip
currentIPRequest, err := http.Get("https://diagnostic.opendns.com/myip")
checkError(err)
defer currentIPRequest.Body.Close()
currentIPRequestParse, err := ioutil.ReadAll(currentIPRequest.Body)
checkError(err)
currentIP := string(currentIPRequestParse)
// get current dns record ip
client := &http.Client{}
request, err := http.NewRequest("GET",
"https://api.digitalocean.com/v2/domains/"+string(config.Domain)+"/records",
nil)
checkError(err)
request.Header.Add("Content-type", "Application/json")
request.Header.Add("Authorization", "Bearer "+string(config.APIKey))
response, err := client.Do(request)
checkError(err)
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
var jsonResponse DOResponse
e := json.Unmarshal(body, &jsonResponse)
checkError(e)
// update ip by matching dns records and config
for _, record := range jsonResponse.DomainRecords {
for _, configRecord := range config.Record {
if configRecord.Name == record.Name && configRecord.Type == record.Type && currentIP != record.Data {
update := []byte(`{"type":"` + configRecord.Type + `","data":"` + currentIP + `"}`)
client := &http.Client{}
request, err := http.NewRequest("PUT",
"https://api.digitalocean.com/v2/domains/"+string(config.Domain)+"/records/"+strconv.FormatInt(int64(record.ID), 10),
bytes.NewBuffer(update))
checkError(err)
request.Header.Set("Content-Type", "application/json")
request.Header.Add("Authorization", "Bearer "+string(config.APIKey))
response, err := client.Do(request)
checkError(err)
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
log.Printf("DO update response for %s: %s", record.Name, string(body))
}
}
}
}