蛋蛋星球-客户端
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 

85 Zeilen
2.2 KiB

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