Ejemplo n.º 1
0
 public IPBanWindowsServiceRunner(IPBanServiceRunner runner, string[] args)
 {
     runner.ThrowIfNull();
     try
     {
         IPBanLog.Warn("Running as a Windows service");
         this.runner = runner;
         CanShutdown = false;
         CanStop     = CanHandleSessionChangeEvent = CanHandlePowerEvent = true;
         var acceptedCommandsField = typeof(ServiceBase).GetField("acceptedCommands", BindingFlags.Instance | BindingFlags.NonPublic);
         if (acceptedCommandsField != null)
         {
             int acceptedCommands = (int)acceptedCommandsField.GetValue(this);
             acceptedCommands |= 0x00000100; // SERVICE_ACCEPT_PRESHUTDOWN;
             acceptedCommandsField.SetValue(this, acceptedCommands);
         }
         Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
         System.ServiceProcess.ServiceBase[] ServicesToRun = new System.ServiceProcess.ServiceBase[] { this };
         System.ServiceProcess.ServiceBase.Run(ServicesToRun);
     }
     catch
     {
         // if anything fails, fallback to running as a console app
         try
         {
             Dispose();
         }
         catch
         {
         }
         IPBanLog.Warn("Failed to run as Windows service, fallback to running as console app");
         runner.RunConsoleService(args);
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// IPBan main method
 /// </summary>
 /// <param name="args">Args</param>
 /// <returns>Task</returns>
 public static async Task Main(string[] args)
 {
     await IPBanServiceRunner.MainService <IPBanService>(args, () =>
     {
         // TODO: IPBan service does not use .NET hosting infrastructure, so send out the message manually, revisit this in the future using host builder
         if (Environment.UserInteractive)
         {
             Console.WriteLine("IPBan service started, press Ctrl+C to exit");
         }
     });
 }
Ejemplo n.º 3
0
 public static async Task MainService(string[] args, Func <string[], Task> start, Action stop, bool requireAdministrator = true)
 {
     try
     {
         using (IPBanServiceRunner runner = new IPBanServiceRunner(args, start, stop))
         {
             await runner.RunAsync(requireAdministrator);
         }
     }
     catch (Exception ex)
     {
         IPBanExtensionMethods.FileWriteAllTextWithRetry(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "startup_fail.txt"), ex.ToString());
         IPBanLog.Fatal("Fatal error starting service", ex);
     }
 }
Ejemplo n.º 4
0
 public static Task <int> MainService(string[] args, Func <string[], Task> start, Action stop, Func <int, bool> stopped, bool requireAdministrator = true)
 {
     try
     {
         using (IPBanServiceRunner runner = new IPBanServiceRunner(args, start, stop, stopped))
         {
             return(runner.RunAsync(requireAdministrator));
         }
     }
     catch (Exception ex)
     {
         System.IO.File.WriteAllText(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "startup_fail.txt"), ex.ToString());
         IPBanLog.Fatal("Fatal error starting service", ex);
         return(Task.FromResult(-1));
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// IPBan main method
        /// </summary>
        /// <param name="args">Args</param>
        /// <returns>Task</returns>
        public static async Task Main(string[] args)
        {
            if (args.Length != 0 && (args[0].Equals("info", StringComparison.OrdinalIgnoreCase) ||
                                     args[0].Equals("-info", StringComparison.OrdinalIgnoreCase)))
            {
                Logger.Warn("System info: {0}", OSUtility.OSString());
                return;
            }

            IPBanService service = null;
            await IPBanServiceRunner.MainService(args, (CancellationToken cancelToken) =>
            {
                service = IPBanService.CreateService <IPBanService>();
                return(service.RunAsync(cancelToken));
            }, (CancellationToken cancelToken) =>
            {
                service?.Dispose();
                return(Task.CompletedTask);
            });
        }
Ejemplo n.º 6
0
        /// <summary>
        /// IPBan main method
        /// </summary>
        /// <param name="args">Args</param>
        /// <returns>Task</returns>
        public static async Task Main(string[] args)
        {
            if (ProcessCommandLine(args))
            {
                return;
            }

            IPBanService service = null;
            await IPBanServiceRunner.MainService(args, (CancellationToken cancelToken) =>
            {
                service = IPBanService.CreateService <IPBanService>();
                Logger.Warn("IPBan is free software created and refined over many years.");
                Logger.Warn("Please consider upgrading to the pro version for more advanced functions, shared ban lists and much more.");
                Logger.Warn("Learn more at https://ipban.com");
                return(service.RunAsync(cancelToken));
            }, (CancellationToken cancelToken) =>
            {
                service?.Dispose();
                return(Task.CompletedTask);
            });
        }
Ejemplo n.º 7
0
 public IPBanWindowsServiceRunner(IPBanServiceRunner runner, string[] args)
 {
     runner.ThrowIfNull();
     try
     {
         IPBanLog.Warn("Running as a Windows service");
         this.runner = runner;
         CanShutdown = false;
         CanStop     = CanHandleSessionChangeEvent = CanHandlePowerEvent = true;
         var acceptedCommandsField = typeof(ServiceBase).GetField("acceptedCommands", BindingFlags.Instance | BindingFlags.NonPublic);
         if (acceptedCommandsField != null)
         {
             int acceptedCommands = (int)acceptedCommandsField.GetValue(this);
             acceptedCommands |= 0x00000100; // SERVICE_ACCEPT_PRESHUTDOWN;
             acceptedCommandsField.SetValue(this, acceptedCommands);
         }
         Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
     }
     catch (Exception ex)
     {
         IPBanLog.Error(ex);
     }
 }
Ejemplo n.º 8
0
        /// <summary>
        /// IPBan main method
        /// </summary>
        /// <param name="args">Args</param>
        /// <returns>Task</returns>
        public static async Task Main(string[] args)
        {
            if (args.Length != 0 && (args[0].Equals("info", StringComparison.OrdinalIgnoreCase) ||
                                     args[0].Equals("-info", StringComparison.OrdinalIgnoreCase)))
            {
                Logger.Warn("System info: {0}", OSUtility.OSString());
                return;
            }

            IPBanService service = null;
            await IPBanServiceRunner.MainService(args, (CancellationToken cancelToken) =>
            {
                service = IPBanService.CreateService <IPBanService>();
                Logger.Warn("IPBan is free software created and refined over many years.");
                Logger.Warn("Please consider upgrading to the pro version for more advanced functions, shared ban lists and much more.");
                Logger.Warn("Learn more at https://ipban.com");
                return(service.RunAsync(cancelToken));
            }, (CancellationToken cancelToken) =>
            {
                service?.Dispose();
                return(Task.CompletedTask);
            });
        }