internal/tun2: DRY tun2.Server.RoundTrip

Closes #4
This commit is contained in:
Cadey Ratio 2017-12-15 11:14:45 -08:00
parent 2998e1d039
commit 8c04a9ac96
1 changed files with 10 additions and 22 deletions

View File

@ -411,16 +411,17 @@ func (s *Server) RoundTrip(req *http.Request) (*http.Response, error) {
var conns []*Connection var conns []*Connection
ctx := req.Context() ctx := req.Context()
f := ln.F{
"remote": req.RemoteAddr,
"host": req.Host,
"uri": req.RequestURI,
}
val, ok := s.domains.Get(req.Host) val, ok := s.domains.Get(req.Host)
if ok { if ok {
conns, ok = val.([]*Connection) conns, ok = val.([]*Connection)
if !ok { if !ok {
ln.Error(ctx, ErrNoSuchBackend, ln.F{ ln.Error(ctx, ErrNoSuchBackend, f, ln.Action("no backend available"))
"action": "no_backend_connected",
"remote": req.RemoteAddr,
"host": req.Host,
"uri": req.RequestURI,
})
return gen502Page(req), nil return gen502Page(req), nil
} }
@ -434,12 +435,7 @@ func (s *Server) RoundTrip(req *http.Request) (*http.Response, error) {
} }
if len(goodConns) == 0 { if len(goodConns) == 0 {
ln.Error(ctx, ErrNoSuchBackend, ln.F{ ln.Error(ctx, ErrNoSuchBackend, f, ln.Action("no good backends available"))
"action": "no_backend_connected",
"remote": req.RemoteAddr,
"host": req.Host,
"uri": req.RequestURI,
})
return gen502Page(req), nil return gen502Page(req), nil
} }
@ -448,19 +444,13 @@ func (s *Server) RoundTrip(req *http.Request) (*http.Response, error) {
resp, err := c.RoundTrip(req) resp, err := c.RoundTrip(req)
if err != nil { if err != nil {
ln.Error(ctx, err, c, ln.F{ ln.Error(ctx, err, c, f, ln.Action("connection roundtrip"))
"action": "connection_roundtrip",
})
defer c.cancel() defer c.cancel()
return nil, err return nil, err
} }
ln.Log(ctx, c, ln.F{ ln.Log(ctx, c, ln.Action("http traffic"), f, ln.F{
"action": "http traffic",
"remote_addr": req.RemoteAddr,
"host": req.Host,
"uri": req.URL.Path,
"status": resp.Status, "status": resp.Status,
"status_code": resp.StatusCode, "status_code": resp.StatusCode,
"content_length": resp.ContentLength, "content_length": resp.ContentLength,
@ -474,5 +464,3 @@ type Auth struct {
Token string `json:"token"` Token string `json:"token"`
Domain string `json:"domain"` Domain string `json:"domain"`
} }
const defaultUser = "Cadey"