Let’s Encrypt颁发的通配符证书每次续期需要验证一下DNS的TXT记录,这个TXT记录的值是会发生变化的。所以续期时候需要更改下DNS的TXT记录。网上流传的都是php版本的脚本。还要写shell+php,挺麻烦的。这里我自己用go写了一个更新DNS记录的小程序
The error was: PluginError(‘An authentication script must be provided with --manual-auth-hook when using the manual plugin non-interactively.’,). Skipping.
All renewal attempts failed. The following certs could not be renewed:
# 阿里云的access key和secret
export CERTBOT_ALI_KEY=""
export CERTBOT_ALI_SECRET=""
# 用法
certbot-auto renew --cert-name shuaibo.wang --manual-auth-hook /usr/bin/certbot-alidns
nginx -s reload
package main
import (
"fmt"
"github.com/denverdino/aliyungo/dns"
"os"
"time"
)
var certbot_ali_key string
var certbot_ali_secret string
func init() {
// 定义阿里云的访问key和secret
certbot_ali_key = os.Getenv("CERTBOT_ALI_KEY")
certbot_ali_secret = os.Getenv("CERTBOT_ALI_SECRET")
// 判断阿里云的key和secret是否存在
if certbot_ali_key == "" || certbot_ali_secret == "" {
fmt.Println("请设置环境变量CERTBOT_ALI_KEY和CERTBOT_ALI_SECRET")
os.Exit(1)
}
}
func main() {
client := dns.NewClient(certbot_ali_key, certbot_ali_secret)
var args = new(dns.DescribeDomainRecordsArgs)
args.DomainName = os.Getenv("CERTBOT_DOMAIN")
args.RRKeyWord = "_acme-challenge"
args.TypeKeyWord = "TXT"
res, err := client.DescribeDomainRecords(args)
if err == nil {
records := res.DomainRecords.Record
// 记录大于1执行更新,小于1执行创建
if len(records) > 0 {
for i := 0; i < len(records); i++ {
var update_args = new(dns.UpdateDomainRecordArgs)
update_args.RecordId = records[i].RecordId
update_args.RR = "_acme-challenge"
update_args.Value = os.Getenv("CERTBOT_VALIDATION")
update_args.Type = "TXT"
res, err := client.UpdateDomainRecord(update_args)
if err == nil {
fmt.Println("更新成功:", res.RecordId)
time.Sleep(time.Duration(20) * time.Second)
} else {
fmt.Println("更新失败:", err.Error())
os.Exit(2)
}
}
} else {
// 执行创建操作
var add_args = new(dns.AddDomainRecordArgs)
add_args.DomainName = os.Getenv("CERTBOT_VALIDATION")
add_args.RR="_acme-challenge"
add_args.Value=os.Getenv("CERTBOT_VALIDATION")
add_args.Type="TXT"
res,err:=client.AddDomainRecord(add_args)
if err == nil {
fmt.Println("创建成功:", res.RecordId)
time.Sleep(time.Duration(20) * time.Second)
} else {
fmt.Println("创建失败:", err.Error())
os.Exit(2)
}
}
}
}
certbot-alidns下载
https://blog.shuaibo.wang/article/29/Let’s Encrypt通配域名自动续期