Initial commit

This commit is contained in:
2026-05-20 16:13:58 +08:00
commit ceffbe382e
500 changed files with 157469 additions and 0 deletions
+115
View File
@@ -0,0 +1,115 @@
package domainmod
import (
"context"
"fmt"
"stat/common/log"
"stat/common/mongos"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// 初始化model
func Init() {
// 获取数据库表
db = mongos.New(mongos.MgoManager, CollDomainCheck)
indexes := []mongo.IndexModel{
{
Keys: bson.D{
{Key: "name", Value: 1},
},
Options: options.Index().SetUnique(true),
},
{
Keys: bson.D{
{Key: "category", Value: 1},
},
},
{
Keys: bson.D{
{Key: "Domainus", Value: 1},
},
},
{
Keys: bson.D{
{Key: "checkTime", Value: 1},
},
},
{
Keys: bson.D{
{Key: "createdAt", Value: 1},
},
},
}
if _, err := db.CreateIndex(context.Background(), indexes); err != nil {
panic(fmt.Sprintf("Model %s InitIndex error %+v", CollDomainCheck, err))
}
}
// CountDomains 统计数量
func CountDomains(ctx context.Context, filter bson.M) (int64, error) {
count, err := db.Count(ctx, filter)
if err != nil {
log.Warn("downloadDomain CountDomains db.Count failed",
log.E(err), log.Any("filter", filter))
return 0, err
}
return count, nil
}
// InsertOneDomain 插入一个
func InsertOneDomain(ctx context.Context, media *Domain) error {
if _, err := db.InsertOne(ctx, media); err != nil {
log.Warn("downloadDomain:InsertOneDomain db.InsertOne failed", log.E(err))
return err
}
return nil
}
// FindOneDomain 查找一个
func FindOneDomain(ctx context.Context, filter bson.M) (*Domain, error) {
var Domain Domain
if err := db.FindOneEx(ctx, &Domain, filter); err != nil {
log.Warn("downloadDomain:FindOneDomainEx db.FindOneEx failed",
log.E(err), log.Any("filter", filter))
return nil, err
}
return &Domain, nil
}
// DelOneDomain 删除一个
func DelOneDomain(ctx context.Context, filter bson.M) (int64, error) {
count, err := db.DeleteOne(ctx, filter)
if err != nil {
log.Warn("downloadDomain:DelOneDomain db.DeleteOne failed",
log.E(err), log.Any("filter", filter))
return 0, err
}
return count, nil
}
// UpdateOneDomain 更新一个
func UpdateOneDomain(ctx context.Context, filter bson.M, update bson.M) error {
_, err := db.UpdateOne(ctx, filter, update)
if err != nil {
log.Warn("downloadDomain:UpdateOneDomain db.UpdateOne failed",
log.E(err), log.Any("filter", filter))
return err
}
return nil
}
// FindManyDomain 查找一批
func FindManyDomain(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (DomainList, error) {
data := make(DomainList, 0)
if err := db.Find(ctx, &data, filter, opts...); err != nil {
log.Warn("downloadDomain:FindManyDomain db.Find failed", log.E(err), log.Any("filter", filter))
return nil, err
}
return data, nil
}
+164
View File
@@ -0,0 +1,164 @@
package domainmod
import (
"stat/common/mongos"
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
)
// 表名
const CollDomainCheck = "domain"
var db *mongos.MongoTool
type Category int32
const (
CategoryNone Category = 0 // 无分类
CategoryAPI Category = 1 // API接口
CategoryWeb Category = 2 // 网站
CategoryLand Category = 3 // 落地页
CategoryH5 Category = 4 // H5页面
CategoryDown Category = 5 // 文件下载
CategoryImage Category = 6 // 图片下载
CategoryVideo Category = 7 // 视频播放
CategoryJump Category = 8 // 跳转域名
CategoryLight Category = 9 // 轻量版域名
)
type CheckType int32
const (
CheckTypeNone CheckType = 0 // 占位符,无意义
CheckTypeWeb CheckType = 1 // 网站检测
CheckTypeDNS CheckType = 2 // DNS检测
CheckTypePing CheckType = 3 // Ping检测
CheckTypeHijack CheckType = 4 // 劫持检测
CheckTypePollute CheckType = 5 // 污染检测
CheckTypeQQ CheckType = 6 // QQ拦截检测
CheckTypeWeChat CheckType = 7 // 微信拦截检测
CheckTypeBlack CheckType = 8 // 域名黑名单检测
CheckTypeGFW CheckType = 9 // 被墙检测
)
func (c CheckType) Name() string {
switch c {
case CheckTypeNone:
return "无检测"
case CheckTypeWeb:
return "网站检测"
case CheckTypeDNS:
return "DNS检测"
case CheckTypePing:
return "PING检测"
case CheckTypeHijack:
return "劫持检测"
case CheckTypePollute:
return "污染检测"
case CheckTypeQQ:
return "QQ拦截"
case CheckTypeWeChat:
return "微信拦截"
case CheckTypeBlack:
return "域名黑名单"
case CheckTypeGFW:
return "被墙检测"
default:
return "未知检测"
}
}
func GetCheckTypes() []CheckType {
return []CheckType{CheckTypeWeb, CheckTypeDNS, CheckTypePing, CheckTypeHijack, CheckTypePollute, CheckTypeQQ, CheckTypeWeChat, CheckTypeBlack, CheckTypeGFW}
}
type CheckStatus int32
const (
CheckStatusNone CheckStatus = 0 // 超时跳过
CheckStatusChecking CheckStatus = 1 // 正在检测
CheckStatusSucc CheckStatus = 2 // 完全通过
CheckStatusFailed CheckStatus = 3 // 没有通过
CheckStatusPartPass CheckStatus = 4 // 部分通过
)
func (v CheckStatus) String() string {
switch v {
case CheckStatusNone:
return "超时跳过"
case CheckStatusChecking:
return "正在检测"
case CheckStatusSucc:
return "完全通过"
case CheckStatusFailed:
return "没有通过"
case CheckStatusPartPass:
return "部分通过"
default:
return "未知状态"
}
}
type CheckResult struct {
Domain string `json:"domain" bson:"domain"`
CheckType CheckType `json:"checkType" bson:"checkType"`
NodeCount int `json:"nodeCount" bson:"nodeCount"`
SuccCount int `json:"succCount" bson:"succCount"`
FailedCount int `json:"failedCount" bson:"failedCount"`
NodeErrors []string `json:"nodeErrors" bson:"nodeErrors"`
SuccRate float64 `json:"succRate" bson:"succRate"`
MinRate float64 `json:"minRate" bson:"minRate"`
BeginTime time.Time `json:"beginTime" bson:"beginTime"`
EndTime time.Time `json:"endTime" bson:"endTime"`
CheckSucc bool `json:"checkSucc" bson:"checkSucc"` // 只有检测成功时,检测结果才有意义
ErrorMessage string `json:"errorMessage" bson:"errorMessage"`
Error error `json:"-" bson:"-"`
}
type CheckResultList []*CheckResult
type Domain struct {
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"`
Name string `json:"name" bson:"name" binding:"required"` // 域名
AppId int32 `json:"appId" bson:"appId"` // AppId
AppName string `json:"appName" bson:"-"` // AppName
Comment string `json:"comment" bson:"comment"` // 备注
Category Category `json:"category" bson:"category"` // 分类
Status CheckStatus `json:"status" bson:"status"` // 状态
Checks []CheckType `json:"checks" bson:"checks"` // 检测方式
Redirected bool `json:"redirected" bson:"redirected"` // 是否已做301跳转
RedirectDomain string `json:"redirectDomain" bson:"redirectDomain"` // 重定向域名
MaxNodes int32 `json:"maxNodes" bson:"maxNodes"` // 最大测试节点数
MinRate float64 `json:"minRate" bson:"minRate"` // 最低检测成功率(成功率低于这个值视为失败)
Report string `json:"report" bson:"report"` // 检测报告
Results CheckResultList `json:"results" bson:"results"` // 检测结果
Enable bool `json:"enable" bson:"enable"` // 是否启用
CheckTime time.Time `json:"checkTime" bson:"checkTime"` // 检测时间
CycleHour int64 `json:"cycleHour" bson:"cycleHour"` // 间隔多少小时检测一次
CheckFinish time.Time `json:"checkFinish" bson:"checkFinish"` // 检测完成时间
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"`
CreatedAt time.Time `json:"createdAt" bson:"createdAt"`
}
type DomainList []*Domain
type DomainDoc struct {
Name *string `json:"name" bson:"name,omitempty"` // 域名
AppId *int32 `json:"appId" bson:"appId,omitempty"` // AppId
Comment *string `json:"comment" bson:"comment,omitempty"` // 备注
Category *Category `json:"category" bson:"category,omitempty"` // 分类
Status *CheckStatus `json:"status" bson:"status,omitempty"` // 状态
Checks *[]CheckType `json:"checks" bson:"checks,omitempty"` // 检测方式
Redirected *bool `json:"redirected" bson:"redirected,omitempty"` // 是否已做301跳转
RedirectDomain *string `json:"redirectDomain" bson:"redirectDomain,omitempty"` // 重定向域名
MinRate *float64 `json:"minRate" bson:"minRate,omitempty"` // 最低检测成功率(成功率低于这个值视为失败)
Report *string `json:"report" bson:"report,omitempty"` // 检测报告
Results *CheckResultList `json:"results" bson:"results,omitempty"` // 检测结果
Enable *bool `json:"enable" bson:"enable,omitempty"` // 是否启用
CycleHour *int64 `json:"cycleHour" bson:"cycleHour,omitempty"` // 间隔多少小时检测一次
CheckTime *time.Time `json:"checkTime" bson:"checkTime,omitempty"` // 检测时间
CheckFinish *time.Time `json:"checkFinish" bson:"checkFinish,omitempty"` // 检测完成时间
UpdatedAt *time.Time `json:"updatedAt" bson:"updatedAt,omitempty"` // 更新时间
}