Guest User

Untitled

a guest
Apr 4th, 2024
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. package models
  2.  
  3. import (
  4. "crypto/md5"
  5. "crypto/sha256"
  6. "errors"
  7. "net"
  8. "net/http"
  9. "strings"
  10.  
  11. "github.com/google/uuid"
  12. )
  13.  
  14. type User struct {
  15. ID uint `json:"id" gorm:"primarykey"`
  16. Username string `json:"username" gorm:"unique"`
  17. Realname string `json:"realname"`
  18. Password string `json:"password"`
  19. Ip string `json:"ip"`
  20. Lastlogin int64 `json:"lastlogin"`
  21. Regdate int64 `json:"regdate"`
  22. Regip string `json:"regip"`
  23. Email string `json:"email"`
  24. IsLogged bool `json:"isLogged" gorm:"column:isLogged"`
  25. HasSession bool `json:"hasSession" gorm:"column:hasSession"`
  26. Coin int64 `json:"coin"`
  27. UUID string `json:"uuid" gorm:"column:UUID"`
  28. }
  29.  
  30. type Skin struct {
  31. Nick string `json:"Nick" gorm:"unique"`
  32. Value string `json:"Value"`
  33. Signature string `json:"Signature"`
  34. Timestamp int64 `json:"timestamp"`
  35. }
  36.  
  37. func (User) TableName() string {
  38. return "authme"
  39. }
  40.  
  41. func (Skin) TableName() string {
  42. return "sr_Skins"
  43. }
  44.  
  45. func (user *User) HashPassword(password string) error {
  46. hasher := sha256.New()
  47. hasher.Write([]byte(password))
  48. user.Password = string(hasher.Sum(nil))
  49. return nil
  50. }
  51.  
  52. func (user *User) CheckPassword(providedPassword string) error {
  53. hasher := sha256.New()
  54. hasher.Write([]byte(providedPassword))
  55. providedHashedPassword := string(hasher.Sum(nil))
  56. if user.Password != providedHashedPassword {
  57. return errors.New("invalid password")
  58. }
  59. return nil
  60. }
  61.  
  62. func (user *User) NameToUUID() error {
  63. if user.Realname == "" {
  64. return errors.New("realname not set")
  65. }
  66. var version = 3
  67. h := md5.New()
  68. h.Write([]byte("OfflinePlayer:"))
  69. h.Write([]byte(user.Realname))
  70. var id uuid.UUID
  71. h.Sum(id[:0])
  72. id[6] = (id[6] & 0x0f) | uint8((version&0xf)<<4)
  73. id[8] = (id[8] & 0x3f) | 0x80 // RFC 4122 variant
  74. user.UUID = id.String()
  75. return nil
  76. }
  77.  
  78. func (user *User) SetIP(r *http.Request) error {
  79. //Get IP from the X-REAL-IP header
  80. ip := r.Header.Get("X-REAL-IP")
  81. netIP := net.ParseIP(ip)
  82. if netIP != nil {
  83. user.Ip = ip
  84. user.Regip = ip
  85. return nil
  86. }
  87.  
  88. //Get IP from X-FORWARDED-FOR header
  89. ips := r.Header.Get("X-FORWARDED-FOR")
  90. splitIps := strings.Split(ips, ",")
  91. for _, ip := range splitIps {
  92. netIP := net.ParseIP(ip)
  93. if netIP != nil {
  94. user.Ip = ip
  95. user.Regip = ip
  96. return nil
  97. }
  98. }
  99.  
  100. //Get IP from RemoteAddr
  101. ip, _, err := net.SplitHostPort(r.RemoteAddr)
  102. if err != nil {
  103. return err
  104. }
  105. netIP = net.ParseIP(ip)
  106. if netIP != nil {
  107. user.Ip = ip
  108. user.Regip = ip
  109. return nil
  110. }
  111. return errors.New("no valid ip found")
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment