34 lines
811 B
Go
34 lines
811 B
Go
|
|
package deconnector
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bufio"
|
||
|
|
"fmt"
|
||
|
|
"log"
|
||
|
|
"net"
|
||
|
|
"net/http"
|
||
|
|
"net/url"
|
||
|
|
)
|
||
|
|
|
||
|
|
func (d *Deconnector) handleDeconnect(clientConn net.Conn, connectReq *http.Request, upstreamURL *url.URL) {
|
||
|
|
// Tell client the tunnel is open
|
||
|
|
fmt.Fprintf(clientConn, "HTTP/1.1 200 Connection established\r\n\r\n")
|
||
|
|
|
||
|
|
// Read the real HTTP request the client sends through the tunnel
|
||
|
|
innerReq, err := http.ReadRequest(bufio.NewReader(clientConn))
|
||
|
|
if err != nil {
|
||
|
|
log.Printf("failed to read inner request after CONNECT:80: %v", err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
innerReq.URL.Scheme = "http"
|
||
|
|
innerReq.URL.Host = connectReq.Host
|
||
|
|
innerReq.RequestURI = ""
|
||
|
|
|
||
|
|
d.app.Logger().
|
||
|
|
WithField("method", innerReq.Method).
|
||
|
|
WithField("url", innerReq.URL).
|
||
|
|
Info("Handling de-CONNECT request")
|
||
|
|
|
||
|
|
d.forwardHTTP(clientConn, innerReq, upstreamURL)
|
||
|
|
}
|