|
@@ -18,7 +18,6 @@ type WebForwardingRepository interface {
|
|
|
AddWebForwarding(ctx context.Context, req *model.WebForwarding) (int, error)
|
|
|
EditWebForwarding(ctx context.Context, req *model.WebForwarding) error
|
|
|
DeleteWebForwarding(ctx context.Context, id int64) error
|
|
|
- GetWebForwardingWafWebIdById(ctx context.Context, id int) (int, error)
|
|
|
GetWebForwardingPortCountByHostId(ctx context.Context, hostId int) (int64, error)
|
|
|
GetWebForwardingDomainCountByHostId(ctx context.Context, hostId int) (int64, []string, error)
|
|
|
GetWebForwardingWafWebAllIds(ctx context.Context, hostId int) ([]int, error)
|
|
@@ -27,7 +26,7 @@ type WebForwardingRepository interface {
|
|
|
GetWebForwardingIpsByID(ctx context.Context, webId int) (*model.WebForwardingRule, error)
|
|
|
DeleteWebForwardingIpsById(ctx context.Context, webId int) error
|
|
|
// 获取域名数量
|
|
|
- GetDomainCount(ctx context.Context, hostId int,domain string) (int, error)
|
|
|
+ GetDomainCount(ctx context.Context, hostId int, domain string) (int, error)
|
|
|
// 获取IP数量等于1的IP
|
|
|
GetIpCountByIp(ctx context.Context, ips []string) ([]v1.IpCountResult, error)
|
|
|
}
|
|
@@ -92,15 +91,6 @@ func (r *webForwardingRepository) DeleteWebForwarding(ctx context.Context, id in
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func (r *webForwardingRepository) GetWebForwardingWafWebIdById(ctx context.Context, id int) (int, error) {
|
|
|
- var WafWebId int
|
|
|
-
|
|
|
- if err := r.db.Model(&model.WebForwarding{}).WithContext(ctx).Where("id = ?", id).Select("waf_web_id").Find(&WafWebId).Error; err != nil {
|
|
|
- return 0, err
|
|
|
- }
|
|
|
- return WafWebId, nil
|
|
|
-}
|
|
|
-
|
|
|
func (r *webForwardingRepository) GetWebForwardingPortCountByHostId(ctx context.Context, hostId int) (int64, error) {
|
|
|
var count int64
|
|
|
if err := r.db.Model(&model.WebForwarding{}).WithContext(ctx).Where("host_id = ?", hostId).Count(&count).Error; err != nil {
|
|
@@ -112,7 +102,7 @@ func (r *webForwardingRepository) GetWebForwardingPortCountByHostId(ctx context.
|
|
|
func (r *webForwardingRepository) GetWebForwardingDomainCountByHostId(ctx context.Context, hostId int) (int64, []string, error) {
|
|
|
var distinctDomains []string
|
|
|
err := r.db.Model(&model.WebForwarding{}).WithContext(ctx).
|
|
|
- Distinct(). // 确保我们只获取唯一的 domain 值
|
|
|
+ Distinct(). // 确保我们只获取唯一的 domain 值
|
|
|
Where("host_id = ? AND domain IS NOT NULL AND domain != ''", hostId). // 额外添加 domain != '' 以排除空字符串
|
|
|
Pluck("domain", &distinctDomains).Error
|
|
|
|
|
@@ -133,7 +123,6 @@ func (r *webForwardingRepository) GetWebForwardingWafWebAllIds(ctx context.Conte
|
|
|
return ids, nil
|
|
|
}
|
|
|
|
|
|
-
|
|
|
// mongodb 插入
|
|
|
func (r *webForwardingRepository) AddWebForwardingIps(ctx context.Context, req model.WebForwardingRule) (primitive.ObjectID, error) {
|
|
|
collection := r.mongoDB.Collection("web_forwarding_rules")
|
|
@@ -163,12 +152,10 @@ func (r *webForwardingRepository) EditWebForwardingIps(ctx context.Context, req
|
|
|
updateData["web_id"] = req.WebId
|
|
|
}
|
|
|
|
|
|
-
|
|
|
if len(req.BackendList) > 0 {
|
|
|
updateData["backend_list"] = req.BackendList
|
|
|
}
|
|
|
|
|
|
-
|
|
|
updateData["cdn_origin_ids"] = req.CdnOriginIds
|
|
|
|
|
|
// 始终更新更新时间
|
|
@@ -181,7 +168,7 @@ func (r *webForwardingRepository) EditWebForwardingIps(ctx context.Context, req
|
|
|
|
|
|
// 执行更新
|
|
|
update := bson.M{"$set": updateData}
|
|
|
- err := collection.UpdateOne(ctx, bson.M{"web_id": req.WebId}, update)
|
|
|
+ err := collection.UpdateOne(ctx, bson.M{"web_id": req.WebId}, update)
|
|
|
if err != nil {
|
|
|
return fmt.Errorf("更新MongoDB文档失败: %w", err)
|
|
|
}
|
|
@@ -217,7 +204,7 @@ func (r *webForwardingRepository) DeleteWebForwardingIpsById(ctx context.Context
|
|
|
|
|
|
collection := r.mongoDB.Collection("web_forwarding_rules")
|
|
|
|
|
|
- err := collection.Remove(ctx, bson.M{"web_id": webId})
|
|
|
+ err := collection.Remove(ctx, bson.M{"web_id": webId})
|
|
|
|
|
|
if err != nil {
|
|
|
if errors.Is(err, mongo.ErrNoDocuments) {
|
|
@@ -229,9 +216,9 @@ func (r *webForwardingRepository) DeleteWebForwardingIpsById(ctx context.Context
|
|
|
}
|
|
|
|
|
|
// 获取域名数量
|
|
|
-func (r *webForwardingRepository) GetDomainCount(ctx context.Context, hostId int,domain string) (int, error) {
|
|
|
+func (r *webForwardingRepository) GetDomainCount(ctx context.Context, hostId int, domain string) (int, error) {
|
|
|
var count int64
|
|
|
- if err := r.db.Model(&model.WebForwarding{}).WithContext(ctx).Where("host_id = ? AND domain = ?", hostId,domain).Count(&count).Error; err != nil {
|
|
|
+ if err := r.db.Model(&model.WebForwarding{}).WithContext(ctx).Where("host_id = ? AND domain = ?", hostId, domain).Count(&count).Error; err != nil {
|
|
|
return 0, err
|
|
|
}
|
|
|
return int(count), nil
|
|
@@ -256,9 +243,9 @@ func (r *webForwardingRepository) GetIpCountByIp(ctx context.Context, ips []stri
|
|
|
},
|
|
|
{
|
|
|
"$project": bson.M{
|
|
|
- "_id": 0, // 不输出默认的_id
|
|
|
- "ip": "$_id", // 将分组的_id字段重命名为ip
|
|
|
- "count": 1, // 保留count字段
|
|
|
+ "_id": 0, // 不输出默认的_id
|
|
|
+ "ip": "$_id", // 将分组的_id字段重命名为ip
|
|
|
+ "count": 1, // 保留count字段
|
|
|
},
|
|
|
},
|
|
|
}
|