This commit is contained in:
Namhyeon Go 2024-08-29 15:34:54 +09:00
parent e6fd3a800e
commit 3602cb4a00
7 changed files with 251 additions and 25 deletions

View File

@ -0,0 +1,125 @@
// HeartbeatClient.cs
// Namhyeon Go <abuse@catswords.net>
// https://github.com/gnh1201/welsonjs
using Grpc.Core;
using System.Threading.Tasks;
using System;
using System.Management;
using System.ServiceProcess;
using Grpc.Net.Client;
using Grpc.Net.Client.Web;
using System.Net.Http;
namespace WelsonJS.Service
{
public class HeartbeatClient
{
private readonly HeartbeatService.HeartbeatServiceClient _client;
private readonly GrpcChannel _channel;
private const int HeartbeatInterval = 2000; // 2 seconds
private ServiceMain _parent;
private string clientId;
private string serverAddress;
public HeartbeatClient(ServiceBase parent)
{
_parent = (ServiceMain)parent;
try
{
serverAddress = _parent.GetSettingsFileHandler().Read("GRPC_HOST", "Service");
if (String.IsNullOrEmpty(serverAddress))
{
throw new Exception("The server address could not be empty.");
}
}
catch (Exception ex)
{
serverAddress = "http://localhost:50051";
_parent.Log($"Failed to read the host address. {ex.Message} Use default value: {serverAddress}");
}
var httpClientHandler = new HttpClientHandler();
var grpcWebHandler = new GrpcWebHandler(GrpcWebMode.GrpcWebText, httpClientHandler);
_channel = GrpcChannel.ForAddress(serverAddress, new GrpcChannelOptions
{
HttpHandler = grpcWebHandler,
Credentials = ChannelCredentials.Insecure
});
_client = new HeartbeatService.HeartbeatServiceClient(_channel);
clientId = GetSystemUUID().ToLower();
_parent.Log($"Use the client ID: {clientId}");
}
public async Task StartHeartbeatAsync()
{
var call = _client.CheckHeartbeat();
try
{
while (true)
{
var request = new HeartbeatRequest {
IsAlive = true
};
await call.RequestStream.WriteAsync(request);
_parent.Log("Sent heartbeat");
if (await call.ResponseStream.MoveNext())
{
var response = call.ResponseStream.Current;
_parent.Log("Heartbeat response received: " + response.IsAlive);
}
await Task.Delay(HeartbeatInterval);
}
}
finally
{
await call.RequestStream.CompleteAsync();
}
}
public async Task StartEventListenerAsync()
{
var eventRequest = new FetchEventsRequest {
ClientId = clientId
};
var eventCall = _client.FetchPendingEvents(eventRequest);
try
{
while (await eventCall.ResponseStream.MoveNext())
{
var response = eventCall.ResponseStream.Current;
_parent.Log($"Received event from server: {response.EventType} with args: {string.Join(", ", response.Args)}");
}
}
finally {}
}
public async Task ShutdownAsync()
{
await _channel.ShutdownAsync();
}
private string GetSystemUUID()
{
try
{
using (var searcher = new ManagementObjectSearcher("SELECT UUID FROM Win32_ComputerSystemProduct"))
{
foreach (var mo in searcher.Get())
{
return mo["UUID"].ToString();
}
}
}
catch (Exception ex)
{
_parent.Log($"An error occurred while retrieving the system UUID: {ex.Message}");
}
return "UNKNOWN";
}
}
}

View File

@ -0,0 +1,27 @@
syntax = "proto3";
option csharp_namespace = "WelsonJS.Service";
package heartbeat;
service HeartbeatService {
rpc CheckHeartbeat (stream HeartbeatRequest) returns (stream HeartbeatResponse);
rpc FetchPendingEvents (FetchEventsRequest) returns (stream FetchEventsResponse);
}
message HeartbeatRequest {
bool is_alive = 1;
}
message HeartbeatResponse {
bool is_alive = 1;
}
message FetchEventsRequest {
string client_id = 1;
}
message FetchEventsResponse {
string event_type = 1;
repeated string args = 2;
}

