package transport
import (
)
const proxyAuthHeaderKey = "Proxy-Authorization"
var (
httpProxyFromEnvironment = http.ProxyFromEnvironment
)
func ( string) (*url.URL, error) {
:= &http.Request{
URL: &url.URL{
Scheme: "https",
Host: ,
},
}
, := httpProxyFromEnvironment()
if != nil {
return nil,
}
return , nil
}
type bufConn struct {
net.Conn
r io.Reader
}
func ( *bufConn) ( []byte) (int, error) {
return .r.Read()
}
func (, string) string {
:= + ":" +
return base64.StdEncoding.EncodeToString([]byte())
}
func ( context.Context, net.Conn, string, *url.URL, string) ( net.Conn, error) {
defer func() {
if != nil {
.Close()
}
}()
:= &http.Request{
Method: http.MethodConnect,
URL: &url.URL{Host: },
Header: map[string][]string{"User-Agent": {}},
}
if := .User; != nil {
:= .Username()
, := .Password()
.Header.Add(proxyAuthHeaderKey, "Basic "+basicAuth(, ))
}
if := sendHTTPRequest(, , ); != nil {
return nil, fmt.Errorf("failed to write the HTTP request: %v", )
}
:= bufio.NewReader()
, := http.ReadResponse(, )
if != nil {
return nil, fmt.Errorf("reading server HTTP response: %v", )
}
defer .Body.Close()
if .StatusCode != http.StatusOK {
, := httputil.DumpResponse(, true)
if != nil {
return nil, fmt.Errorf("failed to do connect handshake, status code: %s", .Status)
}
return nil, fmt.Errorf("failed to do connect handshake, response: %q", )
}
return &bufConn{Conn: , r: }, nil
}
func ( context.Context, string, string) ( net.Conn, error) {
:=
, := mapAddress()
if != nil {
return nil,
}
if != nil {
= .Host
}
, = (&net.Dialer{}).DialContext(, "tcp", )
if != nil {
return
}
if != nil {
, = doHTTPConnectHandshake(, , , , )
}
return
}
func ( context.Context, *http.Request, net.Conn) error {
= .WithContext()
if := .Write(); != nil {
return fmt.Errorf("failed to write the HTTP request: %v", )
}
return nil
}