From 0e7a7f168f1db66e604f7efc6a5da7b84b3c0a9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Sun, 12 Jun 2022 20:30:28 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BF=E9=97=AE=E6=97=A5=E5=BF=97=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E5=A2=9E=E5=8A=A0requestPath:/hello=E3=80=81proto:HTT?= =?UTF-8?q?P/1.1=E3=80=81scheme:http=E7=AD=89=E8=AF=AD=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/db/models/http_access_log_dao.go | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/internal/db/models/http_access_log_dao.go b/internal/db/models/http_access_log_dao.go index ed356b22..bcafc67e 100644 --- a/internal/db/models/http_access_log_dao.go +++ b/internal/db/models/http_access_log_dao.go @@ -395,9 +395,14 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx, var locker = sync.Mutex{} + // 这里正则表达式中的括号不能轻易变更,因为后面有引用 + // TODO 支持多个查询条件的组合,比如 status:200 proto:HTTP/1.1 var statusPrefixReg = regexp.MustCompile(`status:\s*(\d{3})\b`) var statusRangeReg = regexp.MustCompile(`status:\s*(\d{3})-(\d{3})\b`) var urlReg = regexp.MustCompile(`^(http|https)://`) + var requestPathReg = regexp.MustCompile(`requestPath:(\S+)`) + var protoReg = regexp.MustCompile(`proto:(\S+)`) + var schemeReg = regexp.MustCompile(`scheme:(\S+)`) var count = len(tableQueries) var wg = &sync.WaitGroup{} @@ -509,13 +514,25 @@ func (this *HTTPAccessLogDAO) listAccessLogs(tx *dbs.Tx, isSpecialKeyword = true var matches = statusRangeReg.FindStringSubmatch(keyword) query.Between("status", types.Int(matches[1]), types.Int(matches[2])) - - // TODO 处理剩余的关键词 } else if statusPrefixReg.MatchString(keyword) { // status:200 isSpecialKeyword = true var matches = statusPrefixReg.FindStringSubmatch(keyword) query.Attr("status", matches[1]) - // TODO 处理剩余的关键词 + } else if requestPathReg.MatchString(keyword) { + isSpecialKeyword = true + var matches = requestPathReg.FindStringSubmatch(keyword) + query.Where("JSON_EXTRACT(content, '$.requestPath')=:keyword"). + Param("keyword", matches[1]) + } else if protoReg.MatchString(keyword) { + isSpecialKeyword = true + var matches = protoReg.FindStringSubmatch(keyword) + query.Where("JSON_EXTRACT(content, '$.proto')=:keyword"). + Param("keyword", strings.ToUpper(matches[1])) + } else if schemeReg.MatchString(keyword) { + isSpecialKeyword = true + var matches = schemeReg.FindStringSubmatch(keyword) + query.Where("JSON_EXTRACT(content, '$.scheme')=:keyword"). + Param("keyword", strings.ToLower(matches[1])) } else if urlReg.MatchString(keyword) { // https://xxx/yyy u, err := url.Parse(keyword) if err == nil {