226 lines
7.4 KiB
C#
226 lines
7.4 KiB
C#
using EFCDesk.Utils;
|
|
using Microsoft.VisualBasic.ApplicationServices;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.NetworkInformation;
|
|
using System.Reflection;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using System.Threading.Tasks;
|
|
using System.Xml;
|
|
|
|
namespace EFCDesk.Classes
|
|
{
|
|
public class ConfiguracionJSON
|
|
{
|
|
private string? _passwordExpEncriptado;
|
|
|
|
public string? UsuarioExp { get; set; }
|
|
[JsonIgnore]
|
|
public string? PasswordExp
|
|
{
|
|
get
|
|
{
|
|
if (string.IsNullOrEmpty(_passwordExpEncriptado))
|
|
return null;
|
|
|
|
string pwd = Util.Desencriptar(_passwordExpEncriptado);
|
|
|
|
// Si no se pudo desencriptar → config corrupta
|
|
if (string.IsNullOrEmpty(pwd))
|
|
{
|
|
LimpiarCredenciales();
|
|
return null;
|
|
}
|
|
|
|
return pwd;
|
|
}
|
|
set
|
|
{
|
|
if (string.IsNullOrWhiteSpace(value))
|
|
{
|
|
_passwordExpEncriptado = null;
|
|
}
|
|
else
|
|
{
|
|
_passwordExpEncriptado = Util.Encriptar(value);
|
|
}
|
|
}
|
|
}
|
|
public string? DominioExp { get; set; }
|
|
public bool ProxyNingunoGenerico { get; set; }
|
|
public string? ServidorProxyGenerico { get; set; }
|
|
public int? PuertoProxyGenerico { get; set; }
|
|
public string? UsuarioProxyGenerico { get; set; }
|
|
public string? PasswordProxyGenerico { get; set; }
|
|
public bool AuthProxyGenerico { get; set; }
|
|
public bool ProxyFTPNinguno { get; set; }
|
|
public string? ServidorProxyFTP { get; set; }
|
|
public int? PuertoProxyFTP { get; set; }
|
|
public string? UsuarioProxyFTP { get; set; }
|
|
public string? PasswordProxyFTP { get; set; }
|
|
public bool AuthProxyFTP { get; set; }
|
|
public int idUsuarioExp { get; set; }
|
|
public string? FolderExpediente { get; set; }
|
|
public string MacAddress { get; set; }
|
|
public int ExpWinsaai { get; set; }
|
|
public int ExpLogistico { get; set; }
|
|
|
|
[JsonPropertyName("PasswordExp")]
|
|
public string? PasswordExpEncriptado
|
|
{
|
|
get => _passwordExpEncriptado;
|
|
set => _passwordExpEncriptado = value;
|
|
}
|
|
|
|
public ConfiguracionJSON() {
|
|
MacAddress = NetworkInterface.GetAllNetworkInterfaces()
|
|
.Where(nic => nic.OperationalStatus == OperationalStatus.Up && nic.NetworkInterfaceType != NetworkInterfaceType.Loopback)
|
|
.Select(nic => nic.GetPhysicalAddress().ToString())
|
|
.FirstOrDefault();
|
|
DominioExp = "";
|
|
}
|
|
// Método para cargar la configuración desde un archivo JSON
|
|
public static ConfiguracionJSON LoadFromJson()
|
|
{
|
|
string pathConfigJson = AppDomain.CurrentDomain.BaseDirectory+"appSettings.json";
|
|
if (!File.Exists(pathConfigJson))
|
|
{
|
|
var defaultConfig = new ConfiguracionJSON();
|
|
|
|
string json = JsonSerializer.Serialize(defaultConfig, new JsonSerializerOptions { WriteIndented = true });
|
|
File.WriteAllText(pathConfigJson, json);
|
|
}
|
|
|
|
string contenidoJson = File.ReadAllText(pathConfigJson);
|
|
return JsonSerializer.Deserialize<ConfiguracionJSON>(contenidoJson) ?? new ConfiguracionJSON();
|
|
}
|
|
|
|
public void SaveToJson()
|
|
{
|
|
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appSettings.json");
|
|
string json = JsonSerializer.Serialize(this, new JsonSerializerOptions { WriteIndented = true });
|
|
File.WriteAllText(path, json);
|
|
}
|
|
|
|
// Limpieza automática
|
|
public void LimpiarCredenciales()
|
|
{
|
|
UsuarioExp = null;
|
|
PasswordExpEncriptado = null;
|
|
SaveToJson();
|
|
}
|
|
|
|
public string[] Login()
|
|
{
|
|
string [] result = new string[2];
|
|
result[0] = "0";
|
|
result[1] = "Error de conexion";
|
|
try
|
|
{
|
|
string cadena = UsuarioExp + "|" + PasswordExp;
|
|
dynamic root = Consulta(DominioExp+"/api/auth/" + cadena);
|
|
if (root is XmlElement)
|
|
{
|
|
|
|
XmlNodeList nodes = root.SelectNodes("/xml/item");
|
|
foreach (XmlNode node in nodes)
|
|
{
|
|
result[0] = node["login"].InnerText;
|
|
result[1] = node["mensaje"].InnerText;
|
|
}
|
|
|
|
if (result[0] != "0")
|
|
{
|
|
string path = FolderExpediente;
|
|
if (!Directory.Exists(path))
|
|
{
|
|
Directory.CreateDirectory(path);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
MessageBox.Show(e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
|
}
|
|
|
|
return result;
|
|
}
|
|
public dynamic Consulta(string url)
|
|
{
|
|
WebClient wc = new WebClient();
|
|
if (ProxyNingunoGenerico)
|
|
{
|
|
dynamic wp = Proxy();
|
|
if (wp is WebProxy)
|
|
{
|
|
wc.Proxy = wp;
|
|
}
|
|
else
|
|
{
|
|
return wp;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
IWebProxy proxy = WebRequest.GetSystemWebProxy();
|
|
proxy.Credentials = CredentialCache.DefaultCredentials;
|
|
wc.Proxy = proxy;
|
|
}
|
|
|
|
try
|
|
{
|
|
MemoryStream ms = new MemoryStream(wc.DownloadData(url));
|
|
XmlTextReader rdr = new XmlTextReader(ms);
|
|
XmlDocument doc1 = new XmlDocument();
|
|
doc1.Load(rdr);
|
|
XmlElement root = doc1.DocumentElement;
|
|
return root;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
|
|
string sNombreClase = MethodBase.GetCurrentMethod()?.DeclaringType?.Name;
|
|
string currentMethod = MethodBase.GetCurrentMethod().Name;
|
|
//MyClass.LogErrores(e, sNombreClase, currentMethod);
|
|
|
|
return e;
|
|
}
|
|
}
|
|
|
|
public dynamic Proxy()
|
|
{
|
|
try
|
|
{
|
|
WebProxy myproxy = new WebProxy(ServidorProxyGenerico + ":" + PuertoProxyGenerico, true);
|
|
myproxy.BypassProxyOnLocal = false;
|
|
|
|
if (AuthProxyGenerico)
|
|
{
|
|
NetworkCredential credential = new NetworkCredential(UsuarioProxyGenerico, PasswordProxyGenerico);
|
|
myproxy.Credentials = credential;
|
|
}
|
|
|
|
|
|
return myproxy;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
string sNombreClase = MethodBase.GetCurrentMethod()?.DeclaringType?.Name;
|
|
string currentMethod = MethodBase.GetCurrentMethod().Name;
|
|
//MyClass.LogErrores(e, sNombreClase, currentMethod);
|
|
return e;
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|