auth.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package api
  2. import (
  3. "cockData/server/config"
  4. "cockData/server/log"
  5. "cockData/server/pkg"
  6. "cockData/server/sql"
  7. "encoding/json"
  8. "fmt"
  9. "github.com/gin-gonic/gin"
  10. "io/ioutil"
  11. "net/http"
  12. "strconv"
  13. "strings"
  14. )
  15. type DebugAuthParams struct {
  16. Token string `binding:"required" json:"token"`
  17. }
  18. type AuthParams struct {
  19. Token string `binding:"required" json:"token"`
  20. }
  21. type AuthResult struct {
  22. Session string `json:"session"`
  23. UserName string `json:"user_name"`
  24. UserAvatar string `json:"user_avatar"`
  25. Diamond int64 `json:"diamond"`
  26. CreateTime string `json:"create_time"`
  27. }
  28. type SessionCheckParams struct {
  29. SessionKey string `json:"sessionKey"`
  30. }
  31. type SessionCheckResults struct {
  32. Code int64 `json:"code"`
  33. Msg string `json:"msg"`
  34. Data struct {
  35. UserID int64 `json:"userId"`
  36. GameID int64 `json:"gameId"`
  37. TimeSecond int64 `json:"timeoutSecond"`
  38. } `json:"data"`
  39. }
  40. type SessionCheckHttpBuilder struct {
  41. }
  42. func (s SessionCheckHttpBuilder) GetParamsMap() (paramsMap map[string]string) {
  43. return
  44. }
  45. func (s SessionCheckHttpBuilder) GetApiUrl() string {
  46. return config.GetRagdollConfig().MetaConfig.PassPortCheckUrl
  47. }
  48. func (s SessionCheckHttpBuilder) GetHeaders() (headers map[string]string) {
  49. headers = make(map[string]string, 0)
  50. headers["Content-Type"] = "application/json"
  51. return
  52. }
  53. func auth(context *gin.Context) {
  54. log.Warnf("访问服务端 %s", context.Request.URL)
  55. ip := context.Request.RemoteAddr
  56. log.Infof("ip 地址 %s", ip)
  57. withoutPortIp := strings.Split(ip, ":")[0]
  58. log.Infof("不带端口ip 地址 %s", withoutPortIp)
  59. if !config.GetRagdollConfig().MetaConfig.ServerDebug {
  60. log.Warn("访问正式登录")
  61. var params AuthParams
  62. err := CheckParams(context, &params)
  63. authUserID := authSession(params.Token)
  64. log.Debugf("登录用户Id:%d", authUserID)
  65. if authUserID == 0 {
  66. ResponseApi(context, *CreateSessionErrorResponse())
  67. return
  68. }
  69. defaultName := fmt.Sprintf("survivor-%d", authUserID)
  70. defaultAvatar := ""
  71. user, err := sql.FindOrCreateUser(strconv.Itoa(int(authUserID)), defaultName, defaultAvatar)
  72. if err != nil {
  73. log.Warnf("创建用户失败 %v", err)
  74. ResponseApi(context, *CreateSessionErrorResponse())
  75. return
  76. }
  77. session, err := createSessionAndSave(user.ID, "IN")
  78. if err != nil {
  79. log.Warnf("创建session失败 %v", err)
  80. ResponseApi(context, *CreateSessionErrorResponse())
  81. return
  82. }
  83. authResult := AuthResult{
  84. Session: session,
  85. UserName: user.Name,
  86. UserAvatar: user.Avatar,
  87. Diamond: user.Diamond,
  88. CreateTime: pkg.GetFormatTime(user.CreatedAt),
  89. }
  90. ResponseApi(context, *CreateSuccessResponse(authResult))
  91. } else { // debug 模式
  92. var params DebugAuthParams
  93. err := CheckParams(context, &params)
  94. if err != nil {
  95. return
  96. }
  97. user, err := sql.FindOrCreateUser(params.Token, "", "")
  98. if err != nil {
  99. log.Warnf("创建用户失败 %v", err)
  100. ResponseApi(context, *CreateSessionErrorResponse())
  101. return
  102. }
  103. session, err := createSessionAndSave(user.ID, "IN")
  104. if err != nil {
  105. log.Warnf("创建session失败 %v", err)
  106. ResponseApi(context, *CreateSessionErrorResponse())
  107. return
  108. }
  109. authResult := AuthResult{
  110. Session: session,
  111. UserName: user.Name,
  112. UserAvatar: user.Avatar,
  113. Diamond: user.Diamond,
  114. CreateTime: pkg.GetFormatTime(user.CreatedAt),
  115. }
  116. ResponseApi(context, *CreateSuccessResponse(authResult))
  117. }
  118. }
  119. func authSession(token string) int64 {
  120. sessionCheckParams := SessionCheckParams{
  121. SessionKey: token,
  122. } // 切换标准http
  123. body, _ := json.Marshal(&sessionCheckParams)
  124. builder := SessionCheckHttpBuilder{}
  125. resp, err := http.Post(builder.GetApiUrl(), "application/json", strings.NewReader(string(body)))
  126. defer resp.Body.Close()
  127. if err != nil {
  128. log.Warnf("请求校验Session失败 %s", err)
  129. return 0
  130. } else {
  131. respBody, err := ioutil.ReadAll(resp.Body)
  132. if err != nil {
  133. log.Warnf("请求校验Session读取Resp失败 %s", err)
  134. return 0
  135. } else {
  136. var results SessionCheckResults
  137. readErr := json.Unmarshal(respBody, &results)
  138. if readErr != nil {
  139. log.Warnf("请求校验SessionJson解析失败 %s", err)
  140. return 0
  141. } else {
  142. if results.Code != 0 {
  143. log.Warnf("请求校验SessionJson应答码错误 %d %s", results.Code, results.Msg)
  144. return 0
  145. } else {
  146. return results.Data.UserID
  147. }
  148. }
  149. }
  150. }
  151. }