mirror of
https://github.com/hanzoai/oxy.git
synced 2026-09-30 23:42:31 +00:00
Merge pull request #6 from mailgun/alexander/tls
Preserve TLS info and log it
This commit is contained in:
+11
-2
@@ -91,8 +91,17 @@ func (f *Forwarder) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
f.log.Infof("Got response from %v, code: %v, duration: %v",
|
||||
req.URL, response.StatusCode, time.Now().UTC().Sub(start))
|
||||
if req.TLS != nil {
|
||||
f.log.Infof("Round trip: %v, code: %v, duration: %v tls:version: %x, tls:resume:%t, tls:csuite:%x, tls:server:%v",
|
||||
req.URL, response.StatusCode, time.Now().UTC().Sub(start),
|
||||
req.TLS.Version,
|
||||
req.TLS.DidResume,
|
||||
req.TLS.CipherSuite,
|
||||
req.TLS.ServerName)
|
||||
} else {
|
||||
f.log.Infof("Round trip: %v, code: %v, duration: %v",
|
||||
req.URL, response.StatusCode, time.Now().UTC().Sub(start))
|
||||
}
|
||||
|
||||
utils.CopyHeaders(w.Header(), response.Header)
|
||||
w.WriteHeader(response.StatusCode)
|
||||
|
||||
+6
-1
@@ -233,7 +233,10 @@ func (s *FwdSuite) TestForwardedProto(c *C) {
|
||||
})
|
||||
defer srv.Close()
|
||||
|
||||
f, err := New()
|
||||
buf := &bytes.Buffer{}
|
||||
l := utils.NewFileLogger(buf, utils.INFO)
|
||||
|
||||
f, err := New(Logger(l))
|
||||
c.Assert(err, IsNil)
|
||||
|
||||
proxy := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||
@@ -248,6 +251,8 @@ func (s *FwdSuite) TestForwardedProto(c *C) {
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(re.StatusCode, Equals, http.StatusOK)
|
||||
c.Assert(proto, Equals, "https")
|
||||
|
||||
c.Assert(strings.Contains(buf.String(), "tls"), Equals, true)
|
||||
}
|
||||
|
||||
func (s *FwdSuite) TestChunkedResponseConversion(c *C) {
|
||||
|
||||
@@ -2,6 +2,7 @@ package stream
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
@@ -294,3 +295,38 @@ func (s *STSuite) TestNoBody(c *C) {
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(re.StatusCode, Equals, http.StatusOK)
|
||||
}
|
||||
|
||||
// Make sure that stream handler preserves TLS settings
|
||||
func (s *STSuite) TestPreservesTLS(c *C) {
|
||||
srv := testutils.NewHandler(func(w http.ResponseWriter, req *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("ok"))
|
||||
})
|
||||
defer srv.Close()
|
||||
|
||||
// forwarder will proxy the request to whatever destination
|
||||
fwd, err := forward.New()
|
||||
c.Assert(err, IsNil)
|
||||
|
||||
var t *tls.ConnectionState
|
||||
// this is our redirect to server
|
||||
rdr := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||
t = req.TLS
|
||||
req.URL = testutils.ParseURI(srv.URL)
|
||||
fwd.ServeHTTP(w, req)
|
||||
})
|
||||
|
||||
// stream handler will forward requests to redirect
|
||||
st, err := New(rdr, Logger(utils.NewFileLogger(os.Stdout, utils.INFO)))
|
||||
c.Assert(err, IsNil)
|
||||
|
||||
proxy := httptest.NewUnstartedServer(st)
|
||||
proxy.StartTLS()
|
||||
defer proxy.Close()
|
||||
|
||||
re, _, err := testutils.Get(proxy.URL)
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(re.StatusCode, Equals, http.StatusOK)
|
||||
|
||||
c.Assert(t, NotNil)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user