]> Sergey Matveev's repositories - tofuproxy.git/blobdiff - trip.go
Ability to remove hosts from the states, refactoring
[tofuproxy.git] / trip.go
diff --git a/trip.go b/trip.go
index a2e2565047626294254055d3ef3ab2f4a6a76391..90a5ad3b145c2c742d31112a2f5c2c4421020ad4 100644 (file)
--- a/trip.go
+++ b/trip.go
@@ -27,6 +27,7 @@ import (
        "time"
 
        "github.com/dustin/go-humanize"
+       "go.stargrave.org/tofuproxy/caches"
        "go.stargrave.org/tofuproxy/fifos"
        "go.stargrave.org/tofuproxy/rounds"
 )
@@ -58,7 +59,7 @@ type Round func(
 ) (bool, error)
 
 func roundTrip(w http.ResponseWriter, req *http.Request) {
-       fifos.SinkReq <- fmt.Sprintf("%s %s", req.Method, req.URL.String())
+       fifos.LogReq <- fmt.Sprintf("%s %s", req.Method, req.URL)
        host := strings.TrimSuffix(req.URL.Host, ":443")
        for _, round := range []Round{
                rounds.RoundNoHead,
@@ -74,40 +75,44 @@ func roundTrip(w http.ResponseWriter, req *http.Request) {
        reqFlags := []string{}
        unauthorized := false
 
-       authCacheM.Lock()
-       if creds, ok := authCache[req.URL.Host]; ok {
+       caches.HTTPAuthCacheM.RLock()
+       if creds, ok := caches.HTTPAuthCache[req.URL.Host]; ok {
                req.SetBasicAuth(creds[0], creds[1])
+               fifos.LogHTTPAuth <- fmt.Sprintf("%s %s\t%s", req.Method, req.URL, creds[0])
                unauthorized = true
        }
-       authCacheM.Unlock()
+       caches.HTTPAuthCacheM.RUnlock()
 
 Retry:
        resp, err := transport.RoundTrip(req)
        if err != nil {
-               fifos.SinkErr <- fmt.Sprintf("%s\t%s", req.URL.Host, err.Error())
+               fifos.LogErr <- fmt.Sprintf("%s\t%s", req.URL.Host, err.Error())
                http.Error(w, err.Error(), http.StatusBadGateway)
                return
        }
 
        if resp.StatusCode == http.StatusUnauthorized {
                resp.Body.Close()
-               authCacheM.Lock()
+               caches.HTTPAuthCacheM.Lock()
                if unauthorized {
-                       delete(authCache, req.URL.Host)
+                       delete(caches.HTTPAuthCache, req.URL.Host)
                } else {
                        unauthorized = true
                }
-               fifos.SinkOther <- fmt.Sprintf("%s\tauthorization required", req.URL.Host)
+               fifos.LogVarious <- fmt.Sprintf(
+                       "%s %s\tHTTP authorization required", req.Method, req.URL.Host,
+               )
                user, pass, err := authDialog(host, resp.Header.Get("WWW-Authenticate"))
                if err != nil {
-                       authCacheM.Unlock()
-                       fifos.SinkErr <- fmt.Sprintf("%s\t%s", req.URL.Host, err.Error())
+                       caches.HTTPAuthCacheM.Unlock()
+                       fifos.LogErr <- fmt.Sprintf("%s\t%s", req.URL.Host, err.Error())
                        http.Error(w, err.Error(), http.StatusInternalServerError)
                        return
                }
-               authCache[req.URL.Host] = [2]string{user, pass}
-               authCacheM.Unlock()
+               caches.HTTPAuthCache[req.URL.Host] = [2]string{user, pass}
+               caches.HTTPAuthCacheM.Unlock()
                req.SetBasicAuth(user, pass)
+               fifos.LogHTTPAuth <- fmt.Sprintf("%s %s\t%s", req.Method, req.URL, user)
                goto Retry
        }
        if unauthorized {
@@ -156,16 +161,15 @@ Retry:
        resp.Body.Close()
        msg := fmt.Sprintf(
                "%s %s\t%s\t%s\t%s\t%s",
-               req.Method,
-               req.URL.String(),
+               req.Method, req.URL,
                resp.Status,
                resp.Header.Get("Content-Type"),
                humanize.IBytes(uint64(n)),
                strings.Join(reqFlags, ","),
        )
        if resp.StatusCode == http.StatusOK {
-               fifos.SinkOK <- msg
+               fifos.LogOK <- msg
        } else {
-               fifos.SinkOther <- msg
+               fifos.LogNonOK <- msg
        }
 }