add interface and fix

This commit is contained in:
“xHuPo” 2024-08-16 17:48:07 +08:00
parent bb46f5adc4
commit c16ee9ca2c
13 changed files with 573 additions and 89 deletions

View file

@ -1,3 +1,4 @@
# 更新或安装证书
## 安装acme.sh并使用dns方式生成证书
```shell
# 安装acme.sh
@ -21,3 +22,9 @@ acme.sh --install-cert -d turingsynergy.com \
--fullchain-file /opt/webhook/scripts/tlsupdatev1/ssl//turingsynergy.com.pem \
--reloadcmd "curl 'https//webhook-ops.shasoapp.com/hooks/tlsupdate?action=upadte&&id=[华为云中配置的证书id]'"
```
# 查看证书状态
```shell
# 如果证书有效期小于一周,推送通知到飞书,点击链接即可更新证书(前提是本地证书已更新)
curl 'https//webhook-ops.shasoapp.com/hooks/tlsupdate?action=status&&id=[华为云中配置的证书id]'
```

View file

@ -1,14 +1,14 @@
huawei:
ak: DSL6JB41TJ1SSUYQOKBB
sk: oQEJ00xSxPHpw4Xt5MhiqAz7KI04CNnZOBGCNEaQ
ak: xxx
sk: xxx
region:: cn-east-3
tls:
- id: 86756ef7524a49ef8c8f00d2f6690866
region: cn-east-3
private_key: /etc/webhook/scripts/tlsupdatev1/ssl/turingsynergy.com.key
certificate: /etc/webhook/scripts/tlsupdatev1/ssl/turingsynergy.com.pem
private_key: /etc/webhook/scripts/tlsupdate/ssl/turingsynergy.com.key
certificate: /etc/webhook/scripts/tlsupdate/ssl/turingsynergy.com.pem
notify: https://open.feishu.cn/open-apis/bot/v2/hook/986a905f-c8e0-4528-bbae-1fc5ea5a4be6
#notify: https://open.feishu.cn/open-apis/bot/v2/hook/986a905f-c8e0-4528-bbae-1fc5ea5a4be6
notify: https://open.feishu.cn/open-apis/bot/v2/hook/051679ae-0bef-4a21-9ff7-25fd76cd73ee
webhook: http://webhook-ops.shasoapp.com/hooks/hwtlsupdate
webhook: http://webhook-ops.shasoapp.com/hooks/hwtlsupdate

View file

@ -6,11 +6,11 @@ type FeishuPostMessage struct {
}
type PostContent struct {
Post *Post `json:"post"`
Post Post `json:"post"`
}
type Post struct {
ZhCn *Message `json:"zh_cn"`
ZhCn Message `json:"zh_cn"`
}
type Message struct {
@ -19,33 +19,20 @@ type Message struct {
}
type ContentItem struct {
Tag string `json:"tag"`
*TextContent
*AContent
*AtContent
*ImageContent
}
type TextContent struct {
Text string `json:"text"`
Unescape *bool `json:"un_escape"`
}
type AContent struct {
Text string `json:"text"`
Href string `json:"href"`
}
type AtContent struct {
UserID string `json:"user_id"`
Username *string `json:"username"`
}
type ImageContent struct {
ImageKey string `json:"image_key"`
Tag string `json:"tag"`
Text string `json:"text,omitempty"`
Unescape bool `json:"un_escape,omitempty"`
Href string `json:"href,omitempty"`
UserID string `json:"user_id,omitempty"`
UserName *string `json:"user_name,omitempty"`
ImageKey string `json:"image_key,omitempty"`
}
type FeishuTextMessage struct {
MsgType string `json:"msg_type"`
Content TextContent `json:"content"`
}
type TextContent struct {
Text string `json:"text"`
}

View file

@ -113,6 +113,10 @@ var rootCmd = &cobra.Command{
if action == "update" {
msg = genUpdateMsg(region, ak, sk, id, key, cert)
}
postUrl = generateUrl(webhook, "", map[string]interface{}{
"action": "update",
"id": id,
})
}
}
@ -120,54 +124,23 @@ var rootCmd = &cobra.Command{
if notice {
// 如果有通知信息,则发送通知
if msg != "" {
var data interface{}
// 如果post为true即发送飞书富文本信息
if post {
// 构造富文本信息
postData := FeishuPostMessage{
MsgType: "post",
Content: PostContent{
Post: &Post{
ZhCn: &Message{
Title: "华为云TLS",
Content: [][]ContentItem{
{
{
Tag: "text",
TextContent: &TextContent{
Text: msg,
},
},
{
Tag: "a",
AContent: &AContent{
Text: "点击续签TLS证书",
Href: postUrl,
},
},
},
},
},
},
data = genPostAMsg("华为云TLS", msg, "点击续签TLS证书", postUrl)
} else {
// 发送text文本信息
// 构造文本信息
data = FeishuTextMessage{
MsgType: "text",
Content: TextContent{
Text: msg,
},
}
// 发送通知富文本信息
if err := postRequest(notify, postData); err != nil {
fmt.Println("Error sending message:", err)
os.Exit(1)
}
}
// 发送text文本信息
// 构造文本信息
textData := FeishuTextMessage{
MsgType: "text",
Content: TextContent{
Text: msg,
},
}
// 发送通知
if err := postRequest(notify, textData); err != nil {
if err := postRequest(notify, data); err != nil {
fmt.Println("Error sending message:", err)
os.Exit(1)
}
@ -198,15 +171,15 @@ func initConfig() {
fmt.Println(err)
os.Exit(1)
}
bin, err := os.Executable()
exec, err := os.Executable()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
binPath := filepath.Dir(bin)
exPath := filepath.Dir(exec)
viper.AddConfigPath(".")
viper.AddConfigPath(home)
viper.AddConfigPath(binPath)
viper.AddConfigPath(exPath)
viper.SetConfigName("config")
}
@ -314,7 +287,7 @@ func timeDiff(expire string) (bool, error) {
duration := now.Sub(t)
// 检查时间差是否小于1天
if duration > -24*time.Hour {
if duration > -7*24*time.Hour {
return true, nil
}
return false, nil
@ -358,7 +331,7 @@ func genStatusMsg(region, ak, sk, id string) (bool, string) {
}
if diff {
message = fmt.Sprintf("域名:%v\n\n证书有效期不足一,请及时更新:", domain)
message = fmt.Sprintf("域名:%v\n\n证书有效期不足一,请及时更新:", domain)
return true, message
}
return false, message
@ -375,3 +348,29 @@ func genUpdateMsg(region, ak, sk, id, key, cert string) string {
message = fmt.Sprintf("域名:%v\nhttps证书更新成功", domain)
return message
}
func genPostAMsg(title, message, text, url string) *FeishuPostMessage {
return &FeishuPostMessage{
MsgType: "post",
Content: PostContent{
Post: Post{
ZhCn: Message{
Title: title,
Content: [][]ContentItem{
{
{
Tag: "text",
Text: message,
},
{
Tag: "a",
Text: text,
Href: url,
},
},
},
},
},
},
}
}