Skip to content

Commit

Permalink
fix: forward addr implementation for Proxy interface support
Browse files Browse the repository at this point in the history
  • Loading branch information
shoriwe committed Jun 11, 2023
1 parent b019ab3 commit 56c5931
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
15 changes: 9 additions & 6 deletions proxies/forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ func (f *Forward) Handle(client net.Conn) {
}
}

func (f *Forward) Addr() net.Addr {
return f.Listener.Addr()
}

func (f *Forward) Close() {
f.Listener.Close()
}

func (f *Forward) Serve() {
for {
client, aErr := f.Listener.Accept()
if aErr == nil {
go f.Handle(client)
}
func (f *Forward) Serve() (err error) {
var client net.Conn
for client, err = f.Listener.Accept(); err == nil; client, err = f.Listener.Accept() {
go f.Handle(client)
}
return err
}
33 changes: 33 additions & 0 deletions proxies/forward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,39 @@ import (
"github.com/stretchr/testify/assert"
)

func TestForward_Addr(t *testing.T) {
listener := network.ListenAny()
defer listener.Close()
service := network.ListenAny()
defer service.Close()
f := Forward{
Network: service.Addr().Network(),
Address: service.Addr().String(),
Listener: listener,
Dial: net.Dial,
}
defer f.Close()
doneChan := make(chan struct{}, 1)
defer close(doneChan)
testMessage := []byte("TEST")
go func() {
conn, aErr := service.Accept()
assert.Nil(t, aErr)
_, wErr := conn.Write(testMessage)
assert.Nil(t, wErr)
<-doneChan
}()
go f.Serve()
conn, dErr := net.Dial(listener.Addr().Network(), listener.Addr().String())
assert.Nil(t, dErr)
buffer := make([]byte, len(testMessage))
_, rErr := conn.Read(buffer)
assert.Nil(t, rErr)
assert.Equal(t, testMessage, buffer)
assert.NotNil(t, f.Addr())

}

func TestBasicLocalForward(t *testing.T) {
listener := network.ListenAny()
defer listener.Close()
Expand Down

0 comments on commit 56c5931

Please sign in to comment.