From efcdabb802411c25a770f9e93ac86992eff503d3 Mon Sep 17 00:00:00 2001 From: dustoair <107600816+dustoair@users.noreply.github.com> Date: Thu, 28 Jul 2022 14:19:16 +0800 Subject: [PATCH] open src --- .gitignore | 11 +++++ config.json | 6 +++ ddns_test.go | 14 ++++++ global/const.go | 8 ++++ go.mod | 10 ++++ main.go | 77 ++++++++++++++++++++++++++++++ readme.md | 31 ++++++++++++ util/wechat.go | 114 ++++++++++++++++++++++++++++++++++++++++++++ util/wechat_test.go | 7 +++ 9 files changed, 278 insertions(+) create mode 100644 .gitignore create mode 100644 config.json create mode 100644 ddns_test.go create mode 100644 global/const.go create mode 100644 go.mod create mode 100644 main.go create mode 100644 readme.md create mode 100644 util/wechat.go create mode 100644 util/wechat_test.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cab8a76 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +.idea +go.sum +main.exe +server.exe +log.txt +logs +*.log +uploads +gin.log +*.exe +nogit.txt \ No newline at end of file diff --git a/config.json b/config.json new file mode 100644 index 0000000..c7eee92 --- /dev/null +++ b/config.json @@ -0,0 +1,6 @@ +{ + "AccessKeyID": "LTAI5tRhn6zirsA5hocoyW51", + "AccessKeySecret": "JuEl4cj1RJVqrnlluO7MLcgvf1Jv2F", + "DomainName": "netinn.net", + "RR": "*" +} \ No newline at end of file diff --git a/ddns_test.go b/ddns_test.go new file mode 100644 index 0000000..b517fd7 --- /dev/null +++ b/ddns_test.go @@ -0,0 +1,14 @@ +package main + +import ( + "fmt" + "git.sre.ink/go/gtool" + "testing" +) + +func TestGetIP(tt *testing.T) { + // 获取公网IP信息 + publicIP := gtool.NET.PublicIP() + fmt.Println("publicIP: ", publicIP) + +} diff --git a/global/const.go b/global/const.go new file mode 100644 index 0000000..28fc00b --- /dev/null +++ b/global/const.go @@ -0,0 +1,8 @@ +package global + +const ( + AccessKeyID = "3333333333333333" + AccessKeySecret = "33333333333333333333333" + DomainName = "my.com" + RR = "*" +) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..33e5a77 --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module ddns + +go 1.19 + +require ( + git.sre.ink/go/gtool v0.0.0-20220623014517-291a4ebac231 + github.com/denverdino/aliyungo v0.0.0-20220610083100-ab5f747cb559 +) + +require github.com/opentracing/opentracing-go v1.2.0 // indirect diff --git a/main.go b/main.go new file mode 100644 index 0000000..82a3b6b --- /dev/null +++ b/main.go @@ -0,0 +1,77 @@ +package main + +import ( + "ddns/global" + "ddns/util" + "fmt" + "git.sre.ink/go/gtool" + "github.com/denverdino/aliyungo/dns" +) + +func main() { + // 获取公网IP信息 + publicIP := gtool.NET.PublicIP() + if publicIP == "" { + return + } + + // 获取阿里DNS信息 + client := dns.NewClient(global.AccessKeyID, global.AccessKeySecret) + client.SetDebug(false) + domainInfo := new(dns.DescribeDomainRecordsArgs) + domainInfo.DomainName = global.DomainName + domainInfo.PageNumber = 1 + domainInfo.PageSize = 500 //最大是500 + oldRecord, err := client.DescribeDomainRecords(domainInfo) + if err != nil { + fmt.Println("链接服务器错误", err) + msg := "ddns解析链接服务器错误:\n" + msg += "域名信息: " + global.RR + global.DomainName + "\n" + msg += "链接服务器错误: " + err.Error() + "\n" + util.SendWechat(msg) + return + } + + // 匹配公网IP地址 + var exsitRecordID string + for _, record := range oldRecord.DomainRecords.Record { + if record.DomainName == global.DomainName && record.RR == global.RR { + if record.Value == publicIP { + fmt.Println("当前配置解析地址与公网IP相同,不需要修改。") + return + } + exsitRecordID = record.RecordId + } + } + + if 0 < len(exsitRecordID) { + // 有配置记录,更新配置 + updateRecord := new(dns.UpdateDomainRecordArgs) + updateRecord.RecordId = exsitRecordID + updateRecord.RR = global.RR + updateRecord.Value = publicIP + updateRecord.Type = dns.ARecord + rsp := new(dns.UpdateDomainRecordResponse) + rsp, err := client.UpdateDomainRecord(updateRecord) + if nil != err { + fmt.Println("修改解析失败", err) + } else { + fmt.Println("修改解析成功", rsp) + } + } else { + // 没有配置记录,新增配置 + + newRecord := new(dns.AddDomainRecordArgs) + newRecord.DomainName = global.DomainName + newRecord.RR = global.RR + newRecord.Value = publicIP + newRecord.Type = dns.ARecord + rsp := new(dns.AddDomainRecordResponse) + rsp, err = client.AddDomainRecord(newRecord) + if nil != err { + fmt.Println("添加DNS解析失败", err) + } else { + fmt.Println("添加DNS解析成功", rsp) + } + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..11cc539 --- /dev/null +++ b/readme.md @@ -0,0 +1,31 @@ +DDNS for aliyun +*** +## build +```bash +cd /root +rm -rf ddns +git clone https://git.sre.ink/go/ddns.git +cd ddns +CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on GOPROXY="https://goproxy.cn,direct" go mod tidy +CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on GOPROXY="https://goproxy.cn,direct" go build -v -a -o ddns main.go +rm -rf /usr/bin/ddns +mv ddns /usr/bin/ +ddns +``` +## crontab +``` +*/10 * * * * /usr/bin/ddns +``` + +## build on arm n1 +```bash +cd /root +rm -rf ddns +git clone https://git.sre.ink/go/ddns.git +cd ddns +CGO_ENABLED=0 GOOS=linux GOARCH=arm64 GO111MODULE=on GOPROXY="https://goproxy.cn,direct" go mod tidy +CGO_ENABLED=0 GOOS=linux GOARCH=arm64 GO111MODULE=on GOPROXY="https://goproxy.cn,direct" go build -v -a -o ddns main.go +rm -rf /usr/bin/ddns +mv ddns /usr/bin/ +ddns +``` \ No newline at end of file diff --git a/util/wechat.go b/util/wechat.go new file mode 100644 index 0000000..a569a6c --- /dev/null +++ b/util/wechat.go @@ -0,0 +1,114 @@ +package util + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net/http" + "os" + "reflect" +) + +var WECOM_CID string = GetEnvDefault("WECOM_CID", "ww24195de79108759b") +var WECOM_SECRET string = GetEnvDefault("WECOM_SECRET", "JkD2a6EZ7e5NRQ9UEysgS8oLh6OD_esN5q5wtoEn5WQ") +var WECOM_AID string = GetEnvDefault("WECOM_AID", "1000002") +var WECOM_TOUID string = GetEnvDefault("WECOM_TOUID", "Qiaoyang") +var REDIS_STAT string = GetEnvDefault("REDIS_STAT", "OFF") +var REDIS_PASSWORD string = GetEnvDefault("REDIS_PASSWORD", "") +var ctx = context.Background() + +func GetEnvDefault(key, defVal string) string { + val, ex := os.LookupEnv(key) + if !ex { + return defVal + } + return val +} + +func praser_json(json_str string) map[string]interface{} { + var wecom_response map[string]interface{} + if string(json_str) != "" { + err := json.Unmarshal([]byte(string(json_str)), &wecom_response) + if err != nil { + log.Println("生成json字符串错误") + } + } + return wecom_response +} + +func get_token(corpid, app_secret string) string { + resp, err := http.Get("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret=" + app_secret) + if err != nil { + log.Println(err) + } + defer resp.Body.Close() + resp_data, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Println(err) + } + token_response := praser_json(string(resp_data)) + return token_response["access_token"].(string) +} + +func post_msg(text_msg, msg_type, post_url string) string { + type msg struct { + Content string `json:"content"` + } + type JsonData struct { + Touser string `json:"touser"` + Agentid string `json:"agentid"` + Msgtype string `json:"msgtype"` + Text msg `json:"text"` + Duplicate_check_interval int `json:"duplicate_check_interval"` + } + post_data := JsonData{ + Touser: WECOM_TOUID, + Agentid: WECOM_AID, + Msgtype: msg_type, + Duplicate_check_interval: 600, + Text: msg{Content: text_msg}, + } + + post_json, _ := json.Marshal(post_data) + log.Println(string(post_json)) + msg_req, err := http.NewRequest("POST", post_url, bytes.NewBuffer(post_json)) + if err != nil { + log.Println(err) + } + msg_req.Header.Set("Content-Type", "application/json") + client := &http.Client{} + resp, err := client.Do(msg_req) + if err != nil { + panic(err) + } + defer msg_req.Body.Close() + body, _ := ioutil.ReadAll(resp.Body) + return string(body) +} + +func IsZero(v interface{}) (bool, error) { + t := reflect.TypeOf(v) + if !t.Comparable() { + return false, fmt.Errorf("type is not comparable: %v", t) + } + return v == reflect.Zero(t).Interface(), nil +} + +func SendWechat(msg string) { + access_token := get_token(WECOM_CID, WECOM_SECRET) + post_url := "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + access_token + msg_type := "text" + post_status := post_msg(msg, msg_type, post_url) + //log.Println(post_status) + post_response := praser_json(string(post_status)) + //log.Println(post_response) + errcode := post_response["errcode"] + _, err := IsZero(errcode) + if err != nil { + fmt.Printf("%v", err) + } + +} diff --git a/util/wechat_test.go b/util/wechat_test.go new file mode 100644 index 0000000..3bac7c8 --- /dev/null +++ b/util/wechat_test.go @@ -0,0 +1,7 @@ +package util + +import "testing" + +func TestSendWechat(t *testing.T) { + SendWechat("test okok") +}