Skip to content

Commit

Permalink
full support TTL
Browse files Browse the repository at this point in the history
  • Loading branch information
honwen committed Oct 30, 2021
1 parent 8bd4a8e commit 0cbc950
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 23 deletions.
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ ENV AKID=1234567890 \
AKSCT=abcdefghijklmn \
DOMAIN=ddns.example.win \
IPAPI=[IPAPI-GROUP] \
REDO=555r
REDO=555r \
TTL=600

CMD aliyun-ddns-cli \
--ipapi ${IPAPI} \
${IPV6:+-6} \
auto-update \
--domain ${DOMAIN} \
--redo ${REDO}
--redo ${REDO} \
--ttl ${TTL}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ $ docker run -d \
-e "AKSCT=[ALIYUN's AccessKey-Secret]" \
-e "DOMAIN=ddns.aliyun.win" \
-e "REDO=600" \
-e "TTL=600" \
chenhw2/aliyun-ddns-cli
```

Expand Down
3 changes: 2 additions & 1 deletion alidns/AddDomainRecord.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dns

import (
"encoding/json"
"log"

"github.com/denverdino/aliyungo/common"
Expand All @@ -13,7 +14,7 @@ type AddDomainRecordArgs struct {
Value string

//optional
TTL string
TTL json.Number
Line string
}

Expand Down
7 changes: 6 additions & 1 deletion alidns/DescribeDomainRecordInfoNew.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package dns

import "github.com/denverdino/aliyungo/common"
import (
"encoding/json"

"github.com/denverdino/aliyungo/common"
)

// endpoint change to 'http://alidns.aliyuncs.com' then record ttl and priority change to string
type RecordTypeNew struct {
Expand All @@ -9,6 +13,7 @@ type RecordTypeNew struct {
RR string
Type string
Value string
TTL json.Number
Line string
Status string
Locked bool
Expand Down
10 changes: 7 additions & 3 deletions alidns/UpdateDomainRecord.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package dns

import "github.com/denverdino/aliyungo/common"
import (
"encoding/json"

"github.com/denverdino/aliyungo/common"
)

type UpdateDomainRecordArgs struct {
RecordId string
Expand All @@ -9,8 +13,8 @@ type UpdateDomainRecordArgs struct {
Value string

//optional
TTL string
Priority int32
TTL json.Number
Priority json.Number
Line string
}

Expand Down
6 changes: 4 additions & 2 deletions alidns/record.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dns

import "encoding/json"

//
//you can read doc at https://docs.aliyun.com/#/pub/dns/api-reference/enum-type&record-format
const (
Expand All @@ -20,8 +22,8 @@ type RecordType struct {
RR string
Type string
Value string
TTL int32
Priority int32
TTL json.Number
Priority json.Number
Line string
Status string
Locked bool
Expand Down
29 changes: 15 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"errors"
"fmt"
"log"
Expand Down Expand Up @@ -117,31 +118,31 @@ func (ak *AccessKey) DelRecord(rr, domain string) (err error) {
return
}

func (ak *AccessKey) UpdateRecord(recordID, rr, dmType, value, ttl string) (err error) {
func (ak *AccessKey) UpdateRecord(recordID, rr, dmType, value string, ttl int) (err error) {
_, err = ak.getClient().UpdateDomainRecord(
&dns.UpdateDomainRecordArgs{
RecordId: recordID,
RR: rr,
Value: value,
Type: dmType,
TTL: ttl,
TTL: json.Number(fmt.Sprint(ttl)),
})
return
}

func (ak *AccessKey) AddRecord(domain, rr, dmType, value, ttl string) (err error) {
func (ak *AccessKey) AddRecord(domain, rr, dmType, value string, ttl int) (err error) {
_, err = ak.getClient().AddDomainRecord(
&dns.AddDomainRecordArgs{
DomainName: domain,
RR: rr,
Type: dmType,
Value: value,
TTL: ttl,
TTL: json.Number(fmt.Sprint(ttl)),
})
return err
}

func (ak *AccessKey) CheckAndUpdateRecord(rr, domain, ipaddr, recordType, ttl string) (err error) {
func (ak *AccessKey) CheckAndUpdateRecord(rr, domain, ipaddr, recordType string, ttl int) (err error) {
fulldomain := strings.Join([]string{rr, domain}, `.`)
if reslove(fulldomain) == ipaddr {
return // Skip
Expand Down Expand Up @@ -217,7 +218,7 @@ func main() {
fmt.Printf("%+v", err)
} else {
for _, v := range dnsRecords {
fmt.Printf("%20s %-8s %s\n", v.RR+`.`+v.DomainName, v.Type, v.Value)
fmt.Printf("%20s %-16s %s\n", v.RR+`.`+v.DomainName, fmt.Sprintf("%s(TTL:%4s)", v.Type, v.TTL), v.Value)
}
}
return nil
Expand Down Expand Up @@ -263,10 +264,10 @@ func main() {
Name: "ipaddr, i",
Usage: "Specific `IP`. like 1.2.3.4",
},
cli.StringFlag{
cli.IntFlag{
Name: "ttl, t",
Value: "",
Usage: "The resolution effective time is 600 seconds (10 minutes) by default.",
Value: 600,
Usage: "The resolution effective time (in `seconds`)",
},
},
Action: func(c *cli.Context) error {
Expand All @@ -282,7 +283,7 @@ func main() {
if c.GlobalBool("ipv6") {
recordType = "AAAA"
}
if err := accessKey.CheckAndUpdateRecord(rr, domain, c.String("ipaddr"), recordType, c.String("ttl")); err != nil {
if err := accessKey.CheckAndUpdateRecord(rr, domain, c.String("ipaddr"), recordType, c.Int("ttl")); err != nil {
log.Printf("%+v", err)
} else {
log.Println(c.String("domain"), c.String("ipaddr"), ip2locCN(c.String("ipaddr")))
Expand All @@ -304,10 +305,10 @@ func main() {
Value: "",
Usage: "redo Auto-Update, every N `Seconds`; Disable if N less than 10; End with [Rr] enable random delay: [N, 2N]",
},
cli.StringFlag{
cli.IntFlag{
Name: "ttl, t",
Value: "",
Usage: "The resolution effective time is 600 seconds (10 minutes) by default.",
Value: 600,
Usage: "The resolution effective time (in `seconds`)",
},
},
Action: func(c *cli.Context) error {
Expand Down Expand Up @@ -343,7 +344,7 @@ func main() {
if len(autoip) == 0 {
log.Printf("# Err-CheckAndUpdateRecord: [%s]", "IP is empty, PLZ check network")
} else {
if err := accessKey.CheckAndUpdateRecord(rr, domain, autoip, recordType, c.String("ttl")); err != nil {
if err := accessKey.CheckAndUpdateRecord(rr, domain, autoip, recordType, c.Int("ttl")); err != nil {
log.Printf("# Err-CheckAndUpdateRecord: [%+v]", err)
} else {
log.Println(c.String("domain"), autoip, ip2locCN(autoip))
Expand Down

0 comments on commit 0cbc950

Please sign in to comment.