Explorar o código

feat(cdn): 添加获取访问日志功能

- 新增 GetAccessLog 方法,用于获取 CDN 访问日志
- 定义相关请求和响应结构体,包括 Request、Data、HTTPAccessLog等
- 实现 GetAccessLog 方法,通过 API 获取并解析访问日志数据
fusu hai 2 semanas
pai
achega
af649c6b95
Modificáronse 3 ficheiros con 230 adicións e 1 borrados
  1. 4 1
      api/v1/cdn.go
  2. 198 0
      api/v1/cdn/log.go
  3. 28 0
      internal/service/api/flexCdn/cdn.go

+ 4 - 1
api/v1/cdn.go

@@ -277,4 +277,7 @@ type AddIpItem struct {
 	SourceURL string `json:"sourceURL" form:"sourceURL"` 	//  可选项,来源URL
 	SourceUserAgent string `json:"sourceUserAgent" form:"sourceUserAgent"` 	// 可选项,来源UA
 	SourceCategory string `json:"sourceCategory" form:"sourceCategory"` // 分类:cc:CC防御
-}
+}
+
+
+

+ 198 - 0
api/v1/cdn/log.go

@@ -0,0 +1,198 @@
+package cdn
+
+type Request struct {
+	Day           string `json:"day" form:"day"`
+	ServerID      int64  `json:"serverId" form:"serverId"`
+	Size          int64  `json:"size" form:"size"`
+	UserID        int64  `json:"userId" form:"userId"`
+	HasError	  *bool   `json:"hasError" form:"hasError"`
+	Ip            string `json:"ip" form:"ip"`
+	Domain 	  	  string `json:"domain" form:"domain"`
+}
+
+
+type Data struct {
+	HasMore        bool            `json:"hasMore"`
+	HTTPAccessLogs []HTTPAccessLog `json:"httpAccessLogs"`
+	RequestID      string          `json:"requestId"`
+}
+type HTTPAccessLog struct {
+	BodyBytesSent            int64      `json:"bodyBytesSent"`
+	BytesSent                int64      `json:"bytesSent"`
+	ContentType              string     `json:"contentType"`
+	Header                   Header     `json:"header"`
+	Host                     string     `json:"host"`
+	Hostname                 string     `json:"hostname"`
+	Msec                     float64    `json:"msec"`
+	Node                     Node       `json:"node"`
+	NodeID                   int64      `json:"nodeId"`
+	OriginAddress            string     `json:"originAddress"`
+	OriginHeaderResponseTime float64    `json:"originHeaderResponseTime"`
+	OriginID                 int64      `json:"originId"`
+	OriginStatus             int64      `json:"originStatus"`
+	Proto                    string     `json:"proto"`
+	RawRemoteAddr            string     `json:"rawRemoteAddr"`
+	Referer                  string     `json:"referer"`
+	RemoteAddr               string     `json:"remoteAddr"`
+	RemotePort               int64      `json:"remotePort"`
+	Request                  string     `json:"request"`
+	RequestID                string     `json:"requestId"`
+	RequestMethod            string     `json:"requestMethod"`
+	RequestPath              string     `json:"requestPath"`
+	RequestTime              float64    `json:"requestTime"`
+	RequestURI               string     `json:"requestURI"`
+	Scheme                   string     `json:"scheme"`
+	SentHeader               SentHeader `json:"sentHeader"`
+	ServerID                 int64      `json:"serverId"`
+	ServerName               string     `json:"serverName"`
+	ServerPort               int64      `json:"serverPort"`
+	ServerProtocol           string     `json:"serverProtocol"`
+	Status                   int64      `json:"status"`
+	Tags                     []string   `json:"tags,omitempty"`
+	TimeISO8601              string     `json:"timeISO8601"`
+	TimeLocal                string     `json:"timeLocal"`
+	Timestamp                int64      `json:"timestamp"`
+	UserAgent                string     `json:"userAgent"`
+}
+
+type Header struct {
+	Accept                  Accept                   `json:"Accept"`
+	AcceptEncoding          AcceptEncoding           `json:"Accept-Encoding"`
+	AcceptLanguage          AcceptLanguage           `json:"Accept-Language"`
+	CacheControl            *HeaderCacheControl      `json:"Cache-Control,omitempty"`
+	Connection              Connection               `json:"Connection"`
+	IfModifiedSince         *IfModifiedSince         `json:"If-Modified-Since,omitempty"`
+	IfNoneMatch             *IfNoneMatch             `json:"If-None-Match,omitempty"`
+	Referer                 Referer                  `json:"Referer"`
+	UpgradeInsecureRequests *UpgradeInsecureRequests `json:"Upgrade-Insecure-Requests,omitempty"`
+	UserAgent               UserAgent                `json:"User-Agent"`
+	XForwardedBy            XForwardedBy             `json:"X-Forwarded-By"`
+	XForwardedFor           XForwardedFor            `json:"X-Forwarded-For"`
+	XForwardedHost          XForwardedHost           `json:"X-Forwarded-Host"`
+	XForwardedProto         XForwardedProto          `json:"X-Forwarded-Proto"`
+	XRealIP                 XRealIP                  `json:"X-Real-IP"`
+}
+
+type Accept struct {
+	Values []string `json:"values"`
+}
+
+type AcceptEncoding struct {
+	Values []string `json:"values"`
+}
+
+type AcceptLanguage struct {
+	Values []string `json:"values"`
+}
+
+type HeaderCacheControl struct {
+	Values []string `json:"values"`
+}
+
+type Connection struct {
+	Values []string `json:"values"`
+}
+
+type IfModifiedSince struct {
+	Values []string `json:"values"`
+}
+
+type IfNoneMatch struct {
+	Values []string `json:"values"`
+}
+
+type Referer struct {
+	Values []string `json:"values"`
+}
+
+type UpgradeInsecureRequests struct {
+	Values []string `json:"values"`
+}
+
+type UserAgent struct {
+	Values []string `json:"values"`
+}
+
+type XForwardedBy struct {
+	Values []string `json:"values"`
+}
+
+type XForwardedFor struct {
+	Values []string `json:"values"`
+}
+
+type XForwardedHost struct {
+	Values []string `json:"values"`
+}
+
+type XForwardedProto struct {
+	Values []string `json:"values"`
+}
+
+type XRealIP struct {
+	Values []string `json:"values"`
+}
+
+type Node struct {
+	ID          int64       `json:"id"`
+	Name        string      `json:"name"`
+	NodeCluster NodeCluster `json:"nodeCluster"`
+}
+
+type NodeCluster struct {
+	ID   int64  `json:"id"`
+	Name string `json:"name"`
+}
+
+type SentHeader struct {
+	AcceptRanges  AcceptRanges           `json:"Accept-Ranges"`
+	CacheControl  SentHeaderCacheControl `json:"Cache-Control"`
+	ContentLength ContentLength          `json:"Content-Length"`
+	ContentType   ContentType            `json:"Content-Type"`
+	Date          Date                   `json:"Date"`
+	Etag          Etag                   `json:"Etag"`
+	Expires       Expires                `json:"Expires"`
+	LastModified  LastModified           `json:"Last-Modified"`
+	Location      *Location              `json:"Location,omitempty"`
+	Server        Server                 `json:"Server"`
+}
+
+type AcceptRanges struct {
+	Values []string `json:"values"`
+}
+
+type SentHeaderCacheControl struct {
+	Values []string `json:"values"`
+}
+
+type ContentLength struct {
+	Values []string `json:"values"`
+}
+
+type ContentType struct {
+	Values []string `json:"values"`
+}
+
+type Date struct {
+	Values []string `json:"values"`
+}
+
+type Etag struct {
+	Values []string `json:"values"`
+}
+
+type Expires struct {
+	Values []string `json:"values"`
+}
+
+type LastModified struct {
+	Values []string `json:"values"`
+}
+
+type Location struct {
+	Values []string `json:"values"`
+}
+
+type Server struct {
+	Values []string `json:"values"`
+}

