-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathyandex.go
90 lines (78 loc) · 2.5 KB
/
yandex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package dyndns
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
)
func yandexupd(configs Config) error {
//get record id from domain name
for _, domainname := range configs.Domains {
domainarray := strings.Split(domainname, ".")
getdomain := domainarray[len(domainarray)-2] + "." + domainarray[len(domainarray)-1]
client := &http.Client{}
req, err := http.NewRequest("GET", "https://pddimp.yandex.ru/api2/admin/dns/list?domain="+getdomain, nil)
if err != nil {
return err
}
req.Header.Set("PddToken", configs.Auth.Apikey)
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
var f interface{}
err = json.Unmarshal(body, &f)
info := f.(map[string]interface{})
if success, ok := info["success"].(string); ok {
if success == "ok" {
for _, v := range info["records"].([]interface{}) {
inforecord := v.(map[string]interface{})
if recordtype, ok := inforecord["type"].(string); ok && (recordtype == "A") {
if domain, ok := inforecord["fqdn"].(string); ok {
if subdomain, ok := inforecord["subdomain"].(string); ok {
if record_id, ok := inforecord["record_id"].(float64); ok && (domainname == domain) {
ipaddr := inforecord["content"].(string)
ttl := inforecord["ttl"].(float64)
if configs.Ipupdate != ipaddr {
urltpl := "https://pddimp.yandex.ru/api2/admin/dns/edit?domain=%s&subdomain=%s&record_id=%s&ttl=%d&content=%s"
url := fmt.Sprintf(urltpl, getdomain, subdomain, strconv.Itoa(int(record_id)), strconv.Itoa(int(ttl)), configs.Ipupdate)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
req.Header.Set("PddToken", configs.Auth.Apikey)
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
err = json.Unmarshal(body, &f)
info := f.(map[string]interface{})
if success, ok := info["success"].(string); ok {
if success == "ok" {
println("IP ", domainname, " change")
return nil
}
}
}
}
}
}
}
}
}
}
}
return nil
}