Files
AllinSSL/backend/scheduler/site_monitor.go
zhangchenhao f64d2b2764 1.新增eab列表
2.申请证书新增http代理、新增ca选择(zerossl、google)、新增证书算法选择
3.修复数据库连接内存泄漏
2025-05-21 11:31:36 +08:00

91 lines
2.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package scheduler
import (
"ALLinSSL/backend/internal/report"
"ALLinSSL/backend/internal/siteMonitor"
"fmt"
"os"
"path/filepath"
"strconv"
"sync"
"time"
)
func SiteMonitor() {
s, err := siteMonitor.GetSqlite()
if err != nil {
fmt.Println(err)
}
defer s.Close()
data, err := s.Select()
if err != nil {
fmt.Println(err)
}
now := time.Now()
loc := now.Location()
var wg sync.WaitGroup
for _, v := range data {
if v["active"].(int64) == 1 {
lastTimeStr := v["last_time"].(string)
lastTime, err := time.ParseInLocation("2006-01-02 15:04:05", lastTimeStr, loc)
if err != nil {
// fmt.Println(err)
continue
}
if now.Sub(lastTime).Minutes() >= float64(v["cycle"].(int64)) {
wg.Add(1)
go func() {
defer wg.Done()
Err := siteMonitor.UpdInfo(fmt.Sprintf("%d", v["id"].(int64)), v["site_domain"].(string), s, v["report_type"].(string))
path := fmt.Sprintf("data/site_monitor/%d", v["id"].(int64))
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return
}
errCount := 0
file, err := os.ReadFile(path)
if err != nil {
errCount = 0
}
errCount, err = strconv.Atoi(string(file))
if err != nil {
errCount = 0
}
// 此处应该发送错误邮件
if Err != nil {
errCount += 1
os.WriteFile(path, []byte(strconv.Itoa(errCount)), os.ModePerm)
repeatSendGap, ok := v["repeat_send_gap"].(int64)
if !ok {
repeatSendGap = 10
}
reportType, ok := v["report_type"].(string)
if ok && errCount >= int(repeatSendGap) {
s.TableName = "report"
rdata, err := s.Where("type=?", []interface{}{reportType}).Select()
if err != nil {
return
}
if len(rdata) <= 0 {
return
}
_ = report.Notify(map[string]any{
"provider": reportType,
"provider_id": strconv.FormatInt(rdata[0]["id"].(int64), 10),
"body": fmt.Sprintf("检测到域名为%s的网站出现异常请保持关注\n检测时间%s", v["site_domain"].(string), now.Format("2006-01-02 15:04:05")),
"subject": "ALLinSSL网站监控通知",
})
os.Remove(path)
}
} else {
os.Remove(path)
}
}()
}
}
}
wg.Wait()
}