My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include "ProtectClient.h"

NAMESPACE_UPP

ProtectClient::ProtectClient()
{
// generates client id
clientID = -1;

// defaults to Snow2 Cypher
cypher = new Snow2;

// initializes user configuration path
// (can be reset by SetUserConfigPath() )
#ifdef PLATFORM_POSIX
userConfigPath = "/home/" + Upp::GetUserName() + "/." + GetExeTitle() + "/";
#else
userConfigPath = GetAppDataFolder() + "/" + GetExeTitle() + "/";
#endif

// must load config before connection
configLoaded = false;

// setup timeout for http server
// a couple of seconds should be enough !
client.TimeoutMsecs(2000);
}

ProtectClient::~ProtectClient()
{
// if connected, disconnect from server
Disconnect();
}

// stores/retrieve config data from stream
void ProtectClient::Xmlize(XmlIO xml)
{
xml
("EMail", userEMail)
("ClientID", clientID)
("ActivationKey", activationKey)
;
}

bool ProtectClient::StoreConfig(void)
{
RealizePath(userConfigPath);
String path = AppendFileName(userConfigPath, "Protect.xml");
return StoreAsXMLFile(*this, "", path);
}

bool ProtectClient::LoadConfig(void)
{
// don't load if already done
if(configLoaded)
return true;

String path = AppendFileName(userConfigPath, "Protect.xml");
if(FileExists(path))
configLoaded = LoadFromXMLFile(*this, path);
else
configLoaded = false;

return configLoaded;
}

// sets the encrypting engine
// default Cypher at startup is Snow2
// WARNING -- takes Cypher ownership
void ProtectClient::SetCypher(Cypher *c)
{
// sets the new cypher freeing previous one
cypher = c;

}

// sends a VectorMap to server in encrypted form
// and gets its response
VectorMap<String, Value> ProtectClient::SendMap(VectorMap<String, Value> const &v)
{
// copy vectormap adding client id and a magic number
VectorMap<String, Value>dataMap(v, 1);
dataMap.Add("APPID", "ProtectClient");

// add current locale
dataMap.Add("LOCALE", LNGAsText(GetCurrentLanguage()).Left(5));

// sets cypher key (and create a random IV)
cypher->SetKey(communicationKey);

String postData;
postData += "IV=" + HexString(cypher->GetNonce()) + "&";
postData += "DATA=";
postData += HexString((*cypher)(StoreAsXML(dataMap, "ProtectClient")));

client.Post(postData);
String contents = client.ExecuteRedirect();

// if contents start with "ERROR", just fetch the error desc
// and put it in result map
VectorMap<String, Value> resMap;

// if contents don't start with IV field signals it
if(!contents.StartsWith("IV="))
{
resMap.Add("ERROR", PROTECT_MISSING_IV);
resMap.Add("ERRORMSG", ProtectMessage(PROTECT_MISSING_IV));
return resMap;
}

// well, we've at least the IV, so we can fetch it and DATA field and decrypt
StringStream s(contents);
String line = s.GetLine();
String IV = ScanHexString(line.Mid(3));
line = s.GetLine();
if(!line.StartsWith("DATA="))
{
resMap.Add("ERROR", PROTECT_BAD_DATA);
resMap.Add("ERRORMSG", ProtectMessage(PROTECT_BAD_DATA));
return resMap;
}

// decodes DATA field and read VectorMap from it
cypher->SetKey(communicationKey, IV);
String decoded = (*cypher)(ScanHexString(line.Mid(5)));
try
{
LoadFromXML(resMap, decoded);
}
catch(...)
{
resMap.Clear();
resMap.Add("ERROR", PROTECT_BAD_DATA);
resMap.Add("ERRORMSG", ProtectMessage(PROTECT_BAD_DATA));
}
return resMap;
}

// create a persistent link to server
bool ProtectClient::Connect(void)
{
lastError = 0;

// disconnect first, im case we're already connected
Disconnect();

// load config, if not already done so
// this loads previous clientID (reused if still active)
// and activationKey
LoadConfig();

// send a connect packet to server
VectorMap<String, Value>v;
v.Add("REASON", PROTECT_CONNECT);
v.Add("EMAIL", userEMail);
v.Add("CLIENTID", (int)clientID);
v.Add("ACTIVATIONKEY", activationKey);
VectorMap<String, Value> res = SendMap(v);

// check for errors
if(res.Find("ERROR") >= 0)
{
lastError = res.Get("ERROR");
clientID = -1;
return false;
}

int i = res.Find("CLIENTID");
if(i < 0)
{
clientID = -1;
return false;
}
else
{
// if got a new client id (previous was missing/expired)
// replaces old one and store on file
// clientid is used to maintain a persistent client/server relationship
// on every connection, clientID timeout is updated on server side
if(clientID != (dword)(int)res[i])
{
clientID = (dword)(int)res[i];
return StoreConfig();
}
}
return true;
}

// disconnect from server
bool ProtectClient::Disconnect(void)
{
lastError = 0;

// sends a disconnect packet to server
VectorMap<String, Value>v;
v.Add("REASON", PROTECT_DISCONNECT);
v.Add("EMAIL", userEMail);
v.Add("CLIENTID", (int)clientID);
VectorMap<String, Value> res = SendMap(v);

// stores config on server; it will try to reuse clientID
// on next server connection
StoreConfig();

// check for errors
if(res.Find("ERROR") >= 0)
{
lastError = res.Get("ERROR");
return false;
}

return true;
}

