generated from openziti/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 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
Add support for http connect proxying to tls connections. Fixes #54 #55
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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,110 @@ | ||
/* | ||
Copyright NetFoundry Inc. | ||
|
||
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 | ||
|
||
https://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
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. | ||
*/ | ||
|
||
package proxies | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"github.com/michaelquigley/pfxlog" | ||
"github.com/pkg/errors" | ||
"golang.org/x/net/proxy" | ||
"io" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"time" | ||
) | ||
|
||
func NewHttpConnectProxyDialer(addr string, auth *proxy.Auth, timeout time.Duration) *HttpConnectProxyDialer { | ||
return &HttpConnectProxyDialer{ | ||
address: addr, | ||
auth: auth, | ||
timeout: timeout, | ||
} | ||
} | ||
|
||
type HttpConnectProxyDialer struct { | ||
address string | ||
auth *proxy.Auth | ||
timeout time.Duration | ||
} | ||
|
||
func (self *HttpConnectProxyDialer) Dial(network, addr string) (net.Conn, error) { | ||
c, err := net.Dial(network, self.address) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "unable to connect to proxy server at %s", self.address) | ||
} | ||
|
||
if err = self.Connect(c, addr); err != nil { | ||
if closeErr := c.Close(); closeErr != nil { | ||
pfxlog.Logger().WithError(closeErr).Error("failed to close connection to proxy after connect error") | ||
} | ||
return nil, err | ||
} | ||
|
||
return c, nil | ||
} | ||
|
||
func (self *HttpConnectProxyDialer) Connect(c net.Conn, addr string) error { | ||
log := pfxlog.Logger() | ||
|
||
log.Infof("create connect request to %s", addr) | ||
|
||
ctx := context.Background() | ||
if self.timeout > 0 { | ||
timeoutCtx, cancelF := context.WithTimeout(ctx, self.timeout) | ||
defer cancelF() | ||
ctx = timeoutCtx | ||
} | ||
|
||
req := &http.Request{ | ||
Method: http.MethodConnect, | ||
URL: &url.URL{Host: addr}, | ||
Host: addr, | ||
Header: http.Header{}, | ||
Close: false, | ||
} | ||
req = req.WithContext(ctx) | ||
if self.auth != nil { | ||
req.SetBasicAuth(self.auth.User, self.auth.Password) | ||
} | ||
req.Header.Set("User-Agent", "ziti-transport") | ||
|
||
log.Info("writing request to wire") | ||
if err := req.Write(c); err != nil { | ||
return errors.Wrapf(err, "unable to send connect request to proxy server at %s", self.address) | ||
} | ||
|
||
log.Info("reading response from wire") | ||
resp, err := http.ReadResponse(bufio.NewReader(c), req) | ||
if err != nil { | ||
return errors.Wrapf(err, "unable to read response to connect request to proxy server at %s", self.address) | ||
} | ||
|
||
defer func() { | ||
log.Info("closing resp body") | ||
_ = resp.Body.Close() | ||
}() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
respBody, _ := io.ReadAll(resp.Body) | ||
log.Errorf("proxy returned: %s", string(respBody)) | ||
return errors.Errorf("received %v instead of 200 OK in response to connect request to proxy server at %s", resp.StatusCode, self.address) | ||
} | ||
|
||
return nil | ||
} |
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.
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.
This assumes the proxy server is not TLS, correct? I believe that if the HTTP proxy is using TLS this will fail to connect properly.
It looks like the
net.Conn
from here is expected to be used in.Connect()
, which sends credentials. If HTTP is only supported, those creds are sent in the clear.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.
One way to handle TLS vs non-TLS HTTP proxies is to require the protocol prefix on the address and inspect it:
https://proxyhost
vshttp://proxyhost
and then do the right thing. However I don't know how well that pattern meshes with the transport codebase.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.
Yeah, this is just to get the bare minimum working. If/when we get more feature requests we can look at how to expand this out