Files
EdgeNode/internal/nodes/http_request_log.go

190 lines
5.6 KiB
Go
Raw Normal View History

2020-10-10 11:47:53 +08:00
package nodes
import (
"strings"
"time"
2024-07-27 15:42:50 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
2020-10-10 11:47:53 +08:00
)
2021-12-07 15:12:15 +08:00
const (
// AccessLogMaxRequestBodySize 访问日志存储的请求内容最大尺寸 TODO 此值应该可以在访问日志页设置
2024-04-15 19:37:38 +08:00
AccessLogMaxRequestBodySize = 2 << 20
2021-12-07 15:12:15 +08:00
)
2020-10-10 11:47:53 +08:00
// 日志
func (this *HTTPRequest) log() {
// 检查全局配置
if this.nodeConfig != nil && this.nodeConfig.GlobalServerConfig != nil && !this.nodeConfig.GlobalServerConfig.HTTPAccessLog.IsOn {
return
}
2022-04-21 09:40:05 +08:00
var ref *serverconfigs.HTTPAccessLogRef
if !this.forceLog {
if this.disableLog {
return
}
2022-04-21 09:40:05 +08:00
// 计算请求时间
this.requestCost = time.Since(this.requestFromTime).Seconds()
2020-10-10 11:47:53 +08:00
2022-04-21 09:40:05 +08:00
ref = this.web.AccessLogRef
if ref == nil {
ref = serverconfigs.DefaultHTTPAccessLogRef
}
if !ref.IsOn {
return
}
2020-10-10 11:47:53 +08:00
2022-04-21 09:40:05 +08:00
if !ref.Match(this.writer.StatusCode()) {
return
}
2020-10-10 11:47:53 +08:00
2022-04-21 09:40:05 +08:00
if ref.FirewallOnly && this.firewallPolicyId == 0 {
return
}
2022-04-21 09:40:05 +08:00
// 是否记录499
if !ref.EnableClientClosed && this.writer.StatusCode() == 499 {
return
}
2021-12-01 21:13:10 +08:00
}
2022-04-21 09:40:05 +08:00
var addr = this.RawReq.RemoteAddr
var index = strings.LastIndex(addr, ":")
2020-10-10 11:47:53 +08:00
if index > 0 {
addr = addr[:index]
}
var serverGlobalConfig = this.nodeConfig.GlobalServerConfig
2020-10-10 11:47:53 +08:00
// 请求Cookie
2022-04-21 09:40:05 +08:00
var cookies = map[string]string{}
var enableCookies = false
if serverGlobalConfig == nil || serverGlobalConfig.HTTPAccessLog.EnableCookies {
enableCookies = true
if ref == nil || ref.ContainsField(serverconfigs.HTTPAccessLogFieldCookie) {
for _, cookie := range this.RawReq.Cookies() {
cookies[cookie.Name] = cookie.Value
}
2020-10-10 11:47:53 +08:00
}
}
// 请求Header
2022-04-21 09:40:05 +08:00
var pbReqHeader = map[string]*pb.Strings{}
if serverGlobalConfig == nil || serverGlobalConfig.HTTPAccessLog.EnableRequestHeaders {
if ref == nil || ref.ContainsField(serverconfigs.HTTPAccessLogFieldHeader) {
// 是否只记录通用Header
var commonHeadersOnly = serverGlobalConfig != nil && serverGlobalConfig.HTTPAccessLog.CommonRequestHeadersOnly
for k, v := range this.RawReq.Header {
if commonHeadersOnly && !serverconfigs.IsCommonRequestHeader(k) {
continue
}
if !enableCookies && k == "Cookie" {
continue
}
pbReqHeader[k] = &pb.Strings{Values: v}
}
2020-10-10 11:47:53 +08:00
}
}
// 响应Header
2022-04-21 09:40:05 +08:00
var pbResHeader = map[string]*pb.Strings{}
if serverGlobalConfig == nil || serverGlobalConfig.HTTPAccessLog.EnableResponseHeaders {
if ref == nil || ref.ContainsField(serverconfigs.HTTPAccessLogFieldSentHeader) {
for k, v := range this.writer.Header() {
pbResHeader[k] = &pb.Strings{Values: v}
}
2020-10-10 11:47:53 +08:00
}
}
// 参数列表
2022-04-21 09:40:05 +08:00
var queryString = ""
if ref == nil || ref.ContainsField(serverconfigs.HTTPAccessLogFieldArg) {
2020-10-10 11:47:53 +08:00
queryString = this.requestQueryString()
}
// 浏览器
2022-04-21 09:40:05 +08:00
var userAgent = ""
if ref == nil || ref.ContainsField(serverconfigs.HTTPAccessLogFieldUserAgent) || ref.ContainsField(serverconfigs.HTTPAccessLogFieldExtend) {
2020-10-10 11:47:53 +08:00
userAgent = this.RawReq.UserAgent()
}
// 请求来源
2022-04-21 09:40:05 +08:00
var referer = ""
if ref == nil || ref.ContainsField(serverconfigs.HTTPAccessLogFieldReferer) {
2020-10-10 11:47:53 +08:00
referer = this.RawReq.Referer()
}
2022-04-21 09:40:05 +08:00
var accessLog = &pb.HTTPAccessLog{
2021-12-02 11:30:47 +08:00
RequestId: this.requestId,
2022-04-04 12:06:53 +08:00
NodeId: this.nodeConfig.Id,
2022-01-01 20:15:39 +08:00
ServerId: this.ReqServer.Id,
RemoteAddr: this.requestRemoteAddr(true),
2020-10-10 11:47:53 +08:00
RawRemoteAddr: addr,
RemotePort: int32(this.requestRemotePort()),
RemoteUser: this.requestRemoteUser(),
RequestURI: this.rawURI,
RequestPath: this.Path(),
2020-10-10 11:47:53 +08:00
RequestLength: this.requestLength(),
RequestTime: this.requestCost,
RequestMethod: this.RawReq.Method,
RequestFilename: this.requestFilename(),
Scheme: this.requestScheme(),
Proto: this.RawReq.Proto,
BytesSent: this.writer.SentBodyBytes(), // TODO 加上Header Size
BodyBytesSent: this.writer.SentBodyBytes(),
Status: int32(this.writer.StatusCode()),
StatusMessage: "",
TimeISO8601: this.requestFromTime.Format("2006-01-02T15:04:05.000Z07:00"),
TimeLocal: this.requestFromTime.Format("2/Jan/2006:15:04:05 -0700"),
Msec: float64(this.requestFromTime.Unix()) + float64(this.requestFromTime.Nanosecond())/1000000000,
Timestamp: this.requestFromTime.Unix(),
2022-01-01 20:15:39 +08:00
Host: this.ReqHost,
2020-10-10 11:47:53 +08:00
Referer: referer,
UserAgent: userAgent,
Request: this.requestString(),
ContentType: this.writer.Header().Get("Content-Type"),
2020-10-10 11:47:53 +08:00
Cookie: cookies,
Args: queryString,
QueryString: queryString,
Header: pbReqHeader,
ServerName: this.ServerName,
ServerPort: int32(this.requestServerPort()),
ServerProtocol: this.RawReq.Proto,
SentHeader: pbResHeader,
Errors: this.errors,
Hostname: HOSTNAME,
FirewallPolicyId: this.firewallPolicyId,
FirewallRuleGroupId: this.firewallRuleGroupId,
FirewallRuleSetId: this.firewallRuleSetId,
FirewallRuleId: this.firewallRuleId,
2021-07-18 15:51:49 +08:00
FirewallActions: this.firewallActions,
Tags: this.tags,
2020-11-02 15:49:30 +08:00
Attrs: this.logAttrs,
2020-10-10 11:47:53 +08:00
}
if this.origin != nil {
accessLog.OriginId = this.origin.Id
accessLog.OriginAddress = this.originAddr
2022-09-16 10:07:40 +08:00
accessLog.OriginStatus = this.originStatus
2020-10-10 11:47:53 +08:00
}
2021-12-07 15:12:15 +08:00
// 请求Body
2022-07-16 17:05:37 +08:00
if (ref != nil && ref.ContainsField(serverconfigs.HTTPAccessLogFieldRequestBody)) || this.wafHasRequestBody {
2021-12-07 15:12:15 +08:00
accessLog.RequestBody = this.requestBodyData
if len(accessLog.RequestBody) > AccessLogMaxRequestBodySize {
accessLog.RequestBody = accessLog.RequestBody[:AccessLogMaxRequestBodySize]
}
}
2022-04-21 09:40:05 +08:00
// TODO 记录匹配的 locationId和rewriteId非必要需求
2020-10-10 11:47:53 +08:00
sharedHTTPAccessLogQueue.Push(accessLog)
}