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
communicator/ssh: Add support SSH over HTTP Proxy #30274
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package ssh | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"time" | ||
|
||
"golang.org/x/net/proxy" | ||
) | ||
|
||
// Dialer implements for SSH over HTTP Proxy. | ||
type proxyDialer struct { | ||
proxy proxyInfo | ||
// forwarding Dialer | ||
forward proxy.Dialer | ||
} | ||
|
||
type proxyInfo struct { | ||
// HTTP Proxy host or host:port | ||
host string | ||
// HTTP Proxy scheme | ||
scheme string | ||
// An immutable encapsulation of username and password details for a URL | ||
userInfo *url.Userinfo | ||
} | ||
|
||
func newProxyInfo(host, scheme, username, password string) *proxyInfo { | ||
p := &proxyInfo{ | ||
host: host, | ||
scheme: scheme, | ||
} | ||
|
||
p.userInfo = url.UserPassword(username, password) | ||
|
||
if p.scheme == "" { | ||
p.scheme = "http" | ||
} | ||
|
||
return p | ||
} | ||
|
||
func (p *proxyInfo) url() *url.URL { | ||
return &url.URL{ | ||
Scheme: p.scheme, | ||
User: p.userInfo, | ||
Host: p.host, | ||
} | ||
} | ||
|
||
func (p *proxyDialer) Dial(network, addr string) (net.Conn, error) { | ||
// Dial the proxy host | ||
c, err := p.forward.Dial(network, p.proxy.host) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = c.SetDeadline(time.Now().Add(15 * time.Second)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Generate request URL to host accessed through the proxy | ||
reqUrl := &url.URL{ | ||
Scheme: "", | ||
Host: addr, | ||
} | ||
|
||
// Create a request object using the CONNECT method to instruct the proxy server to tunnel a protocol other than HTTP. | ||
req, err := http.NewRequest("CONNECT", reqUrl.String(), nil) | ||
if err != nil { | ||
c.Close() | ||
return nil, err | ||
} | ||
|
||
// If http proxy requires authentication, configure settings for basic authentication. | ||
if p.proxy.userInfo.String() != "" { | ||
username := p.proxy.userInfo.Username() | ||
password, _ := p.proxy.userInfo.Password() | ||
req.SetBasicAuth(username, password) | ||
req.Header.Add("Proxy-Authorization", req.Header.Get("Authorization")) | ||
} | ||
|
||
// Do not close the connection after sending this request and reading its response. | ||
req.Close = false | ||
|
||
// Writes the request in the form expected by an HTTP proxy. | ||
err = req.Write(c) | ||
if err != nil { | ||
c.Close() | ||
return nil, err | ||
} | ||
|
||
res, err := http.ReadResponse(bufio.NewReader(c), req) | ||
|
||
if err != nil { | ||
res.Body.Close() | ||
c.Close() | ||
return nil, err | ||
} | ||
|
||
res.Body.Close() | ||
|
||
if res.StatusCode != http.StatusOK { | ||
c.Close() | ||
return nil, fmt.Errorf("Connection Error: StatusCode: %d", res.StatusCode) | ||
} | ||
|
||
return c, nil | ||
} | ||
|
||
// NewHttpProxyDialer generate Http Proxy Dialer | ||
func newHttpProxyDialer(u *url.URL, forward proxy.Dialer) (proxy.Dialer, error) { | ||
var proxyUserName, proxyPassword string | ||
if u.User != nil { | ||
proxyUserName = u.User.Username() | ||
proxyPassword, _ = u.User.Password() | ||
} | ||
|
||
pd := &proxyDialer{ | ||
proxy: *newProxyInfo(u.Host, u.Scheme, proxyUserName, proxyPassword), | ||
forward: forward, | ||
} | ||
|
||
return pd, nil | ||
} | ||
|
||
// RegisterDialerType register schemes used by `proxy.FromURL` | ||
func RegisterDialerType() { | ||
proxy.RegisterDialerType("http", newHttpProxyDialer) | ||
proxy.RegisterDialerType("https", newHttpProxyDialer) | ||
} | ||
|
||
// NewHttpProxyConn create a connection to connect through the proxy server. | ||
func newHttpProxyConn(p *proxyInfo, targetAddr string) (net.Conn, error) { | ||
pd, err := proxy.FromURL(p.url(), proxy.Direct) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
proxyConn, err := pd.Dial("tcp", targetAddr) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return proxyConn, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Upon further reading, I think we could omit this new type, and use a
*url.URL
representing the proxy connection instead. While I haven't tried it, so I might be missing something, I think this would be simpler.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed