localip.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright (c) 2016 Uber Technologies, Inc.
  2. // Permission is hereby granted, free of charge, to any person obtaining a copy
  3. // of this software and associated documentation files (the "Software"), to deal
  4. // in the Software without restriction, including without limitation the rights
  5. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  6. // copies of the Software, and to permit persons to whom the Software is
  7. // furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in
  10. // all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. // THE SOFTWARE.
  19. package utils
  20. import (
  21. "errors"
  22. "net"
  23. )
  24. // This code is borrowed from https://github.com/uber/tchannel-go/blob/dev/localip.go
  25. // scoreAddr scores how likely the given addr is to be a remote address and returns the
  26. // IP to use when listening. Any address which receives a negative score should not be used.
  27. // Scores are calculated as:
  28. // -1 for any unknown IP addresses.
  29. // +300 for IPv4 addresses
  30. // +100 for non-local addresses, extra +100 for "up" interaces.
  31. func scoreAddr(iface net.Interface, addr net.Addr) (int, net.IP) {
  32. var ip net.IP
  33. if netAddr, ok := addr.(*net.IPNet); ok {
  34. ip = netAddr.IP
  35. } else if netIP, ok := addr.(*net.IPAddr); ok {
  36. ip = netIP.IP
  37. } else {
  38. return -1, nil
  39. }
  40. var score int
  41. if ip.To4() != nil {
  42. score += 300
  43. }
  44. if iface.Flags&net.FlagLoopback == 0 && !ip.IsLoopback() {
  45. score += 100
  46. if iface.Flags&net.FlagUp != 0 {
  47. score += 100
  48. }
  49. }
  50. return score, ip
  51. }
  52. // HostIP tries to find an IP that can be used by other machines to reach this machine.
  53. func HostIP() (net.IP, error) {
  54. interfaces, err := net.Interfaces()
  55. if err != nil {
  56. return nil, err
  57. }
  58. bestScore := -1
  59. var bestIP net.IP
  60. // Select the highest scoring IP as the best IP.
  61. for _, iface := range interfaces {
  62. addrs, err := iface.Addrs()
  63. if err != nil {
  64. // Skip this interface if there is an error.
  65. continue
  66. }
  67. for _, addr := range addrs {
  68. score, ip := scoreAddr(iface, addr)
  69. if score > bestScore {
  70. bestScore = score
  71. bestIP = ip
  72. }
  73. }
  74. }
  75. if bestScore == -1 {
  76. return nil, errors.New("no addresses to listen on")
  77. }
  78. return bestIP, nil
  79. }