Initial commit.
This commit is contained in:
commit
a70a45df8d
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
.r_history
|
||||||
|
bin
|
||||||
|
obj
|
19
Makefile
Normal file
19
Makefile
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
# Author: retoor@molodetz.nl
|
||||||
|
|
||||||
|
# Makefile for building WebSocketClient project
|
||||||
|
|
||||||
|
.PHONY: all clean linux windows
|
||||||
|
|
||||||
|
run:
|
||||||
|
dotnet run
|
||||||
|
|
||||||
|
all: linux windows
|
||||||
|
|
||||||
|
linux:
|
||||||
|
dotnet publish -c Release -r linux-x64 --self-contained --framework net9.0
|
||||||
|
|
||||||
|
windows:
|
||||||
|
dotnet publish -c Release -r win-x64 --self-contained --framework net9.0
|
||||||
|
|
||||||
|
clean:
|
||||||
|
dotnet clean
|
77
Program.cs
Normal file
77
Program.cs
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
using System;
|
||||||
|
using System.Net.WebSockets;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
|
class Program
|
||||||
|
{
|
||||||
|
static async Task Main(string[] args)
|
||||||
|
{
|
||||||
|
string username = Environment.GetEnvironmentVariable("SNEK_USERNAME") ?? "anonymous";
|
||||||
|
string password = Environment.GetEnvironmentVariable("SNEK_PASSWORD") ?? "anonymous";
|
||||||
|
|
||||||
|
using (var client = new ClientWebSocket())
|
||||||
|
{
|
||||||
|
Uri serverUri = new Uri("wss://snek.molodetz.nl/rpc.ws");
|
||||||
|
await client.ConnectAsync(serverUri, CancellationToken.None);
|
||||||
|
|
||||||
|
var jsonMessage = JsonConvert.SerializeObject(new {
|
||||||
|
method = "login",
|
||||||
|
args = new[] { username, password }
|
||||||
|
});
|
||||||
|
|
||||||
|
var bytes = Encoding.UTF8.GetBytes(jsonMessage);
|
||||||
|
var buffer = new ArraySegment<byte>(bytes);
|
||||||
|
await client.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None);
|
||||||
|
|
||||||
|
Console.WriteLine("Message sent: " + jsonMessage);
|
||||||
|
Boolean isLoggedIn = false;
|
||||||
|
var receiveBuffer = new byte[1024000]; // Increased buffer size
|
||||||
|
while (client.State == WebSocketState.Open)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
WebSocketReceiveResult result = await client.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), CancellationToken.None);
|
||||||
|
var responseMessage = Encoding.UTF8.GetString(receiveBuffer, 0, result.Count);
|
||||||
|
Console.WriteLine("Message received: " + responseMessage);
|
||||||
|
|
||||||
|
dynamic jsonResponse = JsonConvert.DeserializeObject(responseMessage);
|
||||||
|
if(!isLoggedIn)
|
||||||
|
{
|
||||||
|
isLoggedIn = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
string receivedUsername = jsonResponse.username != null ? System.Security.SecurityElement.Escape((string)jsonResponse.username) : "Unknown User";
|
||||||
|
string message = jsonResponse.message != null ? System.Security.SecurityElement.Escape((string)jsonResponse.message) : "No message provided";
|
||||||
|
|
||||||
|
if (receivedUsername != username)
|
||||||
|
{
|
||||||
|
string command = $"notify-send '{receivedUsername}' '{message}'";
|
||||||
|
await ExecuteCommand(command);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Error processing message: " + ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task ExecuteCommand(string command)
|
||||||
|
{
|
||||||
|
using (var process = new Process())
|
||||||
|
{
|
||||||
|
process.StartInfo.FileName = "bash";
|
||||||
|
process.StartInfo.Arguments = "-c \"" + command + "\"";
|
||||||
|
process.StartInfo.RedirectStandardOutput = true;
|
||||||
|
process.StartInfo.UseShellExecute = false;
|
||||||
|
process.StartInfo.CreateNoWindow = true;
|
||||||
|
process.Start();
|
||||||
|
await process.WaitForExitAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
12
WebSocketClient.csproj
Normal file
12
WebSocketClient.csproj
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net9.0</TargetFramework>
|
||||||
|
<Configuration>Release</Configuration>
|
||||||
|
<PublishSingleFile>true</PublishSingleFile>
|
||||||
|
<PublishTrimmed>true</PublishTrimmed>
|
||||||
|
</PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
Loading…
Reference in New Issue
Block a user