more proxy debugging

This commit is contained in:
Michael Quigley 2022-07-21 15:26:44 -04:00
parent 7e39a99ade
commit fe7e3acbca
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
3 changed files with 57 additions and 10 deletions

View File

@ -2,6 +2,7 @@ package http
import ( import (
"fmt" "fmt"
"github.com/openziti-test-kitchen/zrok/util"
"github.com/openziti/sdk-golang/ziti" "github.com/openziti/sdk-golang/ziti"
"github.com/openziti/sdk-golang/ziti/config" "github.com/openziti/sdk-golang/ziti/config"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -35,24 +36,33 @@ func Run(cfg *Config) error {
type handler struct{} type handler struct{}
func (self *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (self *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
logrus.Infof("handling request from [%v]", r.RemoteAddr) logrus.Warnf("handling request from [%v]", r.RemoteAddr)
r.Host = "localhost:3000" r.Host = "localhost:3000"
r.URL.Host = "localhost:3000" r.URL.Host = "localhost:3000"
r.URL.Scheme = "http" r.URL.Scheme = "http"
r.RequestURI = "" r.RequestURI = ""
logrus.Info(util.DumpHeaders(r.Header, true))
logrus.Infof("forwarding to: %v [%v]", r.Method, r.URL)
rr, err := http.DefaultClient.Do(r) rr, err := http.DefaultClient.Do(r)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
_, _ = fmt.Fprint(w, err) _, _ = fmt.Fprint(w, err)
return return
} }
w.WriteHeader(rr.StatusCode)
logrus.Infof("response: %v", rr.Status)
// forward headers
for k, v := range rr.Header { for k, v := range rr.Header {
w.Header().Add(k, v[0]) for _, vi := range v {
w.Header().Add(k, vi)
} }
}
logrus.Info(util.DumpHeaders(w.Header(), false))
// copy body
n, err := io.Copy(w, rr.Body) n, err := io.Copy(w, rr.Body)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)

View File

@ -31,29 +31,37 @@ type handler struct {
} }
func (self *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (self *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
logrus.Infof("handling request from [%v]", r.RemoteAddr) logrus.Warnf("handling request from [%v]", r.RemoteAddr)
zDialCtx := util.ZitiDialContext{Context: self.zCtx}
zTransport := http.DefaultTransport.(*http.Transport).Clone()
zTransport.DialContext = zDialCtx.Dial
client := &http.Client{Transport: zTransport}
r.Host = "zrok" r.Host = "zrok"
r.URL.Host = "zrok" r.URL.Host = "zrok"
r.URL.Scheme = "http" r.URL.Scheme = "http"
r.RequestURI = "" r.RequestURI = ""
logrus.Warnf("request: %v", r) logrus.Info(util.DumpHeaders(r.Header, true))
rr, err := client.Do(r) logrus.Infof("forwarding to: %v [%v]", r.Method, r.URL)
zDialCtx := util.ZitiDialContext{Context: self.zCtx}
zTransport := http.DefaultTransport.(*http.Transport).Clone()
zTransport.DialContext = zDialCtx.Dial
zClient := &http.Client{Transport: zTransport}
rr, err := zClient.Do(r)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
_, _ = fmt.Fprint(w, err) _, _ = fmt.Fprint(w, err)
return return
} }
w.WriteHeader(rr.StatusCode)
logrus.Infof("response: %v", rr.Status)
// forward headers
for k, v := range rr.Header { for k, v := range rr.Header {
w.Header().Add(k, v[0]) for _, vi := range v {
w.Header().Add(k, vi)
} }
}
logrus.Info(util.DumpHeaders(w.Header(), false))
// copy body
n, err := io.Copy(w, rr.Body) n, err := io.Copy(w, rr.Body)
if err != nil { if err != nil {
panic(err) panic(err)

29
util/headers.go Normal file
View File

@ -0,0 +1,29 @@
package util
import (
"fmt"
"net/http"
"sort"
)
func DumpHeaders(headers http.Header, in bool) string {
out := "headers {\n"
keys := make([]string, len(headers))
i := 0
for k, _ := range headers {
keys[i] = k
i++
}
sort.Strings(keys)
for _, k := range keys {
for _, v := range headers[k] {
indicator := "->"
if !in {
indicator = "<-"
}
out += fmt.Sprintf("\t%v %v: %v\n", indicator, k, v)
}
}
out += "}"
return out
}