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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
|
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using Agger.Registro;
using Gestor.Application.Migration;
using Gestor.Common.Exceptions;
namespace Gestor.Application.Helpers;
public class ConnectionHelper
{
public static bool BancoAtualizado { get; set; }
private static async Task<string> Get(string command, bool autorizar = true)
{
Uri uri = new Uri(Address.GestorApi(), command);
HttpClient val = new HttpClient
{
Timeout = new TimeSpan(0, 2, 0)
};
if (autorizar)
{
val.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", Connection.BasicKey());
}
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
try
{
HttpResponseMessage val2 = await val.GetAsync(uri).ConfigureAwait(continueOnCapturedContext: false);
if (val2.StatusCode != HttpStatusCode.OK)
{
return await val2.Content.ReadAsStringAsync();
}
return null;
}
catch (Exception)
{
return "ERRO INTERNO";
}
}
private static async Task<string> Post(string command)
{
Uri uri = new Uri(Address.GestorApi(), command);
HttpClient val = new HttpClient();
val.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", Connection.BasicKey());
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
try
{
HttpResponseMessage val2 = await val.PostAsync(uri, (HttpContent)null);
if (val2.StatusCode != HttpStatusCode.OK)
{
return await val2.Content.ReadAsStringAsync();
}
return null;
}
catch (Exception)
{
return "ERRO INTERNO";
}
}
private static async Task<string> DatabaseTest()
{
return await Get("Data/Verify");
}
public async Task<string> AtualizarBanco()
{
bool num = await new Migrator().Execute();
BancoAtualizado = num;
return num ? null : "HOUVE UM PROBLEMA AO ATUALIZAR O BANCO DE DADOS.";
}
internal static async Task<bool> TryConnect(string connectionString, int retryCount = 1)
{
return await Task.Run(delegate
{
//IL_001b: Unknown result type (might be due to invalid IL or missing references)
//IL_0021: Expected O, but got Unknown
List<Exception> list = new List<Exception>();
for (int i = 0; i < retryCount; i++)
{
try
{
SqlConnection val = new SqlConnection(connectionString + "Connection Timeout=5");
try
{
((DbConnection)(object)val).Open();
}
finally
{
((IDisposable)val)?.Dispose();
}
return true;
}
catch (Exception item)
{
list.Add(item);
}
}
return list.Count == 0;
});
}
internal static async Task<bool> HasLogin(string connectionString)
{
return await Task.Run(delegate
{
//IL_000c: Unknown result type (might be due to invalid IL or missing references)
//IL_0012: Expected O, but got Unknown
List<Exception> list = new List<Exception>();
try
{
SqlConnection val = new SqlConnection(connectionString);
try
{
((DbConnection)(object)val).Open();
SqlCommand val2 = val.CreateCommand();
try
{
((DbCommand)(object)val2).CommandText = "SELECT 1 FROM [dbo].[usuario] WHERE [removido] IS NULL OR [removido] = '0'";
SqlDataReader val3 = val2.ExecuteReader();
if (!((DbDataReader)(object)val3).HasRows)
{
return false;
}
((DbDataReader)(object)val3).Close();
((DbCommand)(object)val2).CommandText = "SELECT 1 FROM [dbo].[empresa] WHERE [idempresa] = 1";
val3 = val2.ExecuteReader();
if (!((DbDataReader)(object)val3).HasRows)
{
return false;
}
((DbDataReader)(object)val3).Close();
((DbCommand)(object)val2).CommandText = "SELECT 1 FROM [dbo].[vendedor] WHERE [corretora] = '1'";
val3 = val2.ExecuteReader();
if (!((DbDataReader)(object)val3).HasRows)
{
return false;
}
((DbDataReader)(object)val3).Close();
return true;
}
finally
{
((IDisposable)val2)?.Dispose();
}
}
finally
{
((IDisposable)val)?.Dispose();
}
}
catch (Exception item)
{
list.Add(item);
}
return list.Count == 0;
});
}
public async Task<Tuple<bool, bool>> PrimeiroAcesso()
{
string conn = Connection.GetConnection(tryconnect: false);
if (!(await TryConnect(conn)))
{
if (!(await DatabaseExists()))
{
return new Tuple<bool, bool>(item1: false, item2: false);
}
bool flag = !(await LoginExists());
if (flag)
{
flag = !(await CreateLogin());
}
if (flag)
{
return new Tuple<bool, bool>(item1: false, item2: false);
}
flag = !(await UserExists());
if (flag)
{
flag = !(await CreateUser());
}
if (flag)
{
return new Tuple<bool, bool>(item1: false, item2: false);
}
}
return (!(await HasLogin(conn))) ? new Tuple<bool, bool>(item1: true, item2: false) : new Tuple<bool, bool>(item1: true, item2: true);
}
public bool BDConvertido()
{
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
//IL_000c: Expected O, but got Unknown
try
{
SqlConnection val = new SqlConnection(Connection.GetConnection(tryconnect: false));
try
{
((DbConnection)(object)val).Open();
SqlCommand val2 = val.CreateCommand();
try
{
((DbCommand)(object)val2).CommandText = "SELECT OBJECT_ID('dbo.AtualizacaoDados', 'U')";
if (((DbCommand)(object)val2).ExecuteScalar() == DBNull.Value)
{
return true;
}
}
finally
{
((IDisposable)val2)?.Dispose();
}
SqlCommand val3 = val.CreateCommand();
try
{
((DbCommand)(object)val3).CommandText = "SELECT COUNT(IdArquivo) FROM AtualizacaoDados";
return (int)((DbCommand)(object)val3).ExecuteScalar() == 0;
}
finally
{
((IDisposable)val3)?.Dispose();
}
}
finally
{
((IDisposable)val)?.Dispose();
}
}
catch
{
}
return true;
}
public bool ExisteEmpresa()
{
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
//IL_000c: Expected O, but got Unknown
try
{
SqlConnection val = new SqlConnection(Connection.GetConnection(tryconnect: false));
try
{
((DbConnection)(object)val).Open();
SqlCommand val2 = val.CreateCommand();
try
{
((DbCommand)(object)val2).CommandText = "SELECT OBJECT_ID (N'empresa', N'U')";
if (((DbCommand)(object)val2).ExecuteScalar() == DBNull.Value)
{
return false;
}
}
finally
{
((IDisposable)val2)?.Dispose();
}
SqlCommand val3 = val.CreateCommand();
try
{
((DbCommand)(object)val3).CommandText = "SELECT COUNT(idempresa) FROM empresa WHERE idempresa = 1";
return (int)((DbCommand)(object)val3).ExecuteScalar() > 0;
}
finally
{
((IDisposable)val3)?.Dispose();
}
}
finally
{
((IDisposable)val)?.Dispose();
}
}
catch
{
}
return true;
}
public async Task<string> VerifyConnection()
{
_ = 3;
try
{
if (await TryConnect(Connection.GetConnection(tryconnect: false)))
{
return null;
}
string text = await DatabaseTest();
if (text != null)
{
return text;
}
string text2 = Connection.ConnectionAddress.Catalog + ".cfg";
string path = AppDomain.CurrentDomain.BaseDirectory + "Data_" + text2;
string path2 = AppDomain.CurrentDomain.BaseDirectory + "Files_" + text2;
string path3 = AppDomain.CurrentDomain.BaseDirectory + "Sign_" + text2;
if (File.Exists(path))
{
File.Delete(path);
}
if (File.Exists(path2))
{
File.Delete(path2);
}
if (File.Exists(path3))
{
File.Delete(path3);
}
string returnMessage = await CheckDatabase();
await Task.Delay(2000);
return returnMessage;
}
catch (Exception)
{
return "ERRO AO CONECTAR NO BANCO DE DADOS DA CORRETORA";
}
}
public static async Task<string> CheckDatabase()
{
_ = 5;
try
{
bool hasDatabase = await DatabaseExists();
bool hasLogin = await LoginExists();
bool hasUser = await UserExists();
bool created = false;
if (hasDatabase && hasLogin && hasUser)
{
return null;
}
if (!hasDatabase)
{
hasDatabase = await CreateDatabase();
}
if (!hasDatabase)
{
return "ERRO AO CRIAR BANCO DE DADOS";
}
if (!hasLogin)
{
bool num = await CreateLogin();
bool flag = num;
created = num;
hasLogin = flag;
}
if (!hasLogin)
{
return "ERRO AO CRIAR LOGIN";
}
if (!hasUser)
{
hasUser = await CreateUser();
}
if (created)
{
int num2 = 10;
while (num2 > 1)
{
try
{
SqlConnection val = new SqlConnection(Connection.GetConnection());
try
{
((DbConnection)(object)val).Open();
}
finally
{
((IDisposable)val)?.Dispose();
}
}
catch
{
Thread.Sleep(10000);
num2--;
continue;
}
break;
}
}
return (!hasUser) ? "ERRO AO CRIAR USUÁRIO" : null;
}
catch (ValidationException val2)
{
return ((Exception)val2).Message;
}
catch (Exception)
{
return "ERRO AO CRIAR BANCO DE DADOS";
}
}
private static async Task<bool> CreateDatabase()
{
string text = Uri.EscapeDataString(ApplicationHelper.NumeroSerial);
if (await Post("Data/CreateData?token=" + text) == "ERRO INTERNO")
{
return false;
}
int tries = 0;
bool flag = false;
while (tries < 24 && !flag)
{
await Task.Delay(5000);
tries++;
flag = await DatabaseExists();
}
return flag;
}
private static async Task<bool> CreateLogin()
{
if (await Post("Data/CreateLogin") == "ERRO INTERNO")
{
return false;
}
int tries = 0;
bool flag = false;
while (tries < 24 && !flag)
{
await Task.Delay(5000);
tries++;
flag = await Get("Data/ServerLoginExists") == null;
}
return flag;
}
private static async Task<bool> CreateUser()
{
if (await Post("Data/CreateUser") == "ERRO INTERNO")
{
return false;
}
int tries = 0;
bool flag = false;
while (tries < 24 && !flag)
{
await Task.Delay(5000);
tries++;
flag = await Get("Data/UserExists") == null;
}
return flag;
}
private static async Task<bool> DatabaseExists()
{
return await Get("Data/DataExists") == null;
}
private static async Task<bool> LoginExists()
{
return await Get("Data/ServerLoginExists") == null;
}
private static async Task<bool> UserExists()
{
return await Get("Data/UserExists") == null;
}
}
|