Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Nino Kodabande <[email protected]>
  • Loading branch information
Nino-K committed Oct 11, 2024
1 parent ab0957b commit 932e27b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/go/networking/pkg/portproxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func (p *PortProxy) handleUDP(portBindings []nat.PortBinding, remove bool) {
// the localAddress IP section can either be 0.0.0.0 or 127.0.0.1
localAddress := net.JoinHostPort(portBinding.HostIP, portBinding.HostPort)
sourceAddr, err := net.ResolveUDPAddr("udp", localAddress)
fmt.Println(localAddress)
if err != nil {
logrus.Errorf("failed to resolve UDP source address [%s]: %s", sourceAddr, err)
continue
Expand Down Expand Up @@ -160,10 +161,13 @@ func (p *PortProxy) acceptUDPConn(sourceConn *net.UDPConn, targetAddr *net.UDPAd
}
defer targetConn.Close()
p.wg.Add(1)
fmt.Println("here")
for {
b := make([]byte, p.config.UDPBufferSize)
fmt.Println("here 1")
n, addr, err := sourceConn.ReadFromUDP(b)
if err != nil && n != 0 {
fmt.Println("here 2")
if err != nil && n == 0 {
logrus.Errorf("error reading UDP packet from source: %s : %s", addr, err)
if errors.Is(err, net.ErrClosed) {
p.wg.Done()
Expand All @@ -172,6 +176,7 @@ func (p *PortProxy) acceptUDPConn(sourceConn *net.UDPConn, targetAddr *net.UDPAd
continue
}
logrus.Debugf("received %d data from %s", n, addr)
fmt.Println("---------->", string(b))

n, err = targetConn.Write(b[:n])
if err != nil {
Expand Down
69 changes: 68 additions & 1 deletion src/go/networking/pkg/portproxy/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"net/http"
"syscall"
"testing"
"time"

"github.com/docker/go-connections/nat"
"github.com/rancher-sandbox/rancher-desktop/src/go/guestagent/pkg/types"
Expand All @@ -33,9 +34,74 @@ import (
"golang.org/x/net/nettest"
)

func TestNewPortProxy(t *testing.T) {
func TestNewPortProxyUDP(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
testServerIP, err := availableIP()
require.NoError(t, err, "cannot continue with the test since there are no available IP addresses")

remoteAddr := net.JoinHostPort(testServerIP, "0")
targetAddr, err := net.ResolveUDPAddr("udp", remoteAddr)
require.NoError(t, err)
targetConn, err := net.ListenUDP("udp", targetAddr)
require.NoError(t, err)

t.Logf("created the following UDP target listener: %s", targetConn.LocalAddr().String())

localListener, err := nettest.NewLocalListener("unix")
require.NoError(t, err)
defer localListener.Close()

proxyConfig := &portproxy.ProxyConfig{
UpstreamAddress: testServerIP,
UDPBufferSize: 1024,
}
portProxy := portproxy.NewPortProxy(localListener, proxyConfig)
go portProxy.Start()

_, testPort, err := net.SplitHostPort(targetConn.LocalAddr().String())
require.NoError(t, err)

port, err := nat.NewPort("udp", testPort)
require.NoError(t, err)

portMapping := types.PortMapping{
Remove: false,
Ports: nat.PortMap{
port: []nat.PortBinding{
{
HostIP: "127.0.0.1",
HostPort: testPort,
},
},
},
}
t.Logf("sending the following portMapping to portProxy: %+v", portMapping)
err = marshalAndSend(localListener, portMapping)
require.NoError(t, err)

localAddr := net.JoinHostPort("127.0.0.1", testPort)
sourceAddr, err := net.ResolveUDPAddr("udp", localAddr)
require.NoError(t, err)
sourceConn, err := net.DialUDP("udp", nil, sourceAddr)
require.NoError(t, err)
t.Logf("dialing in to the following UDP connection: %s", localAddr)

expectedString := "this is what we expect"
_, err = sourceConn.Write([]byte(expectedString))
require.NoError(t, err)

targetConn.SetDeadline(time.Now().Add(time.Second * 5))

b := make([]byte, len(expectedString))
n, addr, err := targetConn.ReadFromUDP(b)
require.NoError(t, err)

t.Log(string(b))
t.Logf("read: %d", n)
t.Log(addr)

}

Check failure on line 103 in src/go/networking/pkg/portproxy/server_test.go

View workflow job for this annotation

GitHub Actions / test

unnecessary trailing newline (whitespace)

Check failure on line 103 in src/go/networking/pkg/portproxy/server_test.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

unnecessary trailing newline (whitespace)

Check failure on line 103 in src/go/networking/pkg/portproxy/server_test.go

View workflow job for this annotation

GitHub Actions / test

unnecessary trailing newline (whitespace)

Check failure on line 103 in src/go/networking/pkg/portproxy/server_test.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

unnecessary trailing newline (whitespace)
func TestNewPortProxyTCP(t *testing.T) {
expectedResponse := "called the upstream server"

testServerIP, err := availableIP()
Expand Down Expand Up @@ -88,6 +154,7 @@ func TestNewPortProxy(t *testing.T) {
},
},
}
t.Logf("sending the following portMapping to portProxy: %+v", portMapping)
err = marshalAndSend(localListener, portMapping)
require.NoError(t, err)

Expand Down

0 comments on commit 932e27b

Please sign in to comment.