mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-04 04:30:05 +00:00
@@ -2,169 +2,217 @@ package ddns
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/naiba/nezha/pkg/utils"
|
||||
)
|
||||
|
||||
const baseEndpoint = "https://api.cloudflare.com/client/v4/zones"
|
||||
|
||||
type ProviderCloudflare struct {
|
||||
Secret string
|
||||
secret string
|
||||
zoneId string
|
||||
recordId string
|
||||
domainConfig *DomainConfig
|
||||
}
|
||||
|
||||
func (provider *ProviderCloudflare) UpdateDomain(domainConfig *DomainConfig) bool {
|
||||
if domainConfig == nil {
|
||||
return false
|
||||
}
|
||||
type cfReq struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Content string `json:"content"`
|
||||
TTL uint32 `json:"ttl"`
|
||||
Proxied bool `json:"proxied"`
|
||||
}
|
||||
|
||||
zoneID, err := provider.getZoneID(domainConfig.FullDomain)
|
||||
type cfResp struct {
|
||||
Result []struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
} `json:"result"`
|
||||
}
|
||||
|
||||
func NewProviderCloudflare(s string) *ProviderCloudflare {
|
||||
return &ProviderCloudflare{
|
||||
secret: s,
|
||||
}
|
||||
}
|
||||
|
||||
func (provider *ProviderCloudflare) UpdateDomain(domainConfig *DomainConfig) error {
|
||||
if domainConfig == nil {
|
||||
return fmt.Errorf("获取 DDNS 配置失败")
|
||||
}
|
||||
provider.domainConfig = domainConfig
|
||||
|
||||
err := provider.getZoneID()
|
||||
if err != nil {
|
||||
log.Printf("无法获取 zone ID: %s\n", err)
|
||||
return false
|
||||
return fmt.Errorf("无法获取 zone ID: %s", err)
|
||||
}
|
||||
|
||||
// 当IPv4和IPv6同时成功才算作成功
|
||||
var resultV4 = true
|
||||
var resultV6 = true
|
||||
if domainConfig.EnableIPv4 {
|
||||
if !provider.addDomainRecord(zoneID, domainConfig, true) {
|
||||
resultV4 = false
|
||||
if provider.domainConfig.EnableIPv4 {
|
||||
if err = provider.addDomainRecord(true); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if domainConfig.EnableIpv6 {
|
||||
if !provider.addDomainRecord(zoneID, domainConfig, false) {
|
||||
resultV6 = false
|
||||
if provider.domainConfig.EnableIpv6 {
|
||||
if err = provider.addDomainRecord(false); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return resultV4 && resultV6
|
||||
return nil
|
||||
}
|
||||
|
||||
func (provider *ProviderCloudflare) addDomainRecord(zoneID string, domainConfig *DomainConfig, isIpv4 bool) bool {
|
||||
record, err := provider.findDNSRecord(zoneID, domainConfig.FullDomain, isIpv4)
|
||||
func (provider *ProviderCloudflare) addDomainRecord(isIpv4 bool) error {
|
||||
err := provider.findDNSRecord(isIpv4)
|
||||
if err != nil {
|
||||
log.Printf("查找 DNS 记录时出错: %s\n", err)
|
||||
return false
|
||||
return fmt.Errorf("查找 DNS 记录时出错: %s", err)
|
||||
}
|
||||
|
||||
if record == nil {
|
||||
if provider.recordId == "" {
|
||||
// 添加 DNS 记录
|
||||
return provider.createDNSRecord(zoneID, domainConfig, isIpv4)
|
||||
return provider.createDNSRecord(isIpv4)
|
||||
} else {
|
||||
// 更新 DNS 记录
|
||||
return provider.updateDNSRecord(zoneID, record["id"].(string), domainConfig, isIpv4)
|
||||
return provider.updateDNSRecord(isIpv4)
|
||||
}
|
||||
}
|
||||
|
||||
func (provider *ProviderCloudflare) getZoneID(domain string) (string, error) {
|
||||
_, realDomain := SplitDomain(domain)
|
||||
url := fmt.Sprintf("https://api.cloudflare.com/client/v4/zones?name=%s", realDomain)
|
||||
body, err := provider.sendRequest("GET", url, nil)
|
||||
func (provider *ProviderCloudflare) getZoneID() error {
|
||||
_, realDomain := splitDomain(provider.domainConfig.FullDomain)
|
||||
zu, _ := url.Parse(baseEndpoint)
|
||||
|
||||
q := zu.Query()
|
||||
q.Set("name", realDomain)
|
||||
zu.RawQuery = q.Encode()
|
||||
|
||||
body, err := provider.sendRequest("GET", zu.String(), nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return err
|
||||
}
|
||||
|
||||
var res map[string]interface{}
|
||||
err = json.Unmarshal(body, &res)
|
||||
res := &cfResp{}
|
||||
err = utils.Json.Unmarshal(body, res)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return err
|
||||
}
|
||||
|
||||
result := res["result"].([]interface{})
|
||||
result := res.Result
|
||||
if len(result) > 0 {
|
||||
zoneID := result[0].(map[string]interface{})["id"].(string)
|
||||
return zoneID, nil
|
||||
provider.zoneId = result[0].ID
|
||||
return nil
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("找不到 Zone ID")
|
||||
return fmt.Errorf("找不到 Zone ID")
|
||||
}
|
||||
|
||||
func (provider *ProviderCloudflare) findDNSRecord(zoneID string, domain string, isIPv4 bool) (map[string]interface{}, error) {
|
||||
var ipType = "A"
|
||||
if !isIPv4 {
|
||||
func (provider *ProviderCloudflare) findDNSRecord(isIPv4 bool) error {
|
||||
var ipType string
|
||||
if isIPv4 {
|
||||
ipType = "A"
|
||||
} else {
|
||||
ipType = "AAAA"
|
||||
}
|
||||
url := fmt.Sprintf("https://api.cloudflare.com/client/v4/zones/%s/dns_records?type=%s&name=%s", zoneID, ipType, domain)
|
||||
body, err := provider.sendRequest("GET", url, nil)
|
||||
|
||||
de, _ := url.JoinPath(baseEndpoint, provider.zoneId, "dns_records")
|
||||
du, _ := url.Parse(de)
|
||||
|
||||
q := du.Query()
|
||||
q.Set("name", provider.domainConfig.FullDomain)
|
||||
q.Set("type", ipType)
|
||||
du.RawQuery = q.Encode()
|
||||
|
||||
body, err := provider.sendRequest("GET", du.String(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
var res map[string]interface{}
|
||||
err = json.Unmarshal(body, &res)
|
||||
res := &cfResp{}
|
||||
err = utils.Json.Unmarshal(body, res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
result := res["result"].([]interface{})
|
||||
result := res.Result
|
||||
if len(result) > 0 {
|
||||
return result[0].(map[string]interface{}), nil
|
||||
provider.recordId = result[0].ID
|
||||
return nil
|
||||
}
|
||||
|
||||
return nil, nil // 没有找到 DNS 记录
|
||||
return nil
|
||||
}
|
||||
|
||||
func (provider *ProviderCloudflare) createDNSRecord(zoneID string, domainConfig *DomainConfig, isIPv4 bool) bool {
|
||||
var ipType = "A"
|
||||
var ipAddr = domainConfig.Ipv4Addr
|
||||
if !isIPv4 {
|
||||
func (provider *ProviderCloudflare) createDNSRecord(isIPv4 bool) error {
|
||||
var ipType, ipAddr string
|
||||
if isIPv4 {
|
||||
ipType = "A"
|
||||
ipAddr = provider.domainConfig.Ipv4Addr
|
||||
} else {
|
||||
ipType = "AAAA"
|
||||
ipAddr = domainConfig.Ipv6Addr
|
||||
ipAddr = provider.domainConfig.Ipv6Addr
|
||||
}
|
||||
url := fmt.Sprintf("https://api.cloudflare.com/client/v4/zones/%s/dns_records", zoneID)
|
||||
data := map[string]interface{}{
|
||||
"type": ipType,
|
||||
"name": domainConfig.FullDomain,
|
||||
"content": ipAddr,
|
||||
"ttl": 60,
|
||||
"proxied": false,
|
||||
|
||||
de, _ := url.JoinPath(baseEndpoint, provider.zoneId, "dns_records")
|
||||
data := &cfReq{
|
||||
Name: provider.domainConfig.FullDomain,
|
||||
Type: ipType,
|
||||
Content: ipAddr,
|
||||
TTL: 60,
|
||||
Proxied: false,
|
||||
}
|
||||
jsonData, _ := json.Marshal(data)
|
||||
_, err := provider.sendRequest("POST", url, jsonData)
|
||||
return err == nil
|
||||
|
||||
jsonData, _ := utils.Json.Marshal(data)
|
||||
_, err := provider.sendRequest("POST", de, jsonData)
|
||||
return err
|
||||
}
|
||||
|
||||
func (provider *ProviderCloudflare) updateDNSRecord(zoneID string, recordID string, domainConfig *DomainConfig, isIPv4 bool) bool {
|
||||
var ipType = "A"
|
||||
var ipAddr = domainConfig.Ipv4Addr
|
||||
if !isIPv4 {
|
||||
func (provider *ProviderCloudflare) updateDNSRecord(isIPv4 bool) error {
|
||||
var ipType, ipAddr string
|
||||
if isIPv4 {
|
||||
ipType = "A"
|
||||
ipAddr = provider.domainConfig.Ipv4Addr
|
||||
} else {
|
||||
ipType = "AAAA"
|
||||
ipAddr = domainConfig.Ipv6Addr
|
||||
ipAddr = provider.domainConfig.Ipv6Addr
|
||||
}
|
||||
url := fmt.Sprintf("https://api.cloudflare.com/client/v4/zones/%s/dns_records/%s", zoneID, recordID)
|
||||
data := map[string]interface{}{
|
||||
"type": ipType,
|
||||
"name": domainConfig.FullDomain,
|
||||
"content": ipAddr,
|
||||
"ttl": 60,
|
||||
"proxied": false,
|
||||
|
||||
de, _ := url.JoinPath(baseEndpoint, provider.zoneId, "dns_records", provider.recordId)
|
||||
data := &cfReq{
|
||||
Name: provider.domainConfig.FullDomain,
|
||||
Type: ipType,
|
||||
Content: ipAddr,
|
||||
TTL: 60,
|
||||
Proxied: false,
|
||||
}
|
||||
jsonData, _ := json.Marshal(data)
|
||||
_, err := provider.sendRequest("PATCH", url, jsonData)
|
||||
return err == nil
|
||||
|
||||
jsonData, _ := utils.Json.Marshal(data)
|
||||
_, err := provider.sendRequest("PATCH", de, jsonData)
|
||||
return err
|
||||
}
|
||||
|
||||
// 以下为辅助方法,如发送 HTTP 请求等
|
||||
func (provider *ProviderCloudflare) sendRequest(method string, url string, data []byte) ([]byte, error) {
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest(method, url, bytes.NewBuffer(data))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", provider.Secret))
|
||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", provider.secret))
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
resp, err := utils.HttpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
err := Body.Close()
|
||||
if err != nil {
|
||||
log.Printf("NEZHA>> 无法关闭HTTP响应体流: %s\n", err.Error())
|
||||
log.Printf("NEZHA>> 无法关闭HTTP响应体流: %s", err.Error())
|
||||
}
|
||||
}(resp.Body)
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package ddns
|
||||
|
||||
import "golang.org/x/net/publicsuffix"
|
||||
|
||||
type DomainConfig struct {
|
||||
EnableIPv4 bool
|
||||
EnableIpv6 bool
|
||||
@@ -10,5 +12,11 @@ type DomainConfig struct {
|
||||
|
||||
type Provider interface {
|
||||
// UpdateDomain Return is updated
|
||||
UpdateDomain(domainConfig *DomainConfig) bool
|
||||
UpdateDomain(*DomainConfig) error
|
||||
}
|
||||
|
||||
func splitDomain(domain string) (prefix string, realDomain string) {
|
||||
realDomain, _ = publicsuffix.EffectiveTLDPlusOne(domain)
|
||||
prefix = domain[:len(domain)-len(realDomain)-1]
|
||||
return prefix, realDomain
|
||||
}
|
||||
|
||||
@@ -2,6 +2,6 @@ package ddns
|
||||
|
||||
type ProviderDummy struct{}
|
||||
|
||||
func (provider *ProviderDummy) UpdateDomain(domainConfig *DomainConfig) bool {
|
||||
return false
|
||||
func (provider *ProviderDummy) UpdateDomain(domainConfig *DomainConfig) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
package ddns
|
||||
|
||||
import (
|
||||
"golang.org/x/net/publicsuffix"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (provider ProviderWebHook) FormatWebhookString(s string, config *DomainConfig, ipType string) string {
|
||||
if config == nil {
|
||||
return s
|
||||
}
|
||||
|
||||
result := strings.TrimSpace(s)
|
||||
result = strings.Replace(s, "{ip}", config.Ipv4Addr, -1)
|
||||
result = strings.Replace(result, "{domain}", config.FullDomain, -1)
|
||||
result = strings.Replace(result, "{type}", ipType, -1)
|
||||
// remove \r
|
||||
result = strings.Replace(result, "\r", "", -1)
|
||||
return result
|
||||
}
|
||||
|
||||
func SetStringHeadersToRequest(req *http.Request, headers []string) {
|
||||
if req == nil {
|
||||
return
|
||||
}
|
||||
for _, element := range headers {
|
||||
kv := strings.SplitN(element, ":", 2)
|
||||
if len(kv) == 2 {
|
||||
req.Header.Add(kv[0], kv[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SplitDomain 分割域名为前缀和一级域名
|
||||
func SplitDomain(domain string) (prefix string, realDomain string) {
|
||||
realDomain, _ = publicsuffix.EffectiveTLDPlusOne(domain)
|
||||
prefix = domain[:len(domain)-len(realDomain)-1]
|
||||
return prefix, realDomain
|
||||
}
|
||||
@@ -5,145 +5,180 @@ import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/naiba/nezha/pkg/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
url = "https://dnspod.tencentcloudapi.com"
|
||||
)
|
||||
const te = "https://dnspod.tencentcloudapi.com"
|
||||
|
||||
type ProviderTencentCloud struct {
|
||||
SecretID string
|
||||
SecretKey string
|
||||
secretID string
|
||||
secretKey string
|
||||
domainConfig *DomainConfig
|
||||
resp *tcResp
|
||||
}
|
||||
|
||||
func (provider *ProviderTencentCloud) UpdateDomain(domainConfig *DomainConfig) bool {
|
||||
if domainConfig == nil {
|
||||
return false
|
||||
type tcReq struct {
|
||||
RecordType string `json:"RecordType"`
|
||||
Domain string `json:"Domain"`
|
||||
RecordLine string `json:"RecordLine"`
|
||||
Subdomain string `json:"Subdomain,omitempty"`
|
||||
SubDomain string `json:"SubDomain,omitempty"` // As is
|
||||
Value string `json:"Value,omitempty"`
|
||||
TTL uint32 `json:"TTL,omitempty"`
|
||||
RecordId uint64 `json:"RecordId,omitempty"`
|
||||
}
|
||||
|
||||
type tcResp struct {
|
||||
Response struct {
|
||||
RecordList []struct {
|
||||
RecordId uint64
|
||||
Value string
|
||||
}
|
||||
Error struct {
|
||||
Code string
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func NewProviderTencentCloud(id, key string) *ProviderTencentCloud {
|
||||
return &ProviderTencentCloud{
|
||||
secretID: id,
|
||||
secretKey: key,
|
||||
}
|
||||
}
|
||||
|
||||
func (provider *ProviderTencentCloud) UpdateDomain(domainConfig *DomainConfig) error {
|
||||
if domainConfig == nil {
|
||||
return fmt.Errorf("获取 DDNS 配置失败")
|
||||
}
|
||||
provider.domainConfig = domainConfig
|
||||
|
||||
// 当IPv4和IPv6同时成功才算作成功
|
||||
var resultV4 = true
|
||||
var resultV6 = true
|
||||
if domainConfig.EnableIPv4 {
|
||||
if !provider.addDomainRecord(domainConfig, true) {
|
||||
resultV4 = false
|
||||
var err error
|
||||
if provider.domainConfig.EnableIPv4 {
|
||||
if err = provider.addDomainRecord(true); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if domainConfig.EnableIpv6 {
|
||||
if !provider.addDomainRecord(domainConfig, false) {
|
||||
resultV6 = false
|
||||
if provider.domainConfig.EnableIpv6 {
|
||||
if err = provider.addDomainRecord(false); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return resultV4 && resultV6
|
||||
return err
|
||||
}
|
||||
|
||||
func (provider *ProviderTencentCloud) addDomainRecord(domainConfig *DomainConfig, isIpv4 bool) bool {
|
||||
record, err := provider.findDNSRecord(domainConfig.FullDomain, isIpv4)
|
||||
func (provider *ProviderTencentCloud) addDomainRecord(isIpv4 bool) error {
|
||||
err := provider.findDNSRecord(isIpv4)
|
||||
if err != nil {
|
||||
log.Printf("查找 DNS 记录时出错: %s\n", err)
|
||||
return false
|
||||
return fmt.Errorf("查找 DNS 记录时出错: %s", err)
|
||||
}
|
||||
|
||||
if errResponse, ok := record["Error"].(map[string]interface{}); ok {
|
||||
if errCode, ok := errResponse["Code"].(string); ok && errCode == "ResourceNotFound.NoDataOfRecord" { // 没有找到 DNS 记录
|
||||
// 添加 DNS 记录
|
||||
return provider.createDNSRecord(domainConfig.FullDomain, domainConfig, isIpv4)
|
||||
} else {
|
||||
log.Printf("查询 DNS 记录时出错,错误代码为: %s\n", errCode)
|
||||
}
|
||||
if provider.resp.Response.Error.Code == "ResourceNotFound.NoDataOfRecord" { // 没有找到 DNS 记录
|
||||
return provider.createDNSRecord(isIpv4)
|
||||
} else if provider.resp.Response.Error.Code != "" {
|
||||
return fmt.Errorf("查询 DNS 记录时出错,错误代码为: %s", provider.resp.Response.Error.Code)
|
||||
}
|
||||
|
||||
// 默认情况下更新 DNS 记录
|
||||
return provider.updateDNSRecord(domainConfig.FullDomain, record["RecordList"].([]interface{})[0].(map[string]interface{})["RecordId"].(float64), domainConfig, isIpv4)
|
||||
return provider.updateDNSRecord(isIpv4)
|
||||
}
|
||||
|
||||
func (provider *ProviderTencentCloud) findDNSRecord(domain string, isIPv4 bool) (map[string]interface{}, error) {
|
||||
var ipType = "A"
|
||||
if !isIPv4 {
|
||||
func (provider *ProviderTencentCloud) findDNSRecord(isIPv4 bool) error {
|
||||
var ipType string
|
||||
if isIPv4 {
|
||||
ipType = "A"
|
||||
} else {
|
||||
ipType = "AAAA"
|
||||
}
|
||||
_, realDomain := SplitDomain(domain)
|
||||
prefix, _ := SplitDomain(domain)
|
||||
data := map[string]interface{}{
|
||||
"RecordType": ipType,
|
||||
"Domain": realDomain,
|
||||
"RecordLine": "默认",
|
||||
"Subdomain": prefix,
|
||||
|
||||
prefix, realDomain := splitDomain(provider.domainConfig.FullDomain)
|
||||
data := &tcReq{
|
||||
RecordType: ipType,
|
||||
Domain: realDomain,
|
||||
RecordLine: "默认",
|
||||
Subdomain: prefix,
|
||||
}
|
||||
jsonData, _ := json.Marshal(data)
|
||||
|
||||
jsonData, _ := utils.Json.Marshal(data)
|
||||
body, err := provider.sendRequest("DescribeRecordList", jsonData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
var res map[string]interface{}
|
||||
err = json.Unmarshal(body, &res)
|
||||
provider.resp = &tcResp{}
|
||||
err = utils.Json.Unmarshal(body, provider.resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
result := res["Response"].(map[string]interface{})
|
||||
return result, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (provider *ProviderTencentCloud) createDNSRecord(domain string, domainConfig *DomainConfig, isIPv4 bool) bool {
|
||||
var ipType = "A"
|
||||
var ipAddr = domainConfig.Ipv4Addr
|
||||
if !isIPv4 {
|
||||
func (provider *ProviderTencentCloud) createDNSRecord(isIPv4 bool) error {
|
||||
var ipType, ipAddr string
|
||||
if isIPv4 {
|
||||
ipType = "A"
|
||||
ipAddr = provider.domainConfig.Ipv4Addr
|
||||
} else {
|
||||
ipType = "AAAA"
|
||||
ipAddr = domainConfig.Ipv6Addr
|
||||
ipAddr = provider.domainConfig.Ipv6Addr
|
||||
}
|
||||
_, realDomain := SplitDomain(domain)
|
||||
prefix, _ := SplitDomain(domain)
|
||||
data := map[string]interface{}{
|
||||
"RecordType": ipType,
|
||||
"RecordLine": "默认",
|
||||
"Domain": realDomain,
|
||||
"SubDomain": prefix,
|
||||
"Value": ipAddr,
|
||||
"TTL": 600,
|
||||
|
||||
prefix, realDomain := splitDomain(provider.domainConfig.FullDomain)
|
||||
data := &tcReq{
|
||||
RecordType: ipType,
|
||||
RecordLine: "默认",
|
||||
Domain: realDomain,
|
||||
SubDomain: prefix,
|
||||
Value: ipAddr,
|
||||
TTL: 600,
|
||||
}
|
||||
jsonData, _ := json.Marshal(data)
|
||||
|
||||
jsonData, _ := utils.Json.Marshal(data)
|
||||
_, err := provider.sendRequest("CreateRecord", jsonData)
|
||||
return err == nil
|
||||
return err
|
||||
}
|
||||
|
||||
func (provider *ProviderTencentCloud) updateDNSRecord(domain string, recordID float64, domainConfig *DomainConfig, isIPv4 bool) bool {
|
||||
var ipType = "A"
|
||||
var ipAddr = domainConfig.Ipv4Addr
|
||||
if !isIPv4 {
|
||||
func (provider *ProviderTencentCloud) updateDNSRecord(isIPv4 bool) error {
|
||||
var ipType, ipAddr string
|
||||
if isIPv4 {
|
||||
ipType = "A"
|
||||
ipAddr = provider.domainConfig.Ipv4Addr
|
||||
} else {
|
||||
ipType = "AAAA"
|
||||
ipAddr = domainConfig.Ipv6Addr
|
||||
ipAddr = provider.domainConfig.Ipv6Addr
|
||||
}
|
||||
_, realDomain := SplitDomain(domain)
|
||||
prefix, _ := SplitDomain(domain)
|
||||
data := map[string]interface{}{
|
||||
"RecordType": ipType,
|
||||
"RecordLine": "默认",
|
||||
"Domain": realDomain,
|
||||
"SubDomain": prefix,
|
||||
"Value": ipAddr,
|
||||
"TTL": 600,
|
||||
"RecordId": recordID,
|
||||
|
||||
prefix, realDomain := splitDomain(provider.domainConfig.FullDomain)
|
||||
data := &tcReq{
|
||||
RecordType: ipType,
|
||||
RecordLine: "默认",
|
||||
Domain: realDomain,
|
||||
SubDomain: prefix,
|
||||
Value: ipAddr,
|
||||
TTL: 600,
|
||||
RecordId: provider.resp.Response.RecordList[0].RecordId,
|
||||
}
|
||||
jsonData, _ := json.Marshal(data)
|
||||
|
||||
jsonData, _ := utils.Json.Marshal(data)
|
||||
_, err := provider.sendRequest("ModifyRecord", jsonData)
|
||||
return err == nil
|
||||
return err
|
||||
}
|
||||
|
||||
// 以下为辅助方法,如发送 HTTP 请求等
|
||||
func (provider *ProviderTencentCloud) sendRequest(action string, data []byte) ([]byte, error) {
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
|
||||
req, err := http.NewRequest("POST", te, bytes.NewBuffer(data))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -151,8 +186,8 @@ func (provider *ProviderTencentCloud) sendRequest(action string, data []byte) ([
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-TC-Version", "2021-03-23")
|
||||
|
||||
provider.signRequest(provider.SecretID, provider.SecretKey, req, action, string(data))
|
||||
resp, err := client.Do(req)
|
||||
provider.signRequest(provider.secretID, provider.secretKey, req, action, string(data))
|
||||
resp, err := utils.HttpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -2,58 +2,109 @@ package ddns
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/naiba/nezha/pkg/utils"
|
||||
)
|
||||
|
||||
type ProviderWebHook struct {
|
||||
URL string
|
||||
RequestMethod string
|
||||
RequestBody string
|
||||
RequestHeader string
|
||||
url string
|
||||
requestMethod string
|
||||
requestBody string
|
||||
requestHeader string
|
||||
domainConfig *DomainConfig
|
||||
}
|
||||
|
||||
func (provider *ProviderWebHook) UpdateDomain(domainConfig *DomainConfig) bool {
|
||||
func NewProviderWebHook(s, rm, rb, rh string) *ProviderWebHook {
|
||||
return &ProviderWebHook{
|
||||
url: s,
|
||||
requestMethod: rm,
|
||||
requestBody: rb,
|
||||
requestHeader: rh,
|
||||
}
|
||||
}
|
||||
|
||||
func (provider *ProviderWebHook) UpdateDomain(domainConfig *DomainConfig) error {
|
||||
if domainConfig == nil {
|
||||
return false
|
||||
return fmt.Errorf("获取 DDNS 配置失败")
|
||||
}
|
||||
provider.domainConfig = domainConfig
|
||||
|
||||
if provider.domainConfig.FullDomain == "" {
|
||||
return fmt.Errorf("failed to update an empty domain")
|
||||
}
|
||||
|
||||
if domainConfig.FullDomain == "" {
|
||||
log.Println("NEZHA>> Failed to update an empty domain")
|
||||
return false
|
||||
}
|
||||
updated := false
|
||||
client := &http.Client{}
|
||||
if domainConfig.EnableIPv4 && domainConfig.Ipv4Addr != "" {
|
||||
url := provider.FormatWebhookString(provider.URL, domainConfig, "ipv4")
|
||||
body := provider.FormatWebhookString(provider.RequestBody, domainConfig, "ipv4")
|
||||
header := provider.FormatWebhookString(provider.RequestHeader, domainConfig, "ipv4")
|
||||
headers := strings.Split(header, "\n")
|
||||
req, err := http.NewRequest(provider.RequestMethod, url, bytes.NewBufferString(body))
|
||||
if err == nil && req != nil {
|
||||
SetStringHeadersToRequest(req, headers)
|
||||
if _, err := client.Do(req); err != nil {
|
||||
log.Printf("NEZHA>> Failed to update a domain: %s. Cause by: %s\n", domainConfig.FullDomain, err.Error())
|
||||
} else {
|
||||
updated = true
|
||||
}
|
||||
if provider.domainConfig.EnableIPv4 && provider.domainConfig.Ipv4Addr != "" {
|
||||
req, err := provider.prepareRequest(true)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update a domain: %s. Cause by: %v", provider.domainConfig.FullDomain, err)
|
||||
}
|
||||
if _, err := utils.HttpClient.Do(req); err != nil {
|
||||
return fmt.Errorf("failed to update a domain: %s. Cause by: %v", provider.domainConfig.FullDomain, err)
|
||||
}
|
||||
}
|
||||
if domainConfig.EnableIpv6 && domainConfig.Ipv6Addr != "" {
|
||||
url := provider.FormatWebhookString(provider.URL, domainConfig, "ipv6")
|
||||
body := provider.FormatWebhookString(provider.RequestBody, domainConfig, "ipv6")
|
||||
header := provider.FormatWebhookString(provider.RequestHeader, domainConfig, "ipv6")
|
||||
headers := strings.Split(header, "\n")
|
||||
req, err := http.NewRequest(provider.RequestMethod, url, bytes.NewBufferString(body))
|
||||
if err == nil && req != nil {
|
||||
SetStringHeadersToRequest(req, headers)
|
||||
if _, err := client.Do(req); err != nil {
|
||||
log.Printf("NEZHA>> Failed to update a domain: %s. Cause by: %s\n", domainConfig.FullDomain, err.Error())
|
||||
} else {
|
||||
updated = true
|
||||
}
|
||||
|
||||
if provider.domainConfig.EnableIpv6 && provider.domainConfig.Ipv6Addr != "" {
|
||||
req, err := provider.prepareRequest(false)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update a domain: %s. Cause by: %v", provider.domainConfig.FullDomain, err)
|
||||
}
|
||||
if _, err := utils.HttpClient.Do(req); err != nil {
|
||||
return fmt.Errorf("failed to update a domain: %s. Cause by: %v", provider.domainConfig.FullDomain, err)
|
||||
}
|
||||
}
|
||||
return updated
|
||||
return nil
|
||||
}
|
||||
|
||||
func (provider *ProviderWebHook) prepareRequest(isIPv4 bool) (*http.Request, error) {
|
||||
u, err := url.Parse(provider.url)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed parsing url: %v", err)
|
||||
}
|
||||
|
||||
// Only handle queries here
|
||||
q := u.Query()
|
||||
for p, vals := range q {
|
||||
for n, v := range vals {
|
||||
vals[n] = provider.formatWebhookString(v, isIPv4)
|
||||
}
|
||||
q[p] = vals
|
||||
}
|
||||
|
||||
u.RawQuery = q.Encode()
|
||||
body := provider.formatWebhookString(provider.requestBody, isIPv4)
|
||||
header := provider.formatWebhookString(provider.requestHeader, isIPv4)
|
||||
headers := strings.Split(header, "\n")
|
||||
|
||||
req, err := http.NewRequest(provider.requestMethod, u.String(), bytes.NewBufferString(body))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed creating new request: %v", err)
|
||||
}
|
||||
|
||||
utils.SetStringHeadersToRequest(req, headers)
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func (provider *ProviderWebHook) formatWebhookString(s string, isIPv4 bool) string {
|
||||
var ipAddr, ipType string
|
||||
if isIPv4 {
|
||||
ipAddr = provider.domainConfig.Ipv4Addr
|
||||
ipType = "ipv4"
|
||||
} else {
|
||||
ipAddr = provider.domainConfig.Ipv6Addr
|
||||
ipType = "ipv6"
|
||||
}
|
||||
|
||||
r := strings.NewReplacer(
|
||||
"{ip}", ipAddr,
|
||||
"{domain}", provider.domainConfig.FullDomain,
|
||||
"{type}", ipType,
|
||||
"\r", "",
|
||||
)
|
||||
|
||||
result := r.Replace(strings.TrimSpace(s))
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package utils
|
||||
import (
|
||||
"crypto/rand"
|
||||
"math/big"
|
||||
"net/http"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
@@ -86,3 +87,15 @@ func Uint64SubInt64(a uint64, b int64) uint64 {
|
||||
}
|
||||
return a - uint64(b)
|
||||
}
|
||||
|
||||
func SetStringHeadersToRequest(req *http.Request, headers []string) {
|
||||
if req == nil {
|
||||
return
|
||||
}
|
||||
for _, element := range headers {
|
||||
kv := strings.SplitN(element, ":", 2)
|
||||
if len(kv) == 2 {
|
||||
req.Header.Add(kv[0], kv[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user