From a850a5bc384d931e75838ce7ebb4b087715b5be2 Mon Sep 17 00:00:00 2001 From: qydysky Date: Tue, 7 May 2024 20:31:45 +0000 Subject: [PATCH] 1 --- config.go | 22 +++++++++++----------- http.go | 24 ++++++++++++------------ ws.go | 18 +++++++++--------- 3 files changed, 32 insertions(+), 32 deletions(-) diff --git a/config.go b/config.go index da9cb04..211bc96 100755 --- a/config.go +++ b/config.go @@ -89,39 +89,39 @@ func (t *Config) SwapSign(ctx context.Context, logger Logger) { logger.Info(`I:`, fmt.Sprintf("%v > %v", t.Addr, k)) t.routeMap.Store(k, route) - var logFormat = "%v%v %v %v" + var logFormat = "%v %v%v %v %v" for _, routePath := range route.Path { t.routeP.Store(routePath, func(w http.ResponseWriter, r *http.Request) { if len(r.RequestURI) > 8000 { - logger.Warn(`W:`, fmt.Sprintf(logFormat, route.config.Addr, routePath, "BLOCK", ErrUriTooLong)) + logger.Warn(`W:`, fmt.Sprintf(logFormat, r.RemoteAddr, route.config.Addr, routePath, "BLOCK", ErrUriTooLong)) w.Header().Add(header+"Error", ErrUriTooLong.Error()) w.WriteHeader(http.StatusBadRequest) return } if ok, e := route.Filiter.ReqUri.Match(r); e != nil { - logger.Warn(`W:`, fmt.Sprintf(logFormat, route.config.Addr, routePath, "Err", e)) + logger.Warn(`W:`, fmt.Sprintf(logFormat, r.RemoteAddr, route.config.Addr, routePath, "Err", e)) } else if !ok { - logger.Warn(`W:`, fmt.Sprintf(logFormat, route.config.Addr, routePath, "BLOCK", ErrPatherCheckFail)) + logger.Warn(`W:`, fmt.Sprintf(logFormat, r.RemoteAddr, route.config.Addr, routePath, "BLOCK", ErrPatherCheckFail)) w.Header().Add(header+"Error", ErrPatherCheckFail.Error()) w.WriteHeader(http.StatusForbidden) return } if ok, e := route.Filiter.ReqHeader.Match(r.Header); e != nil { - logger.Warn(`W:`, fmt.Sprintf(logFormat, route.config.Addr, routePath, "Err", e)) + logger.Warn(`W:`, fmt.Sprintf(logFormat, r.RemoteAddr, route.config.Addr, routePath, "Err", e)) } else if !ok { - logger.Warn(`W:`, fmt.Sprintf(logFormat, route.config.Addr, routePath, "BLOCK", ErrHeaderCheckFail)) + logger.Warn(`W:`, fmt.Sprintf(logFormat, r.RemoteAddr, route.config.Addr, routePath, "BLOCK", ErrHeaderCheckFail)) w.Header().Add(header+"Error", ErrHeaderCheckFail.Error()) w.WriteHeader(http.StatusForbidden) return } if ok, e := route.ReqBody.Match(r); e != nil { - logger.Warn(`W:`, fmt.Sprintf(logFormat, route.config.Addr, routePath, "Err", e)) + logger.Warn(`W:`, fmt.Sprintf(logFormat, r.RemoteAddr, route.config.Addr, routePath, "Err", e)) } else if !ok { - logger.Warn(`W:`, fmt.Sprintf(logFormat, route.config.Addr, routePath, "BLOCK", ErrBodyCheckFail)) + logger.Warn(`W:`, fmt.Sprintf(logFormat, r.RemoteAddr, route.config.Addr, routePath, "BLOCK", ErrBodyCheckFail)) w.Header().Add(header+"Error", ErrBodyCheckFail.Error()) w.WriteHeader(http.StatusForbidden) return @@ -133,13 +133,13 @@ func (t *Config) SwapSign(ctx context.Context, logger Logger) { if backP, aok := route.backMap.Load(t.Value); aok { if ok, e := backP.(*Back).getFiliterReqUri().Match(r); e != nil { - logger.Warn(`W:`, fmt.Sprintf(logFormat, route.config.Addr, routePath, "Err", e)) + logger.Warn(`W:`, fmt.Sprintf(logFormat, r.RemoteAddr, route.config.Addr, routePath, "Err", e)) } else if ok { aok = false } if ok, e := backP.(*Back).getFiliterReqHeader().Match(r.Header); e != nil { - logger.Warn(`W:`, fmt.Sprintf(logFormat, route.config.Addr, routePath, "Err", e)) + logger.Warn(`W:`, fmt.Sprintf(logFormat, r.RemoteAddr, route.config.Addr, routePath, "Err", e)) } else if ok { aok = false } @@ -155,7 +155,7 @@ func (t *Config) SwapSign(ctx context.Context, logger Logger) { backIs = append(backIs, route.FiliterBackByRequest(r)...) if len(backIs) == 0 { - logger.Warn(`W:`, fmt.Sprintf(logFormat, route.config.Addr, routePath, "BLOCK", ErrNoRoute)) + logger.Warn(`W:`, fmt.Sprintf(logFormat, r.RemoteAddr, route.config.Addr, routePath, "BLOCK", ErrNoRoute)) w.Header().Add(header+"Error", ErrNoRoute.Error()) w.WriteHeader(http.StatusNotFound) return diff --git a/http.go b/http.go index 39b398d..9a98c01 100644 --- a/http.go +++ b/http.go @@ -20,7 +20,7 @@ func httpDealer(ctx context.Context, w http.ResponseWriter, r *http.Request, rou opT = time.Now() resp *http.Response chosenBack *Back - logFormat = "%v%v > %v http %v %v %v" + logFormat = "%v %v%v > %v http %v %v %v" ) for i := 0; i < len(backs) && resp == nil; i++ { @@ -42,7 +42,7 @@ func httpDealer(ctx context.Context, w http.ResponseWriter, r *http.Request, rou } if e := copyHeader(r.Header, req.Header, chosenBack.getDealerReqHeader()); e != nil { - logger.Warn(`W:`, fmt.Sprintf(logFormat, chosenBack.route.config.Addr, routePath, chosenBack.Name, "BLOCK", e, time.Since(opT))) + logger.Warn(`W:`, fmt.Sprintf(logFormat, r.RemoteAddr, chosenBack.route.config.Addr, routePath, chosenBack.Name, "BLOCK", e, time.Since(opT))) return ErrDealReqHeader } @@ -68,10 +68,10 @@ func httpDealer(ctx context.Context, w http.ResponseWriter, r *http.Request, rou return } } else { - logger.Warn(`W:`, fmt.Sprintf(logFormat, chosenBack.route.config.Addr, routePath, chosenBack.Name, "Err", ErrCerVerify, time.Since(opT))) + logger.Warn(`W:`, fmt.Sprintf(logFormat, r.RemoteAddr, chosenBack.route.config.Addr, routePath, chosenBack.Name, "Err", ErrCerVerify, time.Since(opT))) } } else if err != ErrEmptyVerifyPeerCerByte { - logger.Warn(`W:`, fmt.Sprintf(logFormat, chosenBack.route.config.Addr, routePath, chosenBack.Name, "Err", err, time.Since(opT))) + logger.Warn(`W:`, fmt.Sprintf(logFormat, r.RemoteAddr, chosenBack.route.config.Addr, routePath, chosenBack.Name, "Err", err, time.Since(opT))) } client := http.Client{ @@ -83,13 +83,13 @@ func httpDealer(ctx context.Context, w http.ResponseWriter, r *http.Request, rou resp, e = client.Do(req) if e != nil && !errors.Is(e, ErrRedirect) && !errors.Is(e, context.Canceled) { - logger.Warn(`W:`, fmt.Sprintf(logFormat, chosenBack.route.config.Addr, routePath, chosenBack.Name, "Err", e, time.Since(opT))) + logger.Warn(`W:`, fmt.Sprintf(logFormat, r.RemoteAddr, chosenBack.route.config.Addr, routePath, chosenBack.Name, "Err", e, time.Since(opT))) chosenBack.Disable() resp = nil } if chosenBack.getErrToSec() != 0 && time.Since(opT).Seconds() > chosenBack.getErrToSec() { - logger.Warn(`W:`, fmt.Sprintf(logFormat, chosenBack.route.config.Addr, routePath, chosenBack.Name, "BLOCK", ErrResTO, time.Since(opT))) + logger.Warn(`W:`, fmt.Sprintf(logFormat, r.RemoteAddr, chosenBack.route.config.Addr, routePath, chosenBack.Name, "BLOCK", ErrResTO, time.Since(opT))) chosenBack.Disable() resp = nil } @@ -104,15 +104,15 @@ func httpDealer(ctx context.Context, w http.ResponseWriter, r *http.Request, rou } if ok, e := chosenBack.getFiliterResHeader().Match(resp.Header); e != nil { - logger.Warn(`W:`, fmt.Sprintf(logFormat, chosenBack.route.config.Addr, routePath, chosenBack.Name, "Err", e, time.Since(opT))) + logger.Warn(`W:`, fmt.Sprintf(logFormat, r.RemoteAddr, chosenBack.route.config.Addr, routePath, chosenBack.Name, "Err", e, time.Since(opT))) } else if !ok { - logger.Warn(`W:`, fmt.Sprintf(logFormat, chosenBack.route.config.Addr, routePath, chosenBack.Name, "BLOCK", ErrHeaderCheckFail, time.Since(opT))) + logger.Warn(`W:`, fmt.Sprintf(logFormat, r.RemoteAddr, chosenBack.route.config.Addr, routePath, chosenBack.Name, "BLOCK", ErrHeaderCheckFail, time.Since(opT))) w.Header().Add(header+"Error", ErrHeaderCheckFail.Error()) w.WriteHeader(http.StatusForbidden) return ErrHeaderCheckFail } - logger.Debug(`T:`, fmt.Sprintf(logFormat, chosenBack.route.config.Addr, routePath, chosenBack.Name, r.Method, r.RequestURI, time.Since(opT))) + logger.Debug(`T:`, fmt.Sprintf(logFormat, r.RemoteAddr, chosenBack.route.config.Addr, routePath, chosenBack.Name, r.Method, r.RequestURI, time.Since(opT))) if chosenBack.route.RollRule != `` { chosenBack.be(opT) @@ -135,7 +135,7 @@ func httpDealer(ctx context.Context, w http.ResponseWriter, r *http.Request, rou w.Header().Add(header+"Info", cookie+";"+chosenBack.Name) if e := copyHeader(resp.Header, w.Header(), chosenBack.getDealerResHeader()); e != nil { - logger.Warn(`W:`, fmt.Sprintf(logFormat, chosenBack.route.config.Addr, routePath, chosenBack.Name, "BLOCK", e, time.Since(opT))) + logger.Warn(`W:`, fmt.Sprintf(logFormat, r.RemoteAddr, chosenBack.route.config.Addr, routePath, chosenBack.Name, "BLOCK", e, time.Since(opT))) return ErrDealResHeader } @@ -147,13 +147,13 @@ func httpDealer(ctx context.Context, w http.ResponseWriter, r *http.Request, rou defer resp.Body.Close() if tmpbuf, put, e := blocksi.Get(); e != nil { - logger.Error(`E:`, fmt.Sprintf(logFormat, chosenBack.route.config.Addr, routePath, chosenBack.Name, "BLOCK", e, time.Since(opT))) + logger.Error(`E:`, fmt.Sprintf(logFormat, r.RemoteAddr, chosenBack.route.config.Addr, routePath, chosenBack.Name, "BLOCK", e, time.Since(opT))) chosenBack.Disable() return ErrCopy } else { defer put() if _, e = io.CopyBuffer(w, resp.Body, tmpbuf); e != nil { - logger.Error(`E:`, fmt.Sprintf(logFormat, chosenBack.route.config.Addr, routePath, chosenBack.Name, "BLOCK", e, time.Since(opT))) + logger.Error(`E:`, fmt.Sprintf(logFormat, r.RemoteAddr, chosenBack.route.config.Addr, routePath, chosenBack.Name, "BLOCK", e, time.Since(opT))) if !errors.Is(e, context.Canceled) { chosenBack.Disable() } diff --git a/ws.go b/ws.go index 4c918f7..e515aca 100644 --- a/ws.go +++ b/ws.go @@ -30,7 +30,7 @@ func wsDealer(ctx context.Context, w http.ResponseWriter, r *http.Request, route e error conn net.Conn chosenBack *Back - errFormat = "%v > %v > %v ws %v %v" + errFormat = "%v %v > %v > %v ws %v %v" ) for i := 0; i < len(backs) && (resp == nil || conn == nil); i++ { @@ -49,20 +49,20 @@ func wsDealer(ctx context.Context, w http.ResponseWriter, r *http.Request, route reqHeader := make(http.Header) if e := copyHeader(r.Header, reqHeader, chosenBack.getDealerReqHeader()); e != nil { - logger.Warn(`W:`, fmt.Sprintf(errFormat, chosenBack.route.config.Addr, routePath, chosenBack.Name, e, time.Since(opT))) + logger.Warn(`W:`, fmt.Sprintf(errFormat, r.RemoteAddr, chosenBack.route.config.Addr, routePath, chosenBack.Name, e, time.Since(opT))) return ErrDealReqHeader } conn, resp, e = DialContext(ctx, url, reqHeader, chosenBack) if e != nil && !errors.Is(e, context.Canceled) { - logger.Warn(`W:`, fmt.Sprintf(errFormat, chosenBack.route.config.Addr, routePath, chosenBack.Name, e, time.Since(opT))) + logger.Warn(`W:`, fmt.Sprintf(errFormat, r.RemoteAddr, chosenBack.route.config.Addr, routePath, chosenBack.Name, e, time.Since(opT))) chosenBack.Disable() conn = nil resp = nil } if chosenBack.getErrToSec() != 0 && time.Since(opT).Seconds() > chosenBack.getErrToSec() { - logger.Warn(`W:`, fmt.Sprintf(errFormat, chosenBack.route.config.Addr, routePath, chosenBack.Name, ErrResTO, time.Since(opT))) + logger.Warn(`W:`, fmt.Sprintf(errFormat, r.RemoteAddr, chosenBack.route.config.Addr, routePath, chosenBack.Name, ErrResTO, time.Since(opT))) chosenBack.Disable() conn.Close() conn = nil @@ -83,9 +83,9 @@ func wsDealer(ctx context.Context, w http.ResponseWriter, r *http.Request, route } if ok, e := chosenBack.getFiliterResHeader().Match(resp.Header); e != nil { - logger.Warn(`W:`, fmt.Sprintf(errFormat, chosenBack.route.config.Addr, routePath, chosenBack.Name, e, time.Since(opT))) + logger.Warn(`W:`, fmt.Sprintf(errFormat, r.RemoteAddr, chosenBack.route.config.Addr, routePath, chosenBack.Name, e, time.Since(opT))) } else if !ok { - logger.Warn(`W:`, fmt.Sprintf(errFormat, chosenBack.route.config.Addr, routePath, chosenBack.Name, ErrHeaderCheckFail, time.Since(opT))) + logger.Warn(`W:`, fmt.Sprintf(errFormat, r.RemoteAddr, chosenBack.route.config.Addr, routePath, chosenBack.Name, ErrHeaderCheckFail, time.Since(opT))) w.Header().Add(header+"Error", ErrHeaderCheckFail.Error()) w.WriteHeader(http.StatusForbidden) return ErrHeaderCheckFail @@ -117,7 +117,7 @@ func wsDealer(ctx context.Context, w http.ResponseWriter, r *http.Request, route resHeader := make(http.Header) if e := copyHeader(resp.Header, resHeader, chosenBack.getDealerResHeader()); e != nil { - logger.Warn(`W:`, fmt.Sprintf(errFormat, chosenBack.route.config.Addr, routePath, chosenBack.Name, e, time.Since(opT))) + logger.Warn(`W:`, fmt.Sprintf(errFormat, r.RemoteAddr, chosenBack.route.config.Addr, routePath, chosenBack.Name, e, time.Since(opT))) return ErrDealResHeader } @@ -132,7 +132,7 @@ func wsDealer(ctx context.Context, w http.ResponseWriter, r *http.Request, route if !errors.Is(e, context.Canceled) { chosenBack.Disable() } - logger.Error(`E:`, fmt.Sprintf(errFormat, chosenBack.route.config.Addr, routePath, chosenBack.Name, e, time.Since(opT))) + logger.Error(`E:`, fmt.Sprintf(errFormat, r.RemoteAddr, chosenBack.route.config.Addr, routePath, chosenBack.Name, e, time.Since(opT))) return ErrCopy } case e := <-copyWsMsg(conn, req, blocksi): @@ -140,7 +140,7 @@ func wsDealer(ctx context.Context, w http.ResponseWriter, r *http.Request, route if !errors.Is(e, context.Canceled) { chosenBack.Disable() } - logger.Error(`E:`, fmt.Sprintf(errFormat, chosenBack.route.config.Addr, routePath, chosenBack.Name, e, time.Since(opT))) + logger.Error(`E:`, fmt.Sprintf(errFormat, r.RemoteAddr, chosenBack.route.config.Addr, routePath, chosenBack.Name, e, time.Since(opT))) return ErrCopy } case <-ctx.Done(): -- 2.39.2