Skip to content

Commit

Permalink
日志输出优化
Browse files Browse the repository at this point in the history
  • Loading branch information
Becivells committed Sep 24, 2021
1 parent 0cde987 commit fb426af
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 3 deletions.
3 changes: 3 additions & 0 deletions internal/plugin/dump/determine.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import (
"net/http"
"strconv"
"strings"
"time"

log "unknwon.dev/clog/v2"
)

func (dump *Dump) Determine(maxContentLength int, response *http.Response) (dete, notice bool) {
start := time.Now()
defer log.Info("[time] url: %s, dump hand time: %v", response.Request.RequestURI, time.Since(start))
if dump == nil {
return false, false
}
Expand Down
3 changes: 3 additions & 0 deletions internal/plugin/inject/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ import (
"fmt"
"io/ioutil"
"net/http"
"time"

log "unknwon.dev/clog/v2"
)

func (inject *InjectJs) ReplaceJs(response *http.Response) error {
start := time.Now()
// append
if inject == nil {
return nil
}
if inject.EvilJs != "" {
defer log.Info("[time] url: %s,payload:%s, inject hand time: %v", response.Request.RequestURI, inject.EvilJs, time.Since(start))
log.Info("Host:%s EvilJs: %s", response.Request.URL.Path, inject.EvilJs)
body, err := ioutil.ReadAll(response.Body)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions internal/plugin/replace/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import (
"net/http"
"strconv"
"strings"
"time"

log "unknwon.dev/clog/v2"
)

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

func (rpRule *Response) Response(maxContentLength int, response *http.Response) error {
start := time.Now()
defer log.Info("[time] url: %s, replace hand time: %v", response.Request.RequestURI, time.Since(start))
if rpRule == nil {
return nil
}
Expand Down
14 changes: 13 additions & 1 deletion internal/reverse/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import (
"net/url"
"strconv"
"strings"
"time"

"goblin/internal/plugin"
"goblin/pkg/cache"
"goblin/pkg/ipinfo"
"goblin/pkg/logging"
"goblin/pkg/notice"
"goblin/pkg/utils"

"github.com/sirupsen/logrus"
log "unknwon.dev/clog/v2"
)

Expand Down Expand Up @@ -80,8 +83,17 @@ func (reverse *Reverse) ModifyResponse(shost string) func(response *http.Respons
if utils.EleInArray(response.Request.Method, dp.Request.Method) {
//处理响应数据
dete, msg := dp.Determine(reverse.MaxContentLength, response)
start := time.Now()
if dete {
dplog := dumpReq(response.Request)
dplog := dumpJson(response.Request)
logging.AccLogger.WithFields(logrus.Fields{
"method": response.Request.Method,
"url": response.Request.URL.RequestURI(),
"request_ip": GetClientIP(response.Request),
"cost": time.Since(start),
"user-agent": response.Request.UserAgent(),
"type": "realReq",
}).Warn(dplog)
log.Warn("[Plugin:%s.%s]record: %s\n", rules.Name, rule.URL, dplog)
}
if msg {
Expand Down
15 changes: 13 additions & 2 deletions internal/reverse/reverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http/httputil"
"net/url"
"strings"
"time"

"goblin/pkg/cache"

Expand All @@ -29,17 +30,27 @@ func (reverse *Reverse) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r.Host = remote.Host
//dump req log
if logLevel == logrus.InfoLevel {
start := time.Now()
reqraw := dumpJson(r)
logging.AccLogger.WithFields(logrus.Fields{
"method": r.Method,
"url": r.URL.RequestURI(),
"request_ip": GetClientIP(r),
}).Info(dumpJson(r))
"user-agent": r.UserAgent(),
"cost": time.Since(start),
"type": "clientReq",
}).Info(reqraw)
} else if logLevel == logrus.WarnLevel && r.Method == "POST" {
start := time.Now()
reqraw := dumpJson(r)
logging.AccLogger.WithFields(logrus.Fields{
"method": r.Method,
"url": r.URL.RequestURI(),
"request_ip": GetClientIP(r),
}).Warn(dumpJson(r))
"user-agent": r.UserAgent(),
"cost": time.Since(start),
"type": "clientReq",
}).Warn(reqraw)
}
log.Info("[c->p] host: %s,RemoteAddr: %s,URI: %s", host, GetClientIP(r), r.RequestURI)
//response
Expand Down
3 changes: 3 additions & 0 deletions internal/reverse/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func dumpReq(r *http.Request) string {

func dumpJson(r *http.Request) string {
req, _ := httputil.DumpRequest(r, true)
if r.Method == "POST" {
return string(req) + "\n\n"
}
return string(req)
}

Expand Down

0 comments on commit fb426af

Please sign in to comment.