welsonjs/WelsonJS.Toolkit/WelsonJS.Service/ServiceMain.cs

383 lines
12 KiB
C#
Raw Normal View History

2024-07-22 12:02:07 +00:00
/*
* WelsonJS.Service
*
* filename:
* ServiceMain.cs
*
* description:
* WelsonJS - Build a Windows app on the Windows built-in JavaScript engine
*
* website:
* - https://github.com/gnh1201/welsonjs
* - https://catswords.social/@catswords_oss
2024-07-22 15:23:01 +00:00
* - https://teams.live.com/l/community/FEACHncAhq8ldnojAI
2024-07-22 12:02:07 +00:00
*
* author:
* Namhyeon Go <abuse@catswords.net>
*
* license:
* GPLv3 or MS-RL(Microsoft Reciprocal License)
*
* references:
* - https://learn.microsoft.com/en-us/dotnet/framework/windows-services/how-to-debug-windows-service-applications
* - https://stackoverflow.com/questions/6490979/how-to-pass-parameters-to-windows-service
* - https://stackoverflow.com/questions/42812333/pass-an-argument-to-a-windows-service-at-automatic-startup
2024-08-02 02:07:04 +00:00
* - https://learn.microsoft.com/ko-kr/windows/win32/api/winuser/nf-winuser-getsystemmetrics
2024-07-22 12:02:07 +00:00
*/
using System;
2024-07-22 07:04:40 +00:00
using System.ServiceProcess;
using System.Timers;
2024-08-02 02:07:04 +00:00
using System.Runtime.InteropServices;
2024-07-22 07:04:40 +00:00
using MSScriptControl;
using System.IO;
using System.Collections.Generic;
2024-08-15 08:47:29 +00:00
using WelsonJS.TinyINIController;
2024-07-22 07:04:40 +00:00
namespace WelsonJS.Service
{
2024-07-22 09:47:08 +00:00
public partial class ServiceMain : ServiceBase
2024-07-22 07:04:40 +00:00
{
2024-07-29 12:43:14 +00:00
private static List<Timer> timers;
2024-07-22 07:04:40 +00:00
private string workingDirectory;
2024-07-29 12:43:14 +00:00
private string scriptName;
2024-07-22 07:04:40 +00:00
private string scriptFilePath;
private string scriptText;
private ScriptControl scriptControl;
2024-07-29 12:43:14 +00:00
private readonly string logFilePath = Path.Combine(Path.GetTempPath(), "WelsonJS.Service.Log.txt");
2024-07-22 09:47:08 +00:00
private readonly string appName = "WelsonJS";
2024-08-13 02:16:27 +00:00
private string[] args;
2024-07-29 12:43:14 +00:00
private bool disabledScreenTime = false;
2024-08-12 03:47:19 +00:00
private bool disabledFileMonitor = false;
2024-07-29 12:43:14 +00:00
private ScreenMatching screenMatcher;
2024-08-12 03:47:19 +00:00
private FileEventMonitor fileEventMonitor;
2024-08-19 08:04:50 +00:00
private IniFile settingsFileHandler;
2024-07-22 07:04:40 +00:00
2024-08-02 02:07:04 +00:00
[DllImport("user32.dll")]
2024-08-13 02:21:48 +00:00
private static extern int GetSystemMetrics(int nIndex);
2024-08-13 02:16:27 +00:00
private static int SM_REMOTESESSION = 0x1000;
2024-08-02 02:07:04 +00:00
public ServiceMain(string[] args)
2024-07-22 07:04:40 +00:00
{
InitializeComponent();
2024-07-22 09:47:08 +00:00
// set service arguments
2024-08-13 02:16:27 +00:00
this.args = args;
2024-07-22 07:04:40 +00:00
// mapping arguments to each variables
2024-08-13 02:16:27 +00:00
var arguments = ParseArguments(this.args);
2024-07-22 07:04:40 +00:00
foreach (KeyValuePair<string, string> entry in arguments)
{
switch (entry.Key)
{
case "working-directory":
workingDirectory = entry.Value;
break;
2024-07-22 09:47:08 +00:00
case "script-name":
scriptName = entry.Value;
2024-07-22 07:04:40 +00:00
break;
2024-07-29 12:43:14 +00:00
case "disable-screen-time":
disabledScreenTime = true;
break;
2024-08-12 03:47:19 +00:00
case "disable-file-monitor":
disabledFileMonitor = true;
break;
2024-07-22 07:04:40 +00:00
}
}
2024-07-29 12:43:14 +00:00
// set timers
timers = new List<Timer>();
2024-07-22 07:04:40 +00:00
// set working directory
if (string.IsNullOrEmpty(workingDirectory))
{
2024-07-22 11:15:31 +00:00
workingDirectory = Path.Combine(Path.GetTempPath(), appName);
Log("Working directory not provided. Using default value: " + workingDirectory);
2024-07-29 12:43:14 +00:00
2024-07-22 11:15:31 +00:00
if (!Directory.Exists(workingDirectory))
{
Directory.CreateDirectory(workingDirectory);
Log("Directory created: " + workingDirectory);
}
2024-07-22 07:04:40 +00:00
}
Directory.SetCurrentDirectory(workingDirectory);
// read settings.ini
string settingsFilePath = Path.Combine(workingDirectory, "settings.ini");
if (File.Exists(settingsFilePath))
{
try
{
2024-08-19 08:04:50 +00:00
settingsFileHandler = new IniFile(settingsFilePath);
}
catch (Exception)
{
2024-08-19 08:04:50 +00:00
settingsFileHandler = null;
}
}
2024-07-22 11:21:54 +00:00
// set script name
if (string.IsNullOrEmpty(scriptName))
{
scriptName = "defaultService";
2024-07-22 11:21:54 +00:00
Log("Script name not provided. Using default value: " + scriptName);
}
2024-07-22 11:15:31 +00:00
// set path of the script
2024-07-22 07:04:40 +00:00
scriptFilePath = Path.Combine(workingDirectory, "app.js");
2024-07-29 12:43:14 +00:00
// set default timer
Timer defaultTimer = new Timer
{
Interval = 60000 // 1 minute
};
defaultTimer.Elapsed += OnElapsedTime;
timers.Add(defaultTimer);
2024-08-02 02:07:04 +00:00
// check this session is the user interactive mode
if (Environment.UserInteractive) {
this.OnUserInteractiveEnvironment();
2024-07-29 12:43:14 +00:00
}
else
{
2024-08-02 02:07:04 +00:00
Log("Disabled the User Interactive Mode. (e.g., OnScreenTime)");
2024-07-29 12:43:14 +00:00
}
// set the log file path
logFilePath = Path.Combine(Path.GetTempPath(), "WelsonJS.Service.Log.txt");
Log(appName + " Service Loaded");
}
2024-08-19 08:04:50 +00:00
public IniFile GetSettingsFileHandler()
{
return settingsFileHandler;
}
2024-07-29 12:43:14 +00:00
internal void TestStartupAndStop()
{
2024-08-13 02:21:48 +00:00
this.OnStart(this.args);
2024-07-29 12:43:14 +00:00
Console.ReadLine();
this.OnStop();
}
protected override void OnStart(string[] args)
{
base.OnStart(args);
2024-08-19 08:04:50 +00:00
// Check exists the entry script file
2024-07-22 07:04:40 +00:00
if (File.Exists(scriptFilePath))
{
2024-07-22 11:15:31 +00:00
Log($"Script file found: {scriptFilePath}");
try
2024-07-22 07:04:40 +00:00
{
2024-07-22 11:15:31 +00:00
// load the script
scriptText = File.ReadAllText(scriptFilePath);
scriptControl = new ScriptControl
{
Language = "JScript",
AllowUI = false
};
2024-07-22 15:20:20 +00:00
scriptControl.Reset();
2024-07-22 11:15:31 +00:00
scriptControl.AddCode(scriptText);
// initialize
2024-07-29 12:43:14 +00:00
Log(DispatchServiceEvent("start"));
2024-07-22 11:15:31 +00:00
}
catch (Exception ex)
{
2024-07-22 11:36:34 +00:00
Log("Exception when start: " + ex.Message);
2024-07-22 11:15:31 +00:00
}
2024-07-22 07:04:40 +00:00
}
else
{
Log($"Script file not found: {scriptFilePath}");
}
2024-08-19 08:04:50 +00:00
// Trace a Sysmon file events (If Sysinternals Sysmon installed)
if (!disabledFileMonitor)
{
fileEventMonitor = new FileEventMonitor(this, workingDirectory);
fileEventMonitor.Start();
Log("Trace a Sysmon file events (If Sysinternals Sysmon installed) started.");
}
else
{
Log("Trace a Sysmon file events (If Sysinternals Sysmon installed) is disabled");
}
// Start GRPC based message receiver
MessageReceiver receiver = new MessageReceiver(this, workingDirectory);
receiver.Start();
// Start all the registered timers
timers.ForEach(timer => timer?.Start());
2024-07-22 11:15:31 +00:00
Log(appName + " Service Started");
2024-07-22 07:04:40 +00:00
}
protected override void OnStop()
{
2024-08-12 03:47:19 +00:00
// stop timers
timers.ForEach(timer => timer?.Stop());
2024-07-22 11:15:31 +00:00
2024-08-12 03:47:19 +00:00
// stop the File Event Monitor
fileEventMonitor?.Stop();
// dispatch stop callback
2024-07-22 11:15:31 +00:00
try
{
2024-07-29 12:43:14 +00:00
Log(DispatchServiceEvent("stop"));
2024-08-02 02:07:41 +00:00
scriptControl?.Reset();
2024-07-22 11:15:31 +00:00
}
catch (Exception ex)
{
2024-07-22 11:36:34 +00:00
Log("Exception when stop: " + ex.Message);
2024-07-22 11:15:31 +00:00
}
scriptControl = null;
Log(appName + " Service Stopped");
2024-07-22 07:04:40 +00:00
}
2024-08-02 02:07:04 +00:00
private void OnUserInteractiveEnvironment()
{
// check is it a remote desktop session
if (GetSystemMetrics(SM_REMOTESESSION) > 0)
{
disabledScreenTime = true;
Log("This application may not work correctly in a remote desktop session");
}
// set screen timer
if (!disabledScreenTime)
{
screenMatcher = new ScreenMatching(this, workingDirectory);
2024-08-02 02:07:04 +00:00
Timer screenTimer = new Timer
{
Interval = 5000 // 5 seconds
2024-08-02 02:07:04 +00:00
};
screenTimer.Elapsed += OnScreenTime;
timers.Add(screenTimer);
Log("Screen Time Event Enabled");
}
else
{
disabledScreenTime = true;
Log("Screen Time Event Disabled");
}
}
2024-07-22 07:04:40 +00:00
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
2024-07-22 11:15:31 +00:00
try
{
2024-07-29 12:43:14 +00:00
Log(DispatchServiceEvent("elapsedTime"));
2024-07-22 11:15:31 +00:00
}
catch (Exception ex)
{
2024-07-22 11:36:34 +00:00
Log("Exception when elapsed time: " + ex.Message);
2024-07-22 11:15:31 +00:00
}
2024-07-22 07:04:40 +00:00
}
2024-07-29 12:43:14 +00:00
private void OnScreenTime(object source, ElapsedEventArgs e)
{
2024-07-29 12:43:14 +00:00
try
{
List<ScreenMatchResult> matchedResults = screenMatcher.CaptureAndMatch();
2024-07-29 12:43:14 +00:00
matchedResults.ForEach(result =>
{
2024-07-29 13:42:35 +00:00
if (result.MaxCorrelation > 0.0) {
Log(DispatchServiceEvent("screenTime", new string[]
{
result.FileName,
result.ScreenNumber.ToString(),
result.Location.X.ToString(),
result.Location.Y.ToString(),
result.MaxCorrelation.ToString()
}));
}
2024-07-29 12:43:14 +00:00
});
}
catch (Exception ex)
{
Log("Exception when screen time: " + ex.ToString());
}
}
2024-07-22 07:04:40 +00:00
private string InvokeScriptMethod(string methodName, params object[] parameters)
{
2024-07-22 11:36:34 +00:00
if (scriptControl != null)
2024-07-22 15:20:20 +00:00
{
return scriptControl.Run(methodName, parameters)?.ToString() ?? "void";
2024-07-22 11:36:34 +00:00
}
else
{
Log("InvokeScriptMethod Ignored: " + methodName);
}
2024-07-22 15:20:20 +00:00
return "void";
2024-07-22 07:04:40 +00:00
}
private Dictionary<string, string> ParseArguments(string[] args)
{
var arguments = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
foreach (string arg in args)
{
if (arg.StartsWith("--"))
{
var index = arg.IndexOf('=');
if (index > 2)
{
var key = arg.Substring(2, index - 2);
var value = arg.Substring(index + 1);
arguments[key] = value;
}
2024-07-29 12:43:14 +00:00
else
{
var key = arg.Substring(2, index - 2);
arguments[key] = "";
}
2024-07-22 07:04:40 +00:00
}
}
return arguments;
}
2024-08-12 03:47:19 +00:00
public string DispatchServiceEvent(string eventType, string[] args = null)
{
if (args == null)
{
return InvokeScriptMethod("dispatchServiceEvent", scriptName, eventType, "");
}
else
{
return InvokeScriptMethod("dispatchServiceEvent", scriptName, eventType, String.Join("; ", args));
}
}
public void Log(string message)
{
string _message = $"{DateTime.Now}: {message}";
if (Environment.UserInteractive)
{
Console.WriteLine(_message);
}
using (StreamWriter writer = new StreamWriter(logFilePath, true))
{
writer.WriteLine(_message);
}
}
2024-07-22 07:04:40 +00:00
}
}