Skip to content

Commit c3630e8

Browse files
author
Sai Kiran Anagani
committed
add support for multiple domains
1 parent 057713b commit c3630e8

5 files changed

+83
-32
lines changed

.gitignore

100644100755
File mode changed.

LICENSE.md

100644100755
File mode changed.

README.md

100644100755
+19-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ A simple script in Go language to automatically update Digital ocean DNS records
44
## requirements
55
Requires Git and Go for building.
66

7-
Requires that the record already exists in DigitalOcean's DNS.
7+
Requires that the record already exists in DigitalOcean's DNS so that it can be updated.
8+
(manually find your IP and add it to DO's DNS it will later be updated)
89

910
## Usage
1011
```bash
@@ -15,11 +16,24 @@ create a file ".digitalocean-dynamic-ip.json"(dot prefix to hide the file) and p
1516
```json
1617
{
1718
"apikey": "samplekeydasjkdhaskjdhrwofihsamplekey",
18-
"domain": "example.com",
19-
"records": [
19+
"domains": [
2020
{
21-
"name": "subdomain",
22-
"type": "A"
21+
"domain": "example.com",
22+
"records": [
23+
{
24+
"name": "subdomainOrRecord",
25+
"type": "A"
26+
}
27+
]
28+
},
29+
{
30+
"domain": "example2.com",
31+
"records": [
32+
{
33+
"name": "subdomainOrRecord2",
34+
"type": "A"
35+
}
36+
]
2337
}
2438
]
2539
}

digitalocean-dynamic-ip.go

100644100755
+47-23
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,20 @@ import (
1313

1414
func checkError(err error) {
1515
if err != nil {
16-
panic(err)
16+
log.Fatal(err)
1717
}
1818
}
1919

2020
// ClientConfig : configuration json
2121
type ClientConfig struct {
22-
APIKey string `json:"apiKey"`
23-
Domain string `json:"domain"`
24-
Record []DNSRecord `json:"records"`
22+
APIKey string `json:"apiKey"`
23+
Domains []Domain `json:"domains"`
24+
}
25+
26+
// Domain : domains to be changed
27+
type Domain struct {
28+
Domain string `json:"domain"`
29+
Records []DNSRecord `json:"records"`
2530
}
2631

2732
// DNSRecord : Modifyiable DNS record
@@ -37,60 +42,79 @@ type DOResponse struct {
3742
DomainRecords []DNSRecord `json:"domain_records"`
3843
}
3944

40-
func main() {
45+
//GetConfig : get configuration file ~/.digitalocean-dynamic-ip.json
46+
func GetConfig() ClientConfig {
4147
homeDirectory, err := homedir.Dir()
4248
checkError(err)
4349
getfile, err := ioutil.ReadFile(homeDirectory + "/.digitalocean-dynamic-ip.json")
4450
checkError(err)
4551
var config ClientConfig
4652
json.Unmarshal(getfile, &config)
4753
checkError(err)
54+
return config
55+
}
4856

49-
// check current local ip
50-
57+
//CheckLocalIP : get current IP of server.
58+
func CheckLocalIP() string {
5159
currentIPRequest, err := http.Get("https://diagnostic.opendns.com/myip")
5260
checkError(err)
5361
defer currentIPRequest.Body.Close()
5462
currentIPRequestParse, err := ioutil.ReadAll(currentIPRequest.Body)
5563
checkError(err)
56-
currentIP := string(currentIPRequestParse)
57-
58-
// get current dns record ip
64+
return string(currentIPRequestParse)
65+
}
5966

67+
//GetDomainRecords : Get DNS records of current domain.
68+
func GetDomainRecords(apiKey string, domain string) DOResponse {
6069
client := &http.Client{}
6170
request, err := http.NewRequest("GET",
62-
"https://api.digitalocean.com/v2/domains/"+string(config.Domain)+"/records",
71+
"https://api.digitalocean.com/v2/domains/"+domain+"/records",
6372
nil)
6473
checkError(err)
6574
request.Header.Add("Content-type", "Application/json")
66-
request.Header.Add("Authorization", "Bearer "+string(config.APIKey))
75+
request.Header.Add("Authorization", "Bearer "+apiKey)
6776
response, err := client.Do(request)
6877
checkError(err)
6978
defer response.Body.Close()
7079
body, err := ioutil.ReadAll(response.Body)
71-
var jsonResponse DOResponse
72-
e := json.Unmarshal(body, &jsonResponse)
80+
var jsonDOResponse DOResponse
81+
e := json.Unmarshal(body, &jsonDOResponse)
7382
checkError(e)
83+
return jsonDOResponse
84+
}
7485

75-
// update ip by matching dns records and config
76-
77-
for _, record := range jsonResponse.DomainRecords {
78-
for _, configRecord := range config.Record {
79-
if configRecord.Name == record.Name && configRecord.Type == record.Type && currentIP != record.Data {
80-
update := []byte(`{"type":"` + configRecord.Type + `","data":"` + currentIP + `"}`)
86+
// UpdateRecords : Update DNS records of domain
87+
func UpdateRecords(apiKey string, domain string, currentIP string, currentRecords DOResponse, toUpdateRecords []DNSRecord) {
88+
for _, currentRecord := range currentRecords.DomainRecords {
89+
for _, toUpdateRecord := range toUpdateRecords {
90+
if toUpdateRecord.Name == currentRecord.Name && toUpdateRecord.Type == currentRecord.Type && currentIP != currentRecord.Data {
91+
update := []byte(`{"type":"` + toUpdateRecord.Type + `","data":"` + currentIP + `"}`)
8192
client := &http.Client{}
8293
request, err := http.NewRequest("PUT",
83-
"https://api.digitalocean.com/v2/domains/"+string(config.Domain)+"/records/"+strconv.FormatInt(int64(record.ID), 10),
94+
"https://api.digitalocean.com/v2/domains/"+domain+"/records/"+strconv.FormatInt(int64(currentRecord.ID), 10),
8495
bytes.NewBuffer(update))
8596
checkError(err)
8697
request.Header.Set("Content-Type", "application/json")
87-
request.Header.Add("Authorization", "Bearer "+string(config.APIKey))
98+
request.Header.Add("Authorization", "Bearer "+apiKey)
8899
response, err := client.Do(request)
89100
checkError(err)
90101
defer response.Body.Close()
91102
body, err := ioutil.ReadAll(response.Body)
92-
log.Printf("DO update response for %s: %s", record.Name, string(body))
103+
log.Printf("DO update response for %s: %s\n", currentRecord.Name, string(body))
93104
}
94105
}
95106
}
96107
}
108+
109+
func main() {
110+
config := GetConfig()
111+
currentIP := CheckLocalIP()
112+
113+
for _, domains := range config.Domains {
114+
domainName := domains.Domain
115+
apiKey := config.APIKey
116+
currentDomainRecords := GetDomainRecords(apiKey, domainName)
117+
log.Println(domainName)
118+
UpdateRecords(apiKey, domainName, currentIP, currentDomainRecords, domains.Records)
119+
}
120+
}

digitalocean-dynamic-ip.sample.json

100644100755
+17-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
{
22
"apikey": "samplekeydasjkdhaskjdhrwofihsamplekey",
3-
"domain": "example.com",
4-
"records": [
3+
"domains": [
54
{
6-
"name": "subdomain",
7-
"type": "A"
5+
"domain": "example.com",
6+
"records": [
7+
{
8+
"name": "subdomainOrRecord",
9+
"type": "A"
10+
}
11+
]
12+
},
13+
{
14+
"domain": "example2.com",
15+
"records": [
16+
{
17+
"name": "subdomainOrRecord2",
18+
"type": "A"
19+
}
20+
]
821
}
922
]
1023
}

0 commit comments

Comments
 (0)