Changeset 20 in chevmsgr
- Timestamp:
- 11/24/15 00:21:07 (8 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/cf/bin.cpp
r4 r20 102 102 cf::bin cf::bin::operator +(const cf::bin & in) const 103 103 { 104 bin out( in);104 bin out(*this); 105 105 106 106 out.append(in); -
trunk/msg.cpp
r18 r20 93 93 } 94 94 95 std::string Request::secretWithRandom(const std::string & id, std::string & secret, const std::string & random) const95 std::string Request::secretWithRandom(const std::string & type, const std::string & id, std::string & secret, const std::string & random) const 96 96 { 97 DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::SECRET);97 DECLARE_TEMPLATE_OBJECT(obj, type); 98 98 99 99 obj[ProtocolType::ID] = id; -
trunk/msg.hpp
r19 r20 113 113 std::string login(const std::string & pw) const; 114 114 115 std::string secretWithRandom(const std::string & id, std::string & secret, const std::string & random) const;115 std::string secretWithRandom(const std::string & type, const std::string & id, std::string & secret, const std::string & random) const; 116 116 117 117 std::string logout() const; -
trunk/msgclnt.cpp
r18 r20 117 117 static std::string generateRandom() 118 118 { 119 unsigned int ret = 0;120 119 int t = (int)time(NULL); 121 120 … … 139 138 crypt.setKey(cf::bin(sms + DELIMITER + random)); 140 139 141 return c rypt.encrypt(cf::bin(msg)).toString();140 return cf::codec::hex::getInstance()->encode(crypt.encrypt(cf::bin(msg))); 142 141 } 143 142 … … 176 175 } 177 176 177 static bool handshake(cf::network::tcp & socket, std::string & msg, const std::string & type) 178 { 179 Protocol::Message parser; 180 181 socket.send(msg); 182 183 parser.parse(socket.receive().toString()); 184 if (parser.type() != type) 185 THROW_EXCEPTION("invalid message type"); 186 187 return parser.get<bool>(ProtocolType::RESULT); 188 } 189 178 190 // -------------------------------------------------------------- 179 191 … … 201 213 { 202 214 socket.connect(host, port); 203 204 SMessageQWorkerArg * arg = (SMessageQWorkerArg *)malloc(sizeof(SMessageQWorkerArg)); 205 if (!arg) 206 return false; 207 208 arg->secureSocket = &secureSocket; 209 arg->messageQ = &messageQ; 210 211 listener.start(arg); 212 215 secureSocket.setSocket(&socket); 213 216 return true; 214 217 } … … 226 229 { 227 230 std::string random = generateRandom(); 228 std::string msg= request.join(id, hashPassword(pw), sms);229 std::string secret = getSecret( msg, sms, random);230 s ocket.send(request.secretWithRandom(id, secret, random));231 232 return messageQ.pop(ProtocolType::JOIN).get<bool>(ProtocolType::RESULT);231 std::string sub = request.join(id, hashPassword(pw), sms); 232 std::string secret = getSecret(sub, sms, random); 233 std::string msg = request.secretWithRandom(ProtocolType::JOIN, id, secret, random); 234 235 return handshake(socket, msg, ProtocolType::JOIN); 233 236 } 234 237 catch (cf::exception & e) … … 247 250 248 251 std::string random = generateRandom(); 249 std::string msg = request.login(hashPassword(pw)); 250 std::string secret = getSecret(msg, sms, random); 251 socket.send(request.secretWithRandom(id, secret, random)); 252 253 return messageQ.pop(ProtocolType::LOGIN).get<bool>(ProtocolType::RESULT); 252 std::string sub = request.login(hashPassword(pw)); 253 std::string secret = getSecret(sub, sms, random); 254 std::string msg = request.secretWithRandom(ProtocolType::LOGIN, id, secret, random); 255 256 bool succeed = handshake(socket, msg, ProtocolType::LOGIN); 257 if (succeed) 258 { 259 secureSocket.setKey(socket.local().address(), sms); 260 261 SMessageQWorkerArg * arg = (SMessageQWorkerArg *)malloc(sizeof(SMessageQWorkerArg)); 262 if (!arg) 263 return false; 264 265 arg->secureSocket = &secureSocket; 266 arg->messageQ = &messageQ; 267 268 listener.start(arg); 269 } 270 return succeed; 254 271 } 255 272 catch (cf::exception & e) … … 265 282 try 266 283 { 267 socket.send(request.sms(phone)); 268 269 return messageQ.pop(ProtocolType::SMS).get<bool>(ProtocolType::RESULT); 284 return handshake(socket, request.sms(phone), ProtocolType::SMS); 270 285 } 271 286 catch (cf::exception & e) -
trunk/msgsrv.cpp
r19 r20 308 308 } 309 309 310 static std::string ge tRandomCode()310 static std::string generateSMSCode() 311 311 { 312 312 char random[8] = {0x00,}; … … 321 321 322 322 #define CRLF "\r\n" 323 std::string code = ge tRandomCode();323 std::string code = generateSMSCode(); 324 324 std::string url = "unsigned.kr"; 325 325 std::string uri = "/~cheese/sms/req.php?to=" + phone + "&code=" + code; … … 350 350 351 351 std::string auth = crypt.decrypt(cf::bin(secret)).toString(); 352 353 if (auth[0] != '{') 354 THROW_EXCEPTION("invalid secret data (check cipher and its key)"); 355 356 for (size_t iter = 0; iter < auth.length(); iter++) 357 { 358 if (auth[iter] < ' ' || '~' < auth[iter]) 359 THROW_EXCEPTION("invalid secret data (check cipher and its key)"); 360 } 361 352 362 Protocol::Message authParser; 353 363 … … 360 370 throw (cf::exception) 361 371 { 362 if (sms != parser.get<std::string>(ProtocolType::SMS)) 372 Protocol::Message & authParser = getSecret(parser, sms); 373 374 if (sms != authParser.get<std::string>(ProtocolType::SMS)) 363 375 THROW_EXCEPTION("SMS is not same"); 364 365 Protocol::Message & authParser = getSecret(parser, sms);366 376 367 377 std::string id = authParser.get<std::string>(ProtocolType::ID); … … 468 478 std::string id; 469 479 bool loggedIn = false; 480 bool result = false; 470 481 471 482 while (true) 472 483 { 484 result = false; 473 485 parser.parse(sock.receive().toString()); 474 486 … … 476 488 { 477 489 sms = httpSMS(parser); 490 result = true; 478 491 } 479 492 else if (parser.type() == ProtocolType::JOIN) … … 481 494 if (!join(parser, sms, sock.peer().address())) 482 495 THROW_EXCEPTION("user(" << parser.get<std::string>(ProtocolType::ID) << ") cannot join"); 496 result = true; 483 497 } 484 498 else if (parser.type() == ProtocolType::LOGIN) … … 491 505 // ¿©±ä ¼º°øÇÒ ¶§¿¡¸¸ ¿È 492 506 if (login(parser, sock, ip, sms)) 507 { 493 508 loggedIn = true; 509 result = true; 510 } 494 511 } 495 512 496 513 // success 497 sock.send(Protocol::Response().result(parser.type(), true));514 sock.send(Protocol::Response().result(parser.type(), result)); 498 515 if (loggedIn) 499 516 return id; … … 590 607 int main(int argc, char ** argv) 591 608 { 592 if (argc != 3)609 if (argc != 2) 593 610 { 594 611 std::cerr << "-_-^" << std::endl;
Note:
See TracChangeset
for help on using the changeset viewer.