十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
純粹練手用的,大家輕噴
站在用戶的角度思考問題,與客戶深入溝通,找到天河網(wǎng)站設(shè)計(jì)與天河網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊、虛擬主機(jī)、企業(yè)郵箱。業(yè)務(wù)覆蓋天河地區(qū)。
https://console.cloud.tencent.com/cam/capi
,然后新建密鑰
記錄生成的SecretId,SecretKey
https://console.cloud.tencent.com/api/explorer?Product=dnspod&Version=2021-03-23&Action=DescribeRecordList&SignVersion=
點(diǎn)擊在線調(diào)試
,然后將自己的域名輸入到Domain
下面的框里,點(diǎn)擊發(fā)送請求
,此時(shí)會讓掃碼登錄
,微信掃碼登錄即可,登錄之后在響應(yīng)結(jié)果
框里會出現(xiàn)自己的RecordId
,記錄一下自己需要修改的RecordIdpackage main
import (
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strconv"
"strings"
"time"
"github.com/robfig/cron"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
dnspod "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod/v"
)
//全局IP,本次獲取與上次作比較,相同IP不需要更新
var currentIp = ""
func main() {
c := cron.New()
spec := "*/30 * * * * ?" //可自行更改,默認(rèn)是30秒執(zhí)行一次
c.AddFunc(spec, func() {
fmt.Printf("start:%v \n", time.Now())
modifyRecordAfterGetIp()
fmt.Printf("shop:%v \n", time.Now())
})
c.Start()
select {}
}
//循環(huán)獲取公網(wǎng)IP,對比變化,變化后同步
func modifyRecordAfterGetIp() {
fmt.Printf("currentIp: %s \n", currentIp)
//獲取記錄
records := [][]string{}
row1 := []string{"yourhost.cn", "A", "yourRecordId", "默認(rèn)", "", "*"}
row2 := []string{"yourhost.cn", "A", "yourRecordId", "默認(rèn)", "", "@"}
row3 := []string{"yourhost.cn", "A", "yourRecordId", "默認(rèn)", "", "www"}
records = append(records, row1)
records = append(records, row2)
records = append(records, row3)
//獲取Ip的網(wǎng)址
sliceIp := []string{"myip.ipip.net/s", "api.ip.sb/ip", "ident.me", "ip.3322.net", "ip.cip.cc", "api.ipify.org"}
// 迭代每一個(gè)元素,獲取Ip并判斷Ip是否符合,再調(diào)用修改騰訊云記錄
for index, value := range sliceIp {
fmt.Printf("---- 開始請求IP %s ----\n", value)
result := string(getIp("http://" + value))
isIpv4 := matchIpv4(result)
fmt.Printf("Index: %d Value: %s result:%s matchIpv4:%t \n", index, value, result, isIpv4)
if isIpv4 {
if currentIp != result {
fmt.Printf("---- 結(jié)束請求IP ----\n \n")
currentIp = result
var i int
for i = 0; i < 3; i++ {
intNum, _ := strconv.Atoi(records[i][2])
modifyDynamicDNS(records[i][0], records[i][1], uint64(intNum), records[i][3], currentIp, records[i][5])
}
fmt.Printf("currentIp: %s \n", currentIp)
}
fmt.Printf("Ip未發(fā)生變化 \n")
break
}
}
fmt.Printf("---- 結(jié)束 ---- \n")
fmt.Printf("currentIp: %s \n", currentIp)
}
//調(diào)用騰訊云api修改記錄
func modifyDynamicDNS(domain string, recordType string, recordId uint64, recordLine string, ip string, subDomain string) {
credential := common.NewCredential(
"yourSecretId",
"yourSecretKey",
)
cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = "dnspod.tencentcloudapi.com"
client, _ := dnspod.NewClient(credential, "", cpf)
request := dnspod.NewModifyRecordBatchRequest()
request.RecordIdList = make([]*uint64, 1)
fmt.Printf("開始更新記錄:%d \n", recordId)
request.RecordIdList[0] = &recordId
request.Change = new(string)
request.ChangeTo = new(string)
*request.Change = "value"
*request.ChangeTo = strings.Replace(ip, "\n", "", -1)
fmt.Printf("requestJson:%s \n", request.ToJsonString())
response, err := client.ModifyRecordBatch(request)
if _, ok := err.(*errors.TencentCloudSDKError); ok {
fmt.Printf("error:An API error has returned: %s \n", err)
return
}
if err != nil {
panic(err)
}
fmt.Printf("response:%s \n", response.ToJsonString())
fmt.Printf("結(jié)束更新記錄:%d \n \n", recordId)
}
//獲取公網(wǎng)IP
func getIp(url string) string {
client := http.Client{
Timeout: 2 * time.Second,
}
resp, err := client.Get(url)
if err != nil {
fmt.Printf(" err:%s \n", err)
return "error"
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
result := string(body)
fmt.Printf("body:%s", string(body))
fmt.Printf("StatusCode:%d \n", resp.StatusCode)
if resp.StatusCode == 200 {
fmt.Printf("ok" + " \n")
}
return result
}
//匹配是否是Ipv4
func matchIpv4(ip string) bool {
matched, err := regexp.MatchString("((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}", ip)
if err != nil {
fmt.Println("ip匹配出現(xiàn)錯(cuò)誤")
return false
}
if matched { // 匹配上了
if len(ip) < 16 {
return true
}
}
return false
}
export GOPROXY=https://mirrors.tencent.com/go/
set GOPROXY=https://mirrors.tencent.com/go/
go mod init dnspodRecord
go get -v -u github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common
go get -v -u github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod
go get github.com/robfig/cron
go run dnspodRecord.go