// refresh server connection
bool ProtectClient::Refresh(void)
{
lastError = 0;

// sends a refresh packet to server
VectorMap<String, Value>v;
v.Add("REASON", PROTECT_REFRESH);
v.Add("EMAIL", userEMail);
v.Add("CLIENTID", (int)clientID);
VectorMap<String, Value> res = SendMap(v);

// check for errors
if(res.Find("ERROR") >= 0)
{
lastError = res.Get("ERROR");

// resets clientID, it'll be re-generated
// on next server connection request
clientID = -1;
StoreConfig();

return false;
}

return true;
}

// get license key
String ProtectClient::GetLicenseKey(void)
{
lastError = 0;

// sends a getkey packet to server
VectorMap<String, Value>v;
v.Add("REASON", PROTECT_GETLICENSEKEY);
v.Add("EMAIL", userEMail);
v.Add("CLIENTID", (int)clientID);
VectorMap<String, Value> res = SendMap(v);

// check for errors
if(res.Find("ERROR") >= 0)
{
lastError = res.Get("ERROR");
return "";
}
if(res.Find("KEY") < 0)
{
lastError = 999;
return "";
}
return res.Get("KEY");
}

// gets license info
bool ProtectClient::GetLicenseInfo(void)
{
lastError = 0;

// sends a getinfo packet to server
VectorMap<String, Value>v;
v.Add("REASON", PROTECT_GETLICENSEINFO);
v.Add("EMAIL", userEMail);
v.Add("CLIENTID", (int)clientID);
VectorMap<String, Value> res = SendMap(v);

// check for errors
if(res.Find("ERROR") >= 0)
{
lastError = res.Get("ERROR");
return false;
}
if(res.Find("EMAIL") >= 0) userEMail = res.Get("EMAIL");
if(res.Find("NAME") >= 0) userName = res.Get("NAME");
if(res.Find("SURNAME") >= 0) userSurname = res.Get("SURNAME");
if(res.Find("ZONE") >= 0) userZone = res.Get("ZONE");
if(res.Find("COUNTRY") >= 0) userCountry = res.Get("COUNTRY");
if(res.Find("ZIP") >= 0) userZIP = res.Get("ZIP");
if(res.Find("TOWN") >= 0) userTown = res.Get("TOWN");
if(res.Find("ADDRESS") >= 0) userAddress = res.Get("ADDRESS");
if(res.Find("PHONE") >= 0) userPhone = res.Get("PHONE");
if(res.Find("FAX") >= 0) userFax = res.Get("FAX");
if(res.Find("CELL") >= 0) userCell = res.Get("CELL");
if(res.Find("EXPIRATION") >= 0) expireTime = res.Get("EXPIRATION");
if(res.Find("NUMLICENSES") >= 0) numLicenses = res.Get("NUMLICENSES");
if(res.Find("ALLOWEDVERSION") >=0) maxAllowedVersion = res.Get("ALLOWEDVERSION");

return true;
}

// updates user data on server
bool ProtectClient::UpdateUserData(void)
{
lastError = 0;

// sends a register packet to server
VectorMap<String, Value>v;
v.Add("REASON", PROTECT_UPDATEUSERDATA);
v.Add("CLIENTID", (int)clientID);
v.Add("EMAIL", userEMail);
v.Add("NAME", userName);
v.Add("SURNAME", userSurname);
v.Add("ZONE", userZone);
v.Add("COUNTRY", userCountry);
v.Add("ZIP", userZIP);
v.Add("TOWN", userTown);
v.Add("ADDRESS", userAddress);
v.Add("PHONE", userPhone);
v.Add("FAX", userFax);
v.Add("CELL", userCell);
VectorMap<String, Value> res = SendMap(v);

// check for errors
if(res.Find("ERROR") >= 0)
{
lastError = res.Get("ERROR");
return false;
}
return true;
}

// register app
bool ProtectClient::Register(void)
{
lastError = 0;

// sends a register packet to server
VectorMap<String, Value>v;
v.Add("REASON", PROTECT_REGISTER);
v.Add("EMAIL", userEMail);
v.Add("NAME", userName);
v.Add("SURNAME", userSurname);
v.Add("ZONE", userZone);
v.Add("COUNTRY", userCountry);
v.Add("ZIP", userZIP);
v.Add("TOWN", userTown);
v.Add("ADDRESS", userAddress);
v.Add("PHONE", userPhone);
v.Add("FAX", userFax);
v.Add("CELL", userCell);
VectorMap<String, Value> res = SendMap(v);

// check for errors
if(res.Find("ERROR") >= 0)
{
lastError = res.Get("ERROR");
return false;
}
return true;
}

END_UPP_NAMESPACE

Change log

r4595 by micio on Feb 14, 2012   Diff
Bazaar/Protect : removed some left-overs
Go to: 
Project members, sign in to write a code review

Older revisions

r4587 by micio on Feb 13, 2012   Diff
Bazaar/Protect : Added some fields to
users database
r3190 by micio on Feb 10, 2011   Diff
Bazaar/Protect : fixed Expire field
name in license info
r3187 by micio on Feb 9, 2011   Diff
Bazaar/Protect : added language
handling on client side
All revisions of this file

File info

Size: 8663 bytes, 366 lines
Powered by Google Project Hosting