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

Implement correct half-close sequence for the connections. #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 16 additions & 5 deletions tcpproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,6 @@ func UnderlyingConn(c net.Conn) net.Conn {
return c
}

func goCloseConn(c net.Conn) { go c.Close() }

// HandleConn implements the Target interface.
func (dp *DialProxy) HandleConn(src net.Conn) {
ctx := context.Background()
Expand All @@ -364,13 +362,13 @@ func (dp *DialProxy) HandleConn(src net.Conn) {
dp.onDialError()(src, err)
return
}
defer goCloseConn(dst)
defer dst.Close()

if err = dp.sendProxyHeader(dst, src); err != nil {
dp.onDialError()(src, err)
return
}
defer goCloseConn(src)
defer src.Close()

if ka := dp.keepAlivePeriod(); ka > 0 {
if c, ok := UnderlyingConn(src).(*net.TCPConn); ok {
Expand All @@ -386,7 +384,12 @@ func (dp *DialProxy) HandleConn(src net.Conn) {
errc := make(chan error, 1)
go proxyCopy(errc, src, dst)
go proxyCopy(errc, dst, src)
<-errc

for i := 0; i < 2; i++ {
if err = <-errc; err != nil {
return
}
}
}

func (dp *DialProxy) sendProxyHeader(w io.Writer, src net.Conn) error {
Expand Down Expand Up @@ -437,6 +440,14 @@ func proxyCopy(errc chan<- error, dst, src net.Conn) {
dst = UnderlyingConn(dst)

_, err := io.Copy(dst, src)

if tcpConn, ok := dst.(*net.TCPConn); ok {
tcpConn.CloseWrite()
}
if tcpConn, ok := src.(*net.TCPConn); ok {
tcpConn.CloseRead()
}

errc <- err
}

Expand Down
2 changes: 1 addition & 1 deletion tcpproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ func (t *tlsServer) Close() {
// cert creates a well-formed, but completely insecure self-signed
// cert for domain.
func cert(t *testing.T, domain string) tls.Certificate {
private, err := rsa.GenerateKey(rand.Reader, 512)
private, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatal(err)
}
Expand Down