Skip to content
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

fix: use transport for regular HTTP proxying #79

Merged
merged 2 commits into from
Sep 28, 2023
Merged
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
18 changes: 14 additions & 4 deletions x/httpproxy/connect_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
package httpproxy

import (
"context"
"fmt"
"io"
"net"
"net/http"
"strings"

"github.com/Jigsaw-Code/outline-sdk/transport"
)
Expand All @@ -35,11 +38,12 @@ func (h *handler) ServeHTTP(proxyResp http.ResponseWriter, proxyReq *http.Reques
if proxyReq.Method == http.MethodConnect {
h.handleConnect(proxyResp, proxyReq)
return
} else if proxyReq.URL.Host != "" {
}
if proxyReq.URL.Host != "" {
h.handleHTTPProxyRequest(proxyResp, proxyReq)
} else {
http.Error(proxyResp, "Not Found", http.StatusNotFound)
return
}
http.Error(proxyResp, "Not Found", http.StatusNotFound)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit it's a little bit weird that the normal path resulted in an error, maybe "early return" the Not Found first.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem is that we have two normal paths and one error, which is a fallback from the other two, so we can't do it first.

}

func (h *handler) handleHTTPProxyRequest(proxyResp http.ResponseWriter, proxyReq *http.Request) {
Expand Down Expand Up @@ -124,5 +128,11 @@ func (h *handler) handleConnect(proxyResp http.ResponseWriter, proxyReq *http.Re
// The resulting handler is currently vulnerable to probing attacks. It's ok as a localhost proxy
// but it may be vulnerable if used as a public proxy.
func NewConnectHandler(dialer transport.StreamDialer) http.Handler {
return &handler{dialer, *http.DefaultClient}
dialContext := func(ctx context.Context, network, addr string) (net.Conn, error) {
if !strings.HasPrefix(network, "tcp") {
return nil, fmt.Errorf("protocol not supported: %v", network)
}
return dialer.Dial(ctx, addr)
}
return &handler{dialer, http.Client{Transport: &http.Transport{DialContext: dialContext}}}
}