payment gateway for rpn cn
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

42 lines
910B

  1. package main
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "net"
  6. "net/http"
  7. "strconv"
  8. "strings"
  9. )
  10. func ip2Long(ip string) uint32 {
  11. var long uint32
  12. binary.Read(bytes.NewBuffer(net.ParseIP(ip).To4()), binary.BigEndian, &long)
  13. return long
  14. }
  15. func backtoIP4(ipInt int64) string {
  16. // need to do two bit shifting and “0xff” masking
  17. b0 := strconv.FormatInt((ipInt>>24)&0xff, 10)
  18. b1 := strconv.FormatInt((ipInt>>16)&0xff, 10)
  19. b2 := strconv.FormatInt((ipInt>>8)&0xff, 10)
  20. b3 := strconv.FormatInt((ipInt & 0xff), 10)
  21. return b0 + "." + b1 + "." + b2 + "." + b3
  22. }
  23. func getClientIP(r *http.Request) string {
  24. a := r.RemoteAddr // always be 127.0.0.1:300456 port number may vary
  25. s := strings.Split(a, ":")
  26. if s[0] == "127.0.0.1" { //loopback address
  27. ip := r.Header.Get("X-Forwarded-For")
  28. return ip
  29. }
  30. return s[0]
  31. }
  32. func getClientIPLong(r *http.Request) uint32 {
  33. s := getClientIP(r)
  34. return ip2Long(s)
  35. }