add dnspod provider (#12)

This commit is contained in:
王文慧
2019-08-16 23:25:11 +08:00
committed by Quentin McGaw
parent 5797810ae4
commit 309f76a87b
3 changed files with 118 additions and 2 deletions

View File

@@ -15,6 +15,7 @@ const (
PROVIDERDREAMHOST
PROVIDERCLOUDFLARE
PROVIDERNOIP
PROVIDERDNSPOD
)
func (provider ProviderType) String() string {
@@ -31,6 +32,8 @@ func (provider ProviderType) String() string {
return "cloudflare"
case PROVIDERNOIP:
return "noip"
case PROVIDERDNSPOD:
return "dnspod"
default:
return "unknown"
}
@@ -51,6 +54,8 @@ func ParseProvider(s string) (ProviderType, error) {
return PROVIDERCLOUDFLARE, nil
case "noip":
return PROVIDERNOIP, nil
case "dnspod":
return PROVIDERDNSPOD, nil
default:
return 0, fmt.Errorf("Provider %s not recognized", s)
}

View File

@@ -1,10 +1,12 @@
package update
import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"net/http"
"net/url"
"strings"
"time"
@@ -14,7 +16,7 @@ import (
"ddns-updater/pkg/network"
"ddns-updater/pkg/regex"
uuid "github.com/google/uuid"
"github.com/google/uuid"
)
const (
@@ -106,6 +108,14 @@ func update(
recordConfig.Settings.Password,
ip,
)
case models.PROVIDERDNSPOD:
err = updateDnsPod(
httpClient,
recordConfig.Settings.Domain,
recordConfig.Settings.Host,
recordConfig.Settings.Token,
ip,
)
default:
err = fmt.Errorf("unsupported provider \"%s\"", recordConfig.Settings.Provider)
}
@@ -459,3 +469,89 @@ func updateNoIP(httpClient *http.Client, hostname, username, password, ip string
}
return "", fmt.Errorf("unknown response: %s", s)
}
func updateDnsPod(httpClient *http.Client, domain, host, token, ip string) (err error) {
postValues := url.Values{}
postValues.Set("login_token", token)
postValues.Set("format", "json")
postValues.Set("domain", domain)
postValues.Set("length", "200")
postValues.Set("sub_domain", host)
postValues.Set("record_type", "A")
req, err := http.NewRequest(http.MethodPost, "https://dnsapi.cn/Record.List", bytes.NewBufferString(postValues.Encode()))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
var content []byte
var status int
status, content, err = network.DoHTTPRequest(httpClient, req)
if err != nil {
return err
}
if status != 200 {
return fmt.Errorf("HTTP status %d", status)
}
recordResp := &struct {
Records []*struct {
ID string `json:"id"`
Value string `json:"value"`
Type string `json:"type"`
Name string `json:"name"`
Line string `json:"line"`
} `json:"records"`
}{}
err = json.Unmarshal(content, recordResp)
if err != nil {
return err
}
var recordID string
var line string
for _, record := range recordResp.Records {
if record.Type == "A" && record.Name == host {
recordID = record.ID
line = record.Line
if ip == record.Value {
return nil
}
}
}
if recordID == "" {
return fmt.Errorf("record not found")
}
postValues = url.Values{}
postValues.Set("login_token", token)
postValues.Set("format", "json")
postValues.Set("domain", domain)
postValues.Set("record_id", recordID)
postValues.Set("value", ip)
postValues.Set("record_line", line)
postValues.Set("sub_domain", host)
req, err = http.NewRequest(http.MethodPost, "https://dnsapi.cn/Record.Ddns", bytes.NewBufferString(postValues.Encode()))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
status, content, err = network.DoHTTPRequest(httpClient, req)
if err != nil {
return err
}
if status != 200 {
return fmt.Errorf("HTTP status %d", status)
}
ddnsResp := &struct {
Record struct {
ID int64 `json:"id"`
Value string `json:"value"`
Name string `json:"name"`
} `json:"record"`
}{}
err = json.Unmarshal(content, ddnsResp)
if err != nil {
return err
}
if ddnsResp.Record.Value != ip {
return fmt.Errorf("ip not set")
}
return nil
}