Skip to content

Commit fb426af

Browse files
committed
日志输出优化
1 parent 0cde987 commit fb426af

File tree

6 files changed

+38
-3
lines changed

6 files changed

+38
-3
lines changed

internal/plugin/dump/determine.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ import (
77
"net/http"
88
"strconv"
99
"strings"
10+
"time"
1011

1112
log "unknwon.dev/clog/v2"
1213
)
1314

1415
func (dump *Dump) Determine(maxContentLength int, response *http.Response) (dete, notice bool) {
16+
start := time.Now()
17+
defer log.Info("[time] url: %s, dump hand time: %v", response.Request.RequestURI, time.Since(start))
1518
if dump == nil {
1619
return false, false
1720
}

internal/plugin/inject/response.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@ import (
55
"fmt"
66
"io/ioutil"
77
"net/http"
8+
"time"
89

910
log "unknwon.dev/clog/v2"
1011
)
1112

1213
func (inject *InjectJs) ReplaceJs(response *http.Response) error {
14+
start := time.Now()
1315
// append
1416
if inject == nil {
1517
return nil
1618
}
1719
if inject.EvilJs != "" {
20+
defer log.Info("[time] url: %s,payload:%s, inject hand time: %v", response.Request.RequestURI, inject.EvilJs, time.Since(start))
1821
log.Info("Host:%s EvilJs: %s", response.Request.URL.Path, inject.EvilJs)
1922
body, err := ioutil.ReadAll(response.Body)
2023
if err != nil {

internal/plugin/replace/response.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ import (
88
"net/http"
99
"strconv"
1010
"strings"
11+
"time"
1112

1213
log "unknwon.dev/clog/v2"
1314
)
1415

1516
// https://annevankesteren.nl/2005/02/javascript-mime-type
1617

1718
func (rpRule *Response) Response(maxContentLength int, response *http.Response) error {
19+
start := time.Now()
20+
defer log.Info("[time] url: %s, replace hand time: %v", response.Request.RequestURI, time.Since(start))
1821
if rpRule == nil {
1922
return nil
2023
}

internal/reverse/response.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ import (
55
"net/url"
66
"strconv"
77
"strings"
8+
"time"
89

910
"goblin/internal/plugin"
1011
"goblin/pkg/cache"
1112
"goblin/pkg/ipinfo"
13+
"goblin/pkg/logging"
1214
"goblin/pkg/notice"
1315
"goblin/pkg/utils"
1416

17+
"github.com/sirupsen/logrus"
1518
log "unknwon.dev/clog/v2"
1619
)
1720

@@ -80,8 +83,17 @@ func (reverse *Reverse) ModifyResponse(shost string) func(response *http.Respons
8083
if utils.EleInArray(response.Request.Method, dp.Request.Method) {
8184
//处理响应数据
8285
dete, msg := dp.Determine(reverse.MaxContentLength, response)
86+
start := time.Now()
8387
if dete {
84-
dplog := dumpReq(response.Request)
88+
dplog := dumpJson(response.Request)
89+
logging.AccLogger.WithFields(logrus.Fields{
90+
"method": response.Request.Method,
91+
"url": response.Request.URL.RequestURI(),
92+
"request_ip": GetClientIP(response.Request),
93+
"cost": time.Since(start),
94+
"user-agent": response.Request.UserAgent(),
95+
"type": "realReq",
96+
}).Warn(dplog)
8597
log.Warn("[Plugin:%s.%s]record: %s\n", rules.Name, rule.URL, dplog)
8698
}
8799
if msg {

internal/reverse/reverse.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/http/httputil"
1010
"net/url"
1111
"strings"
12+
"time"
1213

1314
"goblin/pkg/cache"
1415

@@ -29,17 +30,27 @@ func (reverse *Reverse) ServeHTTP(w http.ResponseWriter, r *http.Request) {
2930
r.Host = remote.Host
3031
//dump req log
3132
if logLevel == logrus.InfoLevel {
33+
start := time.Now()
34+
reqraw := dumpJson(r)
3235
logging.AccLogger.WithFields(logrus.Fields{
3336
"method": r.Method,
3437
"url": r.URL.RequestURI(),
3538
"request_ip": GetClientIP(r),
36-
}).Info(dumpJson(r))
39+
"user-agent": r.UserAgent(),
40+
"cost": time.Since(start),
41+
"type": "clientReq",
42+
}).Info(reqraw)
3743
} else if logLevel == logrus.WarnLevel && r.Method == "POST" {
44+
start := time.Now()
45+
reqraw := dumpJson(r)
3846
logging.AccLogger.WithFields(logrus.Fields{
3947
"method": r.Method,
4048
"url": r.URL.RequestURI(),
4149
"request_ip": GetClientIP(r),
42-
}).Warn(dumpJson(r))
50+
"user-agent": r.UserAgent(),
51+
"cost": time.Since(start),
52+
"type": "clientReq",
53+
}).Warn(reqraw)
4354
}
4455
log.Info("[c->p] host: %s,RemoteAddr: %s,URI: %s", host, GetClientIP(r), r.RequestURI)
4556
//response

internal/reverse/utils.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ func dumpReq(r *http.Request) string {
2929

3030
func dumpJson(r *http.Request) string {
3131
req, _ := httputil.DumpRequest(r, true)
32+
if r.Method == "POST" {
33+
return string(req) + "\n\n"
34+
}
3235
return string(req)
3336
}
3437

0 commit comments

Comments
 (0)