netutil.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package netutil
  15. import (
  16. "log"
  17. "net"
  18. "net/url"
  19. "reflect"
  20. )
  21. var (
  22. // indirection for testing
  23. resolveTCPAddr = net.ResolveTCPAddr
  24. )
  25. // ResolveTCPAddrs is a convenience wrapper for net.ResolveTCPAddr.
  26. // ResolveTCPAddrs resolves all DNS hostnames in-place for the given set of
  27. // url.URLs.
  28. func ResolveTCPAddrs(urls ...[]url.URL) error {
  29. for _, us := range urls {
  30. for i, u := range us {
  31. host, _, err := net.SplitHostPort(u.Host)
  32. if err != nil {
  33. log.Printf("netutil: Could not parse url %s during tcp resolving.", u.Host)
  34. return err
  35. }
  36. if host == "localhost" {
  37. continue
  38. }
  39. if net.ParseIP(host) != nil {
  40. continue
  41. }
  42. tcpAddr, err := resolveTCPAddr("tcp", u.Host)
  43. if err != nil {
  44. log.Printf("netutil: Could not resolve host: %s", u.Host)
  45. return err
  46. }
  47. log.Printf("netutil: Resolving %s to %s", u.Host, tcpAddr.String())
  48. us[i].Host = tcpAddr.String()
  49. }
  50. }
  51. return nil
  52. }
  53. // URLsEqual checks equality of url.URLS between two arrays.
  54. // This check pass even if an URL is in hostname and opposite is in IP address.
  55. func URLsEqual(a []url.URL, b []url.URL) bool {
  56. if len(a) != len(b) {
  57. return false
  58. }
  59. for i, urlA := range a {
  60. urlB := b[i]
  61. if !reflect.DeepEqual(urlA, urlB) {
  62. urls := []url.URL{urlA, urlB}
  63. ResolveTCPAddrs(urls)
  64. if !reflect.DeepEqual(urls[0], urls[1]) {
  65. return false
  66. }
  67. }
  68. }
  69. return true
  70. }
  71. func URLStringsEqual(a []string, b []string) bool {
  72. if len(a) != len(b) {
  73. return false
  74. }
  75. urlsA := make([]url.URL, len(a))
  76. for _, str := range a {
  77. u, err := url.Parse(str)
  78. if err != nil {
  79. return false
  80. }
  81. urlsA = append(urlsA, *u)
  82. }
  83. urlsB := make([]url.URL, len(b))
  84. for _, str := range b {
  85. u, err := url.Parse(str)
  86. if err != nil {
  87. return false
  88. }
  89. urlsB = append(urlsB, *u)
  90. }
  91. return URLsEqual(urlsA, urlsB)
  92. }