login.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. package account
  2. import (
  3. "encoding/base64"
  4. "encoding/hex"
  5. "errors"
  6. "fmt"
  7. "mtp20access/client"
  8. "mtp20access/global"
  9. accountModel "mtp20access/model/account"
  10. "mtp20access/model/account/request"
  11. "mtp20access/packet"
  12. "mtp20access/rabbitmq"
  13. "mtp20access/res/pb"
  14. "sync"
  15. "mtp20access/utils"
  16. "strconv"
  17. commonRequest "mtp20access/model/common/request"
  18. "github.com/gin-gonic/gin"
  19. "github.com/gofrs/uuid"
  20. "github.com/golang/protobuf/proto"
  21. "go.uber.org/zap"
  22. )
  23. var (
  24. mtx sync.RWMutex
  25. curSessionID int = 90000 // 本服务SessionID从90000开始,以避免与旧登录服务重叠
  26. )
  27. // Login 用户登录
  28. func Login(req request.LoginReq, addr string) (loginaccount *accountModel.Loginaccount, token string, sessionID int, expiresAt int64, err error) {
  29. // 分别尝试用LoginID、LoginCode和手机号码进行登录
  30. loginaccount, _, err = getLoginAccount(req.UserName, req.Password)
  31. if err != nil {
  32. return
  33. }
  34. // 判断用户状态
  35. if loginaccount.LOGINSTATUS == 2 {
  36. err = errors.New("账户已冻结")
  37. return
  38. }
  39. if loginaccount.LOGINSTATUS == 3 {
  40. err = errors.New("账户已注销")
  41. return
  42. }
  43. // 生成Token,并写入Redis
  44. token, expiresAt, sessionID, err = buildRedisLoginInfo(*loginaccount, addr, req.ClientType)
  45. if err != nil {
  46. return
  47. }
  48. // 发送登录报文给总线
  49. loginReq := pb.LoginReq{}
  50. loginReq.LoginID = utils.SetPointValue(uint64(loginaccount.LOGINID))
  51. // loginReq.LoginPWD = utils.SetPointValue(strings.ToLower(utils.EncoderSha256(fmt.Sprintf("%v%s", loginReq.LoginID, oriPwd))))
  52. loginReq.LoginPWD = utils.SetPointValue(loginaccount.PASSWORD)
  53. loginReq.LoginIp = utils.SetPointValue(addr)
  54. loginReq.LoginType = utils.SetPointValue[uint32](0)
  55. loginReq.ClientType = utils.SetPointValue(uint32(req.ClientType))
  56. uid, _ := uuid.NewV4()
  57. loginReq.GUID = utils.SetPointValue(uid.String())
  58. loginReq.Version = utils.SetPointValue("10.0.0.1")
  59. uid, _ = uuid.NewV4()
  60. loginReq.DeviceID = utils.SetPointValue(uid.String())
  61. loginReq.ClientAppID = utils.SetPointValue("MTP20_GO_ACCESS")
  62. header := pb.MessageHead{}
  63. header.FunCode = utils.SetPointValue(uint32(global.LoginReq))
  64. uid, _ = uuid.NewV4()
  65. header.UUID = utils.SetPointValue(uid.String())
  66. loginReq.Header = &header
  67. if b, e := proto.Marshal(&loginReq); e == nil {
  68. packet := &client.MQPacket{
  69. FunCode: uint32(global.LoginReq),
  70. SessionId: uint32(sessionID),
  71. Data: &b,
  72. }
  73. go rabbitmq.Publish(global.TOPIC_REQ_USER, packet)
  74. }
  75. return
  76. }
  77. // getLoginAccount 分别尝试用LoginID、LoginCode和手机号码进行登录
  78. func getLoginAccount(userName string, password string) (loginaccount *accountModel.Loginaccount, oriPwd string, err error) {
  79. // 密码解密(5.0报文解密)
  80. d, err := base64.StdEncoding.DecodeString(password)
  81. if err != nil {
  82. return
  83. }
  84. d1 := d[4 : len(d)-8] // 解密时要去头尾
  85. p, err := packet.Decrypt(d1, packet.AESKey, true)
  86. if err != nil {
  87. return
  88. }
  89. oriPwd = string(p)
  90. // 通过LoginID查询
  91. if loginID, _ := strconv.Atoi(userName); loginID != 0 {
  92. loginaccount = &accountModel.Loginaccount{
  93. LOGINID: int64(loginID),
  94. PASSWORD: utils.EncoderSha256(fmt.Sprintf("%s%s", userName, oriPwd)), // 构建数据库存储的密码
  95. }
  96. if has, _ := global.M2A_DB.Get(loginaccount); has {
  97. return
  98. }
  99. }
  100. // 通过LoginCode查询
  101. loginaccount = &accountModel.Loginaccount{
  102. LOGINCODE: userName,
  103. }
  104. if has, _ := global.M2A_DB.Get(loginaccount); has {
  105. // 构建数据库存储的密码
  106. if loginaccount.PASSWORD == utils.EncoderSha256(fmt.Sprintf("%d%s", loginaccount.LOGINID, oriPwd)) {
  107. return
  108. }
  109. }
  110. // 通过手机号码查询,需要AES加密
  111. key, _ := hex.DecodeString(utils.AESSecretKey)
  112. if mobileEncrypted, _ := utils.AESEncrypt([]byte(userName), key); mobileEncrypted != nil {
  113. // 从三方表获取LoginID
  114. userauthinfo := &accountModel.Userauthinfo{
  115. AUTHID: hex.EncodeToString(mobileEncrypted),
  116. AUTHTYPE: 3,
  117. }
  118. if has, _ := global.M2A_DB.Get(userauthinfo); has {
  119. loginaccount = &accountModel.Loginaccount{
  120. LOGINID: userauthinfo.LOGINID,
  121. PASSWORD: utils.EncoderSha256(fmt.Sprintf("%v%s", userauthinfo.LOGINID, oriPwd)), // 构建数据库存储的密码
  122. }
  123. if has, _ := global.M2A_DB.Get(loginaccount); has {
  124. return
  125. }
  126. }
  127. // loginaccount = &accountModel.Loginaccount{
  128. // MOBILE: string(mobileEncrypted),
  129. // }
  130. // if has, _ := global.M2A_DB.Get(loginaccount); has {
  131. // // 构建数据库存储的密码
  132. // if loginaccount.PASSWORD == utils.EncoderSha256(fmt.Sprintf("%d%s", loginaccount.LOGINID, pwd)) {
  133. // return
  134. // }
  135. // }
  136. }
  137. err = errors.New("错误的用户名或密码")
  138. return
  139. }
  140. // newSessionID 获取
  141. func newSessionID() int {
  142. mtx.RLock()
  143. defer mtx.RUnlock()
  144. curSessionID += 1
  145. return curSessionID
  146. }
  147. // buildRedisLoginInfo 生成Token,并写入Redis
  148. func buildRedisLoginInfo(loginaccount accountModel.Loginaccount, addr string, group int) (token string, expiresAt int64, sessionID int, err error) {
  149. // 生成SessionID
  150. sessionID = newSessionID()
  151. // 生成本服务Token
  152. j := &utils.JWT{SigningKey: []byte(global.M2A_CONFIG.JWT.SigningKey)} // 唯一签名
  153. claims := j.CreateClaims(commonRequest.BaseClaims{
  154. LoginID: int(loginaccount.LOGINID),
  155. Group: group,
  156. SessionID: sessionID,
  157. })
  158. token, err = j.CreateToken(claims)
  159. if err != nil {
  160. global.M2A_LOG.Error("生成本服token失败", zap.Error(err))
  161. return
  162. }
  163. expiresAt = claims.RegisteredClaims.ExpiresAt.Unix()
  164. loginLogin := client.LoginRedis{
  165. LoginID: strconv.Itoa(int(loginaccount.LOGINID)),
  166. UserID: strconv.Itoa(int(loginaccount.USERID)),
  167. SessionID: strconv.Itoa(sessionID),
  168. Token: token,
  169. Group: strconv.Itoa(group),
  170. Addr: addr,
  171. }
  172. loginMap, err := loginLogin.ToMap()
  173. // loginMap := map[string]interface{}{
  174. // "LoginID": strconv.Itoa(int(loginaccount.LOGINID)),
  175. // "UserID": strconv.Itoa(int(loginaccount.USERID)),
  176. // "SessionID": strconv.Itoa(sessionID),
  177. // "Token": token,
  178. // "Group": strconv.Itoa(group),
  179. // "Addr": addr,
  180. // }
  181. if err != nil {
  182. global.M2A_LOG.Error("生成登录信息MAP失败", zap.Error(err))
  183. return
  184. }
  185. if err = j.SetRedisLogin(int(loginaccount.LOGINID), group, loginMap); err != nil {
  186. global.M2A_LOG.Error("Token写入Redis失败", zap.Error(err))
  187. return
  188. }
  189. // 生成旧登录服务Token
  190. // if err = j.SetOriRedisToken(int(loginaccount.LOGINID), group); err != nil {
  191. // // FIXME: 这里有类事务的回滚问题
  192. // global.M2A_LOG.Error("生成旧登录服务Token失败", zap.Error(err))
  193. // return
  194. // }
  195. // 记录用户信息
  196. mtx.Lock()
  197. defer mtx.Unlock()
  198. if client.Clients == nil {
  199. client.Clients = make(map[int]*client.Client, 0)
  200. }
  201. delete(client.Clients, claims.SessionID)
  202. client.Clients[claims.SessionID] = &client.Client{LoginRedis: loginLogin}
  203. return
  204. }
  205. // RestoreLoginWithToken 通过Token检验恢复登录状态失败
  206. func RestoreLoginWithToken(loginID int, group int, token string) (err error) {
  207. // 从Redis获取登录信息
  208. j := utils.NewJWT()
  209. loginMap, err := j.GetRedisLogin(loginID, group)
  210. if err != nil {
  211. global.M2A_LOG.Error("Token检验恢复登录状态失败", zap.Error(err))
  212. return
  213. }
  214. loginId := loginMap["loginId"]
  215. userId := loginMap["userId"]
  216. sessionId := loginMap["sessionId"]
  217. addr := loginMap["addr"]
  218. loginLogin := client.LoginRedis{
  219. LoginID: loginId,
  220. UserID: userId,
  221. SessionID: sessionId,
  222. Token: token,
  223. Group: strconv.Itoa(group),
  224. Addr: addr,
  225. }
  226. // 记录用户信息
  227. mtx.Lock()
  228. defer mtx.Unlock()
  229. if client.Clients == nil {
  230. client.Clients = make(map[int]*client.Client, 0)
  231. }
  232. s, err := strconv.Atoi(sessionId)
  233. if err != nil {
  234. global.M2A_LOG.Error("Token检验恢复登录状态失败", zap.Error(err))
  235. return
  236. }
  237. delete(client.Clients, s)
  238. client.Clients[s] = &client.Client{LoginRedis: loginLogin}
  239. return
  240. }
  241. // GetClientsByAccountID 通过资金账户获取所有的
  242. func GetClientsByAccountID(accountID uint64) (clients []*client.Client, err error) {
  243. clients = make([]*client.Client, 0)
  244. loginIds := make([]string, 0)
  245. sql := fmt.Sprintf(`
  246. SELECT
  247. to_char(t.loginid)
  248. FROM loginaccount t
  249. INNER JOIN taaccount a ON a.userid = t.userid
  250. WHERE a.accountid = %v
  251. `, accountID)
  252. if err = global.M2A_DB.SQL(sql).Find(&loginIds); err != nil {
  253. global.M2A_LOG.Error("获取LoginID失败", zap.Error(err))
  254. return
  255. }
  256. var mtx sync.RWMutex
  257. mtx.Lock()
  258. defer mtx.Unlock()
  259. if len(loginIds) > 0 && len(client.Clients) > 0 {
  260. for _, item := range loginIds {
  261. for i := range client.Clients {
  262. c := client.Clients[i]
  263. if c.LoginID == item {
  264. clients = append(clients, c)
  265. }
  266. }
  267. }
  268. }
  269. return
  270. }
  271. func Logout(c *gin.Context) (err error) {
  272. // 获取请求账号信息
  273. s, exists := c.Get("claims")
  274. if !exists {
  275. err = errors.New("获取请求账号信息异常")
  276. global.M2A_LOG.Error(err.Error(), zap.Error(err))
  277. return
  278. }
  279. claims := s.(*commonRequest.CustomClaims)
  280. // 获取登录账户信息
  281. t, exists := client.Clients[claims.SessionID]
  282. if !exists {
  283. err = errors.New("获取登录账户信息异常")
  284. global.M2A_LOG.Error(err.Error(), zap.Error(err))
  285. return
  286. }
  287. // 发送登出报文给总线
  288. logoutReq := pb.LogoutReq{}
  289. logoutReq.LoginID = utils.SetPointValue(uint64(claims.LoginID))
  290. logoutReq.Token = utils.SetPointValue(t.OldToken)
  291. logoutReq.LoginIp = utils.SetPointValue(c.ClientIP())
  292. port, _ := strconv.Atoi(c.Request.URL.Port())
  293. logoutReq.LoginPort = utils.SetPointValue(uint32(port))
  294. header := pb.MessageHead{}
  295. header.FunCode = utils.SetPointValue(uint32(global.LogoutReq))
  296. userID, _ := strconv.Atoi(t.UserID)
  297. header.UserID = utils.SetPointValue(uint32(userID))
  298. uid, _ := uuid.NewV4()
  299. header.UUID = utils.SetPointValue(uid.String())
  300. logoutReq.Header = &header
  301. if b, e := proto.Marshal(&logoutReq); e == nil {
  302. packet := &client.MQPacket{
  303. FunCode: uint32(global.LogoutReq),
  304. SessionId: uint32(claims.SessionID),
  305. Data: &b,
  306. }
  307. go rabbitmq.Publish(global.TOPIC_REQ_USER, packet)
  308. }
  309. return
  310. }