+ 28 - 0
internal/service/api/flexCdn/cdn.go

@@ -5,6 +5,7 @@ import (
 	"encoding/json"
 	"fmt"
 	v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
+	"github.com/go-nunu/nunu-layout-advanced/api/v1/cdn"
 	"github.com/go-nunu/nunu-layout-advanced/internal/repository/api/flexCdn"
 	"github.com/go-nunu/nunu-layout-advanced/internal/service"
 	"github.com/spf13/viper"
@@ -72,6 +73,8 @@ type CdnService interface {
 	AddIpItem(ctx context.Context, req v1.AddIpItem) error
 	// 修改IP
 	EditIpItem(ctx context.Context, req v1.AddIpItem) error
+	// 获取访问日志
+	GetAccessLog(ctx context.Context, req cdn.Request) (cdn.Data, error)
 }
 
 func NewCdnService(
@@ -1178,4 +1181,29 @@ func (s *cdnService) EditIpItem(ctx context.Context, req v1.AddIpItem) error {
 		return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
 	}
 	return nil
+}
+
+// 获取访问日志
+func (s *cdnService) GetAccessLog(ctx context.Context, req cdn.Request) (cdn.Data, error) {
+	formData := map[string]interface{}{
+		"serverId": req.ServerID,
+		"size":     50,
+		"day" :     req.Day,
+		"ip":       req.Ip,
+		"domain":   req.Domain,
+		"hasError": &req.HasError,
+	}
+	apiUrl := s.Url + "HTTPAccessLogService/listHTTPAccessLogs"
+	resBody, err := s.sendDataWithTokenRetry(ctx, formData, apiUrl)
+	if err != nil {
+		return cdn.Data{}, err
+	}
+	var res v1.GeneralResponse[cdn.Data]
+	if err := json.Unmarshal(resBody, &res); err != nil {
+		return cdn.Data{}, fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
+	}
+	if res.Code != 200 {
+		return cdn.Data{}, fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
+	}
+	return res.Data, nil
 }