nat.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package nat
  2. // nat is a convenience package for docker's manipulation of strings describing
  3. // network ports.
  4. import (
  5. "fmt"
  6. "net"
  7. "strconv"
  8. "strings"
  9. "github.com/docker/docker/pkg/parsers"
  10. )
  11. const (
  12. PortSpecTemplate = "ip:hostPort:containerPort"
  13. PortSpecTemplateFormat = "ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort"
  14. )
  15. type PortBinding struct {
  16. HostIp string
  17. HostPort string
  18. }
  19. type PortMap map[Port][]PortBinding
  20. type PortSet map[Port]struct{}
  21. // 80/tcp
  22. type Port string
  23. func NewPort(proto, port string) Port {
  24. return Port(fmt.Sprintf("%s/%s", port, proto))
  25. }
  26. func ParsePort(rawPort string) (int, error) {
  27. port, err := strconv.ParseUint(rawPort, 10, 16)
  28. if err != nil {
  29. return 0, err
  30. }
  31. return int(port), nil
  32. }
  33. func (p Port) Proto() string {
  34. proto, _ := SplitProtoPort(string(p))
  35. return proto
  36. }
  37. func (p Port) Port() string {
  38. _, port := SplitProtoPort(string(p))
  39. return port
  40. }
  41. func (p Port) Int() int {
  42. port, err := ParsePort(p.Port())
  43. if err != nil {
  44. panic(err)
  45. }
  46. return port
  47. }
  48. // Splits a port in the format of proto/port
  49. func SplitProtoPort(rawPort string) (string, string) {
  50. parts := strings.Split(rawPort, "/")
  51. l := len(parts)
  52. if len(rawPort) == 0 || l == 0 || len(parts[0]) == 0 {
  53. return "", ""
  54. }
  55. if l == 1 {
  56. return "tcp", rawPort
  57. }
  58. if len(parts[1]) == 0 {
  59. return "tcp", parts[0]
  60. }
  61. return parts[1], parts[0]
  62. }
  63. func validateProto(proto string) bool {
  64. for _, availableProto := range []string{"tcp", "udp"} {
  65. if availableProto == proto {
  66. return true
  67. }
  68. }
  69. return false
  70. }
  71. // We will receive port specs in the format of ip:public:private/proto and these need to be
  72. // parsed in the internal types
  73. func ParsePortSpecs(ports []string) (map[Port]struct{}, map[Port][]PortBinding, error) {
  74. var (
  75. exposedPorts = make(map[Port]struct{}, len(ports))
  76. bindings = make(map[Port][]PortBinding)
  77. )
  78. for _, rawPort := range ports {
  79. proto := "tcp"
  80. if i := strings.LastIndex(rawPort, "/"); i != -1 {
  81. proto = rawPort[i+1:]
  82. rawPort = rawPort[:i]
  83. }
  84. if !strings.Contains(rawPort, ":") {
  85. rawPort = fmt.Sprintf("::%s", rawPort)
  86. } else if len(strings.Split(rawPort, ":")) == 2 {
  87. rawPort = fmt.Sprintf(":%s", rawPort)
  88. }
  89. parts, err := parsers.PartParser(PortSpecTemplate, rawPort)
  90. if err != nil {
  91. return nil, nil, err
  92. }
  93. var (
  94. containerPort = parts["containerPort"]
  95. rawIp = parts["ip"]
  96. hostPort = parts["hostPort"]
  97. )
  98. if rawIp != "" && net.ParseIP(rawIp) == nil {
  99. return nil, nil, fmt.Errorf("Invalid ip address: %s", rawIp)
  100. }
  101. if containerPort == "" {
  102. return nil, nil, fmt.Errorf("No port specified: %s<empty>", rawPort)
  103. }
  104. startPort, endPort, err := parsers.ParsePortRange(containerPort)
  105. if err != nil {
  106. return nil, nil, fmt.Errorf("Invalid containerPort: %s", containerPort)
  107. }
  108. var startHostPort, endHostPort uint64 = 0, 0
  109. if len(hostPort) > 0 {
  110. startHostPort, endHostPort, err = parsers.ParsePortRange(hostPort)
  111. if err != nil {
  112. return nil, nil, fmt.Errorf("Invalid hostPort: %s", hostPort)
  113. }
  114. }
  115. if hostPort != "" && (endPort-startPort) != (endHostPort-startHostPort) {
  116. return nil, nil, fmt.Errorf("Invalid ranges specified for container and host Ports: %s and %s", containerPort, hostPort)
  117. }
  118. if !validateProto(proto) {
  119. return nil, nil, fmt.Errorf("Invalid proto: %s", proto)
  120. }
  121. for i := uint64(0); i <= (endPort - startPort); i++ {
  122. containerPort = strconv.FormatUint(startPort+i, 10)
  123. if len(hostPort) > 0 {
  124. hostPort = strconv.FormatUint(startHostPort+i, 10)
  125. }
  126. port := NewPort(proto, containerPort)
  127. if _, exists := exposedPorts[port]; !exists {
  128. exposedPorts[port] = struct{}{}
  129. }
  130. binding := PortBinding{
  131. HostIp: rawIp,
  132. HostPort: hostPort,
  133. }
  134. bslice, exists := bindings[port]
  135. if !exists {
  136. bslice = []PortBinding{}
  137. }
  138. bindings[port] = append(bslice, binding)
  139. }
  140. }
  141. return exposedPorts, bindings, nil
  142. }