Skip to content
This repository has been archived by the owner on Dec 7, 2020. It is now read-only.

Commit

Permalink
HTTP Hijack Check
Browse files Browse the repository at this point in the history
Ensure the http.Hijack method is implemented by the http.ResponseWriter before attempting to grab the client connection
  • Loading branch information
gambol99 committed Mar 2, 2018
1 parent 8a0b35e commit fdec993
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,32 +261,34 @@ func transferBytes(src io.Reader, dest io.Writer, wg *sync.WaitGroup) (int64, er
// tryUpdateConnection attempt to upgrade the connection to a http pdy stream
func tryUpdateConnection(req *http.Request, writer http.ResponseWriter, endpoint *url.URL) error {
// step: dial the endpoint
tlsConn, err := tryDialEndpoint(endpoint)
server, err := tryDialEndpoint(endpoint)
if err != nil {
return err
}
defer tlsConn.Close()
defer server.Close()

// step: we need to hijack the underlining client connection
clientConn, ok, err := writer.(http.Hijacker).Hijack()
if !ok {
// @check the the response writer implements the Hijack method
if _, ok := writer.(http.Hijacker); !ok {
return errors.New("writer does not implement http.Hijacker method")
}

// @step: get the client connection object
client, _, err := writer.(http.Hijacker).Hijack()
if err != nil {
return err
}
defer clientConn.Close()
defer client.Close()

// step: write the request to upstream
if err = req.Write(tlsConn); err != nil {
if err = req.Write(server); err != nil {
return err
}

// step: copy the date between client and upstream endpoint
// @step: copy the data between client and upstream endpoint
var wg sync.WaitGroup
wg.Add(2)
go transferBytes(tlsConn, clientConn, &wg)
go transferBytes(clientConn, tlsConn, &wg)
go transferBytes(server, client, &wg)
go transferBytes(client, server, &wg)
wg.Wait()

return nil
Expand Down

0 comments on commit fdec993

Please sign in to comment.