蛋蛋星球-客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

get_ip.go 2.1 KiB

3 weken geleden
3 weken geleden
3 weken geleden
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package aes
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/utils"
  5. "fmt"
  6. "net"
  7. "net/http"
  8. "strings"
  9. )
  10. func GetIpAddress(request *http.Request, masterId, pvd string) {
  11. //geoIp2db, _ := geoip2db.NewGeoipDbByStatik()
  12. //defer geoIp2db.Close()
  13. ip := GetRemoteClientIp(request)
  14. if ip != "" {
  15. //record, _ := geoIp2db.City(net.ParseIP(ip))
  16. //if record.Country.Names != nil && record.Subdivisions != nil && record.City.Names != nil {
  17. // aesData := fmt.Sprintf(
  18. // `{"country":"%s","province":"%s","city":"%s","ip":"%s", "master_id":"%s", "url":"%s", "pvd":"%s"}`,
  19. // record.Country.Names["zh-CN"], record.Subdivisions[0].Names["zh-CN"], record.City.Names["zh-CN"], ip, masterId, request.Host, pvd)
  20. aesData := fmt.Sprintf(
  21. `{"ip":"%s", "master_id":"%s", "url":"%s", "pvd":"%s"}`,
  22. ip, masterId, request.Host, pvd)
  23. fmt.Println(aesData)
  24. if cfg.Prd {
  25. //TODO::正式环境,推入rabbitMq
  26. //ch, _ := rabbit.Cfg.Pool.GetChannel()
  27. //defer ch.Release()
  28. //ch.Publish(md.UserVisitIpAddress, aesData, "")
  29. }
  30. //}
  31. }
  32. return
  33. }
  34. //func GetIp() (ip string) {
  35. // conn, _ := net.Dial("udp", "google.com:80")
  36. // if err != nil {
  37. // return
  38. // }
  39. // defer conn.Close()
  40. // ip = strings.Split(conn.LocalAddr().String(), ":")[0]
  41. // return
  42. //}
  43. // GetRemoteClientIp 获取远程客户端IP
  44. func GetRemoteClientIp(r *http.Request) string {
  45. remoteIp := r.RemoteAddr
  46. if ip := r.Header.Get("X-Real-IP"); ip != "" {
  47. if strings.Contains(ip, "172.20") {
  48. ips := strings.Split(r.Header.Get("X-Forwarded-For"), ",")
  49. if len(ips) == 2 {
  50. remoteIp = ips[0]
  51. }
  52. } else {
  53. remoteIp = ip
  54. }
  55. } else if ip = r.Header.Get("X-Forwarded-For"); ip != "" {
  56. remoteIp = ip
  57. } else {
  58. remoteIp, _, _ = net.SplitHostPort(remoteIp)
  59. //remoteIp = ""
  60. }
  61. //本地ip
  62. if remoteIp == "127.0.0.1" || remoteIp == "::1" {
  63. remoteIp = "221.4.210.1651"
  64. }
  65. if r.Host == "h5.99813608.zhiyingos.com" {
  66. utils.FilePutContents("GetRemoteClientIp", utils.SerializeStr(map[string]interface{}{
  67. "header": r.Header,
  68. "remoteIp": remoteIp,
  69. }))
  70. }
  71. fmt.Println(">>>>>>>>>>>>>>>>>>>>GetRemoteClientIp<<<<<<<<<<<<<<<<<<", remoteIp)
  72. return remoteIp
  73. }