Skip to content

Commit 1247aed

Browse files
committed
SSL client & server initial commit
1 parent e45e999 commit 1247aed

9 files changed

Lines changed: 2009 additions & 2 deletions

File tree

NetCoreServer.sln

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UdpEchoServerExample", "exa
3535
EndProject
3636
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UdpMulticastServerExample", "examples\UdpMulticastServer\UdpMulticastServerExample.csproj", "{B2AF0232-BBCF-4A4A-82E5-112F5279EE67}"
3737
EndProject
38-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UdpEchoServer", "performance\UdpEchoServer\UdpEchoServer.csproj", "{A83EDDC0-414B-482D-BFF7-26AC70ED3FBB}"
38+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UdpEchoServer", "performance\UdpEchoServer\UdpEchoServer.csproj", "{A83EDDC0-414B-482D-BFF7-26AC70ED3FBB}"
3939
EndProject
40-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UdpMulticastServer", "performance\UdpMulticastServer\UdpMulticastServer.csproj", "{9B9383BA-57B1-4B69-81F5-7F3FA7ECF6C8}"
40+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UdpMulticastServer", "performance\UdpMulticastServer\UdpMulticastServer.csproj", "{9B9383BA-57B1-4B69-81F5-7F3FA7ECF6C8}"
41+
EndProject
42+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SslChatServer", "examples\SslChatServer\SslChatServer.csproj", "{A091E592-950E-438B-87B2-ABBD0F7812F0}"
43+
EndProject
44+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SslEchoClient", "examples\SslEchoClient\SslEchoClient.csproj", "{E9668313-F7DD-48AD-9BC2-1E015DB238E1}"
4145
EndProject
4246
Global
4347
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -241,6 +245,30 @@ Global
241245
{9B9383BA-57B1-4B69-81F5-7F3FA7ECF6C8}.Release|x64.Build.0 = Release|Any CPU
242246
{9B9383BA-57B1-4B69-81F5-7F3FA7ECF6C8}.Release|x86.ActiveCfg = Release|Any CPU
243247
{9B9383BA-57B1-4B69-81F5-7F3FA7ECF6C8}.Release|x86.Build.0 = Release|Any CPU
248+
{A091E592-950E-438B-87B2-ABBD0F7812F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
249+
{A091E592-950E-438B-87B2-ABBD0F7812F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
250+
{A091E592-950E-438B-87B2-ABBD0F7812F0}.Debug|x64.ActiveCfg = Debug|Any CPU
251+
{A091E592-950E-438B-87B2-ABBD0F7812F0}.Debug|x64.Build.0 = Debug|Any CPU
252+
{A091E592-950E-438B-87B2-ABBD0F7812F0}.Debug|x86.ActiveCfg = Debug|Any CPU
253+
{A091E592-950E-438B-87B2-ABBD0F7812F0}.Debug|x86.Build.0 = Debug|Any CPU
254+
{A091E592-950E-438B-87B2-ABBD0F7812F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
255+
{A091E592-950E-438B-87B2-ABBD0F7812F0}.Release|Any CPU.Build.0 = Release|Any CPU
256+
{A091E592-950E-438B-87B2-ABBD0F7812F0}.Release|x64.ActiveCfg = Release|Any CPU
257+
{A091E592-950E-438B-87B2-ABBD0F7812F0}.Release|x64.Build.0 = Release|Any CPU
258+
{A091E592-950E-438B-87B2-ABBD0F7812F0}.Release|x86.ActiveCfg = Release|Any CPU
259+
{A091E592-950E-438B-87B2-ABBD0F7812F0}.Release|x86.Build.0 = Release|Any CPU
260+
{E9668313-F7DD-48AD-9BC2-1E015DB238E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
261+
{E9668313-F7DD-48AD-9BC2-1E015DB238E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
262+
{E9668313-F7DD-48AD-9BC2-1E015DB238E1}.Debug|x64.ActiveCfg = Debug|Any CPU
263+
{E9668313-F7DD-48AD-9BC2-1E015DB238E1}.Debug|x64.Build.0 = Debug|Any CPU
264+
{E9668313-F7DD-48AD-9BC2-1E015DB238E1}.Debug|x86.ActiveCfg = Debug|Any CPU
265+
{E9668313-F7DD-48AD-9BC2-1E015DB238E1}.Debug|x86.Build.0 = Debug|Any CPU
266+
{E9668313-F7DD-48AD-9BC2-1E015DB238E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
267+
{E9668313-F7DD-48AD-9BC2-1E015DB238E1}.Release|Any CPU.Build.0 = Release|Any CPU
268+
{E9668313-F7DD-48AD-9BC2-1E015DB238E1}.Release|x64.ActiveCfg = Release|Any CPU
269+
{E9668313-F7DD-48AD-9BC2-1E015DB238E1}.Release|x64.Build.0 = Release|Any CPU
270+
{E9668313-F7DD-48AD-9BC2-1E015DB238E1}.Release|x86.ActiveCfg = Release|Any CPU
271+
{E9668313-F7DD-48AD-9BC2-1E015DB238E1}.Release|x86.Build.0 = Release|Any CPU
244272
EndGlobalSection
245273
GlobalSection(SolutionProperties) = preSolution
246274
HideSolutionNode = FALSE
@@ -260,6 +288,8 @@ Global
260288
{B2AF0232-BBCF-4A4A-82E5-112F5279EE67} = {81ED94B5-0C65-432C-AEF5-67C3DFF7F2EE}
261289
{A83EDDC0-414B-482D-BFF7-26AC70ED3FBB} = {8965E5CD-E36E-4DC5-A36E-827AFFC642D1}
262290
{9B9383BA-57B1-4B69-81F5-7F3FA7ECF6C8} = {8965E5CD-E36E-4DC5-A36E-827AFFC642D1}
291+
{A091E592-950E-438B-87B2-ABBD0F7812F0} = {81ED94B5-0C65-432C-AEF5-67C3DFF7F2EE}
292+
{E9668313-F7DD-48AD-9BC2-1E015DB238E1} = {81ED94B5-0C65-432C-AEF5-67C3DFF7F2EE}
263293
EndGlobalSection
264294
GlobalSection(ExtensibilityGlobals) = postSolution
265295
SolutionGuid = {64415837-63D9-474E-A25C-EE91C9CA90C5}

examples/SslChatServer/Program.cs

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
using System;
2+
using System.Net;
3+
using System.Net.Sockets;
4+
using System.Security.Authentication;
5+
using System.Security.Cryptography.X509Certificates;
6+
using System.Text;
7+
using NetCoreServer;
8+
9+
namespace SslChatServer
10+
{
11+
class ChatSession : SslSession
12+
{
13+
public ChatSession(SslServer server) : base(server) {}
14+
15+
protected override void OnConnected()
16+
{
17+
Console.WriteLine($"Chat SSL session with Id {Id} connected!");
18+
}
19+
20+
protected override void OnHandshaked()
21+
{
22+
Console.WriteLine($"Chat SSL session with Id {Id} handshaked!");
23+
24+
// Send invite message
25+
string message = "Hello from SSL chat! Please send a message or '!' to disconnect the client!";
26+
Send(message);
27+
}
28+
29+
protected override void OnDisconnected()
30+
{
31+
Console.WriteLine($"Chat SSL session with Id {Id} disconnected!");
32+
}
33+
34+
protected override void OnReceived(byte[] buffer, long size)
35+
{
36+
string message = Encoding.UTF8.GetString(buffer, 0, (int)size);
37+
Console.WriteLine("Incoming: " + message);
38+
39+
// Multicast message to all connected sessions
40+
Server.Multicast(message);
41+
42+
// If the buffer starts with '!' the disconnect the current session
43+
if (message == "!")
44+
Disconnect();
45+
}
46+
47+
protected override void OnError(SocketError error)
48+
{
49+
Console.WriteLine($"Chat SSL session caught an error with code {error}");
50+
}
51+
}
52+
53+
class ChatServer : SslServer
54+
{
55+
public ChatServer(SslContext context, IPAddress address, int port) : base(context, address, port) {}
56+
57+
protected override SslSession CreateSession()
58+
{
59+
return new ChatSession(this);
60+
}
61+
62+
protected override void OnError(SocketError error)
63+
{
64+
Console.WriteLine($"Chat SSL server caught an error with code {error}");
65+
}
66+
}
67+
68+
class Program
69+
{
70+
static void Main(string[] args)
71+
{
72+
// SSL server port
73+
int port = 2222;
74+
if (args.Length > 0)
75+
port = int.Parse(args[0]);
76+
77+
Console.WriteLine($"SSL server port: {port}");
78+
79+
// Create and prepare a new SSL server context
80+
var context = new SslContext(SslProtocols.Tls12, new X509Certificate2("server.pem", "qwerty"));
81+
82+
// Create a new SSL chat server
83+
var server = new ChatServer(context, IPAddress.Any, port);
84+
85+
// Start the server
86+
Console.Write("Server starting...");
87+
server.Start();
88+
Console.WriteLine("Done!");
89+
90+
Console.WriteLine("Press Enter to stop the server or '!' to restart the server...");
91+
92+
// Perform text input
93+
for (;;)
94+
{
95+
string line = Console.ReadLine();
96+
if (line == string.Empty)
97+
break;
98+
99+
// Restart the server
100+
if (line == "!")
101+
{
102+
Console.Write("Server restarting...");
103+
server.Restart();
104+
Console.WriteLine("Done!");
105+
continue;
106+
}
107+
108+
// Multicast admin message to all sessions
109+
line = "(admin) " + line;
110+
server.Multicast(line);
111+
}
112+
113+
// Stop the server
114+
Console.Write("Server stopping...");
115+
server.Stop();
116+
Console.WriteLine("Done!");
117+
}
118+
}
119+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\..\source\NetCoreServer\NetCoreServer.csproj" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<None Include="..\..\tools\certificates\server.pem">
14+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
15+
</None>
16+
</ItemGroup>
17+
18+
</Project>

examples/SslEchoClient/Program.cs

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
using System;
2+
using System.Net.Sockets;
3+
using System.Security.Authentication;
4+
using System.Security.Cryptography.X509Certificates;
5+
using System.Text;
6+
using System.Threading;
7+
using NetCoreServer;
8+
9+
namespace SslChatClient
10+
{
11+
class ChatClient : SslClient
12+
{
13+
public ChatClient(SslContext context, string address, int port) : base(context, address, port) {}
14+
15+
public void DisconnectAndStop()
16+
{
17+
_stop = true;
18+
Disconnect();
19+
while (IsConnected)
20+
Thread.Yield();
21+
}
22+
23+
protected override void OnConnected()
24+
{
25+
Console.WriteLine($"Chat SSL client connected a new session with Id {Id}");
26+
}
27+
28+
protected override void OnHandshaked()
29+
{
30+
Console.WriteLine($"Chat SSL client handshaked a new session with Id {Id}");
31+
}
32+
33+
protected override void OnDisconnected()
34+
{
35+
Console.WriteLine($"Chat SSL client disconnected a session with Id {Id}");
36+
37+
// Wait for a while...
38+
Thread.Sleep(1000);
39+
40+
// Try to connect again
41+
if (!_stop)
42+
Connect();
43+
}
44+
45+
protected override void OnReceived(byte[] buffer, long size)
46+
{
47+
Console.WriteLine(Encoding.UTF8.GetString(buffer, 0, (int)size));
48+
}
49+
50+
protected override void OnError(SocketError error)
51+
{
52+
Console.WriteLine($"Chat SSL client caught an error with code {error}");
53+
}
54+
55+
private bool _stop;
56+
}
57+
58+
class Program
59+
{
60+
static void Main(string[] args)
61+
{
62+
// SSL server address
63+
string address = "127.0.0.1";
64+
if (args.Length > 0)
65+
address = args[0];
66+
67+
// SSL server port
68+
int port = 2222;
69+
if (args.Length > 1)
70+
port = int.Parse(args[1]);
71+
72+
Console.WriteLine($"SSL server address: {address}");
73+
Console.WriteLine($"SSL server port: {port}");
74+
75+
// Create and prepare a new SSL client context
76+
var context = new SslContext(SslProtocols.Tls12, new X509Certificate2("ca.pem"));
77+
78+
// Create a new SSL chat client
79+
var client = new ChatClient(context, address, port);
80+
81+
// Connect the client
82+
Console.Write("Client connecting...");
83+
client.Connect();
84+
Console.WriteLine("Done!");
85+
86+
Console.WriteLine("Press Enter to stop the client or '!' to reconnect the client...");
87+
88+
// Perform text input
89+
for (;;)
90+
{
91+
string line = Console.ReadLine();
92+
if (line == string.Empty)
93+
break;
94+
95+
// Disconnect the client
96+
if (line == "!")
97+
{
98+
Console.Write("Client disconnecting...");
99+
client.Disconnect();
100+
Console.WriteLine("Done!");
101+
continue;
102+
}
103+
104+
// Send the entered text to the chat server
105+
client.Send(line);
106+
}
107+
108+
// Disconnect the client
109+
Console.Write("Client disconnecting...");
110+
client.DisconnectAndStop();
111+
Console.WriteLine("Done!");
112+
}
113+
}
114+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\..\source\NetCoreServer\NetCoreServer.csproj" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<None Include="..\..\tools\certificates\ca.pem">
14+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
15+
</None>
16+
</ItemGroup>
17+
18+
</Project>

0 commit comments

Comments
 (0)