Skip to content

Commit

Permalink
Merge branch 'new2'
Browse files Browse the repository at this point in the history
merging with elazarl#256
  • Loading branch information
ppmag committed Dec 28, 2019
2 parents c5c05da + b353f55 commit 2a538f3
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
73 changes: 73 additions & 0 deletions fd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package goproxy_test

// build +linux

import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"os"
"strings"
"testing"

"github.com/elazarl/goproxy"
)

func oneShotProxyNoKeepalive(proxy *goproxy.ProxyHttpServer, t *testing.T) (client *http.Client, s *httptest.Server) {
s = httptest.NewServer(proxy)

proxyUrl, _ := url.Parse(s.URL)
tr := &http.Transport{TLSClientConfig: acceptAllCerts, Proxy: http.ProxyURL(proxyUrl), DisableKeepAlives: true}
client = &http.Client{Transport: tr}
return
}

func printfds(msg string, t *testing.T) int {
fd, _ := os.Open("/proc/self/fd")
fds, _ := fd.Readdir(-1)
fd.Close()
names := []string{}
links := []string{}
for _, f := range fds {
names = append(names, f.Name())
link, _ := os.Readlink("/proc/self/fd/" + f.Name())
links = append(links, link)
}
lines := []string{}
for i := range names {
lines = append(lines, fmt.Sprintf("%2v → %v", names[i], links[i]))
}
t.Logf("[%s] /proc/self/fd:\n\t%s", msg, strings.Join(lines, "\n\t"))
return len(fds)
}
func TestFDCountConnect(t *testing.T) {

proxy := goproxy.NewProxyHttpServer()
althttps := httptest.NewTLSServer(ConstantHanlder("althttps"))

proxy.OnRequest().HandleConnectFunc(func(host string, ctx *goproxy.ProxyCtx) (*goproxy.ConnectAction, string) {
u, _ := url.Parse(althttps.URL)
printfds("in handler", t)
return goproxy.OkConnect, u.Host
})

before := printfds("before", t)

for i := range "12345" {
pre := fmt.Sprintf("call %d", i+1)
printfds(pre+", before", t)
client, l := oneShotProxyNoKeepalive(proxy, t)
if resp := string(getOrFail(https.URL+"/alturl", client, t)); resp != "althttps" {
t.Error("Proxy should redirect CONNECT requests to local althttps server, expected 'althttps' got ", resp)
}
l.Close()
printfds(pre+", after", t)
}

after := printfds("after", t)

if before != after {
t.Errorf("#FD before ≠ after! FD before: %d, after: %d", before, after)
}
}
15 changes: 12 additions & 3 deletions https.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,16 @@ func (proxy *ProxyHttpServer) handleHttps(w http.ResponseWriter, r *http.Request
targetTCP, targetOK := targetSiteCon.(*net.TCPConn)
proxyClientTCP, clientOK := proxyClient.(*net.TCPConn)
if targetOK && clientOK {
go copyAndClose(ctx, targetTCP, proxyClientTCP)
go copyAndClose(ctx, proxyClientTCP, targetTCP)
go func() {
var wg sync.WaitGroup
wg.Add(2)
go copyAndClose(ctx, targetTCP, proxyClientTCP, &wg)
go copyAndClose(ctx, proxyClientTCP, targetTCP, &wg)
wg.Wait()
proxyClientTCP.Close()
targetTCP.Close()

}()
} else {
go func() {
var wg sync.WaitGroup
Expand Down Expand Up @@ -303,13 +311,14 @@ func copyOrWarn(ctx *ProxyCtx, dst io.Writer, src io.Reader, wg *sync.WaitGroup)
wg.Done()
}

func copyAndClose(ctx *ProxyCtx, dst, src *net.TCPConn) {
func copyAndClose(ctx *ProxyCtx, dst, src *net.TCPConn, wg *sync.WaitGroup) {
if _, err := io.Copy(dst, src); err != nil {
ctx.Warnf("Error copying to client: %s", err)
}

dst.CloseWrite()
src.CloseRead()
wg.Done()
}

func dialerFromEnv(proxy *ProxyHttpServer) func(network, addr string) (net.Conn, error) {
Expand Down

0 comments on commit 2a538f3

Please sign in to comment.