Proper gRPC protocol

This commit is contained in:
2025-09-18 01:02:25 +08:00
parent 3d47b4e44e
commit f6f0703cb3
6 changed files with 167 additions and 64 deletions

View File

@@ -1,4 +1,5 @@
using DysonNetwork.Shared.Proto;
using DysonNetwork.Shared.Registry;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
@@ -10,15 +11,7 @@ public static class DysonAuthStartup
this IServiceCollection services
)
{
services.AddGrpcClient<AuthService.AuthServiceClient>(o =>
{
o.Address = new Uri("https://pass");
});
services.AddGrpcClient<PermissionService.PermissionServiceClient>(o =>
{
o.Address = new Uri("https://pass");
});
services.AddAuthService();
services.AddAuthentication(options =>
{

View File

@@ -20,8 +20,53 @@ public static class KestrelConfiguration
builder.WebHost.ConfigureKestrel(options =>
{
options.Limits.MaxRequestBodySize = maxRequestBodySize;
// gRPC
options.ListenAnyIP(5001, listenOptions =>
{
listenOptions.Protocols = HttpProtocols.Http2;
var selfSignedCert = _CreateSelfSignedCertificate();
listenOptions.UseHttps(selfSignedCert);
});
var httpPorts = configuration.GetValue<string>("HTTP_PORTS", "5000")
.Split(',', StringSplitOptions.RemoveEmptyEntries)
.Select(p => int.Parse(p.Trim()))
.ToArray();
// Regular HTTP
foreach (var httpPort in httpPorts)
options.ListenAnyIP(httpPort,
listenOptions => { listenOptions.Protocols = HttpProtocols.Http1AndHttp2; });
});
return builder;
}
static X509Certificate2 _CreateSelfSignedCertificate()
{
using var rsa = RSA.Create(2048);
var certRequest = new CertificateRequest(
"CN=dyson.network", // Common Name for the certificate
rsa,
HashAlgorithmName.SHA256,
RSASignaturePadding.Pkcs1);
// Add extensions (e.g., for server authentication)
certRequest.CertificateExtensions.Add(
new X509EnhancedKeyUsageExtension(
new OidCollection { new Oid("1.3.6.1.5.5.7.3.1") }, // Server Authentication
false));
// Set validity period (e.g., 1 year)
var notBefore = DateTimeOffset.UtcNow.AddDays(-1);
var notAfter = notBefore.AddYears(1);
var certificate = certRequest.CreateSelfSigned(notBefore, notAfter);
// Export to PKCS#12 and load using X509CertificateLoader
var pfxBytes = certificate.Export(X509ContentType.Pfx);
return X509CertificateLoader.LoadPkcs12(pfxBytes, password: null);
}
}

View File

@@ -8,72 +8,94 @@ public static class ServiceInjectionHelper
{
public static IServiceCollection AddRingService(this IServiceCollection services)
{
services.AddGrpcClient<RingService.RingServiceClient>(o =>
{
o.Address = new Uri("https://ring");
});
services
.AddGrpcClient<RingService.RingServiceClient>(o => o.Address = new Uri("https://_grpc.ring"))
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
);
return services;
}
public static IServiceCollection AddAuthService(this IServiceCollection services)
{
services
.AddGrpcClient<AuthService.AuthServiceClient>(o => o.Address = new Uri("https://_grpc.pass"))
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
);
services
.AddGrpcClient<PermissionService.PermissionServiceClient>(o => o.Address = new Uri("https://_grpc.pass"))
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
);
return services;
}
public static IServiceCollection AddAccountService(this IServiceCollection services)
{
services.AddGrpcClient<AccountService.AccountServiceClient>(o =>
{
o.Address = new Uri("https://pass");
});
services
.AddGrpcClient<AccountService.AccountServiceClient>(o => o.Address = new Uri("https://_grpc.pass") )
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
);
services.AddSingleton<AccountClientHelper>();
services.AddGrpcClient<BotAccountReceiverService.BotAccountReceiverServiceClient>(o =>
{
o.Address = new Uri("https://pass");
});
services.AddGrpcClient<ActionLogService.ActionLogServiceClient>(o =>
{
o.Address = new Uri("https://pass");
});
services.AddGrpcClient<PaymentService.PaymentServiceClient>(o =>
{
o.Address = new Uri("https://pass");
});
services
.AddGrpcClient<BotAccountReceiverService.BotAccountReceiverServiceClient>(o =>
o.Address = new Uri("https://_grpc.pass")
)
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
);
services.AddGrpcClient<ActionLogService.ActionLogServiceClient>(o => o.Address = new Uri("https://_grpc.pass"))
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
);
services.AddGrpcClient<PaymentService.PaymentServiceClient>(o => o.Address = new Uri("https://_grpc.pass"))
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
);
return services;
}
public static IServiceCollection AddDriveService(this IServiceCollection services)
{
services.AddGrpcClient<FileService.FileServiceClient>(o =>
{
o.Address = new Uri("https://drive");
});
services.AddGrpcClient<FileReferenceService.FileReferenceServiceClient>(o =>
{
o.Address = new Uri("https://drive");
});
services.AddGrpcClient<FileService.FileServiceClient>(o => o.Address = new Uri("https://_grpc.drive"))
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
);
services.AddGrpcClient<FileReferenceService.FileReferenceServiceClient>(o => o.Address = new Uri("https://_grpc.drive"))
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
);
return services;
}
public static IServiceCollection AddPublisherService(this IServiceCollection services)
{
services.AddGrpcClient<PublisherService.PublisherServiceClient>(o =>
{
o.Address = new Uri("https://sphere");
});
services.AddGrpcClient<PublisherService.PublisherServiceClient>(o => o.Address = new Uri("https://_grpc.sphere"))
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
);
return services;
}
public static IServiceCollection AddDevelopService(this IServiceCollection services)
{
services.AddGrpcClient<CustomAppService.CustomAppServiceClient>(o =>
{
o.Address = new Uri("https://develop");
});
services.AddGrpcClient<CustomAppService.CustomAppServiceClient>(o => o.Address = new Uri("https://_grpc.develop"))
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
);
return services;
}
}
}