diff --git a/.gitignore b/.gitignore index 73dec14..4d8edb2 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ /SoloEnLan/bin/* /SoloEnLan/obj/* /packages/* +/MigrationBackup/* +/SoloEnLan/.vs/* diff --git a/SoloEnLan/App.config b/SoloEnLan/App.config index 193aecc..28499cb 100644 --- a/SoloEnLan/App.config +++ b/SoloEnLan/App.config @@ -3,4 +3,19 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/SoloEnLan/Program.cs b/SoloEnLan/Program.cs index be137ce..a275b22 100644 --- a/SoloEnLan/Program.cs +++ b/SoloEnLan/Program.cs @@ -1,8 +1,11 @@ using System; using System.Collections.Generic; +using System.Configuration; +using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; +using System.Threading; using System.Threading.Tasks; namespace SoloEnLan @@ -15,11 +18,24 @@ namespace SoloEnLan static void Main() { ServiceBase[] ServicesToRun; - ServicesToRun = new ServiceBase[] + + if (int.Parse(ConfigurationManager.AppSettings["consola"]) == 1) { - new Service1() - }; - ServiceBase.Run(ServicesToRun); + var s = new SoloEnLan(); + s.inicia(); + while(true) + { + Thread.Sleep(100); + } + } + else + { + ServicesToRun = new ServiceBase[] + { + new SoloEnLan() + }; + ServiceBase.Run(ServicesToRun); + } } } } diff --git a/SoloEnLan/ProjectInstaller.Designer.cs b/SoloEnLan/ProjectInstaller.Designer.cs new file mode 100644 index 0000000..1685dfc --- /dev/null +++ b/SoloEnLan/ProjectInstaller.Designer.cs @@ -0,0 +1,60 @@ +namespace SoloEnLan +{ + partial class ProjectInstaller + { + /// + /// Variable del diseñador necesaria. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Limpiar los recursos que se estén usando. + /// + /// true si los recursos administrados se deben desechar; false en caso contrario. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Código generado por el Diseñador de componentes + + /// + /// Método necesario para admitir el Diseñador. No se puede modificar + /// el contenido de este método con el editor de código. + /// + private void InitializeComponent() + { + this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller(); + this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller(); + // + // serviceProcessInstaller1 + // + this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem; + this.serviceProcessInstaller1.Password = null; + this.serviceProcessInstaller1.Username = null; + // + // serviceInstaller1 + // + this.serviceInstaller1.Description = "Control de seguridad en lan"; + this.serviceInstaller1.DisplayName = "SoloEnLan"; + this.serviceInstaller1.ServiceName = "SoloEnLan"; + this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic; + // + // ProjectInstaller + // + this.Installers.AddRange(new System.Configuration.Install.Installer[] { + this.serviceProcessInstaller1, + this.serviceInstaller1}); + + } + + #endregion + + private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1; + private System.ServiceProcess.ServiceInstaller serviceInstaller1; + } +} \ No newline at end of file diff --git a/SoloEnLan/ProjectInstaller.cs b/SoloEnLan/ProjectInstaller.cs new file mode 100644 index 0000000..a7931f2 --- /dev/null +++ b/SoloEnLan/ProjectInstaller.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.ComponentModel; +using System.Configuration.Install; +using System.Linq; +using System.Threading.Tasks; + +namespace SoloEnLan +{ + [RunInstaller(true)] + public partial class ProjectInstaller : System.Configuration.Install.Installer + { + public ProjectInstaller() + { + InitializeComponent(); + } + } +} diff --git a/SoloEnLan/ProjectInstaller.resx b/SoloEnLan/ProjectInstaller.resx new file mode 100644 index 0000000..9f33750 --- /dev/null +++ b/SoloEnLan/ProjectInstaller.resx @@ -0,0 +1,129 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + + 194, 17 + + + False + + \ No newline at end of file diff --git a/SoloEnLan/Service1.Designer.cs b/SoloEnLan/Service1.Designer.cs index beeaf22..0a17d96 100644 --- a/SoloEnLan/Service1.Designer.cs +++ b/SoloEnLan/Service1.Designer.cs @@ -1,6 +1,6 @@ namespace SoloEnLan { - partial class Service1 + partial class SoloEnLan { /// /// Variable del diseñador necesaria. @@ -28,10 +28,18 @@ /// private void InitializeComponent() { - components = new System.ComponentModel.Container(); - this.ServiceName = "Service1"; + this.eventLog1 = new System.Diagnostics.EventLog(); + ((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit(); + // + // SoloEnLan + // + this.ServiceName = "SoloEnLan"; + ((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit(); + } #endregion + + private System.Diagnostics.EventLog eventLog1; } } diff --git a/SoloEnLan/Service1.cs b/SoloEnLan/Service1.cs index 735101e..f1bf7cc 100644 --- a/SoloEnLan/Service1.cs +++ b/SoloEnLan/Service1.cs @@ -8,27 +8,41 @@ using System.Linq; using System.ServiceProcess; using System.Text; using System.Threading.Tasks; - +using System.Diagnostics; +using SoloEnLan.service; namespace SoloEnLan { - public partial class Service1 : ServiceBase + public partial class SoloEnLan : ServiceBase { monitor m; - public Service1() + private string nombreService = "SoloEnLan"; + public SoloEnLan() { InitializeComponent(); - - m=new monitor(); + if(!EventLog.SourceExists(nombreService)) + { + EventLog.CreateEventSource(nombreService,"Application"); + } + logService.EventLog = eventLog1; + eventLog1.Source = nombreService; + eventLog1.Log = "Application"; + m =new monitor(); } - - protected override void OnStart(string[] args) + public void inicia() { m.start(); } + protected override void OnStart(string[] args) + { + logService.log("Se inicia " + nombreService); + //eventLog1.WriteEntry("Se inicia " + nombreService); + inicia(); + } protected override void OnStop() { m.fin(); + logService.log("Finaliza" + nombreService); } } } diff --git a/SoloEnLan/Service1.resx b/SoloEnLan/Service1.resx new file mode 100644 index 0000000..4cdd336 --- /dev/null +++ b/SoloEnLan/Service1.resx @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + + False + + \ No newline at end of file diff --git a/SoloEnLan/SoloEnLan.csproj b/SoloEnLan/SoloEnLan.csproj index 65db86b..6325951 100644 --- a/SoloEnLan/SoloEnLan.csproj +++ b/SoloEnLan/SoloEnLan.csproj @@ -12,6 +12,8 @@ 512 true true + + AnyCPU @@ -32,10 +34,15 @@ prompt 4 + + solologo_ico.ico + + + @@ -46,6 +53,12 @@ + + Component + + + ProjectInstaller.cs + Component @@ -55,11 +68,34 @@ + + + Always + + + Always + + + + + 7.1.3 + + + + + + + + ProjectInstaller.cs + + + Service1.cs + \ No newline at end of file diff --git a/SoloEnLan/SoloEnLan.csproj.user b/SoloEnLan/SoloEnLan.csproj.user new file mode 100644 index 0000000..9b86104 --- /dev/null +++ b/SoloEnLan/SoloEnLan.csproj.user @@ -0,0 +1,6 @@ + + + + ShowAllFiles + + \ No newline at end of file diff --git a/SoloEnLan/model/monitor.cs b/SoloEnLan/model/monitor.cs index 5b06764..ab64eab 100644 --- a/SoloEnLan/model/monitor.cs +++ b/SoloEnLan/model/monitor.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; +using Windows.Web.UI; namespace SoloEnLan.model { @@ -18,9 +19,9 @@ namespace SoloEnLan.model private int tiempoMensaje; public monitor() { - t = null; + t = new Thread(runMonitor); pirate = false; - tiempoComprobacion = int.Parse(ConfigurationManager.AppSettings["tiempoComprovacion"]); + tiempoComprobacion = int.Parse(ConfigurationManager.AppSettings["tiempoComprovacionPrime"]); tiempoMensaje = int.Parse(ConfigurationManager.AppSettings["tiempoNotificacion"]); } @@ -29,28 +30,90 @@ namespace SoloEnLan.model ((monitor)m).run(); } + private void espera(int nseg) + { + DateTime currentDate = DateTime.Now; + //espera + while (!pirate) + { + Thread.Sleep(1000); + long elapsedTicks = DateTime.Now.Ticks- currentDate.Ticks; + TimeSpan elapsedSpan = new TimeSpan(elapsedTicks); + if (elapsedSpan.TotalSeconds > nseg) + return; + } + } private void run() { - while(!pirate) + while (!pirate) { + DateTime currentDate = DateTime.Now; //espera - Thread.Sleep(tiempoComprobacion); + espera(tiempoComprobacion); + if (pirate) + continue; + + //Thread.Sleep(tiempoComprobacion*1000); //revisa lan - if (conexionService.Getestas()) + logService.log("Se comprueba Autorización"); + if (!ConectaSRV()) { + logService.log("Pc sin autorizacion"); + if (pirate) + continue; //manda mensaje NotificacionService.Notifica(); //espera despues de mensaje - Thread.Sleep(tiempoMensaje); + //Thread.Sleep(tiempoMensaje*1000); + espera(tiempoMensaje); + if (pirate) + continue; //apaga + logService.log("Se apaga maquina"); shutdownService.apagaPc(); } - Thread.Sleep(60); - + else + { + logService.log("Pc autorizado"); + } + tiempoComprobacion = int.Parse(ConfigurationManager.AppSettings["tiempoComprovacion"]); } } + private bool ConectaSRV() + { + for (int i = 0; i < 3; i++) + { + if (pirate) + return false; + //intenta a srv lan + var res= conexionService.ConectLanSRV(); + if( res<0) + espera(5); + else + { + return res == 1; + } + } + for (int i = 0; i < 3; i++) + { + if (pirate) + return false; + //intenta a srv lan + var res = conexionService.ConectExternSRV(); + if (res < 0) + espera(5); + else + { + return res == 1; + } + } + return false; + } + + + public void start() { diff --git a/SoloEnLan/script/install.bat b/SoloEnLan/script/install.bat new file mode 100644 index 0000000..b1592c0 --- /dev/null +++ b/SoloEnLan/script/install.bat @@ -0,0 +1,3 @@ +cd c:\Windows\Microsoft.NET\Framework64\v4.0.30319 +installutil.exe "C:\Program Files\soloEnLan\soloEnLan.exe" +pause \ No newline at end of file diff --git a/SoloEnLan/script/unista.bat b/SoloEnLan/script/unista.bat new file mode 100644 index 0000000..8f4a317 --- /dev/null +++ b/SoloEnLan/script/unista.bat @@ -0,0 +1,3 @@ +cd c:\Windows\Microsoft.NET\Framework64\v4.0.30319 +installutil /uninstall "C:\Program Files\soloEnLan\soloEnLan.exe" +pause \ No newline at end of file diff --git a/SoloEnLan/service/NotificacionService.cs b/SoloEnLan/service/NotificacionService.cs index 90cc448..e1d200b 100644 --- a/SoloEnLan/service/NotificacionService.cs +++ b/SoloEnLan/service/NotificacionService.cs @@ -1,7 +1,8 @@ -//using Microsoft.Toolkit.Uwp.Notifications; +using Microsoft.Toolkit.Uwp.Notifications; using System; using System.Collections.Generic; using System.Configuration; +using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -12,15 +13,19 @@ namespace SoloEnLan.service { public static void Notifica() { - /*string mens = ConfigurationManager.AppSettings["MensajeAviso"]; + string mens = ConfigurationManager.AppSettings["MensajeAviso"]; + string exe = ConfigurationManager.AppSettings["notifica.exe"]; + if(exe!="") + Process.Start(exe, " \"" + mens + "\""); + /* new ToastContentBuilder() .AddArgument("action", "viewConversation") .AddArgument("conversationId", 9813) .AddText("Aviso") .AddText(mens) .Show(); // Not seeing the Show() method? Make sure you have version 7.0, and if you're using .NET 6 (or later), then your TFM must be net6.0-windows10.0.17763.0 or greater - } - */ + */ + } } } diff --git a/SoloEnLan/service/conexionService.cs b/SoloEnLan/service/conexionService.cs index 8bde7ef..4b40865 100644 --- a/SoloEnLan/service/conexionService.cs +++ b/SoloEnLan/service/conexionService.cs @@ -1,6 +1,12 @@ using System; using System.Collections.Generic; +using System.Configuration; +using System.IO; using System.Linq; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Security.Policy; using System.Text; using System.Threading.Tasks; @@ -8,10 +14,76 @@ namespace SoloEnLan.service { internal class conexionService { - - public static bool Getestas() + private static int GetPermisoSRV(string sURL) { - return true; + var resf = -1; + + try + { + HttpClient client = new HttpClient(); + int.Parse(ConfigurationManager.AppSettings["tiempoComprovacion"]); + var nombre = System.Environment.MachineName; + client.BaseAddress = new Uri(sURL); + client.DefaultRequestHeaders.Accept.Clear(); + var res = client.GetAsync( + "?nombre="+ nombre).Result; + + if (res.IsSuccessStatusCode) + { + if (res.StatusCode == HttpStatusCode.OK) + { + var jsonResponse = res.Content.ReadAsStringAsync().Result; + if (jsonResponse != null && jsonResponse.Contains("OK")) + resf=1; + else + resf= 0; + } + } + client.Dispose(); + res.Dispose(); + } + catch (Exception e) + { + var ex = e.ToString(); + return -1; + } + + + return resf; + } + public static int ConectLanSRV() + { + try + { + + var sURL = ConfigurationManager.AppSettings["url"]; + return GetPermisoSRV(sURL); + } + catch(Exception e) + { + var ex = e.ToString(); + return -1; + } + + + return -1; + } + + public static int ConectExternSRV() + { + try + { + var sURL = ConfigurationManager.AppSettings["urlExtern"]; + return GetPermisoSRV(sURL); + } + catch (Exception e) + { + var ex = e.ToString(); + return -1; + } + + + return -1; } } } diff --git a/SoloEnLan/service/logService.cs b/SoloEnLan/service/logService.cs new file mode 100644 index 0000000..2a77b41 --- /dev/null +++ b/SoloEnLan/service/logService.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoloEnLan.service +{ + internal class logService + { + public static EventLog EventLog=null; + + public static void log(string msg) + { + ;if(EventLog!=null) + { + EventLog.WriteEntry(msg); + } + } + } +} diff --git a/SoloEnLan/service/shutdownService.cs b/SoloEnLan/service/shutdownService.cs index 1e0db55..1986ce0 100644 --- a/SoloEnLan/service/shutdownService.cs +++ b/SoloEnLan/service/shutdownService.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -10,7 +11,7 @@ namespace SoloEnLan.service { public static void apagaPc() { - + Process.Start("shutdown", "/s /f /t 0"); } } } diff --git a/SoloEnLan/solologo_ico.ico b/SoloEnLan/solologo_ico.ico new file mode 100644 index 0000000..925e34b Binary files /dev/null and b/SoloEnLan/solologo_ico.ico differ