sdkcheck.go 13 KB

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