summaryrefslogtreecommitdiff
path: root/Decompiler/Gestor.Application.Helpers/ConnectionHelper.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Decompiler/Gestor.Application.Helpers/ConnectionHelper.cs')
-rw-r--r--Decompiler/Gestor.Application.Helpers/ConnectionHelper.cs470
1 files changed, 470 insertions, 0 deletions
diff --git a/Decompiler/Gestor.Application.Helpers/ConnectionHelper.cs b/Decompiler/Gestor.Application.Helpers/ConnectionHelper.cs
new file mode 100644
index 0000000..8bb8e98
--- /dev/null
+++ b/Decompiler/Gestor.Application.Helpers/ConnectionHelper.cs
@@ -0,0 +1,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;
+ }
+}