View File

@ -223,23 +223,26 @@ public class ScreenMatch
throw new Exception("Waiting done a previous job...");
}
toggleIsMatching();
switch (mode)
if (templateImages.Count > 0)
{
case "screen": // 화면 기준
results = CaptureAndMatchAllScreens();
toggleIsMatching();
break;
toggleIsMatching();
case "window": // 윈도우 핸들 기준
results = CaptureAndMatchAllWindows();
toggleIsMatching();
break;
switch (mode)
{
case "screen": // 화면 기준
results = CaptureAndMatchAllScreens();
toggleIsMatching();
break;
default:
toggleIsMatching();
throw new Exception($"Unknown capture mode {mode}");
case "window": // 윈도우 핸들 기준
results = CaptureAndMatchAllWindows();
toggleIsMatching();
break;
default:
toggleIsMatching();
throw new Exception($"Unknown capture mode {mode}");
}
}
return results;

View File

@ -33,6 +33,7 @@ using System.IO;
using System.Collections.Generic;
using WelsonJS.TinyINIController;
using System.Collections;
using System.Threading.Tasks;
namespace WelsonJS.Service
{
@ -47,6 +48,7 @@ namespace WelsonJS.Service
private readonly string logFilePath = Path.Combine(Path.GetTempPath(), "WelsonJS.Service.Log.txt");
private readonly string appName = "WelsonJS";
private string[] args;
private bool disabledHeartbeat = false;
private bool disabledScreenTime = false;
private bool disabledFileMonitor = false;
private ScreenMatch screenMatcher;
@ -79,6 +81,10 @@ namespace WelsonJS.Service
scriptName = entry.Value;
break;
case "disable-heartbeat":
disabledHeartbeat = true;
break;
case "disable-screen-time":
disabledScreenTime = true;
break;
@ -129,9 +135,9 @@ namespace WelsonJS.Service
{
string[] configNames = new string[]
{
"DISABLE_HEARTBEAT",
"DISABLE_SCREEN_TIME",
"DISABLE_FILE_MONITOR",
"DISABLE_MESSAGE_RECEIVER"
"DISABLE_FILE_MONITOR"
};
foreach (string configName in configNames)
{
@ -141,6 +147,10 @@ namespace WelsonJS.Service
{
switch (configName)
{
case "DISABLE_HEARTBEAT":
disabledHeartbeat = true;
break;
case "DISABLE_SCREEN_TIME":
disabledScreenTime = true;
break;
@ -171,6 +181,14 @@ namespace WelsonJS.Service
// set path of the script
scriptFilePath = Path.Combine(workingDirectory, "app.js");
// start the heartbeat
if (!disabledHeartbeat)
{
HeartbeatClient heartbeatClient = new HeartbeatClient(this);
Task.Run(heartbeatClient.StartHeartbeatAsync);
Task.Run(heartbeatClient.StartEventListenerAsync);
}
// set default timer
Timer defaultTimer = new Timer
{

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\Grpc.Tools.2.65.0\build\Grpc.Tools.props" Condition="Exists('..\packages\Grpc.Tools.2.65.0\build\Grpc.Tools.props')" />
<Import Project="..\packages\Grpc.Tools.2.66.0\build\Grpc.Tools.props" Condition="Exists('..\packages\Grpc.Tools.2.66.0\build\Grpc.Tools.props')" />
<Import Project="..\packages\Microsoft.O365.Security.Native.libyara.NET.4.5.1\build\net462\Microsoft.O365.Security.Native.libyara.NET.props" Condition="Exists('..\packages\Microsoft.O365.Security.Native.libyara.NET.4.5.1\build\net462\Microsoft.O365.Security.Native.libyara.NET.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
@ -16,6 +16,21 @@
<TargetFrameworkProfile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<PublishUrl>게시\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
@ -89,9 +104,15 @@
<Reference Include="Grpc.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=d754f35622e28bad, processorArchitecture=MSIL">
<HintPath>..\packages\Grpc.Core.2.46.6\lib\net45\Grpc.Core.dll</HintPath>
</Reference>
<Reference Include="Grpc.Core.Api, Version=2.0.0.0, Culture=neutral, PublicKeyToken=d754f35622e28bad, processorArchitecture=MSIL">
<HintPath>..\packages\Grpc.Core.Api.2.65.0\lib\net462\Grpc.Core.Api.dll</HintPath>
</Reference>
<Reference Include="Grpc.Net.Client, Version=2.0.0.0, Culture=neutral, PublicKeyToken=d754f35622e28bad, processorArchitecture=MSIL">
<HintPath>..\packages\Grpc.Net.Client.2.65.0\lib\net462\Grpc.Net.Client.dll</HintPath>
</Reference>
<Reference Include="Grpc.Net.Client.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=d754f35622e28bad, processorArchitecture=MSIL">
<HintPath>..\packages\Grpc.Net.Client.Web.2.65.0\lib\netstandard2.0\Grpc.Net.Client.Web.dll</HintPath>
</Reference>
<Reference Include="Grpc.Net.Common, Version=2.0.0.0, Culture=neutral, PublicKeyToken=d754f35622e28bad, processorArchitecture=MSIL">
<HintPath>..\packages\Grpc.Net.Common.2.65.0\lib\netstandard2.0\Grpc.Net.Common.dll</HintPath>
</Reference>
@ -109,6 +130,9 @@
<HintPath>..\packages\RestSharp.111.4.1\lib\net48\RestSharp.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.Configuration.Install" />
<Reference Include="System.Data" />
<Reference Include="System.Diagnostics.DiagnosticSource, Version=8.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
@ -116,11 +140,20 @@
</Reference>
<Reference Include="System.Drawing" />
<Reference Include="System.Management" />
<Reference Include="System.Memory, Version=4.0.1.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Memory.4.5.5\lib\net461\System.Memory.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http" />
<Reference Include="System.Net.Http.WinHttpHandler, Version=8.0.0.2, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Net.Http.WinHttpHandler.8.0.2\lib\net462\System.Net.Http.WinHttpHandler.dll</HintPath>
</Reference>
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.ServiceProcess" />
<Reference Include="System.Text.Encodings.Web, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Text.Encodings.Web.8.0.0\lib\net462\System.Text.Encodings.Web.dll</HintPath>
@ -140,6 +173,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="FileEventMonitor.cs" />
<Compile Include="HeartbeatClient.cs" />
<Compile Include="Model\FileMatchResult.cs" />
<Compile Include="ServiceMain.cs">
<SubType>Component</SubType>
@ -186,7 +220,20 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Protos\" />
<BootstrapperPackage Include=".NETFramework,Version=v4.8">
<Visible>False</Visible>
<ProductName>Microsoft .NET Framework 4.8%28x86 및 x64%29</ProductName>
<Install>true</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Protobuf Include="Protos\heartbeat.proto" GrpcServices="Client" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
@ -194,10 +241,10 @@
<ErrorText>이 프로젝트는 이 컴퓨터에 없는 NuGet 패키지를 참조합니다. 해당 패키지를 다운로드하려면 NuGet 패키지 복원을 사용하십시오. 자세한 내용은 http://go.microsoft.com/fwlink/?LinkID=322105를 참조하십시오. 누락된 파일은 {0}입니다.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Microsoft.O365.Security.Native.libyara.NET.4.5.1\build\net462\Microsoft.O365.Security.Native.libyara.NET.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.O365.Security.Native.libyara.NET.4.5.1\build\net462\Microsoft.O365.Security.Native.libyara.NET.props'))" />
<Error Condition="!Exists('..\packages\Grpc.Tools.2.65.0\build\Grpc.Tools.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Grpc.Tools.2.65.0\build\Grpc.Tools.props'))" />
<Error Condition="!Exists('..\packages\Grpc.Tools.2.65.0\build\Grpc.Tools.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Grpc.Tools.2.65.0\build\Grpc.Tools.targets'))" />
<Error Condition="!Exists('..\packages\Grpc.Core.2.46.6\build\net45\Grpc.Core.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Grpc.Core.2.46.6\build\net45\Grpc.Core.targets'))" />
<Error Condition="!Exists('..\packages\Grpc.Tools.2.66.0\build\Grpc.Tools.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Grpc.Tools.2.66.0\build\Grpc.Tools.props'))" />
<Error Condition="!Exists('..\packages\Grpc.Tools.2.66.0\build\Grpc.Tools.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Grpc.Tools.2.66.0\build\Grpc.Tools.targets'))" />
</Target>
<Import Project="..\packages\Grpc.Tools.2.65.0\build\Grpc.Tools.targets" Condition="Exists('..\packages\Grpc.Tools.2.65.0\build\Grpc.Tools.targets')" />
<Import Project="..\packages\Grpc.Core.2.46.6\build\net45\Grpc.Core.targets" Condition="Exists('..\packages\Grpc.Core.2.46.6\build\net45\Grpc.Core.targets')" />
<Import Project="..\packages\Grpc.Tools.2.66.0\build\Grpc.Tools.targets" Condition="Exists('..\packages\Grpc.Tools.2.66.0\build\Grpc.Tools.targets')" />
</Project>

View File

@ -3,16 +3,22 @@
<package id="Google.Protobuf" version="3.27.3" targetFramework="net48" />
<package id="Grpc" version="2.46.6" targetFramework="net48" />
<package id="Grpc.Core" version="2.46.6" targetFramework="net48" />
<package id="Grpc.Core.Api" version="2.65.0" targetFramework="net48" />
<package id="Grpc.Net.Client" version="2.65.0" targetFramework="net48" />
<package id="Grpc.Net.Client.Web" version="2.65.0" targetFramework="net48" />
<package id="Grpc.Net.Common" version="2.65.0" targetFramework="net48" />
<package id="Grpc.Tools" version="2.65.0" targetFramework="net48" developmentDependency="true" />
<package id="Grpc.Tools" version="2.66.0" targetFramework="net48" developmentDependency="true" />
<package id="Microsoft.Bcl.AsyncInterfaces" version="8.0.0" targetFramework="net48" />
<package id="Microsoft.Extensions.DependencyInjection.Abstractions" version="8.0.1" targetFramework="net48" />
<package id="Microsoft.Extensions.Logging.Abstractions" version="8.0.1" targetFramework="net48" />
<package id="Microsoft.O365.Security.Native.libyara.NET" version="4.5.1" targetFramework="net48" />
<package id="RestSharp" version="111.4.1" targetFramework="net48" />
<package id="System.Buffers" version="4.5.1" targetFramework="net48" />
<package id="System.Diagnostics.DiagnosticSource" version="8.0.1" targetFramework="net48" />
<package id="System.Memory" version="4.5.5" targetFramework="net48" />
<package id="System.Net.Http.WinHttpHandler" version="8.0.2" targetFramework="net48" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net48" />
<package id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" targetFramework="net48" />
<package id="System.Text.Encodings.Web" version="8.0.0" targetFramework="net48" />
<package id="System.Text.Json" version="8.0.4" targetFramework="net48" />
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net48" />

View File

@ -8,9 +8,9 @@ WINDIVERT_CONFIG_FILE=data/windivert.config.json
NONFREE_STRICT=false
[Service]
DISABLE_SCREEN_TIME=false
DISABLE_FILE_MONITOR=false
DISABLE_MESSAGE_RECEIVER=false
DISABLE_HEARTBEAT=true
DISABLE_SCREEN_TIME=true
DISABLE_FILE_MONITOR=true
; screen or window
SCREEN_TIME_MODE=screen
;filename.exe,backward,save