sdkcheck.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. package model
  2. import (
  3. "crypto/md5"
  4. "encoding/base64"
  5. "encoding/json"
  6. "fmt"
  7. "net/http"
  8. "rocommon"
  9. "rocommon/rpc"
  10. "rocommon/service"
  11. "rocommon/socket"
  12. "rocommon/util"
  13. "strconv"
  14. "strings"
  15. "sync"
  16. "github.com/gin-gonic/gin"
  17. )
  18. type ServiceParam struct {
  19. ServiceName string
  20. ServiceType string //tcpAcceptor /tcpConnector
  21. ProcName string
  22. LisAddr string
  23. DiscoveryServiceName string //用于服务器发现
  24. DiscoveryServiceZone int //用于服务器发现
  25. }
  26. var (
  27. httpParamMutex sync.RWMutex
  28. HttpNodeParam *ServiceParam
  29. )
  30. func GetHttpNodeParam() ServiceParam {
  31. httpParamMutex.RLock()
  32. defer httpParamMutex.RUnlock()
  33. return *HttpNodeParam
  34. }
  35. func SetHttpNodeParam(param *ServiceParam) {
  36. httpParamMutex.Lock()
  37. defer httpParamMutex.Unlock()
  38. HttpNodeParam = param
  39. }
  40. func CreateHttpConnector(param ServiceParam) rocommon.ServerNode {
  41. if param.ServiceType == "" {
  42. param.ServiceType = "httpConnector"
  43. }
  44. node := socket.NewServerNode(param.ServiceType, param.ServiceName, param.LisAddr, nil)
  45. return node
  46. }
  47. // /////////////////////////////////////////////////////////////
  48. const (
  49. SDKPlatform_Quick = "SDKQuick"
  50. SDKPlatform_HW_Quick = "SDKHwQuick"
  51. SDKPlatform_NBSDK = "SDKNB"
  52. SDKPlatform_UniSDK = "SDKUni" //SDKUni
  53. SDKPlatform_YouYi = "SDKYOUYI"
  54. SDKPlatform_YouYi_IOS = "SDKYOUYI_IOS"
  55. )
  56. // 区分不同平台
  57. func ConvertPlatform(openId, platform string) string {
  58. if platform == SDKPlatform_YouYi || platform == SDKPlatform_YouYi_IOS {
  59. //除了quick和nb其他渠道都要加上渠道标识
  60. openId = platform + openId
  61. }
  62. return openId
  63. }
  64. func SDKCheck(c *gin.Context) {
  65. platform := c.DefaultQuery("platform", "") ///default ""
  66. sdkUid := c.DefaultQuery("sdkuid", "") ///default ""
  67. sauthJson := c.DefaultQuery("sauthjson", "") ///default ""
  68. serverId := c.DefaultQuery("sid", "") ///default ""
  69. // subPlatform := c.DefaultQuery("subplatform", "") ///default ""
  70. uid := c.DefaultQuery("uid", "") ///default ""
  71. retSt := &UniSDKLoginClient{
  72. Code: 1,
  73. Platform: platform,
  74. }
  75. if platform != "" {
  76. switch platform {
  77. case SDKPlatform_HW_Quick:
  78. SDKHwQuickCheck(c, platform, sauthJson, uid)
  79. case SDKPlatform_Quick:
  80. case SDKPlatform_NBSDK:
  81. case SDKPlatform_UniSDK:
  82. SDKUniCheck(sdkUid, sauthJson, serverId, c)
  83. case SDKPlatform_YouYi:
  84. fallthrough
  85. case SDKPlatform_YouYi_IOS:
  86. // SDKYouYiCheck(sauthJson, c, subPlatform, platform)
  87. SDKHwQuickCheck(c, platform, sauthJson, uid) //为了quick海外做的适配
  88. default:
  89. c.JSON(http.StatusOK, retSt)
  90. }
  91. } else {
  92. c.JSON(http.StatusOK, retSt)
  93. }
  94. }
  95. // SDK UniSDK
  96. type SDKUniLoginAuthCheckReq struct {
  97. GameId string `json:"gameid""` //游戏id(手游即游戏代号,小写)
  98. HostId int `json:"hostid"` //大于等于0, 游戏服务器id(指游戏划分的区服),如果有则传,没有传0
  99. LoginChannel string `json:"login_channel"` //登录渠道( 如用网易sdk登录login_channel=netease,苹果支付pay_chan nel=app_store)
  100. AppChannel string `json:"app_channel"` //发行(分发)渠道,支持多级结构,通过UniSDK获取。格式如:netease.gs.123456
  101. Platform string `json:"platform"` //平台名
  102. Ip string `json:"ip"` //游戏登录时客户端用户ip, 形如255.255.255.255
  103. SdkUid string `json:"sdkuid"` //渠道uid,不需要包含域名, 有些渠道的uid中包含有@符号 | wechat_mp_vr渠道传openid
  104. SessionId string `json:"sessionid"` //sdk返回的session
  105. SDKVersion string `json:"sdk_version"` //接入渠道sdk的版本号,从UniSDK获取
  106. Udid string `json:"udid"` //玩家登录的移动设备号,通过UniSDK提供的接口获取
  107. }
  108. type SDKUniLoginAuthCheckResp struct {
  109. Code int `json:"code"` //接口返回码
  110. SubCode int `json:"subcode"` //接口返回子码
  111. Status string `json:"status"` //接口调用状态
  112. UniSDKLoginJson string `json:"unisdk_login_json"`
  113. Aid int `json:"aid"`
  114. AasMsg UniSDKLoginAasMsgSt `json:"aas_msg"` //aas_msg
  115. RetCode string
  116. ClientId uint64
  117. ServiceId string
  118. OpenId string
  119. }
  120. type UniSDKLoginJsonSt struct {
  121. Aid string `json:"aid"`
  122. Sdkuid string `json:"sdkUid"`
  123. Username string `json:"username"`
  124. AccessToken string `json:"access_token"`
  125. ExpiresIn string `json:"expires_in"`
  126. }
  127. type UniSDKLoginAasMsgSt struct {
  128. NeedRealNameV2 bool `json:"need_realname_v2"`
  129. ReasonV2 string `json:"reason_v2"`
  130. NeedRoleEnterV2 bool `json:"need_role_enter_v2"`
  131. }
  132. // 返回给客户端的json结构
  133. type UniSDKLoginClient struct {
  134. Code int `json:"code"` //接口返回码
  135. UniSDKLoginJson string `json:"unisdk_login_json"`
  136. UserInfo string `json:"userinfo"` //玩家信息包括错误码,aid和过期时间 userinfo.sign
  137. //SignInfo string `json:"sign"` //校验信息(userinfo使用aes加密后的结果)
  138. Platform string `json:"platform"` //用户平台
  139. }
  140. type SDKCheckUserInfo struct {
  141. Code int `json:"code"`
  142. Aid string `json:"aid"`
  143. ExpireTime uint64 `json:"expiretime"`
  144. LoginChannel string `json:"login_channel"` //登录渠道( 如用网易sdk登录login_channel=netease,苹果支付pay_chan nel=app_store)
  145. AppChannel string `json:"app_channel"` //发行(分发)渠道,支持多级结构,通过UniSDK获取。格式如:netease.gs.123456
  146. EnterSN string `json:"enter_sn"`
  147. }
  148. var sdkUniSignAesKey = []byte("wenting123456789")
  149. func SDKUniCheck(sdkUid, sauthJson, sid string, c *gin.Context) {
  150. tmpRequest := &rocommon.HTTPRequest{}
  151. tmpRequest.ReqCodecName = "httpjson"
  152. //sauthJson解析并替换
  153. //sauthJson = strings.Replace(sauthJson, "\\", "", -1)
  154. authHttpAddr := service.GetServiceConfig().SDKConfig.UniHttpAddr
  155. ip := c.ClientIP()
  156. serverId, _ := strconv.Atoi(sid)
  157. tmpKVList := map[string]interface{}{}
  158. err := json.Unmarshal([]byte(sauthJson), &tmpKVList)
  159. if err != nil {
  160. util.InfoF("LoginVerifySignUniSDK sdkuid=%v err=%v", sdkUid, err)
  161. c.JSON(http.StatusOK, fmt.Sprintf("invalid sauthjson"))
  162. return
  163. }
  164. tmpKVList["hostid"] = serverId
  165. ipStrList := strings.Split(ip, ":")
  166. if len(ipStrList) > 0 {
  167. tmpKVList["ip"] = ipStrList[0]
  168. }
  169. tmpRequest.ReqMsg = tmpKVList
  170. tmpRequest.ResMsg = &SDKUniLoginAuthCheckResp{}
  171. parm := GetHttpNodeParam()
  172. parm.LisAddr = authHttpAddr
  173. httpNode := CreateHttpConnector(parm)
  174. err = httpNode.(rocommon.HTTPConnector).Request("POST", "", tmpRequest)
  175. if err != nil {
  176. //tmpRequest.ResMsg.(*SDKUniLoginAuthCheckResp).RetCode = "0"
  177. util.InfoF("LoginVerifySignUniSDK http Request sdkUid=%v err=%v", sdkUid, err)
  178. c.JSON(http.StatusOK, err)
  179. return
  180. }
  181. res := tmpRequest.ResMsg.(*SDKUniLoginAuthCheckResp)
  182. if res.AasMsg.NeedRoleEnterV2 {
  183. //todo...
  184. // 获取enter_sn
  185. }
  186. retSt := &UniSDKLoginClient{
  187. Code: res.Code,
  188. Platform: SDKPlatform_UniSDK,
  189. }
  190. tmpJson, err := base64.StdEncoding.DecodeString(res.UniSDKLoginJson)
  191. if err == nil {
  192. loginJsonSt := &UniSDKLoginJsonSt{}
  193. err = json.Unmarshal(tmpJson, loginJsonSt)
  194. if err == nil {
  195. loginChannel := tmpKVList["login_channel"]
  196. appChannel := tmpKVList["app_channel"]
  197. checkUserInfo := &SDKCheckUserInfo{
  198. Code: res.Code,
  199. Aid: loginJsonSt.Aid,
  200. ExpireTime: uint64(util.GetTimeSeconds() + 5*60*60),
  201. LoginChannel: loginChannel.(string),
  202. AppChannel: appChannel.(string),
  203. }
  204. userInfoData, _ := json.Marshal(checkUserInfo)
  205. signInfoData, err := rpc.AESCtrEncrypt(userInfoData, sdkUniSignAesKey, sdkUniSignAesKey...)
  206. userInfoStr := base64.StdEncoding.EncodeToString(userInfoData)
  207. signInfoStr := base64.StdEncoding.EncodeToString(signInfoData)
  208. if err == nil {
  209. retSt.UniSDKLoginJson = res.UniSDKLoginJson
  210. retSt.UserInfo = userInfoStr + "." + signInfoStr
  211. c.JSON(http.StatusOK, retSt)
  212. return
  213. }
  214. }
  215. }
  216. c.JSON(http.StatusOK, retSt)
  217. }
  218. type SDKYouYiLoginAuthCheckResp struct {
  219. Code int `json:"code"` //接口返回码
  220. Message string `json:"message"` //接口调用状态
  221. //Data []YouYiSDKLoginDataSt `json:"data"`
  222. Data interface{} `json:"data"`
  223. RetCode string
  224. ClientId uint64
  225. ServiceId string
  226. OpenId string
  227. }
  228. type YouYiSDKLoginDataSt struct {
  229. CPUid string `json:"cp_uid"`
  230. }
  231. // 返回给客户端的json结构
  232. type YouYiSDKLoginClient struct {
  233. Code int `json:"code"` //接口返回码
  234. UserInfo string `json:"userinfo"` //玩家信息包括错误码,aid和过期时间 userinfo.sign
  235. //SignInfo string `json:"sign"` //校验信息(userinfo使用aes加密后的结果)
  236. Platform string `json:"platform"` //用户平台
  237. }
  238. func SDKYouYiCheck(sauthJson string, c *gin.Context, subPlatform, platform string) {
  239. tmpRequest := &rocommon.HTTPRequest{}
  240. tmpRequest.ReqCodecName = "httpjson"
  241. sConfig := service.GetServiceConfig()
  242. authHttpAddr := sConfig.SDKConfig.YouYiHttpAddr
  243. tmpRequest.ResMsg = &SDKYouYiLoginAuthCheckResp{}
  244. gameId := sConfig.SDKConfig.YouYiGameId
  245. if platform == SDKPlatform_YouYi_IOS {
  246. gameId = sConfig.SDKConfig.YouYiGameIdIOS
  247. for idx := 0; idx < len(sConfig.SDKConfig.YouYiGameIdListIOS); idx++ {
  248. if strings.Contains(sConfig.SDKConfig.YouYiGameIdListIOS[idx], subPlatform) {
  249. idStrList := strings.Split(sConfig.SDKConfig.YouYiGameIdListIOS[idx], ":")
  250. if len(idStrList) >= 2 {
  251. gameId = idStrList[1]
  252. }
  253. break
  254. }
  255. }
  256. } else {
  257. for idx := 0; idx < len(sConfig.SDKConfig.YouYiGameIdList); idx++ {
  258. if strings.Contains(sConfig.SDKConfig.YouYiGameIdList[idx], subPlatform) {
  259. idStrList := strings.Split(sConfig.SDKConfig.YouYiGameIdList[idx], ":")
  260. if len(idStrList) >= 2 {
  261. gameId = idStrList[1]
  262. }
  263. break
  264. }
  265. }
  266. }
  267. authHttpAddr += "?oauth_token=" + sauthJson + "&game_id=" + gameId
  268. retSt := &YouYiSDKLoginClient{
  269. Code: 1001,
  270. Platform: platform,
  271. }
  272. parm := GetHttpNodeParam()
  273. parm.LisAddr = authHttpAddr
  274. httpNode := CreateHttpConnector(parm)
  275. err := httpNode.(rocommon.HTTPConnector).Request("GET", "", tmpRequest)
  276. if err != nil {
  277. //tmpRequest.ResMsg.(*SDKUniLoginAuthCheckResp).RetCode = "0"
  278. util.InfoF("SDKYouYiCheck http Request token=%v err=%v", sauthJson, err)
  279. c.JSON(http.StatusOK, retSt)
  280. return
  281. }
  282. res := tmpRequest.ResMsg.(*SDKYouYiLoginAuthCheckResp)
  283. if res == nil {
  284. util.InfoF("SDKYouYiCheck http Request token=%v err=%v", sauthJson, err)
  285. retSt.Code = 1002
  286. c.JSON(http.StatusOK, retSt)
  287. return
  288. }
  289. retSt.Code = res.Code
  290. if res.Code == 0 {
  291. checkUserInfo := &SDKCheckUserInfo{
  292. Code: res.Code,
  293. //Aid: res.Data[0].CPUid,
  294. Aid: res.Data.(map[string]interface{})["cp_uid"].(string),
  295. ExpireTime: uint64(util.GetTimeSeconds() + 5*60*60),
  296. LoginChannel: "",
  297. AppChannel: "",
  298. }
  299. userInfoData, _ := json.Marshal(checkUserInfo)
  300. signInfoData, err := rpc.AESCtrEncrypt(userInfoData, sdkUniSignAesKey, sdkUniSignAesKey...)
  301. userInfoStr := base64.StdEncoding.EncodeToString(userInfoData)
  302. signInfoStr := base64.StdEncoding.EncodeToString(signInfoData)
  303. if err == nil {
  304. retSt.UserInfo = userInfoStr + "." + signInfoStr
  305. } else {
  306. retSt.Code = 1003
  307. }
  308. }
  309. c.JSON(http.StatusOK, retSt)
  310. }
  311. /*
  312. * 产品名称:仙境传说RO:初心者大冒险
  313. * 产品代号/专区标识(pid):ma112
  314. * 产品key(salt):E0FE384E5D814644
  315. * 产品语言:中文
  316. */
  317. type UniSdkWebTokenRes struct {
  318. Refer string `json:"refer"`
  319. Token string `json:"token"`
  320. Code int `json:"code"`
  321. ExpireTime int64 `json:"expireTime"`
  322. Message string `json:"message"`
  323. }
  324. func UniSdkWebToken(c *gin.Context) {
  325. uidStr := c.DefaultQuery("uid", "") ///default ""
  326. typeStr := c.DefaultQuery("type", "") ///default ""
  327. gameServerStr := c.DefaultQuery("game_server", "") ///default ""
  328. gameUidStr := c.DefaultQuery("game_uid", "") ///default ""
  329. nickName := c.DefaultQuery("nickname", "") ///default ""
  330. osStr := c.DefaultQuery("os", "") ///default ""
  331. curTime := util.GetTimeSeconds()
  332. webTokenHttpAddr := service.GetServiceConfig().SDKConfig.UniWebTokenAddr
  333. pidStr := service.GetServiceConfig().SDKConfig.UniWebPid
  334. saltStr := service.GetServiceConfig().SDKConfig.UniWebSalt
  335. data := pidStr + uidStr + strconv.FormatInt(curTime, 10) + saltStr
  336. md5Str := fmt.Sprintf("%x", md5.Sum([]byte(data)))
  337. webTokenHttpAddr += "?uid=" + uidStr + "&pid=" + pidStr + "&type=" + typeStr + "&game_server=" + gameServerStr +
  338. "&game_uid=" + gameUidStr + "&nickname=" + nickName + "&sign=" + string(md5Str) + "&time=" + strconv.FormatInt(curTime, 10) +
  339. "&os=" + osStr
  340. tmpRequest := &rocommon.HTTPRequest{}
  341. tmpRequest.ReqCodecName = "httpjson"
  342. tmpRequest.ResMsg = &UniSdkWebTokenRes{}
  343. parm := GetHttpNodeParam()
  344. parm.LisAddr = webTokenHttpAddr
  345. httpNode := CreateHttpConnector(parm)
  346. err := httpNode.(rocommon.HTTPConnector).Request("GET", "", tmpRequest)
  347. if err != nil {
  348. //tmpRequest.ResMsg.(*SDKUniLoginAuthCheckResp).RetCode = "0"
  349. util.InfoF("LoginVerifySignUniSDK http Request uid=%v err=%v", uidStr, err)
  350. c.JSON(http.StatusOK, err)
  351. return
  352. }
  353. resMsg := tmpRequest.ResMsg.(*UniSdkWebTokenRes)
  354. c.JSON(http.StatusOK, resMsg)
  355. }
  356. type HwQuickWebTokenRes struct {
  357. Status bool `json:"status"`
  358. Message string `json:"message"`
  359. Data interface{} `json:"data"`
  360. Uid string `json:"uid"`
  361. }
  362. func SDKHwQuickCheck(c *gin.Context, platform, token, uid string) {
  363. authHttpAddr := "http://pxqg.hkhappygame.com"
  364. urlPath := "/webapi/checkUserInfo?token=" + token + "&uid=" + uid
  365. retSt := &YouYiSDKLoginClient{
  366. Code: 0,
  367. Platform: platform,
  368. }
  369. parm := GetHttpNodeParam()
  370. parm.LisAddr = authHttpAddr
  371. httpNode := CreateHttpConnector(parm)
  372. tmpRequest := &rocommon.HTTPRequest{}
  373. tmpRequest.ReqCodecName = "httpjson"
  374. tmpRequest.ResMsg = &HwQuickWebTokenRes{}
  375. err := httpNode.(rocommon.HTTPConnector).Request("GET", urlPath, tmpRequest)
  376. if err != nil {
  377. //tmpRequest.ResMsg.(*SDKUniLoginAuthCheckResp).RetCode = "0"
  378. util.InfoF("SDKHwQuickCheck http Request urlPath=%v err=%v", urlPath, err)
  379. c.JSON(http.StatusOK, err)
  380. return
  381. }
  382. //准备用户信息
  383. userInfoData, err := json.Marshal(tmpRequest.ResMsg.(*HwQuickWebTokenRes).Data)
  384. if err != nil {
  385. retSt.Code = 1003
  386. c.JSON(http.StatusOK, retSt)
  387. return
  388. }
  389. // util.InfoF("userinfo: %v", string(userInfoData))
  390. signInfoData, err := rpc.AESCtrEncrypt(userInfoData, sdkUniSignAesKey, sdkUniSignAesKey...)
  391. userInfoStr := base64.StdEncoding.EncodeToString(userInfoData)
  392. signInfoStr := base64.StdEncoding.EncodeToString(signInfoData)
  393. if err == nil {
  394. retSt.UserInfo = userInfoStr + "." + signInfoStr
  395. } else {
  396. retSt.Code = 1003
  397. }
  398. c.JSON(http.StatusOK, retSt)
  399. }