Changeset 20 in chevmsgr


Ignore:
Timestamp:
11/24/15 00:21:07 (8 years ago)
Author:
cheese
Message:

cf::bin 버그 픽스
msg / client / server 버그 픽스

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/cf/bin.cpp

    r4 r20  
    102102cf::bin cf::bin::operator +(const cf::bin & in) const
    103103{
    104     bin out(in);
     104    bin out(*this);
    105105
    106106    out.append(in);
  • trunk/msg.cpp

    r18 r20  
    9393    }
    9494
    95     std::string Request::secretWithRandom(const std::string & id, std::string & secret, const std::string & random) const
     95    std::string Request::secretWithRandom(const std::string & type, const std::string & id, std::string & secret, const std::string & random) const
    9696    {
    97         DECLARE_TEMPLATE_OBJECT(obj, ProtocolType::SECRET);
     97        DECLARE_TEMPLATE_OBJECT(obj, type);
    9898
    9999        obj[ProtocolType::ID] = id;
  • trunk/msg.hpp

    r19 r20  
    113113        std::string login(const std::string & pw) const;
    114114
    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;
    116116
    117117        std::string logout() const;
  • trunk/msgclnt.cpp

    r18 r20  
    117117static std::string generateRandom()
    118118{
    119     unsigned int ret = 0;
    120119    int t = (int)time(NULL);
    121120
     
    139138    crypt.setKey(cf::bin(sms + DELIMITER + random));
    140139
    141     return crypt.encrypt(cf::bin(msg)).toString();
     140    return cf::codec::hex::getInstance()->encode(crypt.encrypt(cf::bin(msg)));
    142141}
    143142
     
    176175}
    177176
     177static 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
    178190// --------------------------------------------------------------
    179191
     
    201213    {
    202214        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);
    213216        return true;
    214217    }
     
    226229    {
    227230        std::string random = generateRandom();
    228         std::string msg = request.join(id, hashPassword(pw), sms);
    229         std::string secret = getSecret(msg, sms, random);
    230         socket.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);
    233236    }
    234237    catch (cf::exception & e)
     
    247250
    248251        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;
    254271    }
    255272    catch (cf::exception & e)
     
    265282    try
    266283    {
    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);
    270285    }
    271286    catch (cf::exception & e)
  • trunk/msgsrv.cpp

    r19 r20  
    308308}
    309309
    310 static std::string getRandomCode()
     310static std::string generateSMSCode()
    311311{
    312312    char random[8] = {0x00,};
     
    321321
    322322#define CRLF    "\r\n"
    323     std::string code = getRandomCode();
     323    std::string code = generateSMSCode();
    324324    std::string url = "unsigned.kr";
    325325    std::string uri = "/~cheese/sms/req.php?to=" + phone + "&code=" + code;
     
    350350
    351351    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
    352362    Protocol::Message authParser;
    353363
     
    360370    throw (cf::exception)
    361371{
    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))
    363375        THROW_EXCEPTION("SMS is not same");
    364 
    365     Protocol::Message & authParser = getSecret(parser, sms);
    366376
    367377    std::string id = authParser.get<std::string>(ProtocolType::ID);
     
    468478        std::string id;
    469479        bool loggedIn = false;
     480        bool result = false;
    470481
    471482        while (true)
    472483        {
     484            result = false;
    473485            parser.parse(sock.receive().toString());
    474486
     
    476488            {
    477489                sms = httpSMS(parser);
     490                result = true;
    478491            }
    479492            else if (parser.type() == ProtocolType::JOIN)
     
    481494                if (!join(parser, sms, sock.peer().address()))
    482495                    THROW_EXCEPTION("user(" << parser.get<std::string>(ProtocolType::ID) << ") cannot join");
     496                result = true;
    483497            }
    484498            else if (parser.type() == ProtocolType::LOGIN)
     
    491505                // ¿©±ä ¼º°øÇÒ ¶§¿¡¸¸ ¿È
    492506                if (login(parser, sock, ip, sms))
     507                {
    493508                    loggedIn = true;
     509                    result = true;
     510                }
    494511            }
    495512
    496513            // success
    497             sock.send(Protocol::Response().result(parser.type(), true));
     514            sock.send(Protocol::Response().result(parser.type(), result));
    498515            if (loggedIn)
    499516                return id;
     
    590607int main(int argc, char ** argv)
    591608{
    592     if (argc != 3)
     609    if (argc != 2)
    593610    {
    594611        std::cerr << "-_-^" << std::endl;
Note: See TracChangeset for help on using the changeset viewer.