@@ -13,15 +13,20 @@ import (
13
13
14
14
func checkError (err error ) {
15
15
if err != nil {
16
- panic (err )
16
+ log . Fatal (err )
17
17
}
18
18
}
19
19
20
20
// ClientConfig : configuration json
21
21
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"`
25
30
}
26
31
27
32
// DNSRecord : Modifyiable DNS record
@@ -37,60 +42,79 @@ type DOResponse struct {
37
42
DomainRecords []DNSRecord `json:"domain_records"`
38
43
}
39
44
40
- func main () {
45
+ //GetConfig : get configuration file ~/.digitalocean-dynamic-ip.json
46
+ func GetConfig () ClientConfig {
41
47
homeDirectory , err := homedir .Dir ()
42
48
checkError (err )
43
49
getfile , err := ioutil .ReadFile (homeDirectory + "/.digitalocean-dynamic-ip.json" )
44
50
checkError (err )
45
51
var config ClientConfig
46
52
json .Unmarshal (getfile , & config )
47
53
checkError (err )
54
+ return config
55
+ }
48
56
49
- // check current local ip
50
-
57
+ //CheckLocalIP : get current IP of server.
58
+ func CheckLocalIP () string {
51
59
currentIPRequest , err := http .Get ("https://diagnostic.opendns.com/myip" )
52
60
checkError (err )
53
61
defer currentIPRequest .Body .Close ()
54
62
currentIPRequestParse , err := ioutil .ReadAll (currentIPRequest .Body )
55
63
checkError (err )
56
- currentIP := string (currentIPRequestParse )
57
-
58
- // get current dns record ip
64
+ return string (currentIPRequestParse )
65
+ }
59
66
67
+ //GetDomainRecords : Get DNS records of current domain.
68
+ func GetDomainRecords (apiKey string , domain string ) DOResponse {
60
69
client := & http.Client {}
61
70
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" ,
63
72
nil )
64
73
checkError (err )
65
74
request .Header .Add ("Content-type" , "Application/json" )
66
- request .Header .Add ("Authorization" , "Bearer " + string ( config . APIKey ) )
75
+ request .Header .Add ("Authorization" , "Bearer " + apiKey )
67
76
response , err := client .Do (request )
68
77
checkError (err )
69
78
defer response .Body .Close ()
70
79
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 )
73
82
checkError (e )
83
+ return jsonDOResponse
84
+ }
74
85
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 + `"}` )
81
92
client := & http.Client {}
82
93
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 ),
84
95
bytes .NewBuffer (update ))
85
96
checkError (err )
86
97
request .Header .Set ("Content-Type" , "application/json" )
87
- request .Header .Add ("Authorization" , "Bearer " + string ( config . APIKey ) )
98
+ request .Header .Add ("Authorization" , "Bearer " + apiKey )
88
99
response , err := client .Do (request )
89
100
checkError (err )
90
101
defer response .Body .Close ()
91
102
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 ))
93
104
}
94
105
}
95
106
}
96
107
}
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
+ }
0 commit comments