source: chevmsgr/trunk/msg.hpp@ 33

Last change on this file since 33 was 33, checked in by cheese, 8 years ago
  1. 클라이언트 UI ID/PW 입력 안하면 죽는 문제 수정
  2. UI 종료 함수 exit로 수정
  3. 그룹챗 인덱스 변수 형 때문에 나오는 warning 수정
  4. toOpenSession 함수 가끔 죽는 문제 수정
  5. generateRandom()에서 time()하던거 rand()로 수정
  6. sms 전송 url 및 sms 코드 생성 로직 대폭 수정 (주석 참고)
File size: 4.5 KB
Line 
1#pragma once
2
3#include "cf/logger.h"
4#include "cf/network.h"
5
6#include "json.h"
7#include "crypto.h"
8
9#include <iostream>
10#include <string>
11#include <vector>
12#include <time.h>
13
14typedef struct SFriend
15{
16 std::string id;
17 std::string name;
18} SFriend;
19
20#define DELIMITER "$"
21
22// --------------------------------------------------------------
23
24namespace ProtocolType
25{
26#define DECLARE_KEY(_t) static const std::string _t = #_t
27
28 DECLARE_KEY(NONE);
29 DECLARE_KEY(SECRET);
30 DECLARE_KEY(RANDOM);
31 DECLARE_KEY(ID);
32 DECLARE_KEY(TYPE);
33 DECLARE_KEY(NAME);
34 DECLARE_KEY(SMS);
35 DECLARE_KEY(PHONE);
36 DECLARE_KEY(JOIN);
37 DECLARE_KEY(PW);
38 DECLARE_KEY(LOGIN);
39 DECLARE_KEY(LOGOUT);
40 DECLARE_KEY(ADD_FRIEND);
41 DECLARE_KEY(OPEN_SESSION);
42 DECLARE_KEY(TO);
43 DECLARE_KEY(LISTEN);
44 DECLARE_KEY(TELL);
45 DECLARE_KEY(FROM);
46 DECLARE_KEY(SESSION_ID);
47 DECLARE_KEY(SESSION_KEY);
48 DECLARE_KEY(MESSAGE);
49 DECLARE_KEY(SENSITIVE);
50 DECLARE_KEY(GET_FRIEND_LIST);
51 DECLARE_KEY(FRIEND_LIST);
52 DECLARE_KEY(FRIEND_ID);
53 DECLARE_KEY(ID_LIST);
54 DECLARE_KEY(RESULT);
55}
56
57namespace Protocol
58{
59 class Message
60 {
61 public:
62 json::Object mObject;
63
64 Message();
65
66 void parse(const std::string & message);
67
68 template<typename T>
69 T get(const std::string & key) const
70 {
71 return (T)mObject[key];
72 }
73
74 template<typename T>
75 std::vector<T> getList(const std::string & key) const
76 {
77 std::vector<T> ret;
78 json::Array ar = mObject[key];
79
80 for (size_t iter = 0; iter < ar.size(); iter++)
81 ret.push_back(ar[iter]);
82
83 return ret;
84 }
85
86 std::vector<SFriend> getFriendList() const;
87
88 std::string type() const;
89
90 std::string serialize() const;
91 };
92
93 class IProtocol
94 {
95 public:
96 virtual void makeTemplate(json::Object & obj, const std::string & type) const = 0;
97 };
98
99 class Request : public IProtocol
100 {
101 private:
102 std::string mID;
103
104 public:
105 void setUserID(const std::string & id);
106
107 const std::string & getUserID();
108
109 void makeTemplate(json::Object & obj, const std::string & type) const;
110
111 std::string sms(const std::string & phone) const;
112
113 std::string join(const std::string & id, const std::string & pw, const std::string & sms) const;
114
115 std::string login(const std::string & pw) const;
116
117 std::string secretWithRandom(const std::string & type, const std::string & id, std::string & secret, const std::string & random) const;
118
119 std::string logout() const;
120
121 std::string addFriend(const std::string & id) const;
122
123 std::string openSession(const std::vector<std::string> & idList) const;
124
125 std::string tell(const std::string & sessid, const std::string & message, const bool sensitive) const;
126
127 std::string getFriendList() const;
128 };
129
130 class Response : public IProtocol
131 {
132 public:
133 void makeTemplate(json::Object & obj, const std::string & type) const;
134
135 std::string result(const std::string & requestType, const bool status) const;
136
137 std::string friendList(const std::vector<SFriend> & friendList) const;
138 };
139};
140
141// --------------------------------------------------------------
142
143#define LOG(message) log(__FILE__,__func__,__LINE__,message)
144
145static void log(const std::string & file,
146 const std::string & func,
147 const int line,
148 const std::string & message)
149{
150 static cf::logger * logger = NULL;
151
152 if (!logger)
153 {
154 logger = cf::logger::getInstance();
155 logger->init(".");
156 logger->setLevel(1);
157 logger->add("messenger", 1, "INFO", 0);
158 }
159
160 logger->write(1, message);
161 std::cerr << "[" << file << ":" << line << "][" << func << "] "
162 << message << std::endl;
163}
164
165static std::string joinStrings(const std::vector<std::string> & strings)
166{
167 std::string concat = strings[0];
168
169 for (size_t iter = 1; iter < strings.size(); iter++)
170 concat += "," + strings[iter];
171
172 return concat;
173}
174
175static cf::bin generateRandom()
176{
177 srand((unsigned int)time(NULL));
178 int t = rand();
179
180 cf::bin b;
181 b.resize(sizeof(int));
182
183 b.set((cf::uint8_t*)&t, sizeof(int));
184 return crypto().sha256(b);
185}
186
187class SecureSocket
188{
189private:
190 cf::network::tcp * sock;
191 crypto aria;
192
193public:
194 SecureSocket()
195 {
196 sock = NULL;
197 }
198
199 SecureSocket(cf::network::tcp * sock, const std::string & ip, const std::string & sms)
200 {
201 setSocket(sock);
202 setKey(ip, sms);
203 }
204
205 void setSocket(cf::network::tcp * sock)
206 {
207 this->sock = sock;
208 }
209
210 cf::network::tcp & getTcpSocket()
211 {
212 return *sock;
213 }
214
215 void setKey(const std::string & ip, const std::string & sms)
216 {
217 cf::bin seed = sms + DELIMITER + ip;
218 cf::bin key = crypto().sha256(seed);
219
220 aria.setKey(key);
221 }
222
223 void send(const std::string & msg)
224 {
225 sock->send(aria.encrypt(cf::bin(msg)));
226 }
227
228 std::string receive()
229 {
230 return aria.decrypt(sock->receive()).toString();
231 }
232
233 void close()
234 {
235 if (sock)
236 sock->close();
237 }
238};
Note: See TracBrowser for help on using the repository browser.