gipv4_ip.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. //
  7. package gipv4
  8. import (
  9. "errors"
  10. "net"
  11. "strconv"
  12. "strings"
  13. )
  14. // GetIpArray retrieves and returns all the ip of current host.
  15. func GetIpArray() (ips []string, err error) {
  16. interfaceAddr, err := net.InterfaceAddrs()
  17. if err != nil {
  18. return nil, err
  19. }
  20. for _, address := range interfaceAddr {
  21. ipNet, isValidIpNet := address.(*net.IPNet)
  22. if isValidIpNet && !ipNet.IP.IsLoopback() {
  23. if ipNet.IP.To4() != nil {
  24. ips = append(ips, ipNet.IP.String())
  25. }
  26. }
  27. }
  28. return ips, nil
  29. }
  30. // GetIntranetIp retrieves and returns the first intranet ip of current machine.
  31. func GetIntranetIp() (ip string, err error) {
  32. ips, err := GetIntranetIpArray()
  33. if err != nil {
  34. return "", err
  35. }
  36. if len(ips) == 0 {
  37. return "", errors.New("no intranet ip found")
  38. }
  39. return ips[0], nil
  40. }
  41. // GetIntranetIpArray retrieves and returns the intranet ip list of current machine.
  42. func GetIntranetIpArray() (ips []string, err error) {
  43. interFaces, e := net.Interfaces()
  44. if e != nil {
  45. return ips, e
  46. }
  47. for _, interFace := range interFaces {
  48. if interFace.Flags&net.FlagUp == 0 {
  49. // interface down
  50. continue
  51. }
  52. if interFace.Flags&net.FlagLoopback != 0 {
  53. // loopback interface
  54. continue
  55. }
  56. // ignore warden bridge
  57. if strings.HasPrefix(interFace.Name, "w-") {
  58. continue
  59. }
  60. addresses, e := interFace.Addrs()
  61. if e != nil {
  62. return ips, e
  63. }
  64. for _, addr := range addresses {
  65. var ip net.IP
  66. switch v := addr.(type) {
  67. case *net.IPNet:
  68. ip = v.IP
  69. case *net.IPAddr:
  70. ip = v.IP
  71. }
  72. if ip == nil || ip.IsLoopback() {
  73. continue
  74. }
  75. ip = ip.To4()
  76. if ip == nil {
  77. // not an ipv4 address
  78. continue
  79. }
  80. ipStr := ip.String()
  81. if IsIntranet(ipStr) {
  82. ips = append(ips, ipStr)
  83. }
  84. }
  85. }
  86. return ips, nil
  87. }
  88. // IsIntranet checks and returns whether given ip an intranet ip.
  89. //
  90. // Local: 127.0.0.1
  91. // A: 10.0.0.0--10.255.255.255
  92. // B: 172.16.0.0--172.31.255.255
  93. // C: 192.168.0.0--192.168.255.255
  94. func IsIntranet(ip string) bool {
  95. if ip == "127.0.0.1" {
  96. return true
  97. }
  98. array := strings.Split(ip, ".")
  99. if len(array) != 4 {
  100. return false
  101. }
  102. // A
  103. if array[0] == "10" || (array[0] == "192" && array[1] == "168") {
  104. return true
  105. }
  106. // C
  107. if array[0] == "192" && array[1] == "168" {
  108. return true
  109. }
  110. // B
  111. if array[0] == "172" {
  112. second, err := strconv.ParseInt(array[1], 10, 64)
  113. if err != nil {
  114. return false
  115. }
  116. if second >= 16 && second <= 31 {
  117. return true
  118. }
  119. }
  120. return false
  121. }