user_util.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package utils
  2. import (
  3. "fmt"
  4. "net"
  5. "net/http"
  6. "regexp"
  7. "sfbase/utils"
  8. "strings"
  9. )
  10. var (
  11. userAppIDEncrypt = &utils.SimpleEncrypt{"sfis20212120"}
  12. strReg = regexp.MustCompile("^[0-9a-zA-Z]+$")
  13. )
  14. func GetAppID(tn int64) (appID string) {
  15. for {
  16. randomstr := utils.GetLetterRandom(5)
  17. str := fmt.Sprintf("%s%d%s", randomstr[:2], tn, randomstr[2:])
  18. appID = userAppIDEncrypt.EncodeString(str)
  19. if strReg.MatchString(appID) {
  20. break
  21. }
  22. }
  23. appID = "sf" + appID
  24. return
  25. }
  26. func GetIp(req *http.Request) string {
  27. if req == nil {
  28. return ""
  29. }
  30. ip_for := req.Header.Get("x-forwarded-for")
  31. ip_client := req.Header.Get("http_client_ip")
  32. ip_addr := req.Header.Get("Remote_addr")
  33. un := "unknown"
  34. if (ip_for != un) && (len(strings.TrimSpace(ip_for)) > 0) {
  35. return ip_for
  36. }
  37. if (ip_client != un) && (len(strings.TrimSpace(ip_client)) > 0) {
  38. return ip_client
  39. }
  40. if (ip_addr != un) && (len(strings.TrimSpace(ip_addr)) > 0) {
  41. return ip_addr
  42. }
  43. ip, _, _ := net.SplitHostPort(req.RemoteAddr)
  44. return ip
  45. }