Skip to content

Commit

Permalink
Merge pull request #197 from NodeFactoryIo/hotfix/tunnel-fix
Browse files Browse the repository at this point in the history
Tunnel connections not closing fix
  • Loading branch information
mpetrunic authored Feb 23, 2021
2 parents 779007c + 3c306ab commit bbeee18
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
14 changes: 12 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
# Changelog

## [v0.5.3]((https://github.com/NodeFactoryIo/vedran/tree/HEAD))
[Full Changelog](https://github.com/NodeFactoryIo/vedran/compare/v0.5.2...HEAD)
## [unreleased]((https://github.com/NodeFactoryIo/vedran/tree/HEAD))
[Full Changelog](https://github.com/NodeFactoryIo/vedran/compare/v0.5.3...HEAD)

### Added

### Fix
- Fix tunnel tcp connections not closing after requests finish [\#197](https://github.com/NodeFactoryIo/vedran/pull/197) ([mpetrun5](https://github.com/mpetrun5))

### Changed

## [v0.5.3]((https://github.com/NodeFactoryIo/vedran/tree/v0.5.3))
[Full Changelog](https://github.com/NodeFactoryIo/vedran/compare/v0.5.2...v0.5.3)

### Added

Expand Down
15 changes: 8 additions & 7 deletions pkg/http-tunnel/server/tcpproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ package server

import (
"fmt"
"github.com/NodeFactoryIo/vedran/pkg/http-tunnel"
"github.com/NodeFactoryIo/vedran/pkg/http-tunnel/proto"
log "github.com/sirupsen/logrus"
"io"
"net"

tunnel "github.com/NodeFactoryIo/vedran/pkg/http-tunnel"
"github.com/NodeFactoryIo/vedran/pkg/http-tunnel/proto"
log "github.com/sirupsen/logrus"
)

// TCPProxy forwards TCP streams.
Expand Down Expand Up @@ -80,22 +81,19 @@ func (p *TCPProxy) Proxy(w io.Writer, r io.ReadCloser, msg *proto.ControlMessage
}).Error("dial failed", err)
return
}
defer local.Close()

if err := tunnel.KeepAlive(local); err != nil {
clogger.WithFields(log.Fields{
"target": target,
}).Error("TCP keepalive for tunneled connection failed", err)
}

done := make(chan struct{})
go func() {
loggerWithContext := log.WithContext(p.logger.Context).WithFields(log.Fields{
"dst": msg.ForwardedHost,
"src": target,
})
transfer(flushWriter{w}, local, loggerWithContext)
close(done)
}()

loggerWithContext := log.WithContext(p.logger.Context).WithFields(log.Fields{
Expand All @@ -104,7 +102,10 @@ func (p *TCPProxy) Proxy(w io.Writer, r io.ReadCloser, msg *proto.ControlMessage
})
transfer(local, r, loggerWithContext)

<-done
err = local.Close()
if err != nil {
clogger.Errorf("Transfer close failed because of %v", err)
}
}

func (p *TCPProxy) localAddrFor(hostPort string) string {
Expand Down
12 changes: 3 additions & 9 deletions pkg/http-tunnel/server/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,16 @@
package server

import (
log "github.com/sirupsen/logrus"
"io"
"net"
"net/http"
"strings"

log "github.com/sirupsen/logrus"
)

func transfer(dst io.Writer, src io.Reader, logger *log.Entry) {
n, err := io.Copy(dst, src)
if err != nil {
if (!strings.Contains(err.Error(), "context canceled") &&
!strings.Contains(err.Error(), "CANCEL")) &&
!strings.Contains(err.Error(), "stream closed") {
logger.Error("copy error ", err)
}
}
n, _ := io.Copy(dst, src)
log.Debugf("transferred %d bytes", n)
}

Expand Down

0 comments on commit bbeee18

Please sign in to comment.