mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-02-11 09:24:58 +00:00
Fix bugs related to the settings and logger
This commit is contained in:
parent
020f99c5b2
commit
3e78e0b5d2
|
@ -70,13 +70,14 @@ namespace WelsonJS.Service
|
||||||
private string clamAvConenctionString;
|
private string clamAvConenctionString;
|
||||||
private IClamAvClient clamAvClient;
|
private IClamAvClient clamAvClient;
|
||||||
|
|
||||||
public FileEventMonitor(ServiceBase parent, string workingDirectory)
|
public FileEventMonitor(ServiceBase _parent, string workingDirectory, ILogger _logger)
|
||||||
{
|
{
|
||||||
this.parent = (ServiceMain)parent;
|
parent = (ServiceMain)_parent;
|
||||||
|
logger = _logger;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
clamAvConenctionString = this.parent.GetSettingsHandler().Read("CLAMAV_HOST", "Service");
|
clamAvConenctionString = parent.ReadSettingsValue("CLAMAV_HOST");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
@ -86,11 +87,6 @@ namespace WelsonJS.Service
|
||||||
ConnectToClamAv().Start();
|
ConnectToClamAv().Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetLogger(ILogger _logger)
|
|
||||||
{
|
|
||||||
logger = _logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Start()
|
public void Start()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|
|
@ -19,19 +19,20 @@ namespace WelsonJS.Service
|
||||||
private ILogger logger;
|
private ILogger logger;
|
||||||
private readonly GrpcChannel _channel;
|
private readonly GrpcChannel _channel;
|
||||||
private int HeartbeatInterval;
|
private int HeartbeatInterval;
|
||||||
private ServiceMain _parent;
|
private ServiceMain parent;
|
||||||
private string clientId;
|
private string clientId;
|
||||||
private string serverAddress;
|
private string serverAddress;
|
||||||
|
|
||||||
public HeartbeatClient(ServiceBase parent)
|
public HeartbeatClient(ServiceBase _parent, ILogger _logger)
|
||||||
{
|
{
|
||||||
_parent = (ServiceMain)parent;
|
parent = (ServiceMain)_parent;
|
||||||
|
logger = _logger;
|
||||||
|
|
||||||
HeartbeatInterval = int.Parse(_parent.GetSettingsHandler().Read("HEARTBEAT_INTERVAL", "Service") ?? "2000");
|
HeartbeatInterval = int.Parse(parent.ReadSettingsValue("HEARTBEAT_INTERVAL") ?? "2000");
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
serverAddress = _parent.GetSettingsHandler().Read("GRPC_HOST", "Service");
|
serverAddress = parent.ReadSettingsValue("GRPC_HOST");
|
||||||
if (String.IsNullOrEmpty(serverAddress))
|
if (String.IsNullOrEmpty(serverAddress))
|
||||||
{
|
{
|
||||||
throw new Exception("The server address could not be empty.");
|
throw new Exception("The server address could not be empty.");
|
||||||
|
@ -56,11 +57,6 @@ namespace WelsonJS.Service
|
||||||
logger.LogInformation($"Use the client ID: {clientId}");
|
logger.LogInformation($"Use the client ID: {clientId}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetLogger(ILogger _logger)
|
|
||||||
{
|
|
||||||
logger = _logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task StartHeartbeatAsync()
|
public async Task StartHeartbeatAsync()
|
||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
|
|
|
@ -1,17 +1,27 @@
|
||||||
using System;
|
using Microsoft.Extensions.Logging;
|
||||||
using System.Collections.Generic;
|
using System;
|
||||||
|
using System.IO;
|
||||||
using System.ServiceProcess;
|
using System.ServiceProcess;
|
||||||
using System.Text;
|
using WelsonJS.Service.Logging;
|
||||||
|
|
||||||
namespace WelsonJS.Service
|
namespace WelsonJS.Service
|
||||||
{
|
{
|
||||||
internal static class Program
|
internal static class Program
|
||||||
{
|
{
|
||||||
|
private static ILogger logger;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 해당 애플리케이션의 주 진입점입니다.
|
/// 해당 애플리케이션의 주 진입점입니다.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
///
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
|
// create the logger
|
||||||
|
ILoggerFactory factory = LoggerFactory.Create(builder => builder.AddConsole());
|
||||||
|
factory.AddDirectory(Path.GetTempPath());
|
||||||
|
logger = factory.CreateLogger("welsonjs");
|
||||||
|
|
||||||
|
// create the service
|
||||||
if (Environment.UserInteractive)
|
if (Environment.UserInteractive)
|
||||||
{
|
{
|
||||||
Console.WriteLine("WelsonJS Service Application (User Interactive Mode)");
|
Console.WriteLine("WelsonJS Service Application (User Interactive Mode)");
|
||||||
|
@ -19,7 +29,7 @@ namespace WelsonJS.Service
|
||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
Console.WriteLine("Service is running...");
|
Console.WriteLine("Service is running...");
|
||||||
|
|
||||||
ServiceMain svc = new ServiceMain(args);
|
ServiceMain svc = new ServiceMain(args, logger);
|
||||||
svc.TestStartupAndStop();
|
svc.TestStartupAndStop();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -27,7 +37,7 @@ namespace WelsonJS.Service
|
||||||
ServiceBase[] ServicesToRun;
|
ServiceBase[] ServicesToRun;
|
||||||
ServicesToRun = new ServiceBase[]
|
ServicesToRun = new ServiceBase[]
|
||||||
{
|
{
|
||||||
new ServiceMain(args)
|
new ServiceMain(args, logger)
|
||||||
};
|
};
|
||||||
ServiceBase.Run(ServicesToRun);
|
ServiceBase.Run(ServicesToRun);
|
||||||
}
|
}
|
||||||
|
|
|
@ -159,9 +159,10 @@ public class ScreenMatch
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScreenMatch(ServiceBase parent, string workingDirectory)
|
public ScreenMatch(ServiceBase _parent, string workingDirectory, ILogger _logger)
|
||||||
{
|
{
|
||||||
this.parent = (ServiceMain)parent;
|
parent = (ServiceMain)_parent;
|
||||||
|
logger = _logger;
|
||||||
|
|
||||||
SetBusy(false);
|
SetBusy(false);
|
||||||
|
|
||||||
|
@ -193,8 +194,8 @@ public class ScreenMatch
|
||||||
string screen_time_params;
|
string screen_time_params;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
screen_time_mode = this.parent.GetSettingsHandler().Read("SCREEN_TIME_MODE", "Service");
|
screen_time_mode = parent.ReadSettingsValue("SCREEN_TIME_MODE");
|
||||||
screen_time_params = this.parent.GetSettingsHandler().Read("SCREEN_TIME_PARAMS", "Service");
|
screen_time_params = parent.ReadSettingsValue("SCREEN_TIME_PARAMS");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
@ -332,11 +333,6 @@ public class ScreenMatch
|
||||||
LoadTemplateImages();
|
LoadTemplateImages();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetLogger(ILogger _logger)
|
|
||||||
{
|
|
||||||
logger = _logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void SetMode(string mode)
|
public void SetMode(string mode)
|
||||||
{
|
{
|
||||||
if (!String.IsNullOrEmpty(mode))
|
if (!String.IsNullOrEmpty(mode))
|
||||||
|
|
|
@ -36,7 +36,6 @@ using System.Collections;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using WelsonJS.Service.Logging;
|
using WelsonJS.Service.Logging;
|
||||||
using Grpc.Core.Logging;
|
|
||||||
|
|
||||||
namespace WelsonJS.Service
|
namespace WelsonJS.Service
|
||||||
{
|
{
|
||||||
|
@ -44,7 +43,7 @@ namespace WelsonJS.Service
|
||||||
{
|
{
|
||||||
private readonly static string applicationName = "WelsonJS";
|
private readonly static string applicationName = "WelsonJS";
|
||||||
private static List<Timer> timers;
|
private static List<Timer> timers;
|
||||||
private Microsoft.Extensions.Logging.ILogger logger;
|
private ILogger logger;
|
||||||
private string workingDirectory;
|
private string workingDirectory;
|
||||||
private string scriptName;
|
private string scriptName;
|
||||||
private string scriptFilePath;
|
private string scriptFilePath;
|
||||||
|
@ -56,7 +55,7 @@ namespace WelsonJS.Service
|
||||||
private bool disabledFileMonitor = false;
|
private bool disabledFileMonitor = false;
|
||||||
private ScreenMatch screenMatcher;
|
private ScreenMatch screenMatcher;
|
||||||
private FileEventMonitor fileEventMonitor;
|
private FileEventMonitor fileEventMonitor;
|
||||||
private IniFile settingsHandler;
|
private IniFile settingsFileHandler;
|
||||||
private UserVariables userVariablesHandler;
|
private UserVariables userVariablesHandler;
|
||||||
|
|
||||||
[DllImport("user32.dll")]
|
[DllImport("user32.dll")]
|
||||||
|
@ -64,17 +63,13 @@ namespace WelsonJS.Service
|
||||||
|
|
||||||
private static int SM_REMOTESESSION = 0x1000;
|
private static int SM_REMOTESESSION = 0x1000;
|
||||||
|
|
||||||
public ServiceMain(string[] args)
|
public ServiceMain(string[] _args, ILogger _logger)
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
|
||||||
// set service arguments
|
// set arguments and logger
|
||||||
this.args = args;
|
args = _args;
|
||||||
|
logger = _logger;
|
||||||
// set the logger
|
|
||||||
ILoggerFactory factory = LoggerFactory.Create(builder => builder.AddConsole());
|
|
||||||
factory.AddDirectory(Path.GetTempPath());
|
|
||||||
logger = factory.CreateLogger(applicationName.ToLower());
|
|
||||||
|
|
||||||
// mapping arguments to each variables
|
// mapping arguments to each variables
|
||||||
var arguments = ParseArguments(this.args);
|
var arguments = ParseArguments(this.args);
|
||||||
|
@ -131,11 +126,11 @@ namespace WelsonJS.Service
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
settingsHandler = new IniFile(settingsFilePath);
|
settingsFileHandler = new IniFile(settingsFilePath);
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
settingsHandler = null;
|
logger.LogWarning(ex.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -144,7 +139,7 @@ namespace WelsonJS.Service
|
||||||
}
|
}
|
||||||
|
|
||||||
// read configrations from settings.ini
|
// read configrations from settings.ini
|
||||||
if (settingsHandler != null)
|
if (settingsFileHandler != null)
|
||||||
{
|
{
|
||||||
string[] configNames = new string[]
|
string[] configNames = new string[]
|
||||||
{
|
{
|
||||||
|
@ -156,7 +151,7 @@ namespace WelsonJS.Service
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if ("true" == GetSettingsHandler().Read(configName, "Service"))
|
if ("true" == ReadSettingsValue(configName))
|
||||||
{
|
{
|
||||||
switch (configName)
|
switch (configName)
|
||||||
{
|
{
|
||||||
|
@ -197,8 +192,7 @@ namespace WelsonJS.Service
|
||||||
// start the heartbeat
|
// start the heartbeat
|
||||||
if (!disabledHeartbeat)
|
if (!disabledHeartbeat)
|
||||||
{
|
{
|
||||||
HeartbeatClient heartbeatClient = new HeartbeatClient(this);
|
HeartbeatClient heartbeatClient = new HeartbeatClient(this, logger);
|
||||||
heartbeatClient.SetLogger(logger);
|
|
||||||
Task.Run(heartbeatClient.StartHeartbeatAsync);
|
Task.Run(heartbeatClient.StartHeartbeatAsync);
|
||||||
Task.Run(heartbeatClient.StartEventListenerAsync);
|
Task.Run(heartbeatClient.StartEventListenerAsync);
|
||||||
}
|
}
|
||||||
|
@ -213,7 +207,7 @@ namespace WelsonJS.Service
|
||||||
|
|
||||||
// check this session is the user interactive mode
|
// check this session is the user interactive mode
|
||||||
if (Environment.UserInteractive) {
|
if (Environment.UserInteractive) {
|
||||||
this.OnUserInteractiveEnvironment();
|
OnUserInteractiveEnvironment();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -223,9 +217,17 @@ namespace WelsonJS.Service
|
||||||
logger.LogInformation(applicationName + " Service Loaded");
|
logger.LogInformation(applicationName + " Service Loaded");
|
||||||
}
|
}
|
||||||
|
|
||||||
public IniFile GetSettingsHandler()
|
public string ReadSettingsValue(string key, string defaultValue = null)
|
||||||
{
|
{
|
||||||
return settingsHandler;
|
if (settingsFileHandler != null)
|
||||||
|
{
|
||||||
|
return settingsFileHandler.Read(key, "Service") ?? defaultValue;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
logger.LogWarning("Unable to read the value. It seems that settings.ini is not configured correctly.");
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public UserVariables GetUserVariablesHandler()
|
public UserVariables GetUserVariablesHandler()
|
||||||
|
@ -302,8 +304,7 @@ namespace WelsonJS.Service
|
||||||
// Trace a Sysmon file events (If Sysinternals Sysmon installed)
|
// Trace a Sysmon file events (If Sysinternals Sysmon installed)
|
||||||
if (!disabledFileMonitor)
|
if (!disabledFileMonitor)
|
||||||
{
|
{
|
||||||
fileEventMonitor = new FileEventMonitor(this, workingDirectory);
|
fileEventMonitor = new FileEventMonitor(this, workingDirectory, logger);
|
||||||
fileEventMonitor.SetLogger(logger);
|
|
||||||
fileEventMonitor.Start();
|
fileEventMonitor.Start();
|
||||||
|
|
||||||
logger.LogInformation("File Event Monitor Started");
|
logger.LogInformation("File Event Monitor Started");
|
||||||
|
@ -354,8 +355,7 @@ namespace WelsonJS.Service
|
||||||
// set screen timer
|
// set screen timer
|
||||||
if (!disabledScreenTime)
|
if (!disabledScreenTime)
|
||||||
{
|
{
|
||||||
screenMatcher = new ScreenMatch(this, workingDirectory);
|
screenMatcher = new ScreenMatch(this, workingDirectory, logger);
|
||||||
screenMatcher.SetLogger(logger);
|
|
||||||
|
|
||||||
Timer screenTimer = new Timer
|
Timer screenTimer = new Timer
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user