Jelajahi Sumber

feat(cdn): 添加修改网站日志配置功能

- 新增 WebLog 结构体用于网站日志配置
- 在 CdnService接口中添加 EditWebLog 方法- 实现 EditWebLog 方法,用于修改网站日志配置
- 在 WebForwardingService 中添加 EditLog 方法,用于开启访问日志
- 在 GlobalLimitRepository 中移除多余的代码
- 在 WebForwardingRepository 接口中添加 GetWebConfigId 方法
- 实现 GetWebConfigId 方法,用于获取 CDN 的 web 配置 id
fusu 1 bulan lalu
induk
melakukan
1bc9f6c17a

+ 13 - 0
api/v1/cdn.go

@@ -157,3 +157,16 @@ type SslCertsJSON struct {
 	IsOn   bool  `json:"isOn" form:"isOn"`
 	CertId int64 `json:"certId" form:"certId"`
 }
+
+type WebLog struct {
+	IsPrior bool `json:"isPrior" form:"isPrior"`
+	IsOn    bool `json:"isOn" form:"isOn"`
+	Fields  []int64
+	Status1 bool `json:"status1" form:"status1"`
+	Status2 bool `json:"status2" form:"status2"`
+	Status3 bool `json:"status3" form:"status3"`
+	Status4 bool `json:"status4" form:"status4"`
+	Status5 bool `json:"status5" form:"status5"`
+	EnableClientClosed bool `json:"enableClientClosed" form:"enableClientClosed"`
+	FirewallOnly       bool `json:"firewallOnly" form:"firewallOnly"`
+}

+ 0 - 1
internal/repository/globallimit.go

@@ -156,4 +156,3 @@ func (r *globalLimitRepository) GetNodeId(ctx context.Context, cndWebId int) (in
 	}
 	return nodeId, nil
 }
-

+ 11 - 0
internal/repository/webforwarding.go

@@ -32,6 +32,8 @@ type WebForwardingRepository interface {
 	// 获取IP数量等于1的IP
 	GetIpCountByIp(ctx context.Context, ips []string) ([]v1.IpCountResult, error)
 	GetSslCertId (ctx context.Context, sslPocyID int) ([]v1.SslCertsJSON, error)
+	// 获取CDN的web配置的id
+	GetWebConfigId(ctx context.Context, id int64) (int64, error)
 }
 
 func NewWebForwardingRepository(
@@ -301,4 +303,13 @@ func (r *webForwardingRepository) GetSslCertId (ctx context.Context, sslPolicyID
 	}
 
 	return res, nil
+}
+
+// 获取CDN的web配置的id
+func (r *webForwardingRepository) GetWebConfigId(ctx context.Context, id int64) (int64, error) {
+	var webConfigId int64
+	if err := r.DBWithName(ctx,"cdn").Table("cloud_servers").Where("id = ?", id).Select("webId").Scan(&webConfigId).Error; err != nil {
+		return 0, err
+	}
+	return webConfigId, nil
 }

+ 27 - 0
internal/service/cdn.go

@@ -42,6 +42,8 @@ type CdnService interface {
 	EditSSLPolicy(ctx context.Context, req v1.SSLPolicy) error
 	// 修改反向代理
 	EditProxy(ctx context.Context, req v1.Proxy) error
+	// 修改日志
+	EditWebLog(ctx context.Context,webId int64, req v1.WebLog) error
 }
 
 func NewCdnService(
@@ -795,4 +797,29 @@ func (s *cdnService) EditProxy(ctx context.Context, req v1.Proxy) error {
 		return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
 	}
 	return nil
+}
+
+// 修改网站日志配置
+func (s *cdnService) EditWebLog(ctx context.Context,webId int64, req v1.WebLog) error {
+	reqJson, err := json.Marshal(req)
+	if err != nil {
+		return err
+	}
+	formData := map[string]interface{}{
+		"httpWebId": webId,
+		"accessLogJSON":   reqJson,
+	}
+	apiUrl := s.Url + "HTTPWebService/updateHTTPWebAccessLog"
+	resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
+	if err != nil {
+		return err
+	}
+	var res v1.GeneralResponse[any]
+	if err := json.Unmarshal(resBody, &res); err != nil {
+		return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
+	}
+	if res.Code != 200 {
+		return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
+	}
+	return nil
 }

+ 32 - 0
internal/service/webforwarding.go

@@ -417,6 +417,19 @@ func (s *webForwardingService) AddWebForwarding(ctx context.Context, req *v1.Web
 		}
 	}
 
+	// 开启访问日志
+	if webId != 0 {
+		webConfigId, err := s.webForwardingRepository.GetWebConfigId(ctx, webId)
+		if err != nil {
+			return err
+		}
+		err = s.EditLog(ctx, webConfigId)
+		if err != nil {
+			return err
+		}
+
+	}
+
 	webModel := s.buildWebForwardingModel(&req.WebForwardingData, int(webId), require)
 
 	id, err := s.webForwardingRepository.AddWebForwarding(ctx, webModel)
@@ -925,3 +938,22 @@ func (s *webForwardingService) WashDifferentIp(newIpList []string, oldIpList []s
 	addedDenyIps, removedDenyIps = s.wafformatter.findIpDifferences(oldAllowIps, newAllowIps)
 	return addedDenyIps, removedDenyIps
 }
+
+func (s *webForwardingService) EditLog(ctx context.Context,webConfigId int64) error {
+
+	if err := s.cdn.EditWebLog(ctx, webConfigId,v1.WebLog{
+		IsPrior: false,
+		IsOn:    true,
+		Fields:  []int64{1, 2, 6, 7},
+		Status1: true,
+		Status2: true,
+		Status3: true,
+		Status4: true,
+		Status5: true,
+		FirewallOnly: false,
+		EnableClientClosed: false,
+	}); err != nil {
+		return err
+	}
+	return nil
+}