-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 parent
6390cb2
commit 9923217
Showing
8 changed files
with
376 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package cloud | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests" | ||
"github.com/aliyun/alibaba-cloud-sdk-go/services/alidns" | ||
"github.com/huyinghuan/ddns/config" | ||
) | ||
|
||
type InsideAliyun struct { | ||
Client *alidns.Client | ||
} | ||
|
||
/** | ||
* 获取域名介些列表 | ||
*/ | ||
func (cloud *InsideAliyun) GetDomainRecords(domain string) ([]Domain, error) { | ||
|
||
request := alidns.CreateDescribeDomainRecordsRequest() | ||
request.DomainName = domain | ||
request.PageSize = requests.NewInteger(500) | ||
request.Type = "A" | ||
response, err := cloud.Client.DescribeDomainRecords(request) | ||
if err != nil { | ||
return nil, err | ||
} | ||
list := response.DomainRecords.Record | ||
var queue []Domain | ||
for _, record := range list { | ||
queue = append(queue, Domain{ | ||
RecordId: record.RecordId, | ||
RR: record.RR, | ||
IP: record.Value, | ||
}) | ||
} | ||
return queue, nil | ||
} | ||
|
||
func (cloud *InsideAliyun) AddDomainRecord(domain Domain) error { | ||
request := alidns.CreateAddDomainRecordRequest() | ||
request.DomainName = domain.DomainName | ||
request.Value = domain.IP | ||
request.Type = "A" | ||
request.RR = domain.RR | ||
_, err := cloud.Client.AddDomainRecord(request) | ||
return err | ||
} | ||
|
||
func (cloud *InsideAliyun) UpdateDomainRecord(id string, domain Domain) error { | ||
request := alidns.CreateUpdateDomainRecordRequest() | ||
request.RecordId = id | ||
request.Value = domain.IP | ||
request.Type = "A" | ||
request.RR = domain.RR | ||
_, err := cloud.Client.UpdateDomainRecord(request) | ||
return err | ||
} | ||
|
||
// AddOrUpdateDomain | ||
// @return isChange error | ||
func (cloud *InsideAliyun) AddOrUpdateDomain(domain Domain) (bool, error) { | ||
// if insideAliyun == nil { | ||
|
||
// } | ||
records, err := cloud.GetDomainRecords(domain.DomainName) | ||
if err != nil { | ||
return false, err | ||
} | ||
for _, record := range records { | ||
if record.RR == domain.RR { | ||
// ip 没有变化,不需要重新解析 | ||
if record.IP == domain.IP { | ||
return false, nil | ||
} | ||
return true, cloud.UpdateDomainRecord(record.RecordId, domain) | ||
} | ||
} | ||
return true, cloud.AddDomainRecord(domain) | ||
} | ||
|
||
func CreateAliyun(conf config.AliyunConfig) *InsideAliyun { | ||
client, err := alidns.NewClientWithAccessKey("cn-hangzhou", conf.AccessKeyID, conf.AccessKeySecret) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
return &InsideAliyun{ | ||
Client: client, | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
ialidns/domainrecords_test.go → cloud/domainrecords_test.go.bak
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package ialidns | ||
package cloud | ||
|
||
import ( | ||
"encoding/json" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package cloud | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type Domain struct { | ||
RecordId string | ||
DomainName string | ||
RR string | ||
IP string | ||
OriginDomain string | ||
} | ||
|
||
func ParseDomain(domain string) (Domain, error) { | ||
arr := strings.Split(domain, ".") | ||
if len(arr) < 2 { | ||
return Domain{}, fmt.Errorf("域名配置错误: %s", domain) | ||
} | ||
|
||
domainName := strings.Join(arr[len(arr)-2:], ".") | ||
rr := strings.Join(arr[0:len(arr)-2], ".") | ||
return Domain{ | ||
DomainName: domainName, | ||
RR: rr, | ||
OriginDomain: domain, | ||
}, nil | ||
} | ||
|
||
type CloudServer interface { | ||
AddOrUpdateDomain(domain Domain) (bool, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package cloud | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/huyinghuan/ddns/config" | ||
) | ||
|
||
type NameComItem struct { | ||
ID int64 `json:"id,omitempty"` | ||
Domain string `json:"domainName,omitempty"` | ||
Host string `json:"host,omitempty"` | ||
RR string `json:"fqdn,omitempty"` | ||
Type string `json:"type,omitempty"` | ||
IP string `json:"answer,omitempty"` | ||
TTL int `json:"ttl,omitempty"` | ||
} | ||
|
||
type NameComItemList struct { | ||
Records []NameComItem `json:"records"` | ||
} | ||
|
||
type NameComError struct { | ||
Message string `json:"message"` | ||
Details string `json:"details"` | ||
} | ||
|
||
type InsideNameCom struct { | ||
API string | ||
Username string | ||
Token string | ||
} | ||
|
||
var client = http.Client{ | ||
Timeout: 3 * time.Second, | ||
} | ||
|
||
func (cloud *InsideNameCom) GetDomainRecords(domain string) ([]Domain, error) { | ||
api := fmt.Sprintf("%s/v4/domains/%s/records", cloud.API, domain) | ||
request, _ := http.NewRequest("GET", api, nil) | ||
request.SetBasicAuth(cloud.Username, cloud.Token) | ||
response, err := client.Do(request) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer response.Body.Close() | ||
body, err := ioutil.ReadAll(response.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if response.StatusCode != 200 { | ||
return nil, fmt.Errorf(string(body)) | ||
} | ||
var items NameComItemList | ||
if err := json.Unmarshal(body, &items); err != nil { | ||
return nil, nil | ||
} | ||
queue := []Domain{} | ||
for _, record := range items.Records { | ||
queue = append(queue, Domain{ | ||
RecordId: strconv.FormatInt(record.ID, 10), | ||
RR: record.RR, | ||
IP: record.IP, | ||
DomainName: record.Domain, | ||
}) | ||
} | ||
return queue, nil | ||
} | ||
func (cloud *InsideNameCom) AddDomainRecord(domain Domain) error { | ||
api := fmt.Sprintf("%s/v4/domains/%s/records", cloud.API, domain.DomainName) | ||
item := NameComItem{ | ||
Host: domain.DomainName, | ||
RR: domain.RR, | ||
Type: "A", | ||
IP: domain.IP, | ||
TTL: 300, | ||
} | ||
body, _ := json.Marshal(item) | ||
request, _ := http.NewRequest("POST", api, bytes.NewReader(body)) | ||
request.Header.Set("Content-Type", "application/json") | ||
request.SetBasicAuth(cloud.Username, cloud.Token) | ||
response, err := client.Do(request) | ||
if err != nil { | ||
return err | ||
} | ||
defer response.Body.Close() | ||
responseBody, err := ioutil.ReadAll(response.Body) | ||
if err != nil { | ||
return err | ||
} | ||
if response.StatusCode != 200 { | ||
return fmt.Errorf(string(responseBody)) | ||
} | ||
return nil | ||
} | ||
func (cloud *InsideNameCom) UpdateDomainRecord(id string, domain Domain) error { | ||
api := fmt.Sprintf("%s/v4/domains/%s/records/%s", cloud.API, domain.DomainName, domain.RecordId) | ||
item := NameComItem{ | ||
Host: domain.DomainName, | ||
RR: domain.RR, | ||
Type: "A", | ||
IP: domain.IP, | ||
TTL: 300, | ||
} | ||
body, _ := json.Marshal(item) | ||
request, _ := http.NewRequest("PUT", api, bytes.NewReader(body)) | ||
request.Header.Set("Content-Type", "application/json") | ||
request.SetBasicAuth(cloud.Username, cloud.Token) | ||
response, err := client.Do(request) | ||
if err != nil { | ||
return err | ||
} | ||
defer response.Body.Close() | ||
responseBody, err := ioutil.ReadAll(response.Body) | ||
if err != nil { | ||
return err | ||
} | ||
if response.StatusCode != 200 { | ||
return fmt.Errorf(string(responseBody)) | ||
} | ||
return nil | ||
} | ||
|
||
func (cloud *InsideNameCom) AddOrUpdateDomain(domain Domain) (bool, error) { | ||
records, err := cloud.GetDomainRecords(domain.DomainName) | ||
if err != nil { | ||
return false, err | ||
} | ||
for _, record := range records { | ||
if record.RR == domain.RR { | ||
// ip 没有变化,不需要重新解析 | ||
if record.IP == domain.IP { | ||
return false, nil | ||
} | ||
return true, cloud.UpdateDomainRecord(record.RecordId, domain) | ||
} | ||
} | ||
return true, cloud.AddDomainRecord(domain) | ||
} | ||
|
||
func CreateNameCom(conf config.NameComConfig) *InsideNameCom { | ||
return &InsideNameCom{ | ||
API: "https://api.name.com", | ||
Username: conf.Username, | ||
Token: conf.Token, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package config | ||
|
||
type AliyunConfig struct { | ||
AccessKeyID string | ||
AccessKeySecret string | ||
} | ||
|
||
type NameComConfig struct { | ||
Username string | ||
Token string | ||
} | ||
|
||
type Config struct { | ||
Refresh int | ||
Aliyun AliyunConfig | ||
NameCom NameComConfig | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.