gipv4_ip.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. "github.com/gogf/gf/errors/gcode"
  10. "github.com/gogf/gf/errors/gerror"
  11. "net"
  12. "strconv"
  13. "strings"
  14. )
  15. // GetIpArray retrieves and returns all the ip of current host.
  16. func GetIpArray() (ips []string, err error) {
  17. interfaceAddr, err := net.InterfaceAddrs()
  18. if err != nil {
  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. // GetIntranetIp retrieves and returns the first intranet ip of current machine.
  32. func GetIntranetIp() (ip string, err error) {
  33. ips, err := GetIntranetIpArray()
  34. if err != nil {
  35. return "", err
  36. }
  37. if len(ips) == 0 {
  38. return "", gerror.NewCode(gcode.CodeOperationFailed, "no intranet ip found")
  39. }
  40. return ips[0], nil
  41. }
  42. // GetIntranetIpArray retrieves and returns the intranet ip list of current machine.
  43. func GetIntranetIpArray() (ips []string, err error) {
  44. interFaces, e := net.Interfaces()
  45. if e != nil {
  46. return ips, e
  47. }
  48. for _, interFace := range interFaces {
  49. if interFace.Flags&net.FlagUp == 0 {
  50. // interface down
  51. continue
  52. }
  53. if interFace.Flags&net.FlagLoopback != 0 {
  54. // loopback interface
  55. continue
  56. }
  57. // ignore warden bridge
  58. if strings.HasPrefix(interFace.Name, "w-") {
  59. continue
  60. }
  61. addresses, e := interFace.Addrs()
  62. if e != nil {
  63. return ips, e
  64. }
  65. for _, addr := range addresses {
  66. var ip net.IP
  67. switch v := addr.(type) {
  68. case *net.IPNet:
  69. ip = v.IP
  70. case *net.IPAddr:
  71. ip = v.IP
  72. }
  73. if ip == nil || ip.IsLoopback() {
  74. continue
  75. }
  76. ip = ip.To4()
  77. if ip == nil {
  78. // not an ipv4 address
  79. continue
  80. }
  81. ipStr := ip.String()
  82. if IsIntranet(ipStr) {
  83. ips = append(ips, ipStr)
  84. }
  85. }
  86. }
  87. return ips, nil
  88. }
  89. // IsIntranet checks and returns whether given ip an intranet ip.
  90. //
  91. // Local: 127.0.0.1
  92. // A: 10.0.0.0--10.255.255.255
  93. // B: 172.16.0.0--172.31.255.255
  94. // C: 192.168.0.0--192.168.255.255
  95. func IsIntranet(ip string) bool {
  96. if ip == "127.0.0.1" {
  97. return true
  98. }
  99. array := strings.Split(ip, ".")
  100. if len(array) != 4 {
  101. return false
  102. }
  103. // A
  104. if array[0] == "10" || (array[0] == "192" && array[1] == "168") {
  105. return true
  106. }
  107. // C
  108. if array[0] == "192" && array[1] == "168" {
  109. return true
  110. }
  111. // B
  112. if array[0] == "172" {
  113. second, err := strconv.ParseInt(array[1], 10, 64)
  114. if err != nil {
  115. return false
  116. }
  117. if second >= 16 && second <= 31 {
  118. return true
  119. }
  120. }
  121. return false
  122. }