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 |
|
---|
14 | typedef struct SFriend
|
---|
15 | {
|
---|
16 | std::string id;
|
---|
17 | std::string name;
|
---|
18 | } SFriend;
|
---|
19 |
|
---|
20 | #define DELIMITER "$"
|
---|
21 |
|
---|
22 | // --------------------------------------------------------------
|
---|
23 |
|
---|
24 | namespace 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 |
|
---|
57 | namespace 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 |
|
---|
145 | static 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 |
|
---|
165 | static 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 |
|
---|
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 |
|
---|
186 | class SecureSocket
|
---|
187 | {
|
---|
188 | private:
|
---|
189 | cf::network::tcp * sock;
|
---|
190 | crypto aria;
|
---|
191 |
|
---|
192 | public:
|
---|
193 | SecureSocket()
|
---|
194 | {
|
---|
195 | sock = NULL;
|
---|
196 | }
|
---|
197 |
|
---|
198 | SecureSocket(cf::network::tcp * sock, const std::string & ip, const std::string & sms)
|
---|
199 | {
|
---|
200 | setSocket(sock);
|
---|
201 | setKey(ip, sms);
|
---|
202 | }
|
---|
203 |
|
---|
204 | void setSocket(cf::network::tcp * sock)
|
---|
205 | {
|
---|
206 | this->sock = sock;
|
---|
207 | }
|
---|
208 |
|
---|
209 | cf::network::tcp & getTcpSocket()
|
---|
210 | {
|
---|
211 | return *sock;
|
---|
212 | }
|
---|
213 |
|
---|
214 | void setKey(const std::string & ip, const std::string & sms)
|
---|
215 | {
|
---|
216 | cf::bin seed = sms + DELIMITER + ip;
|
---|
217 | cf::bin key = crypto().sha256(seed);
|
---|
218 |
|
---|
219 | aria.setKey(key);
|
---|
220 | }
|
---|
221 |
|
---|
222 | void send(const std::string & msg)
|
---|
223 | {
|
---|
224 | sock->send(aria.encrypt(cf::bin(msg)));
|
---|
225 | }
|
---|
226 |
|
---|
227 | std::string receive()
|
---|
228 | {
|
---|
229 | return aria.decrypt(sock->receive()).toString();
|
---|
230 | }
|
---|
231 |
|
---|
232 | void close()
|
---|
233 | {
|
---|
234 | if (sock)
|
---|
235 | sock->close();
|
---|
236 | }
|
---|
237 | }; |
---|