Skip to content

Commit

Permalink
Isolate the fix from another
Browse files Browse the repository at this point in the history
  • Loading branch information
sakurai-youhei committed Oct 24, 2023
1 parent f8bc962 commit 89e6f4b
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 60 deletions.
4 changes: 2 additions & 2 deletions transport/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func (c *Client) Test(d testing.Driver) {
dialer = TestNetDialer(d, c.config.Timeout)
} else {
dialer = NetDialer(c.config.Timeout)
dialer = TestProxyDialer(d, dialer, c.config.Proxy)
dialer = testProxyDialer(d, dialer, c.config.Proxy)
}
_, err := dialer.Dial("tcp", c.host)
d.Fatal("dial up", err)
Expand All @@ -244,7 +244,7 @@ func (c *Client) Test(d testing.Driver) {
d.Run("TLS", func(d testing.Driver) {
dialer := NetDialer(c.config.Timeout)
if c.config.Proxy.URL != "" {
dialer = TestProxyDialer(d, dialer, c.config.Proxy)
dialer = testProxyDialer(d, dialer, c.config.Proxy)
}
dialer = TestTLSDialer(d, dialer, c.config.TLS, c.config.Timeout)
_, err := dialer.Dial("tcp", c.host)
Expand Down
57 changes: 0 additions & 57 deletions transport/httpcommon/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,10 @@
package httpcommon

import (
"bufio"
"encoding/base64"
"fmt"
"net"
"net/http"
"net/url"

"github.com/elastic/elastic-agent-libs/config"
"github.com/elastic/elastic-agent-libs/transport"
"golang.org/x/net/proxy"
)

// HTTPClientProxySettings provides common HTTP proxy setup support.
Expand Down Expand Up @@ -110,54 +104,3 @@ func (settings *HTTPClientProxySettings) ProxyFunc() func(*http.Request) (*url.U

return http.ProxyURL(settings.URL.URI())
}

// ProxyDialer is a dialer that can be registered to golang.org/x/net/proxy
func (settings *HTTPClientProxySettings) ProxyDialer(_ *url.URL, forward proxy.Dialer) (proxy.Dialer, error) {
return transport.DialerFunc(func(_, address string) (net.Conn, error) {

// Headers given to the CONNECT request
hdr := settings.Headers.Headers()
if settings.URL.User != nil {
username := settings.URL.User.Username()
password, _ := settings.URL.User.Password()
if len(hdr) == 0 {
hdr = http.Header{}
}
hdr.Add("Proxy-Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(username+":"+password)))
}

req := &http.Request{
Method: "CONNECT",
URL: &url.URL{Opaque: address},
Host: address,
Header: hdr,
}

// Dial the proxy host
c, err := forward.Dial("tcp", settings.URL.Host)
if err != nil {
return nil, err
}

// Write the CONNECT request
err = req.Write(c)
if err != nil {
c.Close()
return nil, err
}

res, err := http.ReadResponse(bufio.NewReader(c), req)
if err != nil {
c.Close()
return nil, err
}
res.Body.Close()

if res.StatusCode != http.StatusOK {
c.Close()
return nil, fmt.Errorf("proxy server returned status code %d", res.StatusCode)
}

return c, nil
}), nil
}
2 changes: 1 addition & 1 deletion transport/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func ProxyDialer(log *logp.Logger, config *ProxyConfig, forward Dialer) (Dialer,
}), nil
}

func TestProxyDialer(
func testProxyDialer(
d testing.Driver,
forward Dialer,
config *ProxyConfig,
Expand Down

0 comments on commit 89e6f4b

Please sign in to comment.