Merge pull request #113 from djs55/fix-ucp-proxy

proxy: bind the IP port as well as the vsock port
This commit is contained in:
Justin Cormack 2016-04-29 14:05:23 +01:00
commit 106ef4e5bc
2 changed files with 37 additions and 5 deletions

View File

@ -25,8 +25,8 @@ type Proxy interface {
BackendAddr() net.Addr
}
// NewProxy creates a Proxy according to the specified frontendAddr and backendAddr.
func NewProxy(frontendAddr *vsock.VsockAddr, backendAddr net.Addr) (Proxy, error) {
// NewVsockProxy creates a Proxy listening on Vsock
func NewVsockProxy(frontendAddr *vsock.VsockAddr, backendAddr net.Addr) (Proxy, error) {
switch backendAddr.(type) {
case *net.UDPAddr:
listener, err := vsock.Listen(frontendAddr.Port)
@ -44,3 +44,29 @@ func NewProxy(frontendAddr *vsock.VsockAddr, backendAddr net.Addr) (Proxy, error
panic(fmt.Errorf("Unsupported protocol"))
}
}
// NewIPProxy creates a Proxy according to the specified frontendAddr and backendAddr.
func NewIPProxy(frontendAddr, backendAddr net.Addr) (Proxy, error) {
switch frontendAddr.(type) {
case *net.UDPAddr:
listener, err := net.ListenUDP("udp", frontendAddr.(*net.UDPAddr))
if err != nil {
return nil, err
}
return NewUDPProxy(frontendAddr, listener, backendAddr.(*net.UDPAddr))
case *net.TCPAddr:
listener, err := net.Listen("tcp", frontendAddr.String())
if err != nil {
return nil, err
}
return NewTCPProxy(listener, backendAddr.(*net.TCPAddr))
case *vsock.VsockAddr:
listener, err := vsock.Listen(frontendAddr.(*vsock.VsockAddr).Port)
if err != nil {
return nil, err
}
return NewTCPProxy(listener, backendAddr.(*net.TCPAddr))
default:
panic(fmt.Errorf("Unsupported protocol"))
}
}

View File

@ -14,19 +14,25 @@ import (
func main() {
host, port, container := parseHostContainerAddrs()
p, err := libproxy.NewProxy(&vsock.VsockAddr{Port: uint(port)}, container)
vsockP, err := libproxy.NewVsockProxy(&vsock.VsockAddr{Port: uint(port)}, container)
if err != nil {
sendError(err)
}
ipP, err := libproxy.NewIPProxy(host, container)
if err != nil {
sendError(err)
}
ctl, err := exposePort(host, port)
if err != nil {
sendError(err)
}
go handleStopSignals(p)
go handleStopSignals(ipP)
// TODO: avoid this line if we are running in a TTY
sendOK()
p.Run()
go ipP.Run()
vsockP.Run()
ctl.Close() // ensure ctl remains alive and un-GCed until here
os.Exit(0)
}