需求:监控接口返回的数据本身是否变化(如 Cloudflare ips-v4 的 IP 段清单),
而不是只看服务是否可用。两者是不同的问题:服务 200 OK 但内容变了,拨测发现不了。
后端
- SiteTask 新增 content_check_mode(off/text/json)、content_paths(JSONPath)、
content_ignore_rules、content_min_length、content_confirm_times、content_sort_lines
- 新增 monitor_site_baselines(基线多版本)与 monitor_site_content_changes(变更记录)
- pkg/siteprobe/content.go:零依赖 JSONPath($.a.b[0]、$.a[*].b)、文本/JSON 规范化、
sha256 哈希、LCS 行级 diff、字段级 diff
- 核心判定区分「采集异常」与「内容变更」:状态码非 2xx、空响应、短于最小长度、
非合法 JSON 均不算变更,避免 CDN 拦截页造成误报
- 连续 N 次(默认 2)不一致才落变更记录,抑制抖动
- 基线首次自动建立(用户选择自动接受),之后变更需人工「接受为新基线」并留审计
- 补齐历史遗留死字段:expected_content(正则断言)与 expected_length 此前
只有表结构、执行器从未校验,现已实现
- 告警引擎新增 content_changed 指标,人工处理后自动恢复
- 迁移 0006 用 AutoMigrate 实现幂等(空库 0000 已建表,静态 ALTER 会重复列)
前端
- 任务表单新增内容监控配置区(模式/JSONPath/忽略规则/最小长度/确认次数/排序)
- 详情页新增内容监控面板:当前基线、待确认变更、并排 diff、版本历史、
接受/忽略操作
验证
- 9 项单元测试(JSONPath、时间戳忽略、顺序无关、采集异常、字段级监控、diff)
- 端到端:Cloudflare ips-v4 建基线 v1 → 模拟变更 → 第 2 轮告警 → 接受为 v2
- JSON 模式实测快照仅含指定字段:{"$.db_driver":"mysql","$.version":"v1.0.2"}
// Logan.Li
333 lines
9.1 KiB
Go
333 lines
9.1 KiB
Go
package siteprobe
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"os/exec"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"ezops/model"
|
|
)
|
|
|
|
// ProbeOutcome 单次拨测结果 // Logan.Li
|
|
type ProbeOutcome struct {
|
|
Status model.SiteResultStatus
|
|
HTTPStatus int
|
|
ResponseTime float64
|
|
TotalTime float64
|
|
RemoteIP string
|
|
ErrorMessage string
|
|
// HTTPS 证书信息 // Logan.Li
|
|
SSLCertExpiry *time.Time
|
|
SSLCertIssuer string
|
|
SSLCertSubject string
|
|
SSLDaysRemaining int
|
|
// Body 响应体(内容监控用,按上限截断;非 HTTP 拨测为空) // Logan.Li
|
|
Body string
|
|
}
|
|
|
|
// Execute 按任务类型执行拨测 // Logan.Li
|
|
func Execute(ctx context.Context, task model.SiteTask) ProbeOutcome {
|
|
timeout := time.Duration(task.Timeout) * time.Second
|
|
if timeout <= 0 {
|
|
timeout = 10 * time.Second
|
|
}
|
|
ctx, cancel := context.WithTimeout(ctx, timeout)
|
|
defer cancel()
|
|
|
|
taskType := strings.ToLower(string(task.TaskType))
|
|
if taskType == "" {
|
|
taskType = inferTaskType(task)
|
|
}
|
|
|
|
switch taskType {
|
|
case "ssl":
|
|
return probeSSL(ctx, task)
|
|
case "tcp":
|
|
return probeTCP(ctx, task.URL)
|
|
case "dns":
|
|
return probeDNS(ctx, task.URL, task.DNSResolver)
|
|
case "ping":
|
|
return probePing(ctx, task.URL, task.Timeout)
|
|
case "http", "https":
|
|
return probeHTTP(ctx, task)
|
|
default:
|
|
return probeHTTP(ctx, task)
|
|
}
|
|
}
|
|
|
|
// InferTaskTypePublic 推断任务类型(供列表修复存量数据)// Logan.Li
|
|
func InferTaskTypePublic(task model.SiteTask) string {
|
|
return inferTaskType(task)
|
|
}
|
|
|
|
func inferTaskType(task model.SiteTask) string {
|
|
name := strings.ToLower(task.Name)
|
|
switch {
|
|
case strings.Contains(name, "tcp"):
|
|
return "tcp"
|
|
case strings.Contains(name, "dns"):
|
|
return "dns"
|
|
case strings.Contains(name, "ping"):
|
|
return "ping"
|
|
case strings.Contains(name, "ssl"):
|
|
return "ssl"
|
|
default:
|
|
return "http"
|
|
}
|
|
}
|
|
|
|
func probeHTTP(ctx context.Context, task model.SiteTask) ProbeOutcome {
|
|
start := time.Now()
|
|
url := task.URL
|
|
if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
|
|
url = "http://" + url
|
|
}
|
|
|
|
transport := &http.Transport{
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: !task.VerifySSL},
|
|
}
|
|
client := &http.Client{
|
|
Timeout: time.Until(deadlineFromCtx(ctx)),
|
|
Transport: transport,
|
|
}
|
|
if !task.FollowRedirect {
|
|
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
|
|
return http.ErrUseLastResponse
|
|
}
|
|
}
|
|
|
|
method := strings.ToUpper(string(task.Method))
|
|
if method == "" {
|
|
method = http.MethodGet
|
|
}
|
|
req, err := http.NewRequestWithContext(ctx, method, url, nil)
|
|
if err != nil {
|
|
return failOutcome(start, err.Error())
|
|
}
|
|
|
|
resp, err := client.Do(req)
|
|
elapsed := msSince(start)
|
|
if err != nil {
|
|
if ctx.Err() == context.DeadlineExceeded {
|
|
return ProbeOutcome{Status: model.SiteResultTimeout, TotalTime: elapsed, ResponseTime: elapsed, ErrorMessage: "请求超时"}
|
|
}
|
|
return failOutcome(start, err.Error())
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
statusOK := resp.StatusCode == task.ExpectedStatus
|
|
if task.ExpectedStatus == 0 {
|
|
statusOK = resp.StatusCode >= 200 && resp.StatusCode < 400
|
|
}
|
|
if !statusOK {
|
|
return ProbeOutcome{
|
|
Status: model.SiteResultFail,
|
|
HTTPStatus: resp.StatusCode,
|
|
ResponseTime: elapsed,
|
|
TotalTime: elapsed,
|
|
ErrorMessage: fmt.Sprintf("状态码 %d 不符合期望 %d", resp.StatusCode, task.ExpectedStatus),
|
|
}
|
|
}
|
|
|
|
// 读取响应体:内容监控与 expected_content/expected_length 都依赖它。
|
|
// 上限 4MB,避免误配大文件接口把内存打满。 // Logan.Li
|
|
const maxBodyBytes = 4 << 20
|
|
raw, readErr := io.ReadAll(io.LimitReader(resp.Body, maxBodyBytes))
|
|
if readErr != nil {
|
|
return ProbeOutcome{
|
|
Status: model.SiteResultFail,
|
|
HTTPStatus: resp.StatusCode,
|
|
ResponseTime: elapsed,
|
|
TotalTime: elapsed,
|
|
ErrorMessage: "读取响应体失败: " + readErr.Error(),
|
|
}
|
|
}
|
|
body := string(raw)
|
|
|
|
// 此前这两个字段只有表结构、执行器从未校验,属于"定义了但没实现"。
|
|
// 内容监控依赖它们做正向断言与错误页拦截,此处一并补齐。 // Logan.Li
|
|
if task.ExpectedLength > 0 && len(raw) != task.ExpectedLength {
|
|
return ProbeOutcome{
|
|
Status: model.SiteResultFail,
|
|
HTTPStatus: resp.StatusCode,
|
|
ResponseTime: elapsed,
|
|
TotalTime: elapsed,
|
|
Body: body,
|
|
ErrorMessage: fmt.Sprintf("响应长度 %d 不符合期望 %d", len(raw), task.ExpectedLength),
|
|
}
|
|
}
|
|
if exp := strings.TrimSpace(task.ExpectedContent); exp != "" {
|
|
re, reErr := regexp.Compile(exp)
|
|
if reErr != nil {
|
|
return ProbeOutcome{
|
|
Status: model.SiteResultFail,
|
|
HTTPStatus: resp.StatusCode,
|
|
ResponseTime: elapsed,
|
|
TotalTime: elapsed,
|
|
Body: body,
|
|
ErrorMessage: "expected_content 正则无效: " + reErr.Error(),
|
|
}
|
|
}
|
|
if !re.MatchString(body) {
|
|
return ProbeOutcome{
|
|
Status: model.SiteResultFail,
|
|
HTTPStatus: resp.StatusCode,
|
|
ResponseTime: elapsed,
|
|
TotalTime: elapsed,
|
|
Body: body,
|
|
ErrorMessage: "响应内容未匹配 expected_content",
|
|
}
|
|
}
|
|
}
|
|
|
|
return ProbeOutcome{
|
|
Status: model.SiteResultSuccess,
|
|
HTTPStatus: resp.StatusCode,
|
|
ResponseTime: elapsed,
|
|
TotalTime: elapsed,
|
|
Body: body,
|
|
}
|
|
}
|
|
|
|
func probeTCP(ctx context.Context, target string) ProbeOutcome {
|
|
start := time.Now()
|
|
host, port, err := splitHostPort(target, "80")
|
|
if err != nil {
|
|
return failOutcome(start, err.Error())
|
|
}
|
|
d := net.Dialer{Timeout: time.Until(deadlineFromCtx(ctx))}
|
|
conn, err := d.DialContext(ctx, "tcp", net.JoinHostPort(host, port))
|
|
elapsed := msSince(start)
|
|
if err != nil {
|
|
if ctx.Err() == context.DeadlineExceeded {
|
|
return ProbeOutcome{Status: model.SiteResultTimeout, TotalTime: elapsed, ResponseTime: elapsed, ErrorMessage: "TCP 连接超时"}
|
|
}
|
|
return failOutcome(start, err.Error())
|
|
}
|
|
defer conn.Close()
|
|
remote := ""
|
|
if addr := conn.RemoteAddr(); addr != nil {
|
|
remote = addr.String()
|
|
}
|
|
return ProbeOutcome{
|
|
Status: model.SiteResultSuccess,
|
|
ResponseTime: elapsed,
|
|
TotalTime: elapsed,
|
|
RemoteIP: remote,
|
|
}
|
|
}
|
|
|
|
func probeDNS(ctx context.Context, target, resolver string) ProbeOutcome {
|
|
start := time.Now()
|
|
domain := normalizeDomain(target)
|
|
if domain == "" {
|
|
return failOutcome(start, "域名不能为空")
|
|
}
|
|
r := &net.Resolver{}
|
|
if resolver != "" {
|
|
r = &net.Resolver{
|
|
PreferGo: true,
|
|
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
|
|
d := net.Dialer{Timeout: time.Until(deadlineFromCtx(ctx))}
|
|
return d.DialContext(ctx, network, resolver+":53")
|
|
},
|
|
}
|
|
}
|
|
addrs, err := r.LookupHost(ctx, domain)
|
|
elapsed := msSince(start)
|
|
if err != nil {
|
|
if ctx.Err() == context.DeadlineExceeded {
|
|
return ProbeOutcome{Status: model.SiteResultTimeout, TotalTime: elapsed, ResponseTime: elapsed, ErrorMessage: "DNS 解析超时"}
|
|
}
|
|
return failOutcome(start, err.Error())
|
|
}
|
|
return ProbeOutcome{
|
|
Status: model.SiteResultSuccess,
|
|
ResponseTime: elapsed,
|
|
TotalTime: elapsed,
|
|
RemoteIP: strings.Join(addrs, ","),
|
|
}
|
|
}
|
|
|
|
func probePing(ctx context.Context, target string, timeoutSec int) ProbeOutcome {
|
|
start := time.Now()
|
|
host := normalizeDomain(target)
|
|
if host == "" {
|
|
return failOutcome(start, "主机不能为空")
|
|
}
|
|
if timeoutSec <= 0 {
|
|
timeoutSec = 5
|
|
}
|
|
cmd := exec.CommandContext(ctx, "ping", "-c", "1", "-W", strconv.Itoa(timeoutSec), host)
|
|
out, err := cmd.CombinedOutput()
|
|
elapsed := msSince(start)
|
|
if err != nil {
|
|
msg := strings.TrimSpace(string(out))
|
|
if msg == "" {
|
|
msg = err.Error()
|
|
}
|
|
if ctx.Err() == context.DeadlineExceeded {
|
|
return ProbeOutcome{Status: model.SiteResultTimeout, TotalTime: elapsed, ResponseTime: elapsed, ErrorMessage: "PING 超时"}
|
|
}
|
|
return ProbeOutcome{Status: model.SiteResultFail, TotalTime: elapsed, ResponseTime: elapsed, ErrorMessage: msg}
|
|
}
|
|
if m := regexp.MustCompile(`time[=<]([\d.]+)\s*ms`).FindStringSubmatch(string(out)); len(m) == 2 {
|
|
if v, parseErr := strconv.ParseFloat(m[1], 64); parseErr == nil {
|
|
elapsed = v
|
|
}
|
|
}
|
|
return ProbeOutcome{Status: model.SiteResultSuccess, ResponseTime: elapsed, TotalTime: elapsed, RemoteIP: host}
|
|
}
|
|
|
|
func splitHostPort(raw, defaultPort string) (string, string, error) {
|
|
raw = strings.TrimSpace(raw)
|
|
for _, prefix := range []string{"ssl://", "tcp://", "http://", "https://"} {
|
|
raw = strings.TrimPrefix(raw, prefix)
|
|
}
|
|
if host, port, err := net.SplitHostPort(raw); err == nil {
|
|
return host, port, nil
|
|
}
|
|
if raw == "" {
|
|
return "", "", fmt.Errorf("TCP 地址不能为空")
|
|
}
|
|
return raw, defaultPort, nil
|
|
}
|
|
|
|
func normalizeDomain(raw string) string {
|
|
raw = strings.TrimSpace(raw)
|
|
raw = strings.TrimPrefix(raw, "http://")
|
|
raw = strings.TrimPrefix(raw, "https://")
|
|
if host, _, err := net.SplitHostPort(raw); err == nil {
|
|
return host
|
|
}
|
|
return strings.TrimSuffix(raw, "/")
|
|
}
|
|
|
|
func failOutcome(start time.Time, msg string) ProbeOutcome {
|
|
elapsed := msSince(start)
|
|
return ProbeOutcome{
|
|
Status: model.SiteResultFail,
|
|
ResponseTime: elapsed,
|
|
TotalTime: elapsed,
|
|
ErrorMessage: msg,
|
|
}
|
|
}
|
|
|
|
func msSince(start time.Time) float64 {
|
|
return float64(time.Since(start).Milliseconds())
|
|
}
|
|
|
|
func deadlineFromCtx(ctx context.Context) time.Time {
|
|
if d, ok := ctx.Deadline(); ok {
|
|
return d
|
|
}
|
|
return time.Now().Add(10 * time.Second)
|
|
} |