From 8c04a9ac96b965422173ba71c2d9adc82bbf31ae Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Fri, 15 Dec 2017 11:14:45 -0800 Subject: [PATCH] internal/tun2: DRY tun2.Server.RoundTrip Closes #4 --- internal/tun2/server.go | 32 ++++++++++---------------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/internal/tun2/server.go b/internal/tun2/server.go index 2d495ee..e4e3ab9 100644 --- a/internal/tun2/server.go +++ b/internal/tun2/server.go @@ -411,16 +411,17 @@ func (s *Server) RoundTrip(req *http.Request) (*http.Response, error) { var conns []*Connection ctx := req.Context() + f := ln.F{ + "remote": req.RemoteAddr, + "host": req.Host, + "uri": req.RequestURI, + } + val, ok := s.domains.Get(req.Host) if ok { conns, ok = val.([]*Connection) if !ok { - ln.Error(ctx, ErrNoSuchBackend, ln.F{ - "action": "no_backend_connected", - "remote": req.RemoteAddr, - "host": req.Host, - "uri": req.RequestURI, - }) + ln.Error(ctx, ErrNoSuchBackend, f, ln.Action("no backend available")) return gen502Page(req), nil } @@ -434,12 +435,7 @@ func (s *Server) RoundTrip(req *http.Request) (*http.Response, error) { } if len(goodConns) == 0 { - ln.Error(ctx, ErrNoSuchBackend, ln.F{ - "action": "no_backend_connected", - "remote": req.RemoteAddr, - "host": req.Host, - "uri": req.RequestURI, - }) + ln.Error(ctx, ErrNoSuchBackend, f, ln.Action("no good backends available")) return gen502Page(req), nil } @@ -448,19 +444,13 @@ func (s *Server) RoundTrip(req *http.Request) (*http.Response, error) { resp, err := c.RoundTrip(req) if err != nil { - ln.Error(ctx, err, c, ln.F{ - "action": "connection_roundtrip", - }) + ln.Error(ctx, err, c, f, ln.Action("connection roundtrip")) defer c.cancel() return nil, err } - ln.Log(ctx, c, ln.F{ - "action": "http traffic", - "remote_addr": req.RemoteAddr, - "host": req.Host, - "uri": req.URL.Path, + ln.Log(ctx, c, ln.Action("http traffic"), f, ln.F{ "status": resp.Status, "status_code": resp.StatusCode, "content_length": resp.ContentLength, @@ -474,5 +464,3 @@ type Auth struct { Token string `json:"token"` Domain string `json:"domain"` } - -const defaultUser = "Cadey"