gipv4_ip.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright GoFrame Author(https://goframe.org). 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. "net"
  10. "strconv"
  11. "strings"
  12. "github.com/gogf/gf/v2/errors/gerror"
  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. err = gerror.Wrap(err, `net.InterfaceAddrs failed`)
  19. return nil, err
  20. }
  21. for _, address := range interfaceAddr {
  22. ipNet, isValidIpNet := address.(*net.IPNet)
  23. if isValidIpNet && !ipNet.IP.IsLoopback() {
  24. if ipNet.IP.To4() != nil {
  25. ips = append(ips, ipNet.IP.String())
  26. }
  27. }
  28. }
  29. return ips, nil
  30. }
  31. // MustGetIntranetIp performs as GetIntranetIp, but it panics if any error occurs.
  32. func MustGetIntranetIp() string {
  33. ip, err := GetIntranetIp()
  34. if err != nil {
  35. panic(err)
  36. }
  37. return ip
  38. }
  39. // GetIntranetIp retrieves and returns the first intranet ip of current machine.
  40. func GetIntranetIp() (ip string, err error) {
  41. ips, err := GetIntranetIpArray()
  42. if err != nil {
  43. return "", err
  44. }
  45. if len(ips) == 0 {
  46. return "", gerror.New("no intranet ip found")
  47. }
  48. return ips[0], nil
  49. }
  50. // GetIntranetIpArray retrieves and returns the intranet ip list of current machine.
  51. func GetIntranetIpArray() (ips []string, err error) {
  52. var (
  53. addresses []net.Addr
  54. interFaces []net.Interface
  55. )
  56. interFaces, err = net.Interfaces()
  57. if err != nil {
  58. err = gerror.Wrap(err, `net.Interfaces failed`)
  59. return ips, err
  60. }
  61. for _, interFace := range interFaces {
  62. if interFace.Flags&net.FlagUp == 0 {
  63. // interface down
  64. continue
  65. }
  66. if interFace.Flags&net.FlagLoopback != 0 {
  67. // loop back interface
  68. continue
  69. }
  70. // ignore warden bridge
  71. if strings.HasPrefix(interFace.Name, "w-") {
  72. continue
  73. }
  74. addresses, err = interFace.Addrs()
  75. if err != nil {
  76. err = gerror.Wrap(err, `interFace.Addrs failed`)
  77. return ips, err
  78. }
  79. for _, addr := range addresses {
  80. var ip net.IP
  81. switch v := addr.(type) {
  82. case *net.IPNet:
  83. ip = v.IP
  84. case *net.IPAddr:
  85. ip = v.IP
  86. }
  87. if ip == nil || ip.IsLoopback() {
  88. continue
  89. }
  90. ip = ip.To4()
  91. if ip == nil {
  92. // not an ipv4 address
  93. continue
  94. }
  95. ipStr := ip.String()
  96. if IsIntranet(ipStr) {
  97. ips = append(ips, ipStr)
  98. }
  99. }
  100. }
  101. return ips, nil
  102. }
  103. // IsIntranet checks and returns whether given ip an intranet ip.
  104. //
  105. // Local: 127.0.0.1
  106. // A: 10.0.0.0--10.255.255.255
  107. // B: 172.16.0.0--172.31.255.255
  108. // C: 192.168.0.0--192.168.255.255
  109. func IsIntranet(ip string) bool {
  110. if ip == "127.0.0.1" {
  111. return true
  112. }
  113. array := strings.Split(ip, ".")
  114. if len(array) != 4 {
  115. return false
  116. }
  117. // A
  118. if array[0] == "10" || (array[0] == "192" && array[1] == "168") {
  119. return true
  120. }
  121. // C
  122. if array[0] == "192" && array[1] == "168" {
  123. return true
  124. }
  125. // B
  126. if array[0] == "172" {
  127. second, err := strconv.ParseInt(array[1], 10, 64)
  128. if err != nil {
  129. return false
  130. }
  131. if second >= 16 && second <= 31 {
  132. return true
  133. }
  134. }
  135. return false
  136. }