proxy.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package portmapper
  2. import (
  3. "flag"
  4. "log"
  5. "net"
  6. "os"
  7. "os/exec"
  8. "os/signal"
  9. "strconv"
  10. "syscall"
  11. "github.com/docker/docker/pkg/proxy"
  12. "github.com/docker/docker/reexec"
  13. )
  14. const userlandProxyCommandName = "docker-proxy"
  15. func init() {
  16. reexec.Register(userlandProxyCommandName, execProxy)
  17. }
  18. type UserlandProxy interface {
  19. Start() error
  20. Stop() error
  21. }
  22. // proxyCommand wraps an exec.Cmd to run the userland TCP and UDP
  23. // proxies as separate processes.
  24. type proxyCommand struct {
  25. cmd *exec.Cmd
  26. }
  27. // execProxy is the reexec function that is registered to start the userland proxies
  28. func execProxy() {
  29. host, container := parseHostContainerAddrs()
  30. p, err := proxy.NewProxy(host, container)
  31. if err != nil {
  32. log.Fatal(err)
  33. }
  34. go handleStopSignals(p)
  35. // Run will block until the proxy stops
  36. p.Run()
  37. }
  38. // parseHostContainerAddrs parses the flags passed on reexec to create the TCP or UDP
  39. // net.Addrs to map the host and container ports
  40. func parseHostContainerAddrs() (host net.Addr, container net.Addr) {
  41. var (
  42. proto = flag.String("proto", "tcp", "proxy protocol")
  43. hostIP = flag.String("host-ip", "", "host ip")
  44. hostPort = flag.Int("host-port", -1, "host port")
  45. containerIP = flag.String("container-ip", "", "container ip")
  46. containerPort = flag.Int("container-port", -1, "container port")
  47. )
  48. flag.Parse()
  49. switch *proto {
  50. case "tcp":
  51. host = &net.TCPAddr{IP: net.ParseIP(*hostIP), Port: *hostPort}
  52. container = &net.TCPAddr{IP: net.ParseIP(*containerIP), Port: *containerPort}
  53. case "udp":
  54. host = &net.UDPAddr{IP: net.ParseIP(*hostIP), Port: *hostPort}
  55. container = &net.UDPAddr{IP: net.ParseIP(*containerIP), Port: *containerPort}
  56. default:
  57. log.Fatalf("unsupported protocol %s", *proto)
  58. }
  59. return host, container
  60. }
  61. func handleStopSignals(p proxy.Proxy) {
  62. s := make(chan os.Signal, 10)
  63. signal.Notify(s, os.Interrupt, syscall.SIGTERM, syscall.SIGSTOP)
  64. for _ = range s {
  65. p.Close()
  66. os.Exit(0)
  67. }
  68. }
  69. func NewProxyCommand(proto string, hostIP net.IP, hostPort int, containerIP net.IP, containerPort int) UserlandProxy {
  70. args := []string{
  71. userlandProxyCommandName,
  72. "-proto", proto,
  73. "-host-ip", hostIP.String(),
  74. "-host-port", strconv.Itoa(hostPort),
  75. "-container-ip", containerIP.String(),
  76. "-container-port", strconv.Itoa(containerPort),
  77. }
  78. return &proxyCommand{
  79. cmd: &exec.Cmd{
  80. Path: reexec.Self(),
  81. Args: args,
  82. Stdout: os.Stdout,
  83. Stderr: os.Stderr,
  84. SysProcAttr: &syscall.SysProcAttr{
  85. Pdeathsig: syscall.SIGTERM, // send a sigterm to the proxy if the daemon process dies
  86. },
  87. },
  88. }
  89. }
  90. func (p *proxyCommand) Start() error {
  91. return p.cmd.Start()
  92. }
  93. func (p *proxyCommand) Stop() error {
  94. err := p.cmd.Process.Signal(os.Interrupt)
  95. p.cmd.Wait()
  96. return err
  97. }