116 lines
4.1 KiB
C#
116 lines
4.1 KiB
C#
using EonaCat.Connections.Models;
|
|
using System.Security.Cryptography;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
|
|
namespace EonaCat.Connections.gRPC.Server.Example
|
|
{
|
|
public class Program
|
|
{
|
|
private const string LISTEN_IP = "127.0.0.1";
|
|
private const int LISTEN_PORT = 5443;
|
|
|
|
public static async Task Main(string[] args)
|
|
{
|
|
|
|
using var certificate = GenerateSelfSignedCertificate();
|
|
|
|
var config = new Configuration
|
|
{
|
|
Host = LISTEN_IP,
|
|
Port = LISTEN_PORT,
|
|
Certificate = certificate,
|
|
IsSelfSignedEnabled = true,
|
|
};
|
|
|
|
using var server = new GrpcNetworkServer(config);
|
|
|
|
server.OnConnected += (sender, e) =>
|
|
{
|
|
Console.WriteLine($"[gRPC Server] Client connected: {e.ClientId} from {e.RemoteEndPoint}");
|
|
};
|
|
|
|
server.OnDataReceived += async (sender, e) =>
|
|
{
|
|
Console.WriteLine($"[gRPC Server] Received from {e.Nickname ?? e.ClientId}: {e.StringData}");
|
|
|
|
if (sender is GrpcNetworkServer srv)
|
|
{
|
|
await srv.SendToClientAsync(e.ClientId, $"Echo: {e.StringData}");
|
|
}
|
|
};
|
|
|
|
server.OnDisconnected += (sender, e) =>
|
|
{
|
|
Console.WriteLine($"[gRPC Server] Client disconnected: {e.Nickname ?? e.ClientId} ({e.Reason})");
|
|
};
|
|
|
|
server.OnGeneralError += (sender, e) =>
|
|
{
|
|
Console.WriteLine($"[gRPC Server] Error: {e.Message} - {e.Exception?.Message}");
|
|
};
|
|
|
|
Console.WriteLine($"[gRPC Server] Starting on {LISTEN_IP}:{LISTEN_PORT}...");
|
|
await server.StartAsync();
|
|
Console.WriteLine("[gRPC Server] Started. Press 'q' to stop.");
|
|
|
|
while (true)
|
|
{
|
|
var key = Console.ReadKey(true);
|
|
if (key.KeyChar == 'q')
|
|
{
|
|
break;
|
|
}
|
|
|
|
if (key.KeyChar == 'b')
|
|
{
|
|
Console.Write("Broadcast message: ");
|
|
var msg = Console.ReadLine();
|
|
if (!string.IsNullOrEmpty(msg))
|
|
{
|
|
await server.BroadcastAsync(msg);
|
|
}
|
|
}
|
|
|
|
if (key.KeyChar == 'c')
|
|
{
|
|
var clients = server.GetClients();
|
|
Console.WriteLine($"[gRPC Server] Active connections: {server.ActiveConnections}");
|
|
foreach (var client in clients)
|
|
{
|
|
Console.WriteLine($" - {client.Value.Nickname} ({client.Key}) from {client.Value.RemoteEndPoint}");
|
|
}
|
|
}
|
|
}
|
|
|
|
Console.WriteLine("[gRPC Server] Stopping...");
|
|
await server.StopAsync();
|
|
Console.WriteLine("[gRPC Server] Stopped.");
|
|
}
|
|
|
|
private static X509Certificate2 GenerateSelfSignedCertificate()
|
|
{
|
|
|
|
using var ecdsa = ECDsa.Create(ECCurve.NamedCurves.nistP256);
|
|
var request = new CertificateRequest("CN=localhost", ecdsa, HashAlgorithmName.SHA256);
|
|
|
|
request.CertificateExtensions.Add(
|
|
new X509KeyUsageExtension(
|
|
X509KeyUsageFlags.DigitalSignature,
|
|
critical: false));
|
|
|
|
request.CertificateExtensions.Add(
|
|
new X509EnhancedKeyUsageExtension(
|
|
new OidCollection { new Oid("1.3.6.1.5.5.7.3.1") },
|
|
critical: false));
|
|
|
|
var sanBuilder = new SubjectAlternativeNameBuilder();
|
|
sanBuilder.AddDnsName("localhost");
|
|
sanBuilder.AddIpAddress(System.Net.IPAddress.Loopback);
|
|
request.CertificateExtensions.Add(sanBuilder.Build());
|
|
|
|
using var certificate = request.CreateSelfSigned(DateTimeOffset.UtcNow.AddMinutes(-5), DateTimeOffset.UtcNow.AddYears(200));
|
|
return new X509Certificate2(certificate.Export(X509ContentType.Pfx), (string?)null, X509KeyStorageFlags.Exportable);
|
|
}
|
|
}
|
|
}
|