@@ -7,26 +7,47 @@ import (
77 "github.com/iwind/TeaGo/actions"
88 "github.com/iwind/TeaGo/lists"
99 "github.com/iwind/TeaGo/logs"
10+ "github.com/iwind/TeaGo/maps"
1011 "github.com/iwind/TeaGo/utils/time"
1112 "net/http"
13+ "regexp"
14+ "strings"
1215 "time"
1316)
1417
1518type ListAction actions.Action
1619
20+ var whitespaceSplitReg = regexp .MustCompile (`\s+` )
21+
1722// 获取日志
1823func (this * ListAction ) Run (params struct {
19- ServerId string
20- FromId string
21- Size int64 `default:"10"`
24+ ServerId string
25+ FromId string
26+ Size int64 `default:"10"`
27+
28+ RemoteAddr string // 终端地址
29+ Domain string // 域名
30+ OsName string // 终端OS
31+ Browser string // 终端浏览器
32+ Cost float64 // 耗时
33+ Keyword string // 关键词
34+ BackendId string // 后端服务器
35+ LocationId string // 路径规则
36+ RewriteId string // 重写规则
37+ FastcgiId string // FastcgiId
38+
2239 BodyFetching bool
2340 LogType string
2441}) {
25-
2642 if params .Size < 1 {
2743 params .Size = 20
2844 }
2945
46+ params .Domain = strings .ToLower (params .Domain )
47+ params .OsName = strings .ToLower (params .OsName )
48+ params .Browser = strings .ToLower (params .Browser )
49+ params .Keyword = strings .ToLower (params .Keyword )
50+
3051 serverId := params .ServerId
3152
3253 requestBodyFetching = params .BodyFetching
@@ -57,13 +78,75 @@ func (this *ListAction) Run(params struct {
5778 query .Limit (params .Size )
5879 ones , err := query .FindAll ()
5980
81+ this .Data ["lastId" ] = ""
6082 if err != nil {
6183 logs .Error (err )
6284 this .Data ["logs" ] = []interface {}{}
6385 } else {
64- result := lists .Map (ones , func (k int , v interface {}) interface {} {
65- accessLog := v .(* tealogs.AccessLog )
66- return map [string ]interface {}{
86+ result := []maps.Map {}
87+ if len (ones ) > 0 {
88+ if shouldReverse {
89+ this .Data ["lastId" ] = ones [len (ones )- 1 ].(* tealogs.AccessLog ).Id .Hex ()
90+ } else {
91+ this .Data ["lastId" ] = ones [0 ].(* tealogs.AccessLog ).Id .Hex ()
92+ }
93+ }
94+ for _ , one := range ones {
95+ accessLog := one .(* tealogs.AccessLog )
96+
97+ // filters
98+ if len (params .RemoteAddr ) > 0 && ! this .match (accessLog .RemoteAddr , params .RemoteAddr ) {
99+ continue
100+ }
101+
102+ if len (params .Domain ) > 0 && ! this .match (accessLog .Host , params .Domain ) {
103+ continue
104+ }
105+
106+ if len (params .OsName ) > 0 && ! this .match (accessLog .Extend .Client .OS .Family + " " + accessLog .Extend .Client .OS .Major , params .OsName ) {
107+ continue
108+ }
109+
110+ if len (params .Browser ) > 0 && ! this .match (accessLog .Extend .Client .Browser .Family + " " + accessLog .Extend .Client .Browser .Major , params .Browser ) {
111+ continue
112+ }
113+
114+ if params .Cost > 0 && accessLog .RequestTime * 1000 < params .Cost {
115+ continue
116+ }
117+
118+ if len (params .Keyword ) > 0 &&
119+ ! this .match (accessLog .Request , params .Keyword ) &&
120+ ! this .match (accessLog .Host , params .Keyword ) &&
121+ ! this .match (accessLog .RemoteAddr , params .Keyword ) &&
122+ ! this .match (accessLog .UserAgent , params .Keyword ) &&
123+ ! this .match (accessLog .Extend .Client .OS .Family + " " + accessLog .Extend .Client .OS .Major , params .Keyword ) &&
124+ ! this .match (accessLog .Extend .Client .Browser .Family + " " + accessLog .Extend .Client .Browser .Major , params .Keyword ) &&
125+ ! this .match (fmt .Sprintf ("%d" , accessLog .Status ), params .Keyword ) &&
126+ ! this .match (accessLog .StatusMessage , params .Keyword ) &&
127+ ! this .match (accessLog .ContentType , params .Keyword ) &&
128+ ! this .match (accessLog .TimeLocal , params .Keyword ) &&
129+ ! this .match (accessLog .TimeISO8601 , params .Keyword ) {
130+ continue
131+ }
132+
133+ if len (params .BackendId ) > 0 && accessLog .BackendId != params .BackendId {
134+ continue
135+ }
136+
137+ if len (params .LocationId ) > 0 && accessLog .LocationId != params .LocationId {
138+ continue
139+ }
140+
141+ if len (params .RewriteId ) > 0 && accessLog .RewriteId != params .RewriteId {
142+ continue
143+ }
144+
145+ if len (params .FastcgiId ) > 0 && accessLog .FastcgiId != params .FastcgiId {
146+ continue
147+ }
148+
149+ result = append (result , map [string ]interface {}{
67150 "id" : accessLog .Id .Hex (),
68151 "requestTime" : accessLog .RequestTime ,
69152 "request" : accessLog .Request ,
@@ -93,8 +176,8 @@ func (this *ListAction) Run(params struct {
93176 "rewriteId" : accessLog .RewriteId ,
94177 "fastcgiId" : accessLog .FastcgiId ,
95178 "attrs" : accessLog .Attrs ,
96- }
97- })
179+ })
180+ }
98181
99182 if shouldReverse {
100183 lists .Reverse (result )
@@ -104,3 +187,22 @@ func (this *ListAction) Run(params struct {
104187
105188 this .Success ()
106189}
190+
191+ func (this * ListAction ) match (s string , keyword string ) bool {
192+ if len (keyword ) == 0 {
193+ return false
194+ }
195+ if len (s ) == 0 {
196+ return false
197+ }
198+
199+ s = strings .ToLower (s )
200+ ok := true
201+ for _ , piece := range whitespaceSplitReg .Split (keyword , - 1 ) {
202+ if strings .Index (s , piece ) == - 1 {
203+ ok = false
204+ break
205+ }
206+ }
207+ return ok
208+ }
0 commit comments