Skip to content

Commit

Permalink
feat: support with_server_name option
Browse files Browse the repository at this point in the history
  • Loading branch information
Rijicho committed Nov 22, 2024
1 parent c25960e commit e1f72dd
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 1 deletion.
10 changes: 10 additions & 0 deletions native/yaha_native/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ pub extern "C" fn yaha_client_config_add_root_certificates(
valid
}

#[no_mangle]
pub extern "C" fn yaha_client_config_add_override_server_name(
ctx: *mut YahaNativeContext,
override_server_name: *const StringBuffer,
) {
let ctx = YahaNativeContextInternal::from_raw_context(ctx);
let server_name = unsafe { (*override_server_name).to_str() };
ctx.override_server_name.get_or_insert(server_name.to_string());
}

#[no_mangle]
pub extern "C" fn yaha_client_config_add_client_auth_certificates(
ctx: *mut YahaNativeContext,
Expand Down
12 changes: 11 additions & 1 deletion native/yaha_native/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub struct YahaNativeContextInternal<'a> {
pub client_builder: Option<client::legacy::Builder>,
pub skip_certificate_verification: Option<bool>,
pub root_certificates: Option<rustls::RootCertStore>,
pub override_server_name: Option<String>,
pub client_auth_certificates: Option<Vec<CertificateDer<'a>>>,
pub client_auth_key: Option<PrivateKeyDer<'a>>,
pub client: Option<Client<HttpsConnector<HttpConnector>, BoxBody<Bytes, hyper::Error>>>,
Expand All @@ -79,6 +80,7 @@ impl YahaNativeContextInternal<'_> {
client_builder: Some(Client::builder(TokioExecutor::new())),
skip_certificate_verification: None,
root_certificates: None,
override_server_name: None,
client_auth_certificates: None,
client_auth_key: None,
on_status_code_and_headers_receive,
Expand Down Expand Up @@ -140,7 +142,15 @@ impl YahaNativeContextInternal<'_> {

let builder = hyper_rustls::HttpsConnectorBuilder::new()
.with_tls_config(tls_config)
.https_or_http()
.https_or_http();

let builder = if let Some(override_server_name) = &self.override_server_name {
builder.with_server_name(override_server_name.clone())
} else {
builder
};

let builder = builder
.enable_all_versions();

// Almost the same as `builder.build()`, but specify `set_nodelay(true)`.
Expand Down
10 changes: 10 additions & 0 deletions src/YetAnotherHttpHandler/NativeHttpHandlerCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ private unsafe void Initialize(YahaNativeContext* ctx, NativeClientSettings sett
if (YahaEventSource.Log.IsEnabled()) YahaEventSource.Log.Info($"yaha_client_config_add_root_certificates: ValidCertificatesCount={validCertificatesCount}");
}
}
if (settings.OverrideServerName is { } overrideServerName)
{
if (YahaEventSource.Log.IsEnabled()) YahaEventSource.Log.Info($"Option '{nameof(settings.OverrideServerName)}' = {overrideServerName}");
var overrideServerNameBytes = Encoding.UTF8.GetBytes(overrideServerName);
fixed (byte* buffer = overrideServerNameBytes)
{
var sb = new StringBuffer(buffer, overrideServerNameBytes.Length);
NativeMethods.yaha_client_config_add_override_server_name(ctx, &sb);
}
}
if (settings.ClientAuthKey is { } clientAuthKey)
{
if (YahaEventSource.Log.IsEnabled()) YahaEventSource.Log.Info($"Option '{nameof(settings.ClientAuthKey)}' = {clientAuthKey}");
Expand Down
3 changes: 3 additions & 0 deletions src/YetAnotherHttpHandler/NativeMethods.Uwp.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ internal static unsafe partial class NativeMethods
[DllImport(__DllName, EntryPoint = "yaha_client_config_add_root_certificates", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern nuint yaha_client_config_add_root_certificates(YahaNativeContext* ctx, StringBuffer* root_certs);

[DllImport(__DllName, EntryPoint = "yaha_client_config_add_override_server_name", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void yaha_client_config_add_override_server_name(YahaNativeContext* ctx, StringBuffer* override_server_name);

[DllImport(__DllName, EntryPoint = "yaha_client_config_add_client_auth_certificates", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern nuint yaha_client_config_add_client_auth_certificates(YahaNativeContext* ctx, StringBuffer* auth_certs);

Expand Down
3 changes: 3 additions & 0 deletions src/YetAnotherHttpHandler/NativeMethods.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ internal static unsafe partial class NativeMethods
[DllImport(__DllName, EntryPoint = "yaha_client_config_add_root_certificates", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern nuint yaha_client_config_add_root_certificates(YahaNativeContext* ctx, StringBuffer* root_certs);

[DllImport(__DllName, EntryPoint = "yaha_client_config_add_override_server_name", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern void yaha_client_config_add_override_server_name(YahaNativeContext* ctx, StringBuffer* override_server_name);

[DllImport(__DllName, EntryPoint = "yaha_client_config_add_client_auth_certificates", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern nuint yaha_client_config_add_client_auth_certificates(YahaNativeContext* ctx, StringBuffer* auth_certs);

Expand Down
7 changes: 7 additions & 0 deletions src/YetAnotherHttpHandler/YetAnotherHttpHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public class YetAnotherHttpHandler : HttpMessageHandler
/// </summary>
public string? RootCertificates { get => _settings.RootCertificates; set => _settings.RootCertificates = value; }

/// <summary>
/// Gets or sets a value that specifies subject alternative name (SAN) of the certificate.
/// </summary>
public string? OverrideServerName { get => _settings.OverrideServerName; set => _settings.OverrideServerName = value; }

/// <summary>
/// Gets or sets a custom client auth certificates.
/// </summary>
Expand Down Expand Up @@ -192,6 +197,7 @@ internal class NativeClientSettings
public bool? Http2Only { get; set; }
public bool? SkipCertificateVerification { get; set; }
public string? RootCertificates { get; set; }
public string? OverrideServerName { get; set; }
public string? ClientAuthCertificates { get; set; }
public string? ClientAuthKey { get; set; }
public uint? Http2InitialStreamWindowSize { get; set; }
Expand All @@ -214,6 +220,7 @@ public NativeClientSettings Clone()
Http2Only = this.Http2Only,
SkipCertificateVerification = this.SkipCertificateVerification,
RootCertificates = this.RootCertificates,
OverrideServerName = this.OverrideServerName,
ClientAuthCertificates = this.ClientAuthCertificates,
ClientAuthKey = this.ClientAuthKey,
Http2InitialStreamWindowSize = this.Http2InitialStreamWindowSize,
Expand Down

0 comments on commit e1f72dd

Please sign in to comment.