Skip to content

Commit bbeee18

Browse files
authored
Merge pull request #197 from NodeFactoryIo/hotfix/tunnel-fix
Tunnel connections not closing fix
2 parents 779007c + 3c306ab commit bbeee18

3 files changed

Lines changed: 23 additions & 18 deletions

File tree

CHANGELOG.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
# Changelog
22

3-
## [v0.5.3]((https://github.com/NodeFactoryIo/vedran/tree/HEAD))
4-
[Full Changelog](https://github.com/NodeFactoryIo/vedran/compare/v0.5.2...HEAD)
3+
## [unreleased]((https://github.com/NodeFactoryIo/vedran/tree/HEAD))
4+
[Full Changelog](https://github.com/NodeFactoryIo/vedran/compare/v0.5.3...HEAD)
5+
6+
### Added
7+
8+
### Fix
9+
- Fix tunnel tcp connections not closing after requests finish [\#197](https://github.com/NodeFactoryIo/vedran/pull/197) ([mpetrun5](https://github.com/mpetrun5))
10+
11+
### Changed
12+
13+
## [v0.5.3]((https://github.com/NodeFactoryIo/vedran/tree/v0.5.3))
14+
[Full Changelog](https://github.com/NodeFactoryIo/vedran/compare/v0.5.2...v0.5.3)
515

616
### Added
717

pkg/http-tunnel/server/tcpproxy.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ package server
66

77
import (
88
"fmt"
9-
"github.com/NodeFactoryIo/vedran/pkg/http-tunnel"
10-
"github.com/NodeFactoryIo/vedran/pkg/http-tunnel/proto"
11-
log "github.com/sirupsen/logrus"
129
"io"
1310
"net"
11+
12+
tunnel "github.com/NodeFactoryIo/vedran/pkg/http-tunnel"
13+
"github.com/NodeFactoryIo/vedran/pkg/http-tunnel/proto"
14+
log "github.com/sirupsen/logrus"
1415
)
1516

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

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

91-
done := make(chan struct{})
9291
go func() {
9392
loggerWithContext := log.WithContext(p.logger.Context).WithFields(log.Fields{
9493
"dst": msg.ForwardedHost,
9594
"src": target,
9695
})
9796
transfer(flushWriter{w}, local, loggerWithContext)
98-
close(done)
9997
}()
10098

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

107-
<-done
105+
err = local.Close()
106+
if err != nil {
107+
clogger.Errorf("Transfer close failed because of %v", err)
108+
}
108109
}
109110

110111
func (p *TCPProxy) localAddrFor(hostPort string) string {

pkg/http-tunnel/server/utils.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,16 @@
55
package server
66

77
import (
8-
log "github.com/sirupsen/logrus"
98
"io"
109
"net"
1110
"net/http"
1211
"strings"
12+
13+
log "github.com/sirupsen/logrus"
1314
)
1415

1516
func transfer(dst io.Writer, src io.Reader, logger *log.Entry) {
16-
n, err := io.Copy(dst, src)
17-
if err != nil {
18-
if (!strings.Contains(err.Error(), "context canceled") &&
19-
!strings.Contains(err.Error(), "CANCEL")) &&
20-
!strings.Contains(err.Error(), "stream closed") {
21-
logger.Error("copy error ", err)
22-
}
23-
}
17+
n, _ := io.Copy(dst, src)
2418
log.Debugf("transferred %d bytes", n)
2519
}
2620

0 commit comments

Comments
 (0)