Skip to content
Merged
Show file tree
Hide file tree
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
35 changes: 35 additions & 0 deletions internal/proxy/httpform.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,38 @@ func originForm(data []byte) (hostPort string, out []byte, err error) {
out = append([]byte(b.String()), body...)
return host, out, nil
}

// closeAfterResponse asks the target to close the connection after its
// response. A tunnel through a SOCKS5 proxy or the hosted gateway reaches one
// target, but a client may reuse its connection to the local proxy for a
// plain HTTP request to another host. Once the target closes, the client
// opens a new connection, and so a new tunnel, for its next request. An
// upgrade request (a WebSocket, for example) is left as it is: the upgraded
// connection stays with one target.
func closeAfterResponse(req []byte) []byte {
headEnd := bytes.Index(req, []byte("\r\n\r\n"))
if headEnd == -1 {
return req
}
lines := strings.Split(string(req[:headEnd]), "\r\n")

var b strings.Builder
b.WriteString(lines[0] + "\r\n")
for _, l := range lines[1:] {
name, value, _ := strings.Cut(l, ":")
switch strings.ToLower(strings.TrimSpace(name)) {
case "connection":
for _, token := range strings.Split(value, ",") {
if strings.EqualFold(strings.TrimSpace(token), "upgrade") {
return req
}
}
continue
case "keep-alive":
continue
}
b.WriteString(l + "\r\n")
}
b.WriteString("Connection: close\r\n\r\n")
return append([]byte(b.String()), req[headEnd+4:]...)
}
295 changes: 295 additions & 0 deletions internal/proxy/plain_http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,295 @@
package proxy

import (
"bufio"
"encoding/base64"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"sync"
"testing"

"github.com/shellroute/shellroute-cli/internal/meter"
"github.com/shellroute/shellroute-cli/internal/proxy/proxytest"
)

// tunnelRequest is a request the fake gateway received inside a tunnel.
type tunnelRequest struct {
tunnel string // CONNECT target of the tunnel it arrived on
requestURI string
host string
close bool
proxyAuth string
}

// connectOnlyGateway imitates the hosted gateway: every method except CONNECT
// gets 405. Inside a tunnel it plays the target web server. It answers each
// request with the tunnel's CONNECT target and, like a keep-alive server,
// keeps the connection open unless the request asked to close it.
type connectOnlyGateway struct {
addr string

mu sync.Mutex
connects []string // CONNECT targets, in order
auths []string // Proxy-Authorization of each CONNECT
requests []tunnelRequest
refused int // non-CONNECT requests answered with 405
}

func startConnectOnlyGateway(t *testing.T) *connectOnlyGateway {
t.Helper()
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { ln.Close() })
g := &connectOnlyGateway{addr: ln.Addr().String()}
go func() {
for {
c, err := ln.Accept()
if err != nil {
return
}
go g.handle(c)
}
}()
return g
}

func (g *connectOnlyGateway) handle(c net.Conn) {
defer c.Close()
br := bufio.NewReader(c)
req, err := http.ReadRequest(br)
if err != nil {
return
}
if req.Method != http.MethodConnect {
g.mu.Lock()
g.refused++
g.mu.Unlock()
io.WriteString(c, "HTTP/1.1 405 Method Not Allowed\r\nContent-Length: 0\r\n\r\n")
return
}
g.mu.Lock()
g.connects = append(g.connects, req.Host)
g.auths = append(g.auths, req.Header.Get("Proxy-Authorization"))
g.mu.Unlock()
io.WriteString(c, "HTTP/1.1 200 Connection established\r\n\r\n")

for {
in, err := http.ReadRequest(br)
if err != nil {
return
}
io.Copy(io.Discard, in.Body)
g.mu.Lock()
g.requests = append(g.requests, tunnelRequest{
tunnel: req.Host,
requestURI: in.RequestURI,
host: in.Host,
close: in.Close,
proxyAuth: in.Header.Get("Proxy-Authorization"),
})
g.mu.Unlock()

resp := fmt.Sprintf("HTTP/1.1 200 OK\r\nContent-Length: %d\r\n", len(req.Host))
if in.Close {
resp += "Connection: close\r\n"
}
io.WriteString(c, resp+"\r\n"+req.Host)
if in.Close {
return
}
}
}

func startHostedServer(t *testing.T, gwAddr string) (*Server, string) {
t.Helper()
s := NewServer(&Config{
ListenPort: freePort(t),
Upstream: NewHostedUpstream(gwAddr, false, "test-token"),
Meter: meter.New(),
})
return s, startServer(t, s)
}

