-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
210 lines (180 loc) · 5.48 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// This file is auto-generated, don't edit it. Thanks.
package main
import (
"alidns/log"
"encoding/json"
"flag"
"fmt"
alidns20150109 "github.com/alibabacloud-go/alidns-20150109/v4/client"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
"os"
"strings"
"time"
)
type Client struct {
alidns20150109.Client
}
// CreateClient
/*
* 使用AK&SK初始化账号Client
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
func CreateClient(accessKeyId *string, accessKeySecret *string) (_result *Client, _err error) {
config := &openapi.Config{
// 必填,您的 AccessKey ID
AccessKeyId: accessKeyId,
// 必填,您的 AccessKey Secret
AccessKeySecret: accessKeySecret,
}
// 访问的域名
config.Endpoint = tea.String("alidns.cn-shanghai.aliyuncs.com")
client := new(Client)
err := client.Init(config)
return client, err
}
// AddDomainRecord 添加记录
func (c *Client) AddDomainRecord(DomainName, Type, RR, Value string) (_result *alidns20150109.AddDomainRecordResponse, _err error) {
addDomainRecordRequest := &alidns20150109.AddDomainRecordRequest{
DomainName: tea.String(DomainName),
Type: tea.String(Type),
RR: tea.String(RR),
Value: tea.String(Value),
}
runtime := &util.RuntimeOptions{}
return c.AddDomainRecordWithOptions(addDomainRecordRequest, runtime)
}
// DeleteSubDomainRecords 删除记录
func (c *Client) DeleteSubDomainRecords(DomainName, Type, RR string) (_result *alidns20150109.DeleteSubDomainRecordsResponse, _err error) {
deleteSubDomainRecordsRequest := &alidns20150109.DeleteSubDomainRecordsRequest{
DomainName: tea.String(DomainName),
Type: tea.String(Type),
RR: tea.String(RR),
}
runtime := &util.RuntimeOptions{}
return c.DeleteSubDomainRecordsWithOptions(deleteSubDomainRecordsRequest, runtime)
}
// DeleteDomainRecord 删除记录
func (c *Client) DeleteDomainRecord(RecordId *string) (_result *alidns20150109.DeleteDomainRecordResponse, _err error) {
deleteDomainRecordRequest := &alidns20150109.DeleteDomainRecordRequest{
RecordId: RecordId,
}
runtime := &util.RuntimeOptions{}
return c.DeleteDomainRecordWithOptions(deleteDomainRecordRequest, runtime)
}
// DescribeDomainRecords 查询记录
func (c *Client) DescribeDomainRecords(DomainName, Type, RRKeyWord string) (_result *alidns20150109.DescribeDomainRecordsResponse, _err error) {
describeDomainRecordsRequest := &alidns20150109.DescribeDomainRecordsRequest{
DomainName: tea.String(DomainName),
Type: tea.String(Type),
RRKeyWord: tea.String(RRKeyWord),
}
runtime := &util.RuntimeOptions{}
return c.DescribeDomainRecordsWithOptions(describeDomainRecordsRequest, runtime)
}
var (
AccessKey string
)
type Config struct {
AccessKeyId string `json:"access_key_id"`
AccessKeySecret string `json:"access_key_secret"`
}
func init() {
flag.StringVar(&AccessKey, "AK", "", "Set AccessKey")
flag.Parse()
}
func main() {
Home := os.Getenv("HOME")
ConfigPath := Home + "/.alidns"
ConfigFile := ConfigPath + "/ali.json"
cfg := &Config{}
if AccessKey != "" {
ak := strings.Split(AccessKey, "=")
if AccessKey == "" || len(ak) != 2 {
log.Error("AccessKeyId and AccessKeySecret must be specified. (e.g -AK id=secret)")
os.Exit(1)
}
cfg.AccessKeyId = ak[0]
cfg.AccessKeySecret = ak[1]
data, err := json.Marshal(cfg)
if err != nil {
log.Error("set AccessKey failed:", err.Error())
os.Exit(1)
}
_ = os.MkdirAll(ConfigPath, 0755)
_ = os.WriteFile(ConfigFile, data, 0755)
log.Info("set AccessKey success")
os.Exit(0)
}
data, err := os.ReadFile(ConfigFile)
if err == nil {
err = json.Unmarshal(data, cfg)
}
if err != nil {
log.Error(err)
os.Exit(1)
}
AccessKeyId := tea.String(cfg.AccessKeyId)
AccessKeySecret := tea.String(cfg.AccessKeySecret)
if *util.Empty(AccessKeyId) || *util.Empty(AccessKeySecret) {
log.Error("need set AccessKey")
os.Exit(1)
}
client, err := CreateClient(AccessKeyId, AccessKeySecret)
if err != nil {
panic(err)
}
CertbotDomain := os.Getenv("CERTBOT_DOMAIN")
CertbotValidation := os.Getenv("CERTBOT_VALIDATION")
//CertbotToken := os.Getenv("CERTBOT_TOKEN")
CertbotAuthOutput := os.Getenv("CERTBOT_AUTH_OUTPUT")
if CertbotDomain == "" || CertbotValidation == "" {
flag.Usage()
os.Exit(0)
}
comps := strings.Split(CertbotDomain, ".")
if len(comps) < 2 {
log.Error("domain name error:", CertbotDomain)
os.Exit(1)
}
n := len(comps) - 2
Type := "TXT"
Domain := strings.Join(comps[n:], ".")
RR := "_acme-challenge"
if n > 0 {
RR += fmt.Sprintf(".%s", strings.Join(comps[:n], "."))
}
add := strings.Count(CertbotAuthOutput, CertbotValidation) == 0
if add { // 添加
log.Info("ADD -", CertbotDomain, CertbotValidation)
_, err1 := client.AddDomainRecord(Domain, Type, RR, CertbotValidation)
if err1 == nil {
log.Info("success")
} else {
log.Error("failed:", err1)
}
time.Sleep(3 * time.Second)
} else {
log.Info("DEL -", CertbotDomain, CertbotValidation)
resp, err1 := client.DescribeDomainRecords(Domain, Type, RR)
if err1 != nil {
log.Error("query failed:", err1)
os.Exit(1)
}
for _, record := range resp.Body.DomainRecords.Record {
if CertbotValidation == tea.StringValue(record.Value) && RR == tea.StringValue(record.RR) {
_, err2 := client.DeleteDomainRecord(record.RecordId)
if err2 == nil {
log.Info("success")
} else {
log.Error("failed:", err2)
}
break
}
}
}
}