1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-11-30 08:06:40 +02:00
go-micro/transport/http_proxy.go

118 lines
2.2 KiB
Go
Raw Normal View History

2019-01-02 17:24:17 +02:00
package transport
import (
"bufio"
"encoding/base64"
"fmt"
"io"
"net"
"net/http"
"net/http/httputil"
"net/url"
)
const (
proxyAuthHeader = "Proxy-Authorization"
)
func getURL(addr string) (*url.URL, error) {
r := &http.Request{
URL: &url.URL{
Scheme: "https",
Host: addr,
},
}
2022-09-30 20:32:55 +02:00
2019-01-02 17:24:17 +02:00
return http.ProxyFromEnvironment(r)
}
type pbuffer struct {
net.Conn
r io.Reader
}
func (p *pbuffer) Read(b []byte) (int, error) {
return p.r.Read(b)
}
func proxyDial(conn net.Conn, addr string, proxyURL *url.URL) (_ net.Conn, err error) {
defer func() {
if err != nil {
2022-09-30 20:32:55 +02:00
// trunk-ignore(golangci-lint/errcheck)
2019-01-02 17:24:17 +02:00
conn.Close()
}
}()
r := &http.Request{
Method: http.MethodConnect,
URL: &url.URL{Host: addr},
Header: map[string][]string{"User-Agent": {"micro/latest"}},
}
if user := proxyURL.User; user != nil {
u := user.Username()
p, _ := user.Password()
auth := []byte(u + ":" + p)
basicAuth := base64.StdEncoding.EncodeToString(auth)
r.Header.Add(proxyAuthHeader, "Basic "+basicAuth)
}
if err := r.Write(conn); err != nil {
2022-09-30 20:32:55 +02:00
return nil, fmt.Errorf("failed to write the HTTP request: %w", err)
2019-01-02 17:24:17 +02:00
}
br := bufio.NewReader(conn)
2022-09-30 20:32:55 +02:00
2019-01-02 17:24:17 +02:00
rsp, err := http.ReadResponse(br, r)
if err != nil {
2022-09-30 20:32:55 +02:00
return nil, fmt.Errorf("reading server HTTP response: %w", err)
2019-01-02 17:24:17 +02:00
}
2022-09-30 20:32:55 +02:00
defer func() {
err = rsp.Body.Close()
}()
2019-01-02 17:24:17 +02:00
if rsp.StatusCode != http.StatusOK {
dump, err := httputil.DumpResponse(rsp, true)
if err != nil {
return nil, fmt.Errorf("failed to do connect handshake, status code: %s", rsp.Status)
}
2022-09-30 20:32:55 +02:00
2019-01-02 17:24:17 +02:00
return nil, fmt.Errorf("failed to do connect handshake, response: %q", dump)
}
return &pbuffer{Conn: conn, r: br}, nil
}
2022-09-30 16:27:07 +02:00
// Creates a new connection.
2019-01-02 17:24:17 +02:00
func newConn(dial func(string) (net.Conn, error)) func(string) (net.Conn, error) {
return func(addr string) (net.Conn, error) {
// get the proxy url
proxyURL, err := getURL(addr)
if err != nil {
return nil, err
}
// set to addr
callAddr := addr
// got proxy
if proxyURL != nil {
callAddr = proxyURL.Host
}
// dial the addr
c, err := dial(callAddr)
if err != nil {
return nil, err
}
// do proxy connect if we have proxy url
if proxyURL != nil {
c, err = proxyDial(c, addr, proxyURL)
}
return c, err
}
}