func getBody(t *testing.T, client *http.Client, url string) (int, string) {
t.Helper()
resp, err := client.Get(url)
if err != nil {
t.Fatalf("GET %s through local proxy: %v", url, err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
return resp.StatusCode, string(body)
}

// The hosted gateway accepts only CONNECT. A plain HTTP request must reach
// it as a tunnel to port 80 carrying the request in origin form.
func TestServer_HostedPlainHTTPGoesThroughConnect(t *testing.T) {
g := startConnectOnlyGateway(t)
s, addr := startHostedServer(t, g.addr)

status, body := getBody(t, viaLocalProxy(t, addr, nil), "http://example.test/path?q=1")
if status != http.StatusOK || body != "example.test:80" {
t.Fatalf("got %d %q, want 200 from a tunnel to example.test:80", status, body)
}

g.mu.Lock()
defer g.mu.Unlock()
if g.refused != 0 {
t.Errorf("gateway refused %d requests that were not CONNECT", g.refused)
}
wantAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte("_:test-token"))
if len(g.connects) != 1 || g.connects[0] != "example.test:80" || g.auths[0] != wantAuth {
t.Errorf("CONNECTs %v with auth %v, want one to example.test:80 with the session token", g.connects, g.auths)
}
if len(g.requests) != 1 {
t.Fatalf("requests inside tunnels = %d, want 1", len(g.requests))
}
r := g.requests[0]
if r.requestURI != "/path?q=1" || r.host != "example.test" {
t.Errorf("target saw %q with Host %q, want origin form /path?q=1 with Host example.test", r.requestURI, r.host)
}
if !r.close {
t.Error("request did not ask the target to close after its response")
}
if r.proxyAuth != "" {
t.Error("proxy credentials reached the target")
}
if s.BytesTotal() == 0 {
t.Error("relay metered no bytes")
}
}

// A client may reuse its connection to the local proxy for a plain HTTP
// request to another host. Each request must still reach its own host.
func TestServer_HostedPlainHTTPToTwoHostsUsesTwoTunnels(t *testing.T) {
g := startConnectOnlyGateway(t)
_, addr := startHostedServer(t, g.addr)
client := viaLocalProxy(t, addr, nil)

for _, host := range []string{"a.test", "b.test"} {
if _, body := getBody(t, client, "http://"+host+"/"); body != host+":80" {
t.Errorf("request for %s was answered through the tunnel to %q", host, body)
}
}
g.mu.Lock()
defer g.mu.Unlock()
if len(g.connects) != 2 {
t.Errorf("CONNECTs = %v, want one per host", g.connects)
}
}

// A user's own SOCKS5 proxy also tunnels, so the same holds there.
func TestServer_SOCKS5PlainHTTPToTwoHostsUsesTwoTunnels(t *testing.T) {
var urls []string
for _, name := range []string{"a", "b"} {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, name)
}))
t.Cleanup(srv.Close)
urls = append(urls, srv.URL)
}
sp := proxytest.StartSOCKS5(t, "", "")
s := NewServer(&Config{
ListenPort: freePort(t),
Upstream: NewSOCKS5Upstream(sp.Addr, "", "", "socks5"),
Meter: meter.New(),
FailureThreshold: OwnFailureThreshold,
})
addr := startServer(t, s)
client := viaLocalProxy(t, addr, nil)

for i, want := range []string{"a", "b"} {
if _, body := getBody(t, client, urls[i]+"/"); body != want {
t.Errorf("request for server %s was answered by server %q", want, body)
}
}
if n := sp.Connects.Load(); n != 2 {
t.Errorf("SOCKS5 connects = %d, want one per request", n)
}
}

// A user's own HTTP proxy still gets a plain HTTP request as it is: in
// absolute form, with its credentials, and with the client's connection
// handling.
func TestServer_OwnHTTPProxyStillGetsAbsoluteForm(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { ln.Close() })
got := make(chan *http.Request, 1)
go func() {
c, err := ln.Accept()
if err != nil {
return
}
defer c.Close()
req, err := http.ReadRequest(bufio.NewReader(c))
if err != nil {
return
}
got <- req
io.WriteString(c, "HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok")
}()

s := NewServer(&Config{
ListenPort: freePort(t),
Upstream: NewHTTPUpstream(ln.Addr().String(), false, "u", "p", "own"),
Meter: meter.New(),
FailureThreshold: OwnFailureThreshold,
})
addr := startServer(t, s)

