geoinfo.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package utils
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net"
  6. "net/http"
  7. )
  8. type GeoInfo struct {
  9. IP string
  10. CountryCode string
  11. Location string
  12. }
  13. // GetGeoInfo returns the ip, location and country code of the host it's called on.
  14. func GetGeoInfo(ip ...net.IP) (*GeoInfo, error) {
  15. geoInfo, err := getGeoInfoFromIPAPI(ip...)
  16. if err == nil {
  17. return geoInfo, nil
  18. }
  19. geoInfo, err = getGeoInfoFromCloudFlare(ip...)
  20. if err == nil {
  21. return geoInfo, nil
  22. }
  23. return getGeoInfoFromIpInfo(ip...)
  24. }
  25. func getGeoInfoFromIPAPI(ip ...net.IP) (*GeoInfo, error) {
  26. url := "https://api.ipapi.is"
  27. if len(ip) > 0 {
  28. url = fmt.Sprintf("https://api.ipapi.is/?q=%s", ip[0].String())
  29. }
  30. resp, err := http.Get(url)
  31. if err != nil {
  32. return nil, err
  33. }
  34. defer resp.Body.Close()
  35. var data struct {
  36. IP string `json:"ip"`
  37. Location struct {
  38. CountryCode string `json:"country_code"`
  39. Latitude float64 `json:"latitude"`
  40. Longitude float64 `json:"longitude"`
  41. } `json:"location"`
  42. }
  43. if resp.StatusCode != 200 {
  44. return nil, fmt.Errorf("status code %d", resp.StatusCode)
  45. }
  46. err = json.NewDecoder(resp.Body).Decode(&data)
  47. if err != nil {
  48. return nil, err
  49. }
  50. return &GeoInfo{
  51. IP: data.IP,
  52. Location: fmt.Sprintf("%f,%f", data.Location.Latitude, data.Location.Longitude),
  53. CountryCode: data.Location.CountryCode,
  54. }, nil
  55. }
  56. func getGeoInfoFromCloudFlare(ip ...net.IP) (*GeoInfo, error) {
  57. var geoInfo GeoInfo
  58. resp, err := http.Get("https://speed.cloudflare.com/meta")
  59. if err != nil {
  60. return nil, err
  61. }
  62. defer resp.Body.Close()
  63. respMap := make(map[string]interface{})
  64. err = json.NewDecoder(resp.Body).Decode(&respMap)
  65. if err != nil {
  66. return nil, err
  67. }
  68. _, ok := respMap["clientIp"]
  69. if ok {
  70. geoInfo.IP = respMap["clientIp"].(string)
  71. }
  72. _, ok = respMap["country"]
  73. if ok {
  74. geoInfo.CountryCode = respMap["country"].(string)
  75. }
  76. var latitude, longitude string
  77. _, ok = respMap["latitude"]
  78. if ok {
  79. latitude = respMap["latitude"].(string)
  80. }
  81. _, ok = respMap["longitude"]
  82. if ok {
  83. longitude = respMap["longitude"].(string)
  84. }
  85. if latitude != "" && longitude != "" {
  86. geoInfo.Location = latitude + "," + longitude
  87. }
  88. return &geoInfo, nil
  89. }
  90. func getGeoInfoFromIpInfo(ip ...net.IP) (*GeoInfo, error) {
  91. url := "https://ipinfo.io/json"
  92. if len(ip) > 0 {
  93. url = fmt.Sprintf("https://ipinfo.io/%s/json", ip[0].String())
  94. }
  95. var geoInfo GeoInfo
  96. resp, err := http.Get(url)
  97. if err != nil {
  98. return nil, err
  99. }
  100. defer resp.Body.Close()
  101. var data struct {
  102. IP string `json:"ip"`
  103. Loc string `json:"loc"`
  104. Country string `json:"country"`
  105. }
  106. err = json.NewDecoder(resp.Body).Decode(&data)
  107. if err != nil {
  108. return nil, err
  109. }
  110. geoInfo.IP = data.IP
  111. geoInfo.CountryCode = data.Country
  112. geoInfo.Location = data.Loc
  113. return &geoInfo, nil
  114. }