123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- package api
- import (
- "cockData/server/config"
- "cockData/server/log"
- "cockData/server/pkg"
- "cockData/server/sql"
- "encoding/json"
- "fmt"
- "github.com/gin-gonic/gin"
- "io/ioutil"
- "net/http"
- "strconv"
- "strings"
- )
- type DebugAuthParams struct {
- Token string `binding:"required" json:"token"`
- }
- type AuthParams struct {
- Token string `binding:"required" json:"token"`
- }
- type AuthResult struct {
- Session string `json:"session"`
- UserName string `json:"user_name"`
- UserAvatar string `json:"user_avatar"`
- Diamond int64 `json:"diamond"`
- CreateTime string `json:"create_time"`
- }
- type SessionCheckParams struct {
- SessionKey string `json:"sessionKey"`
- }
- type SessionCheckResults struct {
- Code int64 `json:"code"`
- Msg string `json:"msg"`
- Data struct {
- UserID int64 `json:"userId"`
- GameID int64 `json:"gameId"`
- TimeSecond int64 `json:"timeoutSecond"`
- } `json:"data"`
- }
- type SessionCheckHttpBuilder struct {
- }
- func (s SessionCheckHttpBuilder) GetParamsMap() (paramsMap map[string]string) {
- return
- }
- func (s SessionCheckHttpBuilder) GetApiUrl() string {
- return config.GetRagdollConfig().MetaConfig.PassPortCheckUrl
- }
- func (s SessionCheckHttpBuilder) GetHeaders() (headers map[string]string) {
- headers = make(map[string]string, 0)
- headers["Content-Type"] = "application/json"
- return
- }
- func auth(context *gin.Context) {
- log.Warnf("访问服务端 %s", context.Request.URL)
- ip := context.Request.RemoteAddr
- log.Infof("ip 地址 %s", ip)
- withoutPortIp := strings.Split(ip, ":")[0]
- log.Infof("不带端口ip 地址 %s", withoutPortIp)
- if !config.GetRagdollConfig().MetaConfig.ServerDebug {
- log.Warn("访问正式登录")
- var params AuthParams
- err := CheckParams(context, ¶ms)
- authUserID := authSession(params.Token)
- log.Debugf("登录用户Id:%d", authUserID)
- if authUserID == 0 {
- ResponseApi(context, *CreateSessionErrorResponse())
- return
- }
- defaultName := fmt.Sprintf("survivor-%d", authUserID)
- defaultAvatar := ""
- user, err := sql.FindOrCreateUser(strconv.Itoa(int(authUserID)), defaultName, defaultAvatar)
- if err != nil {
- log.Warnf("创建用户失败 %v", err)
- ResponseApi(context, *CreateSessionErrorResponse())
- return
- }
- session, err := createSessionAndSave(user.ID, "IN")
- if err != nil {
- log.Warnf("创建session失败 %v", err)
- ResponseApi(context, *CreateSessionErrorResponse())
- return
- }
- authResult := AuthResult{
- Session: session,
- UserName: user.Name,
- UserAvatar: user.Avatar,
- Diamond: user.Diamond,
- CreateTime: pkg.GetFormatTime(user.CreatedAt),
- }
- ResponseApi(context, *CreateSuccessResponse(authResult))
- } else { // debug 模式
- var params DebugAuthParams
- err := CheckParams(context, ¶ms)
- if err != nil {
- return
- }
- user, err := sql.FindOrCreateUser(params.Token, "", "")
- if err != nil {
- log.Warnf("创建用户失败 %v", err)
- ResponseApi(context, *CreateSessionErrorResponse())
- return
- }
- session, err := createSessionAndSave(user.ID, "IN")
- if err != nil {
- log.Warnf("创建session失败 %v", err)
- ResponseApi(context, *CreateSessionErrorResponse())
- return
- }
- authResult := AuthResult{
- Session: session,
- UserName: user.Name,
- UserAvatar: user.Avatar,
- Diamond: user.Diamond,
- CreateTime: pkg.GetFormatTime(user.CreatedAt),
- }
- ResponseApi(context, *CreateSuccessResponse(authResult))
- }
- }
- func authSession(token string) int64 {
- sessionCheckParams := SessionCheckParams{
- SessionKey: token,
- } // 切换标准http
- body, _ := json.Marshal(&sessionCheckParams)
- builder := SessionCheckHttpBuilder{}
- resp, err := http.Post(builder.GetApiUrl(), "application/json", strings.NewReader(string(body)))
- defer resp.Body.Close()
- if err != nil {
- log.Warnf("请求校验Session失败 %s", err)
- return 0
- } else {
- respBody, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- log.Warnf("请求校验Session读取Resp失败 %s", err)
- return 0
- } else {
- var results SessionCheckResults
- readErr := json.Unmarshal(respBody, &results)
- if readErr != nil {
- log.Warnf("请求校验SessionJson解析失败 %s", err)
- return 0
- } else {
- if results.Code != 0 {
- log.Warnf("请求校验SessionJson应答码错误 %d %s", results.Code, results.Msg)
- return 0
- } else {
- return results.Data.UserID
- }
- }
- }
- }
- }
|