Skip to content

Commit

Permalink
fix subdomain
Browse files Browse the repository at this point in the history
  • Loading branch information
honwen committed Jul 31, 2021
1 parent a63fa0f commit 4641529
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:

- uses: actions/setup-go@v2
with:
go-version: 1.16.5
go-version: 1.16.6

- name: Prepare environment
run: |-
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ go 1.16

require (
github.com/denverdino/aliyungo v0.0.0-20210518071019-eb3bbb144d8a
github.com/honwen/golibs v0.2.1
github.com/honwen/ip2loc v0.1.3
github.com/honwen/golibs v0.2.4
github.com/honwen/ip2loc v0.1.4
github.com/stretchr/testify v1.7.0
github.com/urfave/cli v1.22.5
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/go-chi/chi v1.5.3/go.mod h1:Q8xfe6s3fjZyMr8ZTv5jL+vxhVaFyCq2s+RvSfzTD0E=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/honwen/golibs v0.2.1 h1:UIn4k9lJLlDcw0WqlrwagEiXfxOlcFyg8py5kA0Vr4M=
github.com/honwen/golibs v0.2.1/go.mod h1:3Y6gdQ2RLIxRSNWdkoyRLetrpKJVw1WB421Qt4PLdrA=
github.com/honwen/ip2loc v0.1.3 h1:k4YtnNSspGp8I033J6HyGVzvxfL0tCzh8wJZ+D876PA=
github.com/honwen/ip2loc v0.1.3/go.mod h1:4loWbEvIxSNNr6pDp2YfPnAnJN/SKN7LiHfFr0rHB9o=
github.com/honwen/golibs v0.2.4 h1:npA/AE0vgIZDpVX4m049wQGGrROpLN07l8GL+NuqEV0=
github.com/honwen/golibs v0.2.4/go.mod h1:3Y6gdQ2RLIxRSNWdkoyRLetrpKJVw1WB421Qt4PLdrA=
github.com/honwen/ip2loc v0.1.4 h1:yM/X48zfplDAQ9QGR+D5nBwpSzvzEcXki4a1x/a6LzI=
github.com/honwen/ip2loc v0.1.4/go.mod h1:4loWbEvIxSNNr6pDp2YfPnAnJN/SKN7LiHfFr0rHB9o=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/knadh/koanf v0.14.0/go.mod h1:H5mEFsTeWizwFXHKtsITL5ipsLTuAMQoGuQpp+1JL9U=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
Expand Down
73 changes: 65 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import (

// AccessKey from https://ak-console.aliyun.com/#/accesskey
type AccessKey struct {
ID string
Secret string
client *dns.Client
ID string
Secret string
client *dns.Client
managedDomains []string
}

func (ak *AccessKey) getClient() *dns.Client {
Expand All @@ -42,6 +43,42 @@ func (ak AccessKey) String() string {
return fmt.Sprintf("Access Key: [ ID: %s ;\t Secret: %s ]", ak.ID, ak.Secret)
}

func (ak *AccessKey) ListManagedDomains() (domains []string, err error) {
var resp []dns.DomainType
resp, err = ak.getClient().DescribeDomains(
&dns.DescribeDomainsArgs{
Pagination: common.Pagination{PageSize: 50},
})
if err != nil {
return
}
domains = make([]string, len(resp))
for i, v := range resp {
domains[i] = v.DomainName
}
return
}

func (ak *AccessKey) AutocheckDomainRR(rr, domain string) (r, d string, err error) {
if contains(ak.managedDomains, domain) {
return rr, domain, nil
} else {
if !strings.Contains(rr, `.`) {
return "", "", fmt.Errorf("Domain [%s.%s] Not Managed", rr, domain)
} else {
rrs := strings.Split(rr, `.`)
for i := len(rrs) - 1; i > 0; i-- {
d = strings.Join(append(rrs[i:], domain), `.`)
if contains(ak.managedDomains, d) {
r = strings.Join(rrs[:i], `.`)
return
}
}
}
}
return "", "", fmt.Errorf("Domain [%s.%s] Not Managed", rr, domain)
}

func (ak *AccessKey) ListRecord(domain string) (dnsRecords []dns.RecordTypeNew, err error) {
var resp *dns.DescribeDomainRecordsNewResponse
for idx := 1; idx <= 99; idx++ {
Expand Down Expand Up @@ -170,7 +207,10 @@ func main() {
return err
}
// fmt.Println(c.Command.Name, "task: ", accessKey, c.String("domain"))
_, domain := domain.SplitDomainToRR(c.String("domain"))
domain := c.String("domain")
if !contains(accessKey.managedDomains, domain) {
return fmt.Errorf("Domain [%s] Not Managed", domain)
}
if dnsRecords, err := accessKey.ListRecord(domain); err != nil {
fmt.Printf("%+v", err)
} else {
Expand All @@ -196,7 +236,11 @@ func main() {
return err
}
// fmt.Println(c.Command.Name, "task: ", accessKey, c.String("domain"))
if err := accessKey.DelRecord(domain.SplitDomainToRR(c.String("domain"))); err != nil {
rr, domain, err := accessKey.AutocheckDomainRR(domain.SplitDomainToRR(c.String("domain")))
if err != nil {
return err
}
if err := accessKey.DelRecord(rr, domain); err != nil {
fmt.Printf("%+v", err)
} else {
fmt.Println(c.String("domain"), "Deleted")
Expand All @@ -222,8 +266,11 @@ func main() {
if err := appInit(c, true); err != nil {
return err
}
fmt.Println(c.Command.Name, "task: ", accessKey, c.String("domain"), c.String("ipaddr"))
rr, domain := domain.SplitDomainToRR(c.String("domain"))
// fmt.Println(c.Command.Name, "task: ", accessKey, c.String("domain"), c.String("ipaddr"))
rr, domain, err := accessKey.AutocheckDomainRR(domain.SplitDomainToRR(c.String("domain")))
if err != nil {
return err
}
recordType := "A"
if c.GlobalBool("ipv6") {
recordType = "AAAA"
Expand Down Expand Up @@ -256,7 +303,10 @@ func main() {
return err
}
// fmt.Println(c.Command.Name, "task: ", accessKey, c.String("domain"), c.Int64("redo"))
rr, domain := domain.SplitDomainToRR(c.String("domain"))
rr, domain, err := accessKey.AutocheckDomainRR(domain.SplitDomainToRR(c.String("domain")))
if err != nil {
return err
}
recordType := "A"
if c.GlobalBool("ipv6") {
recordType = "AAAA"
Expand Down Expand Up @@ -369,6 +419,13 @@ func appInit(c *cli.Context, checkAccessKey bool) error {
cli.ShowAppHelp(c)
return errors.New("access-key is empty")
}
if domains, err := accessKey.ListManagedDomains(); err == nil {
// log.Println(domains)
accessKey.managedDomains = domains
} else {
cli.ShowAppHelp(c)
return errors.New("No Managed Domains")
}

if c.GlobalBool("ipv6") {
funcs["myip"] = cip.MyIPv6
Expand Down
9 changes: 9 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ var funcs = map[string]interface{}{
"reslove": cip.ResloveIPv4,
}

func contains(slice []string, item string) bool {
set := make(map[string]struct{}, len(slice))
for _, s := range slice {
set[s] = struct{}{}
}
_, ok := set[item]
return ok
}

func ip2locCN(ip string) (str string) {
if strings.Count(ip, `.`) < 3 {
return
Expand Down

0 comments on commit 4641529

Please sign in to comment.