I have no clue what I am doing

This commit is contained in:
2026-02-04 14:27:36 +01:00
commit b8d02bb115
23 changed files with 509 additions and 0 deletions

176
YetAnotherDummy/Program.cs Normal file
View File

@@ -0,0 +1,176 @@
// See https://aka.ms/new-console-template for more information
using System.Net;
using System.Net.Sockets;
using static RemoteFrameBufferClientProtocol;
IPAddress tessia = new([192,168,16,253]);
IRemoteFrameBufferClientStreamProvider tessia6001vnc = new IRemoteFrameBufferClientStreamProvider.RemoteFrameBufferTcpClientStreamProvider(tessia, 5901);
RemoteFramebufferClient client = new(tessia6001vnc);
client.Start();
Console.ReadLine();
client.Stop();
public interface INetEndPoint
{
public IPEndPoint EndPoint { get; }
public IPAddress Address { get; }
public Int32 Port { get; }
public class NetEndPointFromIP : INetEndPoint
{
public IPEndPoint EndPoint { get; }
public IPAddress Address => this.EndPoint.Address;
public Int32 Port => this.EndPoint.Port;
public NetEndPointFromIP(IPEndPoint endpoint)
{
this.EndPoint = endpoint;
}
public NetEndPointFromIP(IPAddress address, Int32 port) : this(new(address, port)) { }
}
public class NetEndPointFromHostname(String hostname, Int32 port) : INetEndPoint
{
public String Hostname { get; } = hostname;
public IPEndPoint EndPoint => new(this.Address, this.Port);
public IPAddress Address {
get
{
IPHostEntry entry = Dns.GetHostEntry(this.Hostname);
return entry.AddressList[0];
}
}
public Int32 Port { get; } = port;
}
}
public interface IRemoteFrameBufferClientStreamProvider
{
public Stream GetRemoteFrameBufferClientStream();
public class RemoteFrameBufferTcpClientStreamProvider : IRemoteFrameBufferClientStreamProvider
{
private readonly INetEndPoint remoteEndPoint;
private readonly IPEndPoint localEndPoint;
public RemoteFrameBufferTcpClientStreamProvider(INetEndPoint remoteEndPoint, IPEndPoint? localEndPoint)
{
this.remoteEndPoint = remoteEndPoint;
this.localEndPoint = localEndPoint ?? new(IPAddress.Any, 0);
}
public Stream GetRemoteFrameBufferClientStream()
{
TcpClient tcpClient = new TcpClient(this.localEndPoint);
tcpClient.Connect(this.remoteEndPoint.EndPoint);
return tcpClient.GetStream();
}
public RemoteFrameBufferTcpClientStreamProvider(String hostname, Int16 port, IPEndPoint? localEndPoint = null) : this(new INetEndPoint.NetEndPointFromHostname(hostname, port), localEndPoint) { }
public RemoteFrameBufferTcpClientStreamProvider(IPAddress address, Int32 port, IPEndPoint? localEndPoint = null) : this(new INetEndPoint.NetEndPointFromIP(address, port), localEndPoint) { }
public RemoteFrameBufferTcpClientStreamProvider(IPEndPoint remoteEndPoint, IPEndPoint? localEndPoint = null) : this(new INetEndPoint.NetEndPointFromIP(remoteEndPoint), localEndPoint) { }
}
}
public class RemoteFramebufferClient
{
private static Dictionary<RfbProtoVersion, RemoteFrameBufferClientProtocol.IRemoteFramebufferClientProtocolFactory> _protocolHandlers;
private readonly IRemoteFrameBufferClientStreamProvider _socketProvider;
private Task? backgroundWorker;
private CancellationTokenSource cancellationTokenSource = new();
static RemoteFramebufferClient() {
_protocolHandlers = new() {
{ new RfbProtoVersion(3, 3), new RemoteFrameBufferClientProtocolFactory<RemoteFrameBufferClientProtocol.RemoteFrameBufferClientProtocol_3_3>() },
};
}
public RemoteFramebufferClient(IRemoteFrameBufferClientStreamProvider socketProvider)
{
this._socketProvider = socketProvider;
}
public void Start()
{
if (this.backgroundWorker == null)
{
Console.Error.WriteLine("VNC client starting");
this.backgroundWorker = new Task(this.Worker, this.cancellationTokenSource.Token);
this.backgroundWorker.ContinueWith(this.WorkerStopped);
this.backgroundWorker.Start();
}
}
public void Stop()
{
if (this.backgroundWorker != null)
{
this.cancellationTokenSource.Cancel();
this.backgroundWorker.Wait();
}
}
private void Worker()
{
while (!this.cancellationTokenSource.Token.IsCancellationRequested)
{
Console.Error.WriteLine("Attempting to connect");
Stream s = this._socketProvider.GetRemoteFrameBufferClientStream();
using (s)
{
}
}
}
private void WorkerStopped(Task t)
{
Console.Error.WriteLine("VNC client exited");
this.backgroundWorker = null;
if (!this.cancellationTokenSource.TryReset())
{
this.cancellationTokenSource = new();
}
}
private struct RfbProtoVersion(Int16 major, Int16 minor)
{
public Int16 Major = major;
public Int16 Minor = minor;
}
}
public abstract class RemoteFrameBufferClientProtocol
{
protected readonly Stream vncStream;
public RemoteFrameBufferClientProtocol(Stream s)
{
this.vncStream = s;
}
public interface IRemoteFramebufferClientProtocolFactory {
public abstract RemoteFrameBufferClientProtocol Construct(Stream s);
}
public class RemoteFrameBufferClientProtocolFactory<T> : IRemoteFramebufferClientProtocolFactory where T : RemoteFrameBufferClientProtocol
{
public RemoteFrameBufferClientProtocol Construct(Stream s)
{
return (RemoteFrameBufferClientProtocol)Activator.CreateInstance(typeof(T), s)!;
}
}
public class RemoteFrameBufferClientProtocol_3_3 : RemoteFrameBufferClientProtocol
{
public RemoteFrameBufferClientProtocol_3_3(Stream s) : base(s)
{
}
}
}