8000 tcpproxy: implement half-close dance in proxy by raggi · Pull Request #38 · inetaf/tcpproxy · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

tcpproxy: implement half-close dance in proxy #38

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

Merged
merged 1 commit into from
Nov 2, 2023
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
40 changes: 32 additions & 8 deletions tcpproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,30 @@ func UnderlyingConn(c net.Conn) net.Conn {
return c
}

func tcpConn(c net.Conn) (t *net.TCPConn, ok bool) {
if c, ok := UnderlyingConn(c).(*net.TCPConn); ok {
return c, ok
}
if c, ok := c.(*net.TCPConn); ok {
return c, ok
}
return nil, false
}

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

func closeRead(c net.Conn) {
if c, ok := tcpConn(c); ok {
c.CloseRead()
}
}

func closeWrite(c net.Conn) {
if c, ok := tcpConn(c); ok {
c.CloseWrite()
}
}

// HandleConn implements the Target interface.
func (dp *DialProxy) HandleConn(src net.Conn) {
ctx := context.Background()
Expand All @@ -371,20 +393,19 @@ func (dp *DialProxy) HandleConn(src net.Conn) {
defer goCloseConn(src)

if ka := dp.keepAlivePeriod(); ka > 0 {
if c, ok := UnderlyingConn(src).(*net.TCPConn); ok {
c.SetKeepAlive(true)
c.SetKeepAlivePeriod(ka)
}
if c, ok := dst.(*net.TCPConn); ok {
c.SetKeepAlive(true)
c.SetKeepAlivePeriod(ka)
for _, c := range []net.Conn{src, dst} {
if c, ok := tcpConn(c); ok {
c.SetKeepAlive(true)
c.SetKeepAlivePeriod(ka)
}
}
}

errc := make(chan error, 1)
errc := make(chan error, 2)
go proxyCopy(errc, src, dst)
go proxyCopy(errc, dst, src)
<-errc
<-errc
}

func (dp *DialProxy) sendProxyHeader(w io.Writer, src net.Conn) error {
Expand Down Expand Up @@ -420,6 +441,9 @@ func (dp *DialProxy) sendProxyHeader(w io.Writer, src net.Conn) error {
// It's a named function instead of a func literal so users get
// named goroutines in debug goroutine stack dumps.
func proxyCopy(errc chan<- error, dst, src net.Conn) {
defer closeRead(src)
defer closeWrite(dst)

// Before we unwrap src and/or dst, copy any buffered data.
if wc, ok := src.(*Conn); ok && len(wc.Peeked) > 0 {
if _, err := dst.Write(wc.Peeked); err != nil {
Expand Down
40 changes: 40 additions & 0 deletions tcpproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,45 @@ func testProxy(t *testing.T, front net.Listener) *Proxy {
}
}

func TestBufferedClose(t *testing.T) {
front := newLocalListener(t)
defer front.Close()
back := newLocalListener(t)
defer back.Close()

p := testProxy(t, front)
p.AddRoute(testFrontAddr, To(back.Addr().String()))
if err := p.Start(); err != nil {
t.Fatal(err)
}

toFront, err := net.Dial("tcp", front.Addr().String())
if err != nil {
t.Fatal(err)
}
defer toFront.Close()

fromProxy, err := back.Accept()
if err != nil {
t.Fatal(err)
}
defer fromProxy.Close()
const msg = "message"
if _, err := io.WriteString(toFront, msg); err != nil {
t.Fatal(err)
}
// actively close toFront, the write should still make to the back.
toFront.Close()

buf := make([]byte, len(msg))
if _, err := io.ReadFull(fromProxy, buf); err != nil {
t.Fatal(err)
}
if string(buf) != msg {
t.Fatalf("got %q; want %q", buf, msg)
}
}

func TestProxyAlwaysMatch(t *testing.T) {
front := newLocalListener(t)
defer front.Close()
Expand All @@ -196,6 +235,7 @@ func TestProxyAlwaysMatch(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer fromProxy.Close()
const msg = "message"
io.WriteString(toFront, msg)

Expand Down
0