Added Proxy Tester
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
||||
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||
|
||||
using System.Net;
|
||||
using System.Net.Security;
|
||||
using System.Reflection;
|
||||
using System.Security.Authentication;
|
||||
using System.Security.Cryptography;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
using EonaCat.WebSockets;
|
||||
|
||||
internal class Program
|
||||
@@ -18,44 +20,125 @@ internal class Program
|
||||
// Menu loop
|
||||
while (true)
|
||||
{
|
||||
Console.WriteLine("0. Create a HTTPS certificate");
|
||||
Console.WriteLine("1. Start the server and client");
|
||||
Console.WriteLine("2. Send Message");
|
||||
Console.WriteLine("3. Quit");
|
||||
|
||||
var choice = Console.ReadLine();
|
||||
|
||||
switch (choice)
|
||||
try
|
||||
{
|
||||
case "0":
|
||||
CreateCertificate();
|
||||
break;
|
||||
Console.WriteLine("0. Create a HTTPS certificate");
|
||||
Console.WriteLine("1. Start the server and client");
|
||||
Console.WriteLine("2. Send Message");
|
||||
Console.WriteLine("3. Tcp Server and Tcp Client Test With SSL");
|
||||
Console.WriteLine("4. Quit");
|
||||
|
||||
case "1":
|
||||
await CreateServerAndClientAsync().ConfigureAwait(false);
|
||||
break;
|
||||
var choice = Console.ReadLine();
|
||||
|
||||
case "2":
|
||||
if (_client != null)
|
||||
{
|
||||
Console.Write("Enter message: ");
|
||||
var message = Console.ReadLine();
|
||||
await _client.SendTextAsync(message).ConfigureAwait(false);
|
||||
}
|
||||
switch (choice)
|
||||
{
|
||||
case "0":
|
||||
CreateCertificate();
|
||||
break;
|
||||
|
||||
break;
|
||||
case "1":
|
||||
await CreateServerAndClientAsync().ConfigureAwait(false);
|
||||
break;
|
||||
|
||||
case "3":
|
||||
_client?.CloseAsync().ConfigureAwait(false);
|
||||
return;
|
||||
case "2":
|
||||
if (_client != null)
|
||||
{
|
||||
Console.Write("Enter message: ");
|
||||
var message = Console.ReadLine();
|
||||
await _client.SendTextAsync(message).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
default:
|
||||
Console.WriteLine("Invalid choice. Try again.");
|
||||
break;
|
||||
break;
|
||||
|
||||
case "3":
|
||||
await CreateTcpClientAndServerTestWithSSL().ConfigureAwait(false);
|
||||
break;
|
||||
|
||||
case "4":
|
||||
_client?.CloseAsync().ConfigureAwait(false);
|
||||
return;
|
||||
|
||||
default:
|
||||
Console.WriteLine("Invalid choice. Try again.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Exception at: {DateTime.Now} {ex.Message}");
|
||||
Console.ReadKey();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task CreateTcpClientAndServerTestWithSSL()
|
||||
{
|
||||
var certificatePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "localhost.pfx");
|
||||
var certificatePassword = "";
|
||||
var loadedCertificate = new X509Certificate2(certificatePath, certificatePassword);
|
||||
|
||||
var sslOptions = new EonaCat.Network.SslOptions
|
||||
{
|
||||
SslProtocol = SslProtocols.Tls12 | SslProtocols.Tls13,
|
||||
CheckCertificateRevocation = false,
|
||||
ClientCertificates = new X509CertificateCollection { loadedCertificate },
|
||||
CertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
|
||||
{
|
||||
// Accept all certificates for testing purposes
|
||||
return true;
|
||||
},
|
||||
};
|
||||
|
||||
long serverMessageCount = 0;
|
||||
var tcpServer = new EonaCat.Network.SocketTcpServer(IPAddress.Loopback, 1111, loadedCertificate, sslOptions);
|
||||
tcpServer.OnConnect += async (remoteInfo) =>
|
||||
{
|
||||
Console.WriteLine($"Connected {remoteInfo.NickName}");
|
||||
await tcpServer.SendToAsync(remoteInfo.Socket, Encoding.UTF8.GetBytes($"ServerMessage: {++serverMessageCount}"));
|
||||
};
|
||||
|
||||
tcpServer.OnDisconnect += (remoteInfo) => Console.WriteLine($"Disconnected from {remoteInfo.EndPoint}");
|
||||
tcpServer.OnError += (ex, message) => Console.WriteLine($"Error: {message}{Environment.NewLine}{ex}");
|
||||
tcpServer.OnReceive += async (remoteInfo, data) =>
|
||||
{
|
||||
Console.WriteLine($"Received from {remoteInfo}: {data}");
|
||||
await tcpServer.SendToAsync(remoteInfo.Socket, Encoding.UTF8.GetBytes($"ServerMessage: {++serverMessageCount}"));
|
||||
};
|
||||
await tcpServer.StartAsync().ConfigureAwait(false);
|
||||
|
||||
Console.WriteLine("TCP Server started.");
|
||||
Console.WriteLine("Press any key to connect the TCP client...");
|
||||
Console.ReadKey();
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
Console.WriteLine($"Connecting TCP client {i + 1}...");
|
||||
|
||||
// Create a TCP client
|
||||
long clientMessageCount = 0;
|
||||
var tcpClient = new EonaCat.Network.SocketTcpClient();
|
||||
tcpClient.OnConnect += async (remoteInfo) =>
|
||||
{
|
||||
Console.WriteLine($"Connected to {remoteInfo.EndPoint}");
|
||||
await tcpClient.SendAsync(Encoding.UTF8.GetBytes($"{++clientMessageCount}")).ConfigureAwait(false);
|
||||
};
|
||||
|
||||
tcpClient.OnReceive += async (remoteInfo) =>
|
||||
{
|
||||
Console.WriteLine($"Received from {remoteInfo}: {remoteInfo.Data}");
|
||||
await tcpServer.SendToAsync(remoteInfo.Socket, Encoding.UTF8.GetBytes($"ServerMessage: {++serverMessageCount}"));
|
||||
};
|
||||
tcpClient.OnDisconnect += (remoteInfo) => Console.WriteLine($"Disconnected from {remoteInfo.EndPoint}");
|
||||
tcpClient.OnError += (ex, message) => Console.WriteLine($"Error: {message}{Environment.NewLine}{ex}");
|
||||
await tcpClient.ConnectAsync("127.0.0.1", 1111, true, sslOptions).ConfigureAwait(false);
|
||||
|
||||
Console.WriteLine($"TCP Client {i} connected to server.");
|
||||
}
|
||||
|
||||
Console.WriteLine("Press any key to stop the server and client...");
|
||||
Console.ReadKey();
|
||||
}
|
||||
|
||||
private static async Task CreateServerAndClientAsync()
|
||||
{
|
||||
var serverUri = "wss://localhost:8443";
|
||||
|
Reference in New Issue
Block a user