void getChangedUser(CImPdu* pPdu, uint32_t conn_uuid) { IM::Buddy::IMAllUserReq msg; IM::Buddy::IMAllUserRsp msgResp; if(msg.ParseFromArray(pPdu->GetBodyData(), pPdu->GetBodyLength())) { CImPdu* pPduRes = new CImPdu; uint32_t nReqId = msg.user_id(); uint32_t nLastTime = msg.latest_update_time(); uint32_t nLastUpdate = CSyncCenter::getInstance()->getLastUpdate(); list<IM::BaseDefine::UserInfo> lsUsers; //if( nLastUpdate > nLastTime) { list<uint32_t> lsIds; CUserModel::getInstance()->getRelationId(nReqId, nLastTime, lsIds); CUserModel::getInstance()->getUsers(lsIds, lsUsers); } msgResp.set_user_id(nReqId); msgResp.set_latest_update_time(nLastTime); for (list<IM::BaseDefine::UserInfo>::iterator it=lsUsers.begin(); it!=lsUsers.end(); ++it) { IM::BaseDefine::UserInfo* pUser = msgResp.add_user_list(); // *pUser = *it; pUser->set_user_id(it->user_id()); pUser->set_user_gender(it->user_gender()); pUser->set_user_nick_name(it->user_nick_name()); pUser->set_avatar_url(it->avatar_url()); pUser->set_sign_info(it->sign_info()); pUser->set_department_id(it->department_id()); pUser->set_email(it->email()); pUser->set_user_real_name(it->user_real_name()); pUser->set_user_tel(it->user_tel()); pUser->set_user_domain(it->user_domain()); pUser->set_status(it->status()); pUser->set_user_type(it->user_type()); } log("userId=%u,nLastUpdate=%u, last_time=%u, userCnt=%u", nReqId,nLastUpdate, nLastTime, msgResp.user_list_size()); msgResp.set_attach_data(msg.attach_data()); pPduRes->SetPBMsg(&msgResp); pPduRes->SetSeqNum(pPdu->GetSeqNum()); pPduRes->SetServiceId(IM::BaseDefine::SID_BUDDY_LIST); pPduRes->SetCommandId(IM::BaseDefine::CID_BUDDY_LIST_ALL_USER_RESPONSE); CProxyConn::AddResponsePdu(conn_uuid, pPduRes); } else { log("parse pb failed"); } }
void getUserInfo(CImPdu* pPdu, uint32_t conn_uuid) { IM::Buddy::IMUsersInfoReq msg; IM::Buddy::IMUsersInfoRsp msgResp; if(msg.ParseFromArray(pPdu->GetBodyData(), pPdu->GetBodyLength())) { CImPdu* pPduRes = new CImPdu; uint32_t from_user_id = msg.user_id(); uint32_t userCount = msg.user_id_list_size(); std::list<uint32_t> idList; for(uint32_t i = 0; i < userCount;++i) { idList.push_back(msg.user_id_list(i)); } std::list<IM::BaseDefine::UserInfo> lsUser; CUserModel::getInstance()->getUsers(idList, lsUser); msgResp.set_user_id(from_user_id); for(list<IM::BaseDefine::UserInfo>::iterator it=lsUser.begin(); it!=lsUser.end(); ++it) { IM::BaseDefine::UserInfo* pUser = msgResp.add_user_info_list(); // *pUser = *it; pUser->set_user_id(it->user_id()); pUser->set_user_gender(it->user_gender()); pUser->set_user_nick_name(it->user_nick_name()); pUser->set_avatar_url(it->avatar_url()); pUser->set_sign_info(it->sign_info()); pUser->set_department_id(it->department_id()); pUser->set_email(it->email()); pUser->set_user_real_name(it->user_real_name()); pUser->set_user_tel(it->user_tel()); pUser->set_user_domain(it->user_domain()); pUser->set_status(it->status()); pUser->set_user_type(it->user_type()); } log("userId=%u, userCnt=%u", from_user_id, userCount); msgResp.set_attach_data(msg.attach_data()); pPduRes->SetPBMsg(&msgResp); pPduRes->SetSeqNum(pPdu->GetSeqNum()); pPduRes->SetServiceId(IM::BaseDefine::SID_BUDDY_LIST); pPduRes->SetCommandId(IM::BaseDefine::CID_BUDDY_LIST_USER_INFO_RESPONSE); CProxyConn::AddResponsePdu(conn_uuid, pPduRes); } else { log("parse pb failed"); } }
void doLogin(CImPdu* pPdu, uint32_t conn_uuid) { CImPdu* pPduResp = new CImPdu; IM::Server::IMValidateReq msg; IM::Server::IMValidateRsp msgResp; if(msg.ParseFromArray(pPdu->GetBodyData(), pPdu->GetBodyLength())) { string strDomain = msg.user_name(); string strPass = msg.password(); msgResp.set_user_name(strDomain); msgResp.set_attach_data(msg.attach_data()); do { CAutoLock cAutoLock(&g_cLimitLock); list<uint32_t>& lsErrorTime = g_hmLimits[strDomain]; uint32_t tmNow = time(NULL); //清理超过30分钟的错误时间点记录 /* 清理放在这里还是放在密码错误后添加的时候呢? 放在这里,每次都要遍历,会有一点点性能的损失。 放在后面,可能会造成30分钟之前有10次错的,但是本次是对的就没办法再访问了。 */ auto itTime=lsErrorTime.begin(); for(; itTime!=lsErrorTime.end();++itTime) { if(tmNow - *itTime > 30*60) { break; } } if(itTime != lsErrorTime.end()) { lsErrorTime.erase(itTime, lsErrorTime.end()); } // 判断30分钟内密码错误次数是否大于10 if(lsErrorTime.size() > 10) { itTime = lsErrorTime.begin(); if(tmNow - *itTime <= 30*60) { msgResp.set_result_code(6); msgResp.set_result_string("用户名/密码错误次数太多"); pPduResp->SetPBMsg(&msgResp); pPduResp->SetSeqNum(pPdu->GetSeqNum()); pPduResp->SetServiceId(IM::BaseDefine::SID_OTHER); pPduResp->SetCommandId(IM::BaseDefine::CID_OTHER_VALIDATE_RSP); CProxyConn::AddResponsePdu(conn_uuid, pPduResp); return ; } } } while(false); log("%s request login.", strDomain.c_str()); IM::BaseDefine::UserInfo cUser; if(g_loginStrategy.doLogin(strDomain, strPass, cUser)) { IM::BaseDefine::UserInfo* pUser = msgResp.mutable_user_info(); pUser->set_user_id(cUser.user_id()); pUser->set_user_gender(cUser.user_gender()); pUser->set_department_id(cUser.department_id()); pUser->set_user_nick_name(cUser.user_nick_name()); pUser->set_user_domain(cUser.user_domain()); pUser->set_avatar_url(cUser.avatar_url()); pUser->set_email(cUser.email()); pUser->set_user_tel(cUser.user_tel()); pUser->set_user_real_name(cUser.user_real_name()); pUser->set_status(0); pUser->set_sign_info(cUser.sign_info()); msgResp.set_result_code(0); msgResp.set_result_string("成功"); //如果登陆成功,则清除错误尝试限制 CAutoLock cAutoLock(&g_cLimitLock); list<uint32_t>& lsErrorTime = g_hmLimits[strDomain]; lsErrorTime.clear(); } else { //密码错误,记录一次登陆失败 uint32_t tmCurrent = time(NULL); CAutoLock cAutoLock(&g_cLimitLock); list<uint32_t>& lsErrorTime = g_hmLimits[strDomain]; lsErrorTime.push_front(tmCurrent); log("get result false"); msgResp.set_result_code(1); msgResp.set_result_string("用户名/密码错误"); } } else { msgResp.set_result_code(2); msgResp.set_result_string("服务端内部错误"); } pPduResp->SetPBMsg(&msgResp); pPduResp->SetSeqNum(pPdu->GetSeqNum()); pPduResp->SetServiceId(IM::BaseDefine::SID_OTHER); pPduResp->SetCommandId(IM::BaseDefine::CID_OTHER_VALIDATE_RSP); CProxyConn::AddResponsePdu(conn_uuid, pPduResp); }
bool CInterLoginStrategy::doLogin(const std::string &strName, const std::string &strPass, IM::BaseDefine::UserInfo& user) { bool bRet = false; CDBManager* pDBManger = CDBManager::getInstance(); CDBConn* pDBConn = pDBManger->GetDBConn("teamtalk_slave"); if (pDBConn) { string strSql = "select * from IMUser where name='" + strName + "' and status=0"; CResultSet* pResultSet = pDBConn->ExecuteQuery(strSql.c_str()); if(pResultSet) { string strResult, strSalt; uint32_t nId, nGender, nDeptId, nStatus; string strNick, strAvatar, strEmail, strRealName, strTel, strDomain,strSignInfo; while (pResultSet->Next()) { nId = pResultSet->GetInt("id"); strResult = pResultSet->GetString("password"); strSalt = pResultSet->GetString("salt"); strNick = pResultSet->GetString("nick"); nGender = pResultSet->GetInt("sex"); strRealName = pResultSet->GetString("name"); strDomain = pResultSet->GetString("domain"); strTel = pResultSet->GetString("phone"); strEmail = pResultSet->GetString("email"); strAvatar = pResultSet->GetString("avatar"); nDeptId = pResultSet->GetInt("departId"); nStatus = pResultSet->GetInt("status"); strSignInfo = pResultSet->GetString("sign_info"); } string strInPass = strPass + strSalt; char szMd5[33]; CMd5::MD5_Calculate(strInPass.c_str(), strInPass.length(), szMd5); string strOutPass(szMd5); if(strOutPass == strResult) { bRet = true; user.set_user_id(nId); user.set_user_nick_name(strNick); user.set_user_gender(nGender); user.set_user_real_name(strRealName); user.set_user_domain(strDomain); user.set_user_tel(strTel); user.set_email(strEmail); user.set_avatar_url(strAvatar); user.set_department_id(nDeptId); user.set_status(nStatus); user.set_sign_info(strSignInfo); } delete pResultSet; } pDBManger->RelDBConn(pDBConn); } return bRet; }
void LoginOperation::processOpertion() { LOG__(APP,_T("login start,uname:%s,status:%d"), m_loginParam.csUserName , m_loginParam.mySelectedStatus); LoginParam* pParam = new LoginParam; pParam->csUserName = m_loginParam.csUserName; pParam->mySelectedStatus = m_loginParam.mySelectedStatus; //连接消息服务器 module::TTConfig* pCfg = module::getSysConfigModule()->getSystemConfig(); CString server = util::stringToCString(pCfg->msgSevPriorIP); LOG__(APP, _T("MsgServeIp:%s,Port:%d"), server, pCfg->msgServPort); IM::Login::IMLoginRes* pImLoginResp = (IM::Login::IMLoginRes*)module::getTcpClientModule() ->doLogin(server, pCfg->msgServPort,m_loginParam.csUserName,m_loginParam.password); if (0 == pImLoginResp || pImLoginResp->result_code() != IM::BaseDefine::REFUSE_REASON_NONE || !pImLoginResp->has_user_info()) { //TODO,若失败,尝试备用IP LOG__(ERR,_T("add:%s:%d,uname:%s,login for msg server failed"),server,pCfg->msgServPort, m_loginParam.csUserName); if (pImLoginResp) { CString errInfo = util::stringToCString(pImLoginResp->result_string()); pParam->errInfo = errInfo; pParam->result = LOGIN_FAIL; pParam->server_result = pImLoginResp->result_code(); LOG__(ERR, _T("error code :%d,error info:%s"), pImLoginResp->result_code(), errInfo); } else { pParam->result = IM::BaseDefine::REFUSE_REASON_NO_MSG_SERVER; LOG__(ERR, _T("login msg server faild!")); } asyncCallback(std::shared_ptr<void>(pParam)); return; } pParam->result = LOGIN_OK; pParam->serverTime = pImLoginResp->server_time(); pParam->mySelectedStatus = pImLoginResp->online_status(); //存储服务器端返回的userId IM::BaseDefine::UserInfo userInfo = pImLoginResp->user_info(); pCfg->userId = util::uint32ToString(userInfo.user_id()); pCfg->csUserId = util::stringToCString(pCfg->userId); //登陆成功,创建自己的信息 module::UserInfoEntity myInfo; myInfo.sId = pCfg->userId; myInfo.csName = m_loginParam.csUserName; myInfo.onlineState = IM::BaseDefine::USER_STATUS_ONLINE; myInfo.csNickName = util::stringToCString(userInfo.user_nick_name()); myInfo.avatarUrl = userInfo.avatar_url(); myInfo.dId = util::uint32ToString(userInfo.department_id()); myInfo.department = myInfo.dId; myInfo.email = userInfo.email(); myInfo.gender = userInfo.user_gender(); myInfo.user_domain = userInfo.user_domain(); myInfo.telephone = userInfo.user_tel(); myInfo.status = userInfo.status(); myInfo.signature = userInfo.sign_info(); module::getUserListModule()->createUserInfo(myInfo); asyncCallback(std::shared_ptr<void>(pParam)); LOG__(APP, _T("login succeed! Name = %s Nickname = %s sId = %s status = %d") , m_loginParam.csUserName , util::stringToCString(userInfo.user_nick_name()) , module::getSysConfigModule()->UserID() , m_loginParam.mySelectedStatus); //开始发送心跳包 module::getTcpClientModule()->startHeartbeat(); }