Support https (resolves #3).
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,2 +1,6 @@
|
||||
/bin/
|
||||
/obj/
|
||||
|
||||
/localhost.crt
|
||||
/localhost.key
|
||||
/localhost.pfx
|
||||
|
||||
32
Program.cs
32
Program.cs
@@ -1,8 +1,10 @@
|
||||
using System.Net.WebSockets;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Hosting.Server;
|
||||
using Microsoft.AspNetCore.Http.Features;
|
||||
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
||||
using Microsoft.AspNetCore.Server.Kestrel.Https;
|
||||
using Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets;
|
||||
using Microsoft.AspNetCore.WebSockets;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
@@ -66,12 +68,39 @@ class Application : IHttpApplication<Context>
|
||||
}
|
||||
}
|
||||
|
||||
class AppServices : IServiceProvider
|
||||
{
|
||||
public object? GetService(Type serviceType)
|
||||
{
|
||||
if (serviceType == typeof(ILoggerFactory))
|
||||
return new NullLoggerFactory();
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
class Program
|
||||
{
|
||||
static async Task Main()
|
||||
{
|
||||
var serverOptions = new KestrelServerOptions();
|
||||
KestrelServerOptions serverOptions = new();
|
||||
if (File.Exists("localhost.pfx"))
|
||||
{
|
||||
HttpsConnectionAdapterOptions httpsOptions = new()
|
||||
{
|
||||
ServerCertificate = new X509Certificate2("localhost.pfx")
|
||||
};
|
||||
|
||||
serverOptions.ListenAnyIP(8080, options =>
|
||||
{
|
||||
options.KestrelServerOptions.ApplicationServices = new AppServices();
|
||||
options.UseHttps(httpsOptions);
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
serverOptions.ListenAnyIP(8080);
|
||||
}
|
||||
|
||||
var transportOptions = new SocketTransportOptions();
|
||||
var loggerFactory = new NullLoggerFactory();
|
||||
@@ -83,6 +112,7 @@ class Program
|
||||
new OptionsWrapper<KestrelServerOptions>(serverOptions), transportFactory, loggerFactory);
|
||||
|
||||
await server.StartAsync(new Application(loggerFactory), CancellationToken.None);
|
||||
Console.WriteLine("Server started");
|
||||
await Task.Delay(Timeout.Infinite);
|
||||
}
|
||||
}
|
||||
|
||||
19
README.md
19
README.md
@@ -24,3 +24,22 @@ dotnet publish -r linux-x64 -c Release
|
||||
(This has only been tested on Linux. If you are not on Linux, you could try building an executable, but you will need to substitute a different runtime identifier in the above command.)
|
||||
|
||||
You will see a few warnings during compilation, but as far as I can tell, the resulting executable works well. The executable is about 46M with debug information, 15M stripped. After the process has been invoked (using both HTTP GET and websockets) the resident memory is about 22M, far better than even the simplest ASP.NET application, and similar to Go.
|
||||
|
||||
TLS
|
||||
===
|
||||
|
||||
The server now supports TLS. If you store a PKCS12 certificate in `localhost.pfx`, the server will listen for https connections on port 8080, rather than plain http.
|
||||
|
||||
For testing purposes (and *only* testing purposes) you can create a suitable self-signed certificate like this:
|
||||
|
||||
```
|
||||
openssl req -x509 -newkey rsa:2048 -sha256 -keyout localhost.key -out localhost.crt -subj '/CN=test.com' -nodes
|
||||
openssl pkcs12 -export -name localhost -out localhost.pfx -inkey localhost.key -in localhost.crt -passout pass:
|
||||
```
|
||||
|
||||
To test the server, you will now need these commands:
|
||||
|
||||
```
|
||||
curl --insecure https://localhost:8080
|
||||
wscat --no-check -c wss://desktop.chown.org.uk:8080/ws
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user