Skip to content

Add kubectl over socks5 proxy support #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"net/url"
"strings"

"golang.org/x/net/proxy"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -129,6 +130,18 @@ func (s *SpdyRoundTripper) dial(req *http.Request) (net.Conn, error) {
return s.dialWithoutProxy(req.Context(), req.URL)
}

switch proxyURL.Scheme {
case "socks5":
return s.dialWithSocks5Proxy(req.URL, proxyURL)
case "https", "http":
return s.dialWithHttpProxy(req.URL, proxyURL)
}

return nil, fmt.Errorf("proxy URL scheme not supported: %s", proxyURL.Scheme)
}

// dialWithoutProxy dials the host specified by url through an http or an https proxy.
func (s *SpdyRoundTripper) dialWithHttpProxy(requestUrl *url.URL, proxyURL *url.URL) (net.Conn, error) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name of the function and the name in the comment do not match.

// ensure we use a canonical host with proxyReq
targetHost := netutil.CanonicalAddr(req.URL)

Expand Down Expand Up @@ -157,7 +170,34 @@ func (s *SpdyRoundTripper) dial(req *http.Request) (net.Conn, error) {

rwc, _ := proxyClientConn.Hijack()

if req.URL.Scheme != "https" {
return s.tlsConn(req.URL, rwc, targetHost)
}

// dialWithoutProxy dials the host specified by url through a socks5 proxy.
func (s *SpdyRoundTripper) dialWithSocks5Proxy(requestUrl *url.URL, proxyURL *url.URL) (net.Conn, error) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function name and comment do not match.

// ensure we use a canonical host with proxyReq
targetHost := netutil.CanonicalAddr(requestUrl)

proxyDialAddr := netutil.CanonicalAddr(proxyURL)
proxyDialer, err := proxy.SOCKS5("tcp", proxyDialAddr, nil, proxy.Direct)

proxyConn, err := proxyDialer.Dial("tcp", targetHost)

if err != nil {
return nil, err
}

proxyClientConn := httputil.NewProxyClientConn(proxyConn, nil)

rwc, _ := proxyClientConn.Hijack()

return s.tlsConn(requestUrl, rwc, targetHost)
}

// tlsConn returns a TLS client side connection using rwc as the underlying transport.
func (s *SpdyRoundTripper) tlsConn(requestUrl *url.URL, rwc net.Conn, targetHost string) (net.Conn, error) {

if requestUrl.Scheme != "https" {
return rwc, nil
}

Expand Down