2014-08-12 18:04:00 -04:00
|
|
|
package portmapper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2014-09-08 06:22:50 -04:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2014-08-12 18:04:00 -04:00
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"os/signal"
|
|
|
|
"strconv"
|
|
|
|
"syscall"
|
2014-09-07 15:33:51 -04:00
|
|
|
"time"
|
2014-08-12 18:04:00 -04:00
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/proxy"
|
2014-10-30 08:48:30 -04:00
|
|
|
"github.com/docker/docker/pkg/reexec"
|
2014-08-12 18:04:00 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const userlandProxyCommandName = "docker-proxy"
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
reexec.Register(userlandProxyCommandName, execProxy)
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserlandProxy interface {
|
|
|
|
Start() error
|
|
|
|
Stop() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// proxyCommand wraps an exec.Cmd to run the userland TCP and UDP
|
|
|
|
// proxies as separate processes.
|
|
|
|
type proxyCommand struct {
|
|
|
|
cmd *exec.Cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// execProxy is the reexec function that is registered to start the userland proxies
|
|
|
|
func execProxy() {
|
2014-10-08 08:41:26 -04:00
|
|
|
f := os.NewFile(3, "signal-parent")
|
2014-08-12 18:04:00 -04:00
|
|
|
host, container := parseHostContainerAddrs()
|
|
|
|
|
|
|
|
p, err := proxy.NewProxy(host, container)
|
|
|
|
if err != nil {
|
2014-10-08 08:41:26 -04:00
|
|
|
fmt.Fprintf(f, "1\n%s", err)
|
|
|
|
f.Close()
|
2014-09-07 15:33:51 -04:00
|
|
|
os.Exit(1)
|
2014-08-12 18:04:00 -04:00
|
|
|
}
|
|
|
|
go handleStopSignals(p)
|
2014-10-08 08:41:26 -04:00
|
|
|
fmt.Fprint(f, "0\n")
|
|
|
|
f.Close()
|
2014-08-12 18:04:00 -04:00
|
|
|
|
|
|
|
// Run will block until the proxy stops
|
|
|
|
p.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseHostContainerAddrs parses the flags passed on reexec to create the TCP or UDP
|
|
|
|
// net.Addrs to map the host and container ports
|
|
|
|
func parseHostContainerAddrs() (host net.Addr, container net.Addr) {
|
|
|
|
var (
|
|
|
|
proto = flag.String("proto", "tcp", "proxy protocol")
|
|
|
|
hostIP = flag.String("host-ip", "", "host ip")
|
|
|
|
hostPort = flag.Int("host-port", -1, "host port")
|
|
|
|
containerIP = flag.String("container-ip", "", "container ip")
|
|
|
|
containerPort = flag.Int("container-port", -1, "container port")
|
|
|
|
)
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
switch *proto {
|
|
|
|
case "tcp":
|
|
|
|
host = &net.TCPAddr{IP: net.ParseIP(*hostIP), Port: *hostPort}
|
|
|
|
container = &net.TCPAddr{IP: net.ParseIP(*containerIP), Port: *containerPort}
|
|
|
|
case "udp":
|
|
|
|
host = &net.UDPAddr{IP: net.ParseIP(*hostIP), Port: *hostPort}
|
|
|
|
container = &net.UDPAddr{IP: net.ParseIP(*containerIP), Port: *containerPort}
|
|
|
|
default:
|
|
|
|
log.Fatalf("unsupported protocol %s", *proto)
|
|
|
|
}
|
|
|
|
|
|
|
|
return host, container
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleStopSignals(p proxy.Proxy) {
|
|
|
|
s := make(chan os.Signal, 10)
|
|
|
|
signal.Notify(s, os.Interrupt, syscall.SIGTERM, syscall.SIGSTOP)
|
|
|
|
|
|
|
|
for _ = range s {
|
|
|
|
p.Close()
|
|
|
|
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProxyCommand(proto string, hostIP net.IP, hostPort int, containerIP net.IP, containerPort int) UserlandProxy {
|
|
|
|
args := []string{
|
|
|
|
userlandProxyCommandName,
|
|
|
|
"-proto", proto,
|
|
|
|
"-host-ip", hostIP.String(),
|
|
|
|
"-host-port", strconv.Itoa(hostPort),
|
|
|
|
"-container-ip", containerIP.String(),
|
|
|
|
"-container-port", strconv.Itoa(containerPort),
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proxyCommand{
|
|
|
|
cmd: &exec.Cmd{
|
2014-09-07 15:33:51 -04:00
|
|
|
Path: reexec.Self(),
|
|
|
|
Args: args,
|
2014-08-12 18:04:00 -04:00
|
|
|
SysProcAttr: &syscall.SysProcAttr{
|
|
|
|
Pdeathsig: syscall.SIGTERM, // send a sigterm to the proxy if the daemon process dies
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *proxyCommand) Start() error {
|
2014-10-08 08:41:26 -04:00
|
|
|
r, w, err := os.Pipe()
|
2014-09-07 15:33:51 -04:00
|
|
|
if err != nil {
|
2014-10-08 08:41:26 -04:00
|
|
|
return fmt.Errorf("proxy unable to open os.Pipe %s", err)
|
2014-09-08 06:22:50 -04:00
|
|
|
}
|
2014-10-08 08:41:26 -04:00
|
|
|
defer r.Close()
|
|
|
|
p.cmd.ExtraFiles = []*os.File{w}
|
2014-09-07 15:33:51 -04:00
|
|
|
if err := p.cmd.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-10-08 08:41:26 -04:00
|
|
|
w.Close()
|
2014-09-07 15:33:51 -04:00
|
|
|
|
2014-09-08 06:22:50 -04:00
|
|
|
errchan := make(chan error, 1)
|
2014-09-07 15:33:51 -04:00
|
|
|
go func() {
|
|
|
|
buf := make([]byte, 2)
|
2014-10-08 08:41:26 -04:00
|
|
|
r.Read(buf)
|
2014-09-07 15:33:51 -04:00
|
|
|
|
|
|
|
if string(buf) != "0\n" {
|
2014-10-31 00:20:26 -04:00
|
|
|
errStr, err := ioutil.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
errchan <- fmt.Errorf("Error reading exit status from userland proxy: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-08 06:22:50 -04:00
|
|
|
errchan <- fmt.Errorf("Error starting userland proxy: %s", errStr)
|
|
|
|
return
|
2014-09-07 15:33:51 -04:00
|
|
|
}
|
2014-09-08 06:22:50 -04:00
|
|
|
errchan <- nil
|
2014-09-07 15:33:51 -04:00
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
2014-09-08 06:22:50 -04:00
|
|
|
case err := <-errchan:
|
|
|
|
return err
|
2014-11-20 12:02:21 -05:00
|
|
|
case <-time.After(16 * time.Second):
|
2014-09-08 06:22:50 -04:00
|
|
|
return fmt.Errorf("Timed out proxy starting the userland proxy")
|
2014-09-07 15:33:51 -04:00
|
|
|
}
|
2014-08-12 18:04:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *proxyCommand) Stop() error {
|
2014-09-07 15:33:51 -04:00
|
|
|
if p.cmd.Process != nil {
|
2014-09-08 06:22:50 -04:00
|
|
|
if err := p.cmd.Process.Signal(os.Interrupt); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return p.cmd.Wait()
|
2014-09-07 15:33:51 -04:00
|
|
|
}
|
|
|
|
return nil
|
2014-08-12 18:04:00 -04:00
|
|
|
}
|