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
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"