Skip to content

Commit

Permalink
Merge 3c7c9e7 into f736ced
Browse files Browse the repository at this point in the history
  • Loading branch information
rekby committed Dec 2, 2020
2 parents f736ced + 3c7c9e7 commit c5ddbd7
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cmd/a_main-packr.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type logConfig struct {
EnableLogToFile bool
EnableLogToStdErr bool
LogLevel string
EnableAccessLog bool
EnableRotate bool
DeveloperMode bool
File string
Expand Down
1 change: 1 addition & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func startProgram(config *configType) {
err = tlsListener.Start(ctx, registry)
log.DebugFatal(logger, err, "StartAutoRenew tls listener")

config.Proxy.EnableAccessLog = config.Log.EnableAccessLog
p := proxy.NewHTTPProxy(ctx, tlsListener)
p.GetContext = func(req *http.Request) (i context.Context, e error) {
localAddr := req.Context().Value(http.LocalAddrContextKey).(net.Addr)
Expand Down
3 changes: 3 additions & 0 deletions cmd/static/default-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ EnableLogToStdErr = true
# verbose level of log, one of: debug, info, warning, error, fatal
LogLevel = "info"

# Enable write info about every http request (but write info about connections if need by level)
EnableAccessLog = true

# Enable self log rotating
EnableRotate = true

Expand Down
4 changes: 3 additions & 1 deletion internal/proxy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Config struct {
KeepAliveTimeoutSeconds int
HTTPSBackend bool
HTTPSBackendIgnoreCert bool
EnableAccessLog bool
}

func (c *Config) Apply(ctx context.Context, p *HTTPProxy) error {
Expand All @@ -45,7 +46,8 @@ func (c *Config) Apply(ctx context.Context, p *HTTPProxy) error {
appendDirector(c.getMapDirector)
appendDirector(c.getHeadersDirector)
appendDirector(c.getSchemaDirector)
p.httpReverseProxy.Transport = Transport{c.HTTPSBackendIgnoreCert}
p.HTTPTransport = Transport{c.HTTPSBackendIgnoreCert}
p.EnableAccessLog = c.EnableAccessLog

if resErr != nil {
zc.L(ctx).Error("Can't parse proxy config", zap.Error(resErr))
Expand Down
4 changes: 2 additions & 2 deletions internal/proxy/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,12 @@ func TestConfig_Apply(t *testing.T) {
c = Config{HTTPSBackendIgnoreCert: false}
p = &HTTPProxy{}
_ = c.Apply(ctx, p)
transport := p.httpReverseProxy.Transport.(Transport)
transport := p.HTTPTransport.(Transport)
transport.IgnoreHTTPSCertificate = false

c = Config{HTTPSBackendIgnoreCert: true}
p = &HTTPProxy{}
_ = c.Apply(ctx, p)
transport = p.httpReverseProxy.Transport.(Transport)
transport = p.HTTPTransport.(Transport)
transport.IgnoreHTTPSCertificate = true
}
7 changes: 7 additions & 0 deletions internal/proxy/http-proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type HTTPProxy struct {
HandleHTTPValidation func(w http.ResponseWriter, r *http.Request) bool
Director Director // modify requests to backend.
HTTPTransport http.RoundTripper
EnableAccessLog bool

logger *zap.Logger
listener net.Listener
Expand Down Expand Up @@ -61,9 +62,15 @@ func (p *HTTPProxy) Close() error {
// Any public fields must not change after Start called
func (p *HTTPProxy) Start() error {
if p.HTTPTransport != nil {
p.logger.Info("Set transport to reverse proxy")
p.httpReverseProxy.Transport = p.HTTPTransport
}

if p.EnableAccessLog {
p.httpReverseProxy.Transport = NewTransportLogger(p.httpReverseProxy.Transport)
}
p.logger.Info("Access log", zap.Bool("enabled", p.EnableAccessLog))

mux := &http.ServeMux{}
mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
if !p.HandleHTTPValidation(writer, request) {
Expand Down
41 changes: 41 additions & 0 deletions internal/proxy/transport_logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package proxy

import (
"net/http"
"time"

"go.uber.org/zap"

"github.com/rekby/lets-proxy2/internal/log"
)

type TransportLogger struct {
Transport http.RoundTripper
}

func (t TransportLogger) RoundTrip(request *http.Request) (resp *http.Response, err error) {
start := time.Now()

defer func() {
log.InfoErrorCtx(request.Context(), err, "Request",
zap.Duration("duration_without_body", time.Since(start)),
zap.String("initiator_addr", request.RemoteAddr),
zap.String("metod", request.Method),
zap.String("host", request.Host),
zap.String("path", request.URL.Path),
zap.String("query", request.URL.RawQuery),
zap.Int("status_code", resp.StatusCode),
zap.Int64("request_content_length", request.ContentLength),
zap.Int64("resp_content_length", resp.ContentLength),
)
}()

return t.Transport.RoundTrip(request)
}

func NewTransportLogger(transport http.RoundTripper) TransportLogger {
if transport == nil {
return TransportLogger{Transport: http.DefaultTransport}
}
return TransportLogger{Transport: transport}
}
7 changes: 7 additions & 0 deletions internal/proxy/transport_logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package proxy

import (
"net/http"
)

var _ http.RoundTripper = TransportLogger{}

0 comments on commit c5ddbd7

Please sign in to comment.