forked from jeessy2/ddns-go
-
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.
- Loading branch information
Showing
13 changed files
with
284 additions
and
255 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
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 |
---|---|---|
|
@@ -32,6 +32,7 @@ type Config struct { | |
} | ||
DNS DNSConfig | ||
User | ||
Webhook | ||
} | ||
|
||
// DNSConfig DNS配置 | ||
|
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,184 @@ | ||
package config | ||
|
||
import ( | ||
"ddns-go/util" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// updateStatusType 更新状态 | ||
type updateStatusType string | ||
|
||
const ( | ||
// UpdatedNothing 未改变 | ||
UpdatedNothing updateStatusType = "未改变" | ||
// UpdatedFailed 更新失败 | ||
UpdatedFailed = "失败" | ||
// UpdatedSuccess 更新成功 | ||
UpdatedSuccess = "成功" | ||
) | ||
|
||
// Domains Ipv4/Ipv6 domains | ||
type Domains struct { | ||
Ipv4Addr string | ||
Ipv4Domains []*Domain | ||
Ipv6Addr string | ||
Ipv6Domains []*Domain | ||
} | ||
|
||
// Domain 域名实体 | ||
type Domain struct { | ||
DomainName string | ||
SubDomain string | ||
UpdateStatus updateStatusType // 更新状态 | ||
} | ||
|
||
func (d Domain) String() string { | ||
if d.SubDomain != "" { | ||
return d.SubDomain + "." + d.DomainName | ||
} | ||
return d.DomainName | ||
} | ||
|
||
// GetFullDomain 获得全部的,子域名 | ||
func (d Domain) GetFullDomain() string { | ||
if d.SubDomain != "" { | ||
return d.SubDomain + "." + d.DomainName | ||
} | ||
return "@" + "." + d.DomainName | ||
} | ||
|
||
// GetSubDomain 获得子域名,为空返回@ | ||
// 阿里云,dnspod需要 | ||
func (d Domain) GetSubDomain() string { | ||
if d.SubDomain != "" { | ||
return d.SubDomain | ||
} | ||
return "@" | ||
} | ||
|
||
// ParseDomain 接口获得ip并校验用户输入的域名 | ||
func (domains *Domains) ParseDomain(conf *Config) { | ||
// IPV4 | ||
ipv4Addr := conf.GetIpv4Addr() | ||
if ipv4Addr != "" { | ||
domains.Ipv4Addr = ipv4Addr | ||
domains.Ipv4Domains = parseDomainArr(conf.Ipv4.Domains) | ||
} | ||
// IPV6 | ||
ipv6Addr := conf.GetIpv6Addr() | ||
if ipv6Addr != "" { | ||
domains.Ipv6Addr = ipv6Addr | ||
domains.Ipv6Domains = parseDomainArr(conf.Ipv6.Domains) | ||
} | ||
} | ||
|
||
// parseDomainArr 校验用户输入的域名 | ||
func parseDomainArr(domainArr []string) (domains []*Domain) { | ||
for _, domainStr := range domainArr { | ||
domainStr = strings.Trim(domainStr, " ") | ||
if domainStr != "" { | ||
domain := &Domain{} | ||
sp := strings.Split(domainStr, ".") | ||
length := len(sp) | ||
if length <= 1 { | ||
log.Println(domainStr, "域名不正确") | ||
continue | ||
} else if length == 2 { | ||
domain.DomainName = domainStr | ||
} else { | ||
// >=3 | ||
domain.DomainName = sp[length-2] + "." + sp[length-1] | ||
domain.SubDomain = domainStr[:len(domainStr)-len(domain.DomainName)-1] | ||
} | ||
domains = append(domains, domain) | ||
} | ||
} | ||
return | ||
} | ||
|
||
// ParseDomainResult 获得ParseDomain结果 | ||
func (domains *Domains) ParseDomainResult(recordType string) (ipAddr string, retDomains []*Domain) { | ||
if recordType == "AAAA" { | ||
return domains.Ipv6Addr, domains.Ipv6Domains | ||
} | ||
return domains.Ipv4Addr, domains.Ipv4Domains | ||
|
||
} | ||
|
||
// ExecWebhook 添加或更新IPV4/IPV6记录 | ||
func (domains *Domains) ExecWebhook(conf *Config) { | ||
v4Status := getDomainsStatus(domains.Ipv4Domains) | ||
v6Status := getDomainsStatus(domains.Ipv6Domains) | ||
|
||
if conf.WebhookURL != "" && (v4Status != UpdatedNothing || v6Status != UpdatedNothing) { | ||
// 成功和失败都要触发webhook | ||
method := "GET" | ||
postPara := "" | ||
if conf.DNS.Secret != "" { | ||
method = "POST" | ||
postPara = domains.replacePara(conf.WebhookRequestBody, v4Status, v6Status) | ||
} | ||
requestURL := domains.replacePara(conf.WebhookURL, v4Status, v6Status) | ||
req, err := http.NewRequest(method, requestURL, strings.NewReader(postPara)) | ||
|
||
clt := http.Client{} | ||
clt.Timeout = 30 * time.Second | ||
resp, err := clt.Do(req) | ||
body, err := util.GetHTTPResponseOrg(resp, requestURL, err) | ||
if err == nil { | ||
log.Println(fmt.Sprintf("Webhook调用成功, 返回数据: %s", string(body))) | ||
} else { | ||
log.Println("Webhook调用失败") | ||
} | ||
} | ||
} | ||
|
||
// getDomainsStr 用逗号分割域名 | ||
func getDomainsStatus(domains []*Domain) updateStatusType { | ||
successNum := 0 | ||
for _, v46 := range domains { | ||
switch v46.UpdateStatus { | ||
case UpdatedFailed: | ||
// 一个失败,全部失败 | ||
return UpdatedFailed | ||
case UpdatedSuccess: | ||
successNum++ | ||
} | ||
} | ||
|
||
if successNum > 0 { | ||
// 迭代完成后一个成功,就成功 | ||
return UpdatedSuccess | ||
} | ||
return UpdatedNothing | ||
} | ||
|
||
// replacePara 替换参数 | ||
func (domains *Domains) replacePara(orgPara string, ipv4Result updateStatusType, ipv6Result updateStatusType) (newPara string) { | ||
orgPara = strings.ReplaceAll(orgPara, "#{ipv4Addr}", domains.Ipv4Addr) | ||
orgPara = strings.ReplaceAll(orgPara, "#{ipv4Result}", string(ipv4Result)) | ||
orgPara = strings.ReplaceAll(orgPara, "#{ipv4Domains}", getDomainsStr(domains.Ipv4Domains)) | ||
|
||
orgPara = strings.ReplaceAll(orgPara, "#{ipv6New}", domains.Ipv6Addr) | ||
orgPara = strings.ReplaceAll(orgPara, "#{ipv6Result}", string(ipv6Result)) | ||
orgPara = strings.ReplaceAll(orgPara, "#{ipv6Domains}", getDomainsStr(domains.Ipv6Domains)) | ||
|
||
return orgPara | ||
} | ||
|
||
// getDomainsStr 用逗号分割域名 | ||
func getDomainsStr(domains []*Domain) string { | ||
str := "" | ||
for i, v46 := range domains { | ||
str += v46.String() | ||
if i != len(domains)-1 { | ||
str += "," | ||
} | ||
} | ||
|
||
return str | ||
} |
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,7 @@ | ||
package config | ||
|
||
// Webhook Webhook | ||
type Webhook struct { | ||
WebhookURL string | ||
WebhookRequestBody string | ||
} |
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
Oops, something went wrong.