if status, body := getBody(t, viaLocalProxy(t, addr, nil), "http://example.test/x"); status != http.StatusOK || body != "ok" {
t.Fatalf("got %d %q, want 200 ok", status, body)
}
req := <-got
if req.Method != http.MethodGet || req.RequestURI != "http://example.test/x" {
t.Errorf("own proxy got %s %s, want GET http://example.test/x", req.Method, req.RequestURI)
}
if want := "Basic " + base64.StdEncoding.EncodeToString([]byte("u:p")); req.Header.Get("Proxy-Authorization") != want {
t.Errorf("own proxy got Proxy-Authorization %q, want %q", req.Header.Get("Proxy-Authorization"), want)
}
if req.Close {
t.Error("request to the own proxy was changed to close the connection")
}
}

func TestCloseAfterResponse(t *testing.T) {
cases := []struct {
name, in, want string
}{
{
"adds the header",
"GET / HTTP/1.1\r\nHost: a\r\n\r\n",
"GET / HTTP/1.1\r\nHost: a\r\nConnection: close\r\n\r\n",
},
{
"replaces keep-alive and keeps the body",
"POST / HTTP/1.1\r\nHost: a\r\nConnection: keep-alive\r\nKeep-Alive: timeout=5\r\nContent-Length: 2\r\n\r\nhi",
"POST / HTTP/1.1\r\nHost: a\r\nContent-Length: 2\r\nConnection: close\r\n\r\nhi",
},
{
"leaves an upgrade request alone",
"GET /ws HTTP/1.1\r\nHost: a\r\nConnection: keep-alive, Upgrade\r\nUpgrade: websocket\r\n\r\n",
"GET /ws HTTP/1.1\r\nHost: a\r\nConnection: keep-alive, Upgrade\r\nUpgrade: websocket\r\n\r\n",
},
{
"leaves an incomplete head alone",
"GET / HTTP/1.1\r\nHost: a",
"GET / HTTP/1.1\r\nHost: a",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := string(closeAfterResponse([]byte(tc.in))); got != tc.want {
t.Errorf("got %q\nwant %q", got, tc.want)
}
})
}
}
6 changes: 5 additions & 1 deletion internal/proxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func (s *Server) handleHTTP(ctx context.Context, clientConn net.Conn, initialDat

var upstreamConn net.Conn
var req []byte
if hp, ok := up.(HTTPProxyUpstream); ok {
if hp, ok := up.(HTTPProxyUpstream); ok && hp.ForwardsPlainHTTP() {
conn, err := hp.DialProxy(ctx)
if err != nil {
s.handleDialError(clientConn, err)
Expand All @@ -449,11 +449,15 @@ func (s *Server) handleHTTP(ctx context.Context, clientConn net.Conn, initialDat
req = []byte(text)
}
} else {
// SOCKS5, or the hosted gateway, which accepts only CONNECT: open a
// tunnel to the target and send the request in origin form, one
// request per tunnel.
target, origin, err := originForm(initialData)
if err != nil {
clientConn.Write([]byte("HTTP/1.1 400 Bad Request\r\n\r\n"))
return
}
origin = closeAfterResponse(origin)
conn, err := up.DialTarget(ctx, target)
if err != nil {
s.handleDialError(clientConn, err)
Expand Down
6 changes: 5 additions & 1 deletion internal/proxy/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@ type Upstream interface {
}

// HTTPProxyUpstream is implemented by upstreams that are HTTP proxies
// themselves and can take a plain HTTP request in absolute form directly.
// themselves.
type HTTPProxyUpstream interface {
Upstream
// DialProxy opens a raw connection to the proxy itself.
DialProxy(ctx context.Context) (net.Conn, error)
// ProxyAuthorization returns the Proxy-Authorization header value, or ""
// when the proxy takes no credentials.
ProxyAuthorization() string
// ForwardsPlainHTTP reports whether the proxy takes a plain HTTP request
// in absolute form directly. The hosted gateway accepts only CONNECT, so
// a plain HTTP request goes through a tunnel to its target instead.
ForwardsPlainHTTP() bool
}

// OwnFailureThreshold is how many consecutive upstream failures mark a
Expand Down
4 changes: 4 additions & 0 deletions internal/proxy/upstream_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ func (u *HTTPUpstream) ProxyAuthorization() string {
return "Basic " + u.auth
}

// ForwardsPlainHTTP is true for a user's own HTTP or HTTPS proxy and false
// for the hosted gateway, which accepts only CONNECT.
func (u *HTTPUpstream) ForwardsPlainHTTP() bool { return !u.hosted }

func (u *HTTPUpstream) setExitIPCallback(f func(string)) { u.onExitIP = f }

// setTLSConfig overrides TLS verification; used by tests with a private CA.
Expand Down
Loading