Changeset 31 in chevmsgr
- Timestamp:
- 12/01/15 00:36:56 (8 years ago)
- Location:
- trunk
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/msg.hpp
r24 r31 10 10 #include <string> 11 11 #include <vector> 12 #include <time.h> 12 13 13 14 typedef struct SFriend … … 44 45 DECLARE_KEY(FROM); 45 46 DECLARE_KEY(SESSION_ID); 47 DECLARE_KEY(SESSION_KEY); 46 48 DECLARE_KEY(MESSAGE); 47 49 DECLARE_KEY(SENSITIVE); … … 171 173 } 172 174 175 static cf::bin generateRandom() 176 { 177 int t = (int)time(NULL); 178 179 cf::bin b; 180 b.resize(sizeof(int)); 181 182 b.set((cf::uint8_t*)&t, sizeof(int)); 183 return crypto().sha256(b); 184 } 185 173 186 class SecureSocket 174 187 { 175 private 188 private: 176 189 cf::network::tcp * sock; 177 190 crypto aria; 178 191 179 public 192 public: 180 193 SecureSocket() 181 194 { 182 195 sock = NULL; 183 196 } 197 184 198 SecureSocket(cf::network::tcp * sock, const std::string & ip, const std::string & sms) 185 199 { … … 193 207 } 194 208 209 cf::network::tcp & getTcpSocket() 210 { 211 return *sock; 212 } 213 195 214 void setKey(const std::string & ip, const std::string & sms) 196 215 { -
trunk/msgclnt.cpp
r25 r31 7 7 8 8 #include <stdlib.h> 9 #include <time.h>10 9 11 10 // -------------------------------------------------------------- … … 21 20 MessageQ * messageQ; 22 21 SCallback callback; 22 std::map<std::string, cf::bin> * keyMap; 23 23 } SCallbackWorkerArg; 24 24 … … 63 63 } 64 64 65 static inline SConversation toConversation(const Protocol::Message & message )65 static inline SConversation toConversation(const Protocol::Message & message, std::map<std::string, cf::bin> * keyMap) 66 66 { 67 67 SConversation c; 68 std::string chat = message.get<std::string>(ProtocolType::MESSAGE);69 70 if (chat.length() > 0)71 c.message = cf::codec::hex::getInstance()->decode(chat).toString();72 68 73 69 c.sessid = message.get<std::string>(ProtocolType::SESSION_ID); 74 70 c.sensitive = message.get<bool>(ProtocolType::SENSITIVE); 75 76 71 c.from = message.get<std::string>(ProtocolType::FROM); 77 72 73 std::string chat = message.get<std::string>(ProtocolType::MESSAGE); 74 if (chat.length() > 0) 75 { 76 crypto aria; 77 aria.setKey((*keyMap)[c.sessid]); 78 79 cf::bin decoded = cf::codec::hex::getInstance()->decode(chat); 80 cf::bin plain = aria.decrypt(decoded); 81 c.message = plain.toString(); 82 } 83 78 84 return c; 79 85 } 80 86 81 static inline SOpenSession toOpenSession(const Protocol::Message & message )87 static inline SOpenSession toOpenSession(const Protocol::Message & message, std::map<std::string, cf::bin> * keyMap) 82 88 { 83 89 SOpenSession o; … … 86 92 o.idList = message.getList<std::string>(ProtocolType::ID_LIST); 87 93 94 if (message.mObject.HasKey(ProtocolType::SESSION_KEY)) 95 { 96 std::string encodedKey = message.get<std::string>(ProtocolType::SESSION_KEY); 97 cf::bin key = cf::codec::hex::getInstance()->decode(encodedKey); 98 (*keyMap)[o.sessid] = key; 99 } 100 88 101 return o; 89 102 } … … 99 112 message = inst->messageQ->pop(ProtocolType::LISTEN, false); 100 113 if (message.type() != ProtocolType::NONE) 101 inst->callback.onListen(toConversation(message ));114 inst->callback.onListen(toConversation(message, inst->keyMap)); 102 115 103 116 message = inst->messageQ->pop(ProtocolType::OPEN_SESSION, false); 104 117 if (message.type() != ProtocolType::NONE) 105 inst->callback.onOpenSession(toOpenSession(message ));118 inst->callback.onOpenSession(toOpenSession(message, inst->keyMap)); 106 119 107 120 message = inst->messageQ->pop(ProtocolType::LOGOUT, false); … … 113 126 114 127 return 0; 115 }116 117 static std::string generateRandom()118 {119 int t = (int)time(NULL);120 121 cf::bin b;122 b.resize(sizeof(int));123 124 b.set((cf::uint8_t*)&t, sizeof(int));125 cf::bin s = crypto().sha256(b);126 127 return cf::codec::hex::getInstance()->encode(s);128 128 } 129 129 … … 226 226 try 227 227 { 228 std::string random = generateRandom();228 std::string random = cf::codec::hex::getInstance()->encode(generateRandom()); 229 229 std::string sub = request.join(id, hashPassword(pw), sms); 230 230 std::string secret = getSecret(sub, sms, random); … … 247 247 request.setUserID(id); 248 248 249 std::string random = generateRandom();249 std::string random = cf::codec::hex::getInstance()->encode(generateRandom()); 250 250 std::string sub = request.login(hashPassword(pw)); 251 251 std::string secret = getSecret(sub, sms, random); … … 341 341 Protocol::Message message = messageQ.pop(ProtocolType::OPEN_SESSION); 342 342 343 sessionMap[concat] = message.get<std::string>(ProtocolType::SESSION_ID); 343 std::string sessionID = message.get<std::string>(ProtocolType::SESSION_ID); 344 sessionMap[concat] = sessionID; 345 346 if (message.mObject.HasKey(ProtocolType::SESSION_KEY)) 347 { 348 std::string encodedKey = message.get<std::string>(ProtocolType::SESSION_KEY); 349 cf::bin key = cf::codec::hex::getInstance()->decode(encodedKey); 350 keyMap[sessionID] = key; 351 } 344 352 345 353 bool result = messageQ.pop(ProtocolType::OPEN_SESSION).get<bool>(ProtocolType::RESULT); … … 365 373 366 374 if (c.message.length() > 0) 367 c.message = cf::codec::hex::getInstance()->encode(c.message); 375 { 376 crypto aria; 377 aria.setKey(keyMap[c.sessid]); 378 379 cf::bin cipher = aria.encrypt(cf::bin(c.message)); 380 c.message = cf::codec::hex::getInstance()->encode(cipher); 381 } 368 382 369 383 secureSocket.send(request.tell(c.sessid, c.message, c.sensitive)); … … 387 401 arg->messageQ = &messageQ; 388 402 arg->callback = callback; 403 arg->keyMap = &keyMap; 389 404 390 405 caller.start(arg); -
trunk/msgclnt.h
r25 r31 58 58 59 59 std::map<std::string, std::string> sessionMap; 60 std::map<std::string, cf::bin> keyMap; 60 61 61 62 public: -
trunk/msgsrv.cpp
r30 r31 115 115 "(" 116 116 " id TEXT NOT NULL," 117 " friend TEXT NOT NULL," 118 " unique (friend)" 117 " friend TEXT NOT NULL" 119 118 ")"; 120 119 … … 456 455 static bool openSession(const Protocol::Message & message) 457 456 { 457 const static cf::bin delim(DELIMITER); 458 458 459 Protocol::Message parser = message; 459 bool result = false;460 bool result = true; 460 461 std::string sessid; 461 462 std::vector<std::string> idList = parser.getList<std::string>(ProtocolType::ID_LIST); … … 465 466 466 467 parser.mObject[ProtocolType::SESSION_ID] = sessid; 468 469 cf::bin allKeys; 470 471 for (size_t iter = 0; iter < idList.size(); iter++) 472 { 473 std::string id = idList[iter]; 474 if (isOnline(id)) 475 { 476 SecureSocket & secsock = gOnlineUsers[id]; 477 std::string ip = secsock.getTcpSocket().local().address(); 478 std::string sms = dbmgr.getSMS(id, ip); 479 480 cf::bin seed = sms + DELIMITER + ip; 481 cf::bin key = crypto().sha256(seed); 482 483 allKeys += key; 484 } 485 } 486 487 allKeys += generateRandom(); 488 std::string hashedKey = cf::codec::hex::getInstance()->encode(crypto().sha256(allKeys)); 489 parser.mObject[ProtocolType::SESSION_KEY] = hashedKey; 490 467 491 std::string serialized = parser.serialize(); 468 492 … … 472 496 if (isOnline(id)) 473 497 gOnlineUsers[id].send(serialized); 474 475 result = true;476 498 } 477 499 … … 562 584 { 563 585 id = authenticator(*sock); 564 SecureSocket * secsock = &gOnlineUsers[id];586 SecureSocket & secsock = gOnlineUsers[id]; 565 587 566 588 while (true) … … 568 590 bool result = true; 569 591 Protocol::Response response; 570 std::string message = secsock ->receive();592 std::string message = secsock.receive(); 571 593 Protocol::Message parser; 572 594 parser.parse(message); … … 591 613 { 592 614 std::vector<SFriend> fl = getFriendList(parser); 593 secsock ->send(response.friendList(fl));615 secsock.send(response.friendList(fl)); 594 616 } catch (cf::exception & e) 595 617 { … … 599 621 } 600 622 601 secsock ->send(response.result(parser.type(), result));623 secsock.send(response.result(parser.type(), result)); 602 624 } 603 625 } -
trunk/msvc14/ChevMsgrClient_MFC/AddFriendDlg.cpp
r27 r31 39 39 { 40 40 // TODO: ¿©±â¿¡ ÄÁÆ®·Ñ ¾Ë¸² 󸮱â Äڵ带 Ãß°¡ÇÕ´Ï´Ù. 41 CString friendId;42 43 41 GetDlgItemText(IDC_EDIT_FRIEND_ID, friendId); 44 chev->addFriend(cstr2str(friendId)); 45 46 mMsgrClntDlg->mFriendListBox.InsertString(mMsgrClntDlg->mFriendListBox.GetCount(), friendId); 47 42 48 43 CDialogEx::OnOK(); 49 44 } -
trunk/msvc14/ChevMsgrClient_MFC/AddFriendDlg.h
r27 r31 13 13 CAddFriendDlg(CWnd* pParent = NULL); // Ç¥ÁØ »ý¼ºÀÚÀÔ´Ï´Ù. 14 14 virtual ~CAddFriendDlg(); 15 chev *chev; 16 CChevMsgrClient_MFCDlg *mMsgrClntDlg; 15 CString friendId; 17 16 18 17 // ´ëÈ »óÀÚ µ¥ÀÌÅÍÀÔ´Ï´Ù. -
trunk/msvc14/ChevMsgrClient_MFC/ChevMsgrClient_MFCDlg.cpp
r30 r31 9 9 #include <fstream> 10 10 #include <algorithm> 11 #include <time.h> 11 12 12 13 #include "LoginDlg.h" … … 19 20 #define new DEBUG_NEW 20 21 #endif 22 23 #define SENSITIVE_TIME 10 24 #define TIMER_EVENT WM_USER +1 21 25 22 26 int cb_listen(SConversation & chat); … … 86 90 ON_BN_CLICKED(IDC_BTN_ADD_CHAT, &CChevMsgrClient_MFCDlg::OnBnClickedBtnAddChat) 87 91 ON_LBN_SELCHANGE(IDC_LIST_FRIENDS, &CChevMsgrClient_MFCDlg::OnLbnSelchangeListFriends) 92 ON_WM_TIMER() 88 93 END_MESSAGE_MAP() 89 94 … … 121 126 122 127 // TODO: ¿©±â¿¡ Ãß°¡ ÃʱâÈ ÀÛ¾÷À» Ãß°¡ÇÕ´Ï´Ù. 128 this->SetTimer(TIMER_EVENT, 2000, NULL); 123 129 124 130 std::ifstream f("cnct2srv.ini"); … … 158 164 159 165 UpdateFriendList(); 166 167 CString cstrId; 168 169 for (size_t iter = 0; iter < idList.size(); iter++) 170 { 171 cstrId = str2cstr(idList[iter].id); 172 this->mFriendListBox.InsertString(this->mFriendListBox.GetCount(), cstrId); 173 } 160 174 161 175 scl.resize(idList.size()); … … 221 235 if (chat.sessid == gDlg->scl[idx].sessionId) 222 236 { 237 if (chat.sensitive == true) 238 { 239 SensitiveCtrl sc; 240 241 sc.sensitiveIdx = gDlg->scl[idx].chatList.size(); 242 sc.timestamp = (int)time(NULL); 243 gDlg->scl[idx].sensitiveCtrl.push_back(sc); 244 } 245 223 246 gDlg->scl[idx].chatList.push_back(chat); 247 224 248 break; 225 249 } … … 238 262 int cb_opensession(SOpenSession & opensession) 239 263 { 240 if (opensession.idList.size() == 2)241 return 0;242 243 264 CString cstrIdList = _T(""); 244 265 … … 254 275 } 255 276 256 gDlg->mFriendListBox.InsertString(gDlg->mFriendListBox.GetCount(), cstrIdList); 257 258 Session s; 259 s.sessionId = opensession.sessid; 260 261 gDlg->scl.push_back(s); 277 if (opensession.idList.size() > 2) 278 { 279 gDlg->mFriendListBox.InsertString(gDlg->mFriendListBox.GetCount(), cstrIdList); 280 281 Session s; 282 283 s.sessionId = opensession.sessid; 284 285 gDlg->scl.push_back(s); 286 } 287 else 288 { 289 CString cmpIdList; 290 for (int iter = 0; iter < gDlg->mFriendListBox.GetCount(); iter++) 291 { 292 gDlg->mFriendListBox.GetText(iter, cmpIdList); 293 if (cstrIdList == cmpIdList) 294 gDlg->scl[iter].sessionId = opensession.sessid; 295 } 296 } 262 297 263 298 return 0; … … 298 333 if (chev.tell(conversation)) 299 334 { 335 if (conversation.sensitive == true) 336 { 337 SensitiveCtrl sc; 338 339 sc.sensitiveIdx = gDlg->scl[idx].chatList.size(); 340 sc.timestamp = (int)time(NULL); 341 gDlg->scl[idx].sensitiveCtrl.push_back(sc); 342 } 343 gDlg->scl[idx].chatList.push_back(conversation); 344 300 345 mChatListBox.InsertString(mChatListBox.GetCount(), str2cstr("<" + mId + ">")); 301 346 mChatListBox.InsertString(mChatListBox.GetCount(), chat); 302 347 mChatListBox.InsertString(mChatListBox.GetCount(), _T("")); 303 348 } 304 305 gDlg->scl[gDlg->mFriendListBox.GetCurSel()].chatList.push_back(conversation);306 349 307 350 SetDlgItemText(IDC_EDIT_INPUT, _T("")); … … 313 356 CAddFriendDlg AFDlg; 314 357 315 AFDlg.chev = &this->chev;316 AFDlg.mMsgrClntDlg = this;317 318 358 AFDlg.DoModal(); 359 360 CString existFriendId; 361 for (int iter = 0; iter < mFriendListBox.GetCount(); iter++) 362 { 363 mFriendListBox.GetText(iter, existFriendId); 364 if (AFDlg.friendId == existFriendId) 365 return; 366 } 367 if (!chev.addFriend(cstr2str(AFDlg.friendId))) 368 { 369 AfxMessageBox(_T("Add Friend Fail")); 370 return; 371 } 319 372 UpdateFriendList(); 373 scl.resize(scl.size() + 1); 374 mFriendListBox.InsertString(mFriendListBox.GetCount(), AFDlg.friendId); 320 375 } 321 376 … … 344 399 void CChevMsgrClient_MFCDlg::UpdateFriendList() 345 400 { 346 CString cstrId;347 401 if (!chev.getFriendList(idList)) 348 402 { 349 403 AfxMessageBox(_T("cannot get friend list")); 350 404 ::SendMessage(GetSafeHwnd(), WM_QUIT, NULL, NULL); 351 }352 353 this->mFriendListBox.ResetContent();354 355 for (size_t iter = 0; iter < idList.size(); iter++)356 {357 cstrId = str2cstr(idList[iter].id);358 this->mFriendListBox.InsertString(this->mFriendListBox.GetCount(), cstrId);359 405 } 360 406 } … … 373 419 return; 374 420 375 for (std::list<SConversation>::iterator iter = scl[idx].chatList.begin();376 iter != scl[idx].chatList.end(); iter++)421 std::vector<SConversation>::iterator iter = scl[idx].chatList.begin(); 422 for ( ; iter != scl[idx].chatList.end(); iter++) 377 423 { 378 424 mChatListBox.InsertString(mChatListBox.GetCount(), str2cstr("<" + iter->from + ">")); … … 381 427 } 382 428 } 429 430 431 void CChevMsgrClient_MFCDlg::OnTimer(UINT_PTR nIDEvent) 432 { 433 // TODO: ¿©±â¿¡ ¸Þ½ÃÁö 󸮱â Äڵ带 Ãß°¡ ¹×/¶Ç´Â ±âº»°ªÀ» È£ÃâÇÕ´Ï´Ù. 434 int timestamp = (int)time(NULL); 435 436 for (size_t iter = 0; iter < scl.size(); iter++) 437 { 438 if (scl[iter].sensitiveCtrl.size() == 0) 439 continue; 440 441 SensitiveCtrl &sc = scl[iter].sensitiveCtrl.front(); 442 if ((timestamp - sc.timestamp) >= SENSITIVE_TIME) 443 { 444 scl[iter].chatList.erase(scl[iter].chatList.begin() + sc.sensitiveIdx); 445 if (iter == mFriendListBox.GetCurSel()) 446 { 447 mChatListBox.DeleteString(sc.sensitiveIdx*3); 448 mChatListBox.DeleteString(sc.sensitiveIdx*3); 449 mChatListBox.DeleteString(sc.sensitiveIdx*3); 450 } 451 scl[iter].sensitiveCtrl.pop_front(); 452 453 std::list<SensitiveCtrl>::iterator it = scl[iter].sensitiveCtrl.begin(); 454 for (; it != scl[iter].sensitiveCtrl.end(); it++) 455 { 456 it->sensitiveIdx--; 457 } 458 459 } 460 } 461 462 CDialogEx::OnTimer(nIDEvent); 463 } -
trunk/msvc14/ChevMsgrClient_MFC/ChevMsgrClient_MFCDlg.h
r28 r31 9 9 // CChevMsgrClient_MFCDlg ´ëÈ »óÀÚ 10 10 typedef struct { 11 int timestamp; 12 int sensitiveIdx; 13 } SensitiveCtrl; 14 15 typedef struct { 11 16 std::string sessionId; 12 std::list<SConversation> chatList; 17 std::vector<SConversation> chatList; 18 std::list<SensitiveCtrl> sensitiveCtrl; 13 19 } Session; 14 20 … … 50 56 void UpdateFriendList(); 51 57 afx_msg void OnLbnSelchangeListFriends(); 58 afx_msg void OnTimer(UINT_PTR nIDEvent); 52 59 }; -
trunk/msvc14/ChevMsgrClient_MFC/LoginDlg.cpp
r25 r31 71 71 GetDlgItemText(IDC_EDIT_LOGIN_PW, pw); 72 72 f >> str; 73 decoded = cf::codec::hex::getInstance()->decode(str);74 73 75 c.setKey(cf::bin(cstr2str(pw))); 76 str = c.decrypt(decoded).toString(); 74 try 75 { 76 decoded = cf::codec::hex::getInstance()->decode(str); 77 77 78 c.setKey(cf::bin(cstr2str(pw))); 79 str = c.decrypt(decoded).toString(); 80 } 81 catch(cf::exception &e) 82 { 83 AfxMessageBox(_T("Invalid Password")); 84 (void)e; 85 return; 86 } 87 78 88 if (str.size() == 6) 79 89 {
Note:
See TracChangeset
for help on using the changeset viewer.