-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for Dynadot. #1050
Merged
Merged
Add support for Dynadot. #1050
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,162 @@ | ||
package dns | ||
|
||
import ( | ||
"bytes" | ||
"github.com/jeessy2/ddns-go/v6/config" | ||
"github.com/jeessy2/ddns-go/v6/util" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
// https://www.dynadot.com/set_ddns | ||
const ( | ||
dynadotEndpoint string = "https://www.dynadot.com/set_ddns" | ||
) | ||
|
||
// Dynadot Dynadot | ||
type Dynadot struct { | ||
DNS config.DNS | ||
Domains config.Domains | ||
TTL string | ||
} | ||
|
||
// DynadotRecord record | ||
type DynadotRecord struct { | ||
DomainName string | ||
SubDomainNames []string | ||
CustomParams url.Values | ||
Domains []*config.Domain | ||
} | ||
|
||
// DynadotResp 修改/添加返回结果 | ||
type DynadotResp struct { | ||
Status string `json:"status"` | ||
ErrorCode int `json:"error_code"` | ||
Content []string `json:"content"` | ||
} | ||
|
||
// Init 初始化 | ||
func (dynadot *Dynadot) Init(dnsConf *config.DnsConfig, ipv4cache *util.IpCache, ipv6cache *util.IpCache) { | ||
dynadot.Domains.Ipv4Cache = ipv4cache | ||
dynadot.Domains.Ipv6Cache = ipv6cache | ||
dynadot.DNS = dnsConf.DNS | ||
dynadot.Domains.GetNewIp(dnsConf) | ||
if dnsConf.TTL == "" { | ||
// 默认600s | ||
dynadot.TTL = "600" | ||
} else { | ||
dynadot.TTL = dnsConf.TTL | ||
} | ||
} | ||
|
||
// AddUpdateDomainRecords 添加或更新IPv4/IPv6记录 | ||
func (dynadot *Dynadot) AddUpdateDomainRecords() config.Domains { | ||
dynadot.addOrUpdateDomainRecords("A") | ||
dynadot.addOrUpdateDomainRecords("AAAA") | ||
return dynadot.Domains | ||
} | ||
|
||
// addOrUpdateDomainRecords 添加或更新记录 | ||
func (dynadot *Dynadot) addOrUpdateDomainRecords(recordType string) { | ||
ipAddr, domains := dynadot.Domains.GetNewIpResult(recordType) | ||
|
||
if len(ipAddr) == 0 { | ||
return | ||
} | ||
|
||
records := mergeDomains(domains) | ||
// dynadot 仅支持一个域名对应一个dynamic password | ||
if len(records) != 1 { | ||
util.Log("dynadot仅支持单域名配置,多个域名请添加更多配置") | ||
return | ||
} | ||
for _, record := range records { | ||
// 创建或更新 | ||
dynadot.createOrModify(record, recordType, ipAddr) | ||
} | ||
} | ||
|
||
// 合并域名的子域名 | ||
func mergeDomains(domains []*config.Domain) (records []*DynadotRecord) { | ||
records = make([]*DynadotRecord, 0) | ||
for _, domain := range domains { | ||
var record *DynadotRecord | ||
for _, r := range records { | ||
if r.DomainName == domain.DomainName { | ||
record = r | ||
params := domain.GetCustomParams() | ||
for key := range params { | ||
record.CustomParams.Add(key, params.Get(key)) | ||
} | ||
record.Domains = append(record.Domains, domain) | ||
record.SubDomainNames = append(record.SubDomainNames, domain.GetSubDomain()) | ||
break | ||
} | ||
} | ||
if record == nil { | ||
record = &DynadotRecord{ | ||
DomainName: domain.DomainName, | ||
CustomParams: domain.GetCustomParams(), | ||
Domains: []*config.Domain{domain}, | ||
SubDomainNames: []string{domain.GetSubDomain()}, | ||
} | ||
records = append(records, record) | ||
} | ||
} | ||
return records | ||
} | ||
|
||
// 创建或变更记录 | ||
func (dynadot *Dynadot) createOrModify(record *DynadotRecord, recordType string, ipAddr string) { | ||
params := record.CustomParams | ||
params.Set("domain", record.DomainName) | ||
params.Set("subDomain", strings.Join(record.SubDomainNames, ",")) | ||
params.Set("type", recordType) | ||
params.Set("ip", ipAddr) | ||
params.Set("pwd", dynadot.DNS.Secret) | ||
params.Set("ttl", dynadot.TTL) | ||
|
||
var result DynadotResp | ||
err := dynadot.request(params, &result) | ||
|
||
domains := record.Domains | ||
for _, domain := range domains { | ||
|
||
if err != nil { | ||
util.Log("更新域名解析 %s 失败! 异常信息: %s", domain, err) | ||
domain.UpdateStatus = config.UpdatedFailed | ||
return | ||
} | ||
|
||
if result.ErrorCode != -1 { | ||
util.Log("更新域名解析 %s 成功! IP: %s", domain, ipAddr) | ||
domain.UpdateStatus = config.UpdatedSuccess | ||
} else { | ||
util.Log("更新域名解析 %s 失败! 异常信息: %s", domain, strings.Join(result.Content, ",")) | ||
domain.UpdateStatus = config.UpdatedFailed | ||
} | ||
} | ||
|
||
} | ||
|
||
// request 统一请求接口 | ||
func (dynadot *Dynadot) request(params url.Values, result interface{}) (err error) { | ||
|
||
req, err := http.NewRequest( | ||
"GET", | ||
dynadotEndpoint, | ||
bytes.NewBuffer(nil), | ||
) | ||
req.URL.RawQuery = params.Encode() | ||
|
||
if err != nil { | ||
return | ||
} | ||
|
||
client := util.CreateHTTPClient() | ||
resp, err := client.Do(req) | ||
err = util.GetHTTPResponse(resp, err, result) | ||
|
||
return | ||
} |
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
每次都要请求接口,所以IP没有变动也会请求接口,会导致收到webhook通知过多
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
参考
godaddy.go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好,我这两天改一下