Skip to content

Commit

Permalink
Merge pull request #89 from guillaumerose/pr77
Browse files Browse the repository at this point in the history
#77 extract of unix-to-tcp part
  • Loading branch information
guillaumerose authored Jan 14, 2022
2 parents e943b18 + 12b1d90 commit 7ea3564
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
26 changes: 26 additions & 0 deletions pkg/services/forwarder/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,32 @@ func (f *PortsForwarder) Expose(protocol types.TransportProtocol, local, remote
}

switch protocol {
case types.UNIX:
var p tcpproxy.Proxy
p.ListenFunc = func(_, socketPath string) (net.Listener, error) {
return net.Listen("unix", socketPath) // override tcp to use unix socket
}
p.AddRoute(local, &tcpproxy.DialProxy{
Addr: remote,
DialContext: func(ctx context.Context, network, addr string) (conn net.Conn, e error) {
return gonet.DialContextTCP(ctx, f.stack, address, ipv4.ProtocolNumber)
},
})
if err := p.Start(); err != nil {
return err
}
go func() {
if err := p.Wait(); err != nil {
log.Error(err)
}
}()

f.proxies[key(protocol, local)] = proxy{
Protocol: "unix",
Local: local,
Remote: remote,
underlying: &p,
}
case types.UDP:
addr, err := net.ResolveUDPAddr("udp", local)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions pkg/types/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package types
type TransportProtocol string

const (
UDP TransportProtocol = "udp"
TCP TransportProtocol = "tcp"
UDP TransportProtocol = "udp"
TCP TransportProtocol = "tcp"
UNIX TransportProtocol = "unix"
)

type ExposeRequest struct {
Expand Down
35 changes: 35 additions & 0 deletions test/port_forwarding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import (
"io"
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"

gvproxyclient "github.com/containers/gvisor-tap-vsock/pkg/client"
"github.com/containers/gvisor-tap-vsock/pkg/transport"
Expand Down Expand Up @@ -180,4 +183,36 @@ var _ = Describe("port forwarding", func() {
g.Expect(string(reply)).To(Equal("OK"))
}).Should(Succeed())
})

It("should expose and reach an http service using unix to tcp forwarding", func() {
if runtime.GOOS == "windows" {
Skip("AF_UNIX not supported on Windows")
}

unix2tcpfwdsock, _ := filepath.Abs(filepath.Join(tmpDir, "podman-unix-to-unix-forwarding.sock"))

out, err := sshExec(`curl http://gateway.containers.internal/services/forwarder/expose -X POST -d'{"protocol":"unix","local":"` + unix2tcpfwdsock + `","remote":"192.168.127.2:8080"}'`)
Expect(string(out)).Should(Equal(""))
Expect(err).ShouldNot(HaveOccurred())

Eventually(func(g Gomega) {
sockfile, err := os.Stat(unix2tcpfwdsock)
g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(sockfile.Mode().Type().String()).To(Equal(os.ModeSocket.String()))
}).Should(Succeed())

httpClient := &http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return net.Dial("unix", unix2tcpfwdsock)
},
},
}

Eventually(func(g Gomega) {
resp, err := httpClient.Get("http://placeholder/")
g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(resp.StatusCode).To(Equal(http.StatusOK))
}).Should(Succeed())
})
})

0 comments on commit 7ea3564

Please sign in to comment.