using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; using System.Threading.Tasks; using Agger.Registro; using Gestor.Common.Helpers; using Gestor.Common.Validation; using Gestor.Model.API; using Gestor.Model.Domain.Generic; using Gestor.Model.License; using Microsoft.Win32; using Newtonsoft.Json; namespace Gestor.Application.Helpers; public class LicenseHelper { public static int? DiasRestantes { get; set; } public static StatusLicenca Status { get; set; } = (StatusLicenca)1; public static List Produtos { get; set; } public static Instalacao Instalacao { get; set; } public static DateTime DataAtual { get; private set; } private string _guid { get; set; } public async Task VerificarRegistro() { return await Task.Run(delegate { //IL_0005: Unknown result type (might be due to invalid IL or missing references) //IL_000b: Expected O, but got Unknown RegistryHelper val = new RegistryHelper(ApplicationHelper.Subkey); if (string.IsNullOrEmpty(val.Read("NS", true))) { return false; } string s = EncryptionHelper.Decrypt(val.Read("CUSTOMER", true)); if (!long.TryParse(s, out var _)) { ApplicationHelper.IdFornecedor = long.Parse(Connection.Get("Configuration/Customer").Result); val.Write("CUSTOMER", EncryptionHelper.Encrypt(ApplicationHelper.IdFornecedor.ToString()), true); } else { ApplicationHelper.IdFornecedor = long.Parse(s); } string text = Connection.Get("Installation/Time", autorizar: false).Result ?? Funcoes.GetNetworkTime().ToString("d"); DataAtual = DateTime.Parse(text); val.Write("CURRENTDATE", EncryptionHelper.Encrypt(text), true); val.Write("DATE", text, true); ApplicationHelper.Beta = Connection.Get>($"Configuration/Customer/{ApplicationHelper.IdFornecedor}").Result?.Any((object x) => x.ToString().Contains("\"Tipo\": 45")) ?? false; Address.Beta = ApplicationHelper.Beta; return true; }); } public async Task> VerificarAcesso() { return await Task.Run(delegate { //IL_000b: Unknown result type (might be due to invalid IL or missing references) //IL_0011: Expected O, but got Unknown //IL_053f: Unknown result type (might be due to invalid IL or missing references) //IL_0545: Invalid comparison between Unknown and I4 //IL_0547: Unknown result type (might be due to invalid IL or missing references) //IL_0555: Unknown result type (might be due to invalid IL or missing references) //IL_02d4: Unknown result type (might be due to invalid IL or missing references) //IL_035e: Unknown result type (might be due to invalid IL or missing references) //IL_0363: Unknown result type (might be due to invalid IL or missing references) //IL_036f: Expected O, but got Unknown //IL_03b1: Unknown result type (might be due to invalid IL or missing references) //IL_040b: Unknown result type (might be due to invalid IL or missing references) //IL_03d7: Unknown result type (might be due to invalid IL or missing references) //IL_03dc: Unknown result type (might be due to invalid IL or missing references) //IL_03e3: Unknown result type (might be due to invalid IL or missing references) //IL_03ef: Unknown result type (might be due to invalid IL or missing references) //IL_03f7: Unknown result type (might be due to invalid IL or missing references) //IL_03fe: Unknown result type (might be due to invalid IL or missing references) //IL_040a: Expected O, but got Unknown RegistryHelper val = new RegistryHelper(ApplicationHelper.Subkey); DateTime now = Funcoes.GetNetworkTime(); DateTime today = now.Date; if (ApplicationHelper.Conectado) { List result = Connection.Get>($"Access/Customer/{ApplicationHelper.IdFornecedor}").Result; List liberados = new List { 2L, 3L, 4L, 5L, 24L, 10024L }; Produtos = (from x in result group x by new { x.ProductId, x.Status }).Select(x => { //IL_0014: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_0038: Unknown result type (might be due to invalid IL or missing references) //IL_00b0: Unknown result type (might be due to invalid IL or missing references) //IL_00cd: Unknown result type (might be due to invalid IL or missing references) //IL_0102: Unknown result type (might be due to invalid IL or missing references) //IL_0127: Expected O, but got Unknown Licenca val2 = new Licenca { Produto = (Produto)x.Key.ProductId.GetValueOrDefault() }; AccessControl? obj2 = (from l in x.SelectMany((Access c) => c.Control) orderby l.Expiration descending select l).FirstOrDefault(); val2.Liberacao = ((obj2 != null) ? obj2.Expiration : now.AddDays(-1.0)); val2.AcessoLiberado = x.All(delegate(Access l) { if (!l.AgreementId.HasValue || !liberados.Contains(l.AgreementId.Value)) { AccessControl? obj3 = (from d in x.SelectMany((Access c) => c.Control) orderby d.Expiration descending select d).FirstOrDefault(); if (obj3 == null) { return false; } return obj3.Expiration >= today; } return true; }); val2.Quantidade = (int)x.Sum((Access l) => l.Ammount.GetValueOrDefault()); val2.Status = int.Parse(x.Key.Status ?? "1"); return val2; }).ToList(); if (Produtos == null || Produtos.Count == 0 || Produtos.All((Licenca x) => x.Status == 3)) { val.Write("EXPIRATION", EncryptionHelper.Encrypt(now.AddDays(-1.0).ToString("d")), true); val.Write("BLOCK", EncryptionHelper.Encrypt("TRUE"), true); Status = (StatusLicenca)3; return new Tuple(item1: false, ""); } Licenca? obj = ((IEnumerable)Produtos.OrderBy((Licenca x) => x.Liberacao)).FirstOrDefault((Func)((Licenca x) => !x.AcessoLiberado && x.Status == 1)); DateTime dateTime = ((obj != null) ? obj.Liberacao : now); DiasRestantes = (int)(dateTime - today).TotalDays; val.Write("EXPIRATION", EncryptionHelper.Encrypt(dateTime.ToString("d")), true); val.Write("BLOCK", (dateTime >= today) ? EncryptionHelper.Encrypt("FALSE") : EncryptionHelper.Encrypt("TRUE"), true); bool flag = Produtos.Any((Licenca x) => (int)x.Produto == 1 && x.Status == 4); val.Write("CONSULTA", EncryptionHelper.Encrypt(flag.ToString().ToUpper()), true); if (flag) { Status = (StatusLicenca)2; } Status = (StatusLicenca)((dateTime < today) ? 1 : 0); if (Produtos.Any((Licenca x) => (int)x.Produto == 1) && (int)Status == 0) { val.Write("P1", EncryptionHelper.Encrypt("1$P1#" + ApplicationHelper.NumeroSerial), true); val.Write("P5", EncryptionHelper.Encrypt("1$P1#" + ApplicationHelper.NumeroSerial), true); val.Write("M1", EncryptionHelper.Encrypt("TRUE"), true); } string text = JsonConvert.SerializeObject((object)Produtos.Where((Licenca x) => x.Status == 1), (Formatting)1, new JsonSerializerSettings { ReferenceLoopHandling = (ReferenceLoopHandling)1 }); val.Write("PRODUTOS", EncryptionHelper.Encrypt(text), true); if (Produtos.Find((Licenca p) => (int)p.Produto == 86) != null) { return new Tuple(item1: true, EnumHelper.GetDescription(Status)); } if (ValidationHelper.IsNotNullOrEmpty(AssinadorHelper.Key().Result)) { Produtos.Add(new Licenca { AcessoLiberado = true, Liberacao = today, Produto = (Produto)86, Quantidade = 0, Status = 1 }); } return new Tuple(item1: true, EnumHelper.GetDescription(Status)); } if ((today - DataAtual).TotalDays > 2.0) { return new Tuple(item1: true, "DATA INVÁLIDA"); } DiasRestantes = (int)(DateTime.Parse(EncryptionHelper.Decrypt(val.Read("EXPIRATION", true))).AddDays(5.0) - DataAtual).TotalDays; if (DiasRestantes < 1) { return new Tuple(item1: true, "SISTEMA BLOQUEADO"); } if (EncryptionHelper.Decrypt(val.Read("BLOCK", true)) == "TRUE") { return new Tuple(item1: true, "SISTEMA BLOQUEADO"); } if (EncryptionHelper.Decrypt(val.Read("CONSULTA", true)) == "TRUE") { Status = (StatusLicenca)2; } string text2 = EncryptionHelper.Decrypt(val.Read("PRODUTOS", true)); if (string.IsNullOrEmpty(text2)) { Status = (StatusLicenca)3; return new Tuple(item1: false, "SISTEMA CANCELADO"); } Produtos = JsonConvert.DeserializeObject>(text2); Status = (StatusLicenca)(((int)Status == 2) ? ((int)Status) : 0); return new Tuple(item1: true, EnumHelper.GetDescription(Status)); }); } public async Task VerificarMaquina() { return await Task.Run(delegate { //IL_0005: Unknown result type (might be due to invalid IL or missing references) //IL_000b: Expected O, but got Unknown //IL_029d: Unknown result type (might be due to invalid IL or missing references) //IL_02a2: Unknown result type (might be due to invalid IL or missing references) //IL_02ad: Unknown result type (might be due to invalid IL or missing references) //IL_02b8: Unknown result type (might be due to invalid IL or missing references) //IL_02c0: Unknown result type (might be due to invalid IL or missing references) //IL_02cb: Unknown result type (might be due to invalid IL or missing references) //IL_02d6: Unknown result type (might be due to invalid IL or missing references) //IL_02e6: Unknown result type (might be due to invalid IL or missing references) //IL_02f6: Unknown result type (might be due to invalid IL or missing references) //IL_0301: Unknown result type (might be due to invalid IL or missing references) //IL_0346: Expected O, but got Unknown RegistryHelper val = new RegistryHelper(ApplicationHelper.Subkey); string macAddress = GetMacAddress(); string value = val.Read("MACHINEID", false); if (string.IsNullOrEmpty(value)) { value = GetMachineSerial(); val.Write("MACHINEID", EncryptionHelper.Encrypt(macAddress + " " + value), false); } string[] array = EncryptionHelper.Decrypt(val.Read("MACHINEID", false)).Split(new char[1] { ' ' }); string text = ((array.Length > 1) ? array[1] : array[0]); if (ApplicationHelper.Conectado) { List result = Connection.Get>("Installation/Machine/" + text + "/GerenciadorList").Result; if (!Guid.TryParse(text, out var _)) { text = GetMachineSerial(); } _guid = text; Instalacao = result?.FirstOrDefault((Func)((Instalacao x) => x.IdFornecedor == ApplicationHelper.IdFornecedor)); if (Instalacao != null && Instalacao.IdFornecedor != ApplicationHelper.IdFornecedor) { Instalacao = null; } if (Instalacao != null) { Instalacao.Gerenciador = text; Instalacao.UsuarioId = ((DomainBase)Recursos.Usuario).Id; Instalacao.UsuarioSistema = Recursos.Usuario.Nome; Instalacao.Ip = Recursos.Host.AddressList.FirstOrDefault((IPAddress ip) => ip.AddressFamily == AddressFamily.InterNetwork)?.ToString(); Instalacao.UltimoAcesso = DateTime.Now; Instalacao.OSInfo = string.Format("P={0};V={1};x64={2};N={3}", (Environment.OSVersion.Platform == PlatformID.Win32NT) ? "W" : "O", Environment.OSVersion.Version, Environment.Is64BitOperatingSystem ? "1" : "0", GetFrameworkVersion()); Instalacao = Connection.Put("Installation/Machine", Instalacao).Result; val.Write("MACHINEID", EncryptionHelper.Encrypt(macAddress + " " + Instalacao.Gerenciador), false); } val.Write("ACCESS", (Instalacao != null || RegistrarMaquina().Result) ? EncryptionHelper.Encrypt("TRUE") : EncryptionHelper.Encrypt("FALSE"), false); } else if (EncryptionHelper.Decrypt(val.Read("ACCESS", false)) == "TRUE") { Instalacao = new Instalacao { IdFornecedor = ApplicationHelper.IdFornecedor, Data = DataAtual, Gerenciador = text, NomeMaquina = Environment.MachineName, UsuarioMaquina = Environment.UserName, UsuarioId = ((DomainBase)Recursos.Usuario).Id, UsuarioSistema = Recursos.Usuario.Nome, UltimoAcesso = DateTime.Now, Ip = Recursos.Host.AddressList.FirstOrDefault((IPAddress ip) => ip.AddressFamily == AddressFamily.InterNetwork)?.ToString() }; } return Produtos.Any((Licenca x) => (int)x.Produto == 1) && (Instalacao != null || RegistrarMaquina().Result); }); } public async Task Registrar(string chave) { return await Task.Run(delegate { //IL_001f: Unknown result type (might be due to invalid IL or missing references) //IL_0029: Expected O, but got Unknown if (string.IsNullOrEmpty(chave)) { return "false"; } RegistryHelper helper = new RegistryHelper(""); string[] array = EncryptionHelper.Decrypt(chave).Split(new char[1] { ' ' }); if (array.Length < 2) { return "false"; } string text = helper.Verificar(array[0], EncryptionHelper.Encrypt(array[0])); if (text != "false") { if (text == "root") { return "root"; } ApplicationHelper.NumeroSerial = array[0]; ApplicationHelper.IdFornecedor = long.Parse(Connection.Get("Maintenance/Customer").Result); Instancia.Conexao = null; ApplicationHelper.Subkey = array[0]; return "true"; } DateTime networkTime = Funcoes.GetNetworkTime(); string text2 = Connection.Get("Installation/Time", autorizar: false, verificarConexao: true).Result ?? networkTime.ToString("d"); if (!ApplicationHelper.Conectado) { return "false"; } DataAtual = DateTime.Parse($"{text2} {networkTime.Hour}:{networkTime.Minute}:{networkTime.Second}"); DateTime dateTime = DateTime.Parse(array[1]); if (Math.Abs((DataAtual - dateTime).TotalDays) > 1.0) { return "false"; } string text3 = EncryptionHelper.Decrypt(helper.Read("NS", true)); string text4 = helper.Read("SERIALS", false); if (string.IsNullOrWhiteSpace(text4) && !string.IsNullOrWhiteSpace(text3)) { helper.Write("SERIALS", EncryptionHelper.Encrypt(text3), false); text4 = helper.Read("SERIALS", false); } List source = ((text4 != null) ? EncryptionHelper.Decrypt(text4).Split(new char[1] { ':' }).ToList() : new List()); List seriais = new List(); source.Distinct().ToList().ForEach(delegate(string x) { if (!(helper.Verificar(x, EncryptionHelper.Encrypt(x)) == "false")) { seriais.Add(x); } }); seriais.Add(array[0]); helper.Write("SERIALS", EncryptionHelper.Encrypt(string.Join(":", seriais.Distinct())), false); helper.Serial = ((seriais.Count > 1) ? ("\\" + array[0]) : ""); ApplicationHelper.Subkey = ((seriais.Count > 1) ? array[0] : ""); ApplicationHelper.NumeroSerial = array[0]; string result = Connection.Get("Customer/Name/" + array[0]).Result; helper.Write("PERFIL", result, true); helper.Write("CURRENTDATE", EncryptionHelper.Encrypt(text2), true); helper.Write("DATE", text2, true); helper.Write("NS", EncryptionHelper.Encrypt(array[0]), true); helper.Write("INSTALL", EncryptionHelper.Encrypt(array[0]), true); ApplicationHelper.IdFornecedor = long.Parse(Connection.Get("Maintenance/Customer").Result); helper.Write("CUSTOMER", EncryptionHelper.Encrypt(ApplicationHelper.IdFornecedor.ToString()), true); Instancia.Conexao = null; return "true"; }); } public async Task> GetInstalacoes() { return await Connection.Get>($"Installation/Machine/{ApplicationHelper.IdFornecedor}"); } private string GetMachineSerial() { if (string.IsNullOrEmpty(_guid)) { _guid = Guid.NewGuid().ToString(); } return _guid; } public async Task RegistrarMaquina() { return await Task.Run(delegate { //IL_007e: Unknown result type (might be due to invalid IL or missing references) //IL_0084: Expected O, but got Unknown //IL_00ac: Unknown result type (might be due to invalid IL or missing references) //IL_00b3: Expected O, but got Unknown if (!ApplicationHelper.Conectado) { return false; } List result = GetInstalacoes().Result; Licenca? obj = ((IEnumerable)Produtos).FirstOrDefault((Func)((Licenca x) => (int)x.Produto == 1)); if (((obj != null) ? obj.Quantidade : 0) <= result.Count((Instalacao x) => x.Gerenciador != null)) { return false; } string text = ""; RegistryHelper val = new RegistryHelper(""); if (Instalacao == null) { string machineSerial = GetMachineSerial(); text = EncryptionHelper.Encrypt(GetMacAddress() + " " + machineSerial); Instalacao val2 = new Instalacao(); val2.IdFornecedor = ApplicationHelper.IdFornecedor; val2.Data = DataAtual; val2.Gerenciador = machineSerial; val2.NomeMaquina = Environment.MachineName; val2.UsuarioMaquina = Environment.UserName; val2.UsuarioId = ((DomainBase)Recursos.Usuario).Id; val2.UsuarioSistema = Recursos.Usuario.Nome; val2.UltimoAcesso = DateTime.Now; val2.Ip = Recursos.Host.AddressList.FirstOrDefault((IPAddress ip) => ip.AddressFamily == AddressFamily.InterNetwork)?.ToString(); val2.OSInfo = string.Format("P={0};V={1};x64={2};N={3}", (Environment.OSVersion.Platform == PlatformID.Win32NT) ? "W" : "O", Environment.OSVersion.Version, Environment.Is64BitOperatingSystem ? "1" : "0", GetFrameworkVersion()); Instalacao = val2; } val.Write("MACHINEID", text, false); Instalacao = Connection.Put("Installation/Machine", Instalacao).Result; return true; }); } public string GetMacAddress() { IEnumerable values = from ni in NetworkInterface.GetAllNetworkInterfaces() where ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet || ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 orderby ni.Id descending select ni into x select x.GetPhysicalAddress().ToString(); return string.Join("", values); } public NetworkInterface FindMacAddress(string macToSearch) { string[] macList = macToSearch.Split(new char[1] { '|' }); return NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault((NetworkInterface ni) => macList.Contains(ni.GetPhysicalAddress().ToString())); } public async Task> VerificarVersao() { return new Tuple(bool.Parse(await Connection.Get($"Installation/Version/{ApplicationHelper.IdFornecedor}")), "VERSÃO INDISPONÍVEL"); } public async Task FindKey() { string text = EncryptionHelper.Decrypt(new RegistryHelper(ApplicationHelper.Subkey).ForceRead("MACHINEID", false)); string text2 = (string.IsNullOrEmpty(text) ? (GetMacAddress() + GetMachineSerial()) : text); return await Connection.Get("Installation/Serial/" + text2, autorizar: false); } private string GetFrameworkVersion() { try { using RegistryKey registryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full\\"); if (registryKey != null && registryKey.GetValue("Version") != null) { return registryKey.GetValue("Version").ToString(); } } catch { } return "<4.5"; } }