nat.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // Package nat is a convenience package for manipulation of strings describing network ports.
  2. package nat
  3. import (
  4. "fmt"
  5. "net"
  6. "strconv"
  7. "strings"
  8. )
  9. // PortBinding represents a binding between a Host IP address and a Host Port
  10. type PortBinding struct {
  11. // HostIP is the host IP Address
  12. HostIP string `json:"HostIp"`
  13. // HostPort is the host port number
  14. HostPort string
  15. }
  16. // PortMap is a collection of PortBinding indexed by Port
  17. type PortMap map[Port][]PortBinding
  18. // PortSet is a collection of structs indexed by Port
  19. type PortSet map[Port]struct{}
  20. // Port is a string containing port number and protocol in the format "80/tcp"
  21. type Port string
  22. // NewPort creates a new instance of a Port given a protocol and port number or port range
  23. func NewPort(proto, port string) (Port, error) {
  24. // Check for parsing issues on "port" now so we can avoid having
  25. // to check it later on.
  26. portStartInt, portEndInt, err := ParsePortRangeToInt(port)
  27. if err != nil {
  28. return "", err
  29. }
  30. if portStartInt == portEndInt {
  31. return Port(fmt.Sprintf("%d/%s", portStartInt, proto)), nil
  32. }
  33. return Port(fmt.Sprintf("%d-%d/%s", portStartInt, portEndInt, proto)), nil
  34. }
  35. // ParsePort parses the port number string and returns an int
  36. func ParsePort(rawPort string) (int, error) {
  37. if len(rawPort) == 0 {
  38. return 0, nil
  39. }
  40. port, err := strconv.ParseUint(rawPort, 10, 16)
  41. if err != nil {
  42. return 0, err
  43. }
  44. return int(port), nil
  45. }
  46. // ParsePortRangeToInt parses the port range string and returns start/end ints
  47. func ParsePortRangeToInt(rawPort string) (int, int, error) {
  48. if len(rawPort) == 0 {
  49. return 0, 0, nil
  50. }
  51. start, end, err := ParsePortRange(rawPort)
  52. if err != nil {
  53. return 0, 0, err
  54. }
  55. return int(start), int(end), nil
  56. }
  57. // Proto returns the protocol of a Port
  58. func (p Port) Proto() string {
  59. proto, _ := SplitProtoPort(string(p))
  60. return proto
  61. }
  62. // Port returns the port number of a Port
  63. func (p Port) Port() string {
  64. _, port := SplitProtoPort(string(p))
  65. return port
  66. }
  67. // Int returns the port number of a Port as an int
  68. func (p Port) Int() int {
  69. portStr := p.Port()
  70. // We don't need to check for an error because we're going to
  71. // assume that any error would have been found, and reported, in NewPort()
  72. port, _ := ParsePort(portStr)
  73. return port
  74. }
  75. // Range returns the start/end port numbers of a Port range as ints
  76. func (p Port) Range() (int, int, error) {
  77. return ParsePortRangeToInt(p.Port())
  78. }
  79. // SplitProtoPort splits a port in the format of proto/port
  80. func SplitProtoPort(rawPort string) (string, string) {
  81. parts := strings.Split(rawPort, "/")
  82. l := len(parts)
  83. if len(rawPort) == 0 || l == 0 || len(parts[0]) == 0 {
  84. return "", ""
  85. }
  86. if l == 1 {
  87. return "tcp", rawPort
  88. }
  89. if len(parts[1]) == 0 {
  90. return "tcp", parts[0]
  91. }
  92. return parts[1], parts[0]
  93. }
  94. func validateProto(proto string) bool {
  95. for _, availableProto := range []string{"tcp", "udp", "sctp"} {
  96. if availableProto == proto {
  97. return true
  98. }
  99. }
  100. return false
  101. }
  102. // ParsePortSpecs receives port specs in the format of ip:public:private/proto and parses
  103. // these in to the internal types
  104. func ParsePortSpecs(ports []string) (map[Port]struct{}, map[Port][]PortBinding, error) {
  105. var (
  106. exposedPorts = make(map[Port]struct{}, len(ports))
  107. bindings = make(map[Port][]PortBinding)
  108. )
  109. for _, rawPort := range ports {
  110. portMappings, err := ParsePortSpec(rawPort)
  111. if err != nil {
  112. return nil, nil, err
  113. }
  114. for _, portMapping := range portMappings {
  115. port := portMapping.Port
  116. if _, exists := exposedPorts[port]; !exists {
  117. exposedPorts[port] = struct{}{}
  118. }
  119. bslice, exists := bindings[port]
  120. if !exists {
  121. bslice = []PortBinding{}
  122. }
  123. bindings[port] = append(bslice, portMapping.Binding)
  124. }
  125. }
  126. return exposedPorts, bindings, nil
  127. }
  128. // PortMapping is a data object mapping a Port to a PortBinding
  129. type PortMapping struct {
  130. Port Port
  131. Binding PortBinding
  132. }
  133. func splitParts(rawport string) (string, string, string) {
  134. parts := strings.Split(rawport, ":")
  135. n := len(parts)
  136. containerPort := parts[n-1]
  137. switch n {
  138. case 1:
  139. return "", "", containerPort
  140. case 2:
  141. return "", parts[0], containerPort
  142. case 3:
  143. return parts[0], parts[1], containerPort
  144. default:
  145. return strings.Join(parts[:n-2], ":"), parts[n-2], containerPort
  146. }
  147. }
  148. // ParsePortSpec parses a port specification string into a slice of PortMappings
  149. func ParsePortSpec(rawPort string) ([]PortMapping, error) {
  150. var proto string
  151. ip, hostPort, containerPort := splitParts(rawPort)
  152. proto, containerPort = SplitProtoPort(containerPort)
  153. if ip != "" && ip[0] == '[' {
  154. // Strip [] from IPV6 addresses
  155. rawIP, _, err := net.SplitHostPort(ip + ":")
  156. if err != nil {
  157. return nil, fmt.Errorf("invalid IP address %v: %w", ip, err)
  158. }
  159. ip = rawIP
  160. }
  161. if ip != "" && net.ParseIP(ip) == nil {
  162. return nil, fmt.Errorf("invalid IP address: %s", ip)
  163. }
  164. if containerPort == "" {
  165. return nil, fmt.Errorf("no port specified: %s<empty>", rawPort)
  166. }
  167. startPort, endPort, err := ParsePortRange(containerPort)
  168. if err != nil {
  169. return nil, fmt.Errorf("invalid containerPort: %s", containerPort)
  170. }
  171. var startHostPort, endHostPort uint64 = 0, 0
  172. if len(hostPort) > 0 {
  173. startHostPort, endHostPort, err = ParsePortRange(hostPort)
  174. if err != nil {
  175. return nil, fmt.Errorf("invalid hostPort: %s", hostPort)
  176. }
  177. }
  178. if hostPort != "" && (endPort-startPort) != (endHostPort-startHostPort) {
  179. // Allow host port range iff containerPort is not a range.
  180. // In this case, use the host port range as the dynamic
  181. // host port range to allocate into.
  182. if endPort != startPort {
  183. return nil, fmt.Errorf("invalid ranges specified for container and host Ports: %s and %s", containerPort, hostPort)
  184. }
  185. }
  186. if !validateProto(strings.ToLower(proto)) {
  187. return nil, fmt.Errorf("invalid proto: %s", proto)
  188. }
  189. ports := []PortMapping{}
  190. for i := uint64(0); i <= (endPort - startPort); i++ {
  191. containerPort = strconv.FormatUint(startPort+i, 10)
  192. if len(hostPort) > 0 {
  193. hostPort = strconv.FormatUint(startHostPort+i, 10)
  194. }
  195. // Set hostPort to a range only if there is a single container port
  196. // and a dynamic host port.
  197. if startPort == endPort && startHostPort != endHostPort {
  198. hostPort = fmt.Sprintf("%s-%s", hostPort, strconv.FormatUint(endHostPort, 10))
  199. }
  200. port, err := NewPort(strings.ToLower(proto), containerPort)
  201. if err != nil {
  202. return nil, err
  203. }
  204. binding := PortBinding{
  205. HostIP: ip,
  206. HostPort: hostPort,
  207. }
  208. ports = append(ports, PortMapping{Port: port, Binding: binding})
  209. }
  210. return ports, nil
  211. }