commit d2cff7ecc2207cc1148fac65aafac9511d3847fb Author: Sai Kiran Anagani Date: Wed Aug 30 16:18:43 2017 +0530 init added past ip compare with current removed binary removed binary fix relative path of config change cron change cron diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..97745eb --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +config.json +digitalocean-dynamic-ip \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..ad1fe22 --- /dev/null +++ b/LICENSE.md @@ -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. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..7949f7d --- /dev/null +++ b/README.md @@ -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 +``` \ No newline at end of file diff --git a/config.sample.json b/config.sample.json new file mode 100644 index 0000000..5be77a7 --- /dev/null +++ b/config.sample.json @@ -0,0 +1,10 @@ +{ + "apikey": "samplekeydasjkdhaskjdhrwofihsamplekey", + "domain": "example.com", + "records": [ + { + "name": "subdomain", + "type": "A" + } + ] +} \ No newline at end of file diff --git a/digitalocean-dynamic-ip.go b/digitalocean-dynamic-ip.go new file mode 100644 index 0000000..3f0a030 --- /dev/null +++ b/digitalocean-dynamic-ip.go @@ -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)) + } + } + } +}