-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #145 from Beyley/https
Add rudimentary HTTPS support
- Loading branch information
Showing
13 changed files
with
212 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Bunkum.Protocols.Http\Bunkum.Protocols.Http.csproj" /> | ||
<ProjectReference Include="..\Bunkum.Protocols.TlsSupport\Bunkum.Protocols.TlsSupport.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System.Security.Cryptography.X509Certificates; | ||
using Bunkum.Core; | ||
using Bunkum.Core.Configuration; | ||
using Bunkum.Listener; | ||
using Bunkum.Protocols.TlsSupport; | ||
using NotEnoughLogs; | ||
using NotEnoughLogs.Sinks; | ||
|
||
namespace Bunkum.Protocols.Https; | ||
|
||
public class BunkumHttpsServer : BunkumServer | ||
{ | ||
private readonly X509Certificate2? _cert; | ||
private readonly SslConfiguration _sslConfiguration; | ||
|
||
public BunkumHttpsServer(LoggerConfiguration? configuration = null, List<ILoggerSink>? sinks = null, | ||
SslConfiguration? sslConfiguration = null) : base(configuration, sinks) | ||
{ | ||
//If the SSL configuration is not specified, load the config from JSON | ||
this._sslConfiguration = sslConfiguration ?? Config.LoadFromJsonFile<SslConfiguration>("ssl.json", this.Logger); | ||
|
||
this._cert = new X509Certificate2(File.ReadAllBytes(this._sslConfiguration.SslCertificate), this._sslConfiguration.CertificatePassword); | ||
} | ||
|
||
/// <inherit-doc/> | ||
protected override BunkumListener CreateDefaultListener(Uri listenEndpoint, bool useForwardedIp, Logger logger) | ||
{ | ||
return new Http.Socket.SocketHttpListener(listenEndpoint, useForwardedIp, logger, this._cert, this._sslConfiguration.EnabledSslProtocols, this._sslConfiguration.EnabledCipherSuites); | ||
} | ||
|
||
/// <inherit-doc/> | ||
protected override string ProtocolUriName => "https"; | ||
} |
13 changes: 13 additions & 0 deletions
13
Bunkum.Protocols.TlsSupport/Bunkum.Protocols.TlsSupport.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Bunkum.Core\Bunkum.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System.Net.Security; | ||
using System.Security.Authentication; | ||
using Bunkum.Core.Configuration; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Converters; | ||
|
||
namespace Bunkum.Protocols.TlsSupport; | ||
|
||
public class SslConfiguration : Config | ||
{ | ||
public override int CurrentConfigVersion => 1; | ||
public override int Version { get; set; } | ||
|
||
protected override void Migrate(int oldVer, dynamic oldConfig) | ||
{} | ||
|
||
/// <summary> | ||
/// The path to the certificate | ||
/// </summary> | ||
public string SslCertificate { get; set; } = "cert.pfx"; | ||
/// <summary> | ||
/// The password for the certificate, null if none | ||
/// </summary> | ||
public string? CertificatePassword { get; set; } | ||
|
||
/// <summary> | ||
/// The SSL protocols which are enabled. If null, enables TLS1.3 and TLS1.2 | ||
/// </summary> | ||
[JsonProperty("EnabledSslProtocols", ItemConverterType = typeof(StringEnumConverter))] | ||
private SslProtocols[]? _EnabledSslProtocols { get; set; } | ||
/// <summary> | ||
/// The cipher suites which are enabled. If null, lets the system decide | ||
/// </summary> | ||
[JsonProperty(ItemConverterType = typeof(StringEnumConverter))] | ||
public TlsCipherSuite[]? EnabledCipherSuites { get; set; } | ||
|
||
[JsonIgnore] | ||
public SslProtocols EnabledSslProtocols | ||
{ | ||
get | ||
{ | ||
SslProtocols protocols = SslProtocols.None; | ||
|
||
if (this._EnabledSslProtocols == null) | ||
protocols = SslProtocols.Tls12 | SslProtocols.Tls13; | ||
else | ||
protocols = this._EnabledSslProtocols | ||
.Aggregate(protocols, (current, protocol) => current | protocol); | ||
|
||
return protocols; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters