ports.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package docker
  2. import (
  3. "net"
  4. "strconv"
  5. "strings"
  6. )
  7. /*
  8. ports.go
  9. Parses the Ports column of `docker ps` ("0.0.0.0:8081->80/tcp, :::8081->80/tcp")
  10. into the host ports a container publishes, so the container app publisher
  11. can offer them as proxy targets.
  12. */
  13. // maxExpandedRange caps how many ports a published range expands into
  14. const maxExpandedRange = 32
  15. // PublishedPort is one container port published on the host
  16. type PublishedPort struct {
  17. HostIP string
  18. HostPort int
  19. ContainerPort int
  20. Protocol string
  21. }
  22. // Target is the address this host reaches the published port on
  23. func (p PublishedPort) Target() string {
  24. ip := p.HostIP
  25. if ip == "" || ip == "0.0.0.0" || ip == "::" {
  26. ip = "127.0.0.1"
  27. }
  28. return net.JoinHostPort(ip, strconv.Itoa(p.HostPort))
  29. }
  30. // parsePortRange parses "80" or "8000-8003"
  31. func parsePortRange(s string) (int, int, bool) {
  32. lo, hi, isRange := strings.Cut(s, "-")
  33. start, err := strconv.Atoi(lo)
  34. if err != nil || start < 1 || start > 65535 {
  35. return 0, 0, false
  36. }
  37. if !isRange {
  38. return start, start, true
  39. }
  40. end, err := strconv.Atoi(hi)
  41. if err != nil || end < start || end > 65535 {
  42. return 0, 0, false
  43. }
  44. return start, end, true
  45. }
  46. // ParsePublishedPorts returns the tcp ports of a `docker ps` Ports value that
  47. // are published on the host, without the IPv4/IPv6 duplicates
  48. func ParsePublishedPorts(ports string) []PublishedPort {
  49. results := []PublishedPort{}
  50. seen := map[string]bool{}
  51. for _, entry := range strings.Split(ports, ",") {
  52. entry = strings.TrimSpace(entry)
  53. host, container, published := strings.Cut(entry, "->")
  54. if !published {
  55. continue
  56. }
  57. containerPorts, proto, _ := strings.Cut(container, "/")
  58. if proto == "" {
  59. proto = "tcp"
  60. }
  61. if proto != "tcp" {
  62. continue
  63. }
  64. sep := strings.LastIndex(host, ":")
  65. if sep < 0 {
  66. continue
  67. }
  68. hostIP := strings.Trim(host[:sep], "[]")
  69. hStart, hEnd, ok := parsePortRange(host[sep+1:])
  70. if !ok {
  71. continue
  72. }
  73. cStart, _, ok := parsePortRange(containerPorts)
  74. if !ok {
  75. continue
  76. }
  77. for i := 0; hStart+i <= hEnd && i < maxExpandedRange; i++ {
  78. p := PublishedPort{HostIP: hostIP, HostPort: hStart + i, ContainerPort: cStart + i, Protocol: proto}
  79. //"0.0.0.0" and "::" publish the same port twice
  80. key := strconv.Itoa(p.HostPort) + ">" + strconv.Itoa(p.ContainerPort)
  81. if hostIP != "" && hostIP != "0.0.0.0" && hostIP != "::" {
  82. key = hostIP + ":" + key
  83. }
  84. if seen[key] {
  85. continue
  86. }
  87. seen[key] = true
  88. results = append(results, p)
  89. }
  90. }
  91. return results
  92. }