38 lines
1.2 KiB
C#
38 lines
1.2 KiB
C#
using System.Net.Http;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using Grpc.Net.Client;
|
|
using Microsoft.Extensions.Options;
|
|
using ScrapperAPI.AgentGrpc;
|
|
|
|
namespace VoyagerAgent;
|
|
|
|
public sealed class GrpcAgentClient
|
|
{
|
|
private readonly AgentClientOptions _opts;
|
|
|
|
public GrpcAgentClient(IOptions<AgentClientOptions> options)
|
|
{
|
|
_opts = options.Value;
|
|
}
|
|
|
|
public AgentService.AgentServiceClient CreateClient()
|
|
{
|
|
var handler = new HttpClientHandler();
|
|
|
|
if (!string.IsNullOrWhiteSpace(_opts.ClientCertificatePath))
|
|
{
|
|
var cert = new X509Certificate2(_opts.ClientCertificatePath, _opts.ClientCertificatePassword);
|
|
handler.ClientCertificates.Add(cert);
|
|
}
|
|
|
|
if (_opts.InsecureSkipServerCertificateValidation)
|
|
{
|
|
handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
|
|
}
|
|
|
|
var httpClient = new HttpClient(handler);
|
|
var channel = GrpcChannel.ForAddress(_opts.CentralGrpcAddress, new GrpcChannelOptions { HttpClient = httpClient });
|
|
return new AgentService.AgentServiceClient(channel);
|
|
}
|
|
}
|