2017-07-02 03:02:24 -04:00
|
|
|
/*
|
|
|
|
*
|
2018-06-08 05:26:10 -04:00
|
|
|
* Copyright 2017 gRPC authors.
|
2017-07-02 03:02:24 -04:00
|
|
|
*
|
2018-06-08 05:26:10 -04:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2017-07-02 03:02:24 -04:00
|
|
|
*
|
2018-06-08 05:26:10 -04:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-07-02 03:02:24 -04:00
|
|
|
*
|
2018-06-08 05:26:10 -04:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2017-07-02 03:02:24 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2021-08-12 11:11:44 -04:00
|
|
|
package transport
|
2017-07-02 03:02:24 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2019-04-12 14:36:17 -04:00
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
2017-07-02 03:02:24 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
2019-04-12 14:36:17 -04:00
|
|
|
const proxyAuthHeaderKey = "Proxy-Authorization"
|
|
|
|
|
2017-07-02 03:02:24 -04:00
|
|
|
var (
|
|
|
|
// The following variable will be overwritten in the tests.
|
|
|
|
httpProxyFromEnvironment = http.ProxyFromEnvironment
|
|
|
|
)
|
|
|
|
|
2022-03-24 08:21:41 -04:00
|
|
|
func mapAddress(address string) (*url.URL, error) {
|
2017-07-02 03:02:24 -04:00
|
|
|
req := &http.Request{
|
|
|
|
URL: &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: address,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
url, err := httpProxyFromEnvironment(req)
|
|
|
|
if err != nil {
|
2019-04-12 14:36:17 -04:00
|
|
|
return nil, err
|
2017-07-02 03:02:24 -04:00
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
return url, nil
|
2017-07-02 03:02:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// To read a response from a net.Conn, http.ReadResponse() takes a bufio.Reader.
|
|
|
|
// It's possible that this reader reads more than what's need for the response and stores
|
|
|
|
// those bytes in the buffer.
|
|
|
|
// bufConn wraps the original net.Conn and the bufio.Reader to make sure we don't lose the
|
|
|
|
// bytes in the buffer.
|
|
|
|
type bufConn struct {
|
|
|
|
net.Conn
|
|
|
|
r io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *bufConn) Read(b []byte) (int, error) {
|
|
|
|
return c.r.Read(b)
|
|
|
|
}
|
|
|
|
|
2019-04-12 14:36:17 -04:00
|
|
|
func basicAuth(username, password string) string {
|
|
|
|
auth := username + ":" + password
|
|
|
|
return base64.StdEncoding.EncodeToString([]byte(auth))
|
|
|
|
}
|
|
|
|
|
2021-08-12 11:11:44 -04:00
|
|
|
func doHTTPConnectHandshake(ctx context.Context, conn net.Conn, backendAddr string, proxyURL *url.URL, grpcUA string) (_ net.Conn, err error) {
|
2017-07-02 03:02:24 -04:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-04-12 14:36:17 -04:00
|
|
|
req := &http.Request{
|
2017-07-02 03:02:24 -04:00
|
|
|
Method: http.MethodConnect,
|
2019-04-12 14:36:17 -04:00
|
|
|
URL: &url.URL{Host: backendAddr},
|
2017-07-02 03:02:24 -04:00
|
|
|
Header: map[string][]string{"User-Agent": {grpcUA}},
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
|
|
|
if t := proxyURL.User; t != nil {
|
|
|
|
u := t.Username()
|
|
|
|
p, _ := t.Password()
|
|
|
|
req.Header.Add(proxyAuthHeaderKey, "Basic "+basicAuth(u, p))
|
|
|
|
}
|
2017-07-02 03:02:24 -04:00
|
|
|
|
|
|
|
if err := sendHTTPRequest(ctx, req, conn); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to write the HTTP request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := bufio.NewReader(conn)
|
|
|
|
resp, err := http.ReadResponse(r, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("reading server HTTP response: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
dump, err := httputil.DumpResponse(resp, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to do connect handshake, status code: %s", resp.Status)
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("failed to do connect handshake, response: %q", dump)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &bufConn{Conn: conn, r: r}, nil
|
|
|
|
}
|
|
|
|
|
2021-08-12 11:11:44 -04:00
|
|
|
// proxyDial dials, connecting to a proxy first if necessary. Checks if a proxy
|
|
|
|
// is necessary, dials, does the HTTP CONNECT handshake, and returns the
|
|
|
|
// connection.
|
|
|
|
func proxyDial(ctx context.Context, addr string, grpcUA string) (conn net.Conn, err error) {
|
|
|
|
newAddr := addr
|
2022-03-24 08:21:41 -04:00
|
|
|
proxyURL, err := mapAddress(addr)
|
2021-08-12 11:11:44 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if proxyURL != nil {
|
|
|
|
newAddr = proxyURL.Host
|
|
|
|
}
|
2017-07-02 03:02:24 -04:00
|
|
|
|
2021-08-12 11:11:44 -04:00
|
|
|
conn, err = (&net.Dialer{}).DialContext(ctx, "tcp", newAddr)
|
|
|
|
if err != nil {
|
2017-07-02 03:02:24 -04:00
|
|
|
return
|
|
|
|
}
|
2021-08-12 11:11:44 -04:00
|
|
|
if proxyURL != nil {
|
|
|
|
// proxy is disabled if proxyURL is nil.
|
|
|
|
conn, err = doHTTPConnectHandshake(ctx, conn, addr, proxyURL, grpcUA)
|
|
|
|
}
|
|
|
|
return
|
2017-07-02 03:02:24 -04:00
|
|
|
}
|
2019-04-12 14:36:17 -04:00
|
|
|
|
|
|
|
func sendHTTPRequest(ctx context.Context, req *http.Request, conn net.Conn) error {
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
if err := req.Write(conn); err != nil {
|
|
|
|
return fmt.Errorf("failed to write the HTTP request: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|