Add PCAP API client
This commit is contained in:
parent
fc1139ffb5
commit
2fb3546b76
5 changed files with 137 additions and 4 deletions
|
@ -1,10 +1,18 @@
|
|||
root = true
|
||||
|
||||
[*]
|
||||
indent_style = space
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
charset = utf-8
|
||||
insert_final_newline = true
|
||||
max_line_length = 120
|
||||
|
||||
[*.xml]
|
||||
indent_size = 4
|
||||
|
||||
[*.cs]
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
|
||||
[*.json]
|
||||
indent_size = 2
|
||||
insert_final_newline = false
|
||||
|
|
|
@ -27,7 +27,7 @@ namespace INetMock.Client.Audit.Client
|
|||
_auditClient = new AuditService.AuditServiceClient(channel);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<string>> ListSinksAsync(CancellationToken token = default)
|
||||
public async Task<IReadOnlyList<string>> ListSinksAsync(CancellationToken token = default)
|
||||
{
|
||||
var sinks = await _auditClient.ListSinksAsync(new ListSinksRequest(), Metadata.Empty, null, token);
|
||||
return sinks.Sinks;
|
||||
|
|
|
@ -7,7 +7,7 @@ namespace INetMock.Client.Audit
|
|||
public interface IAuditApiClient
|
||||
{
|
||||
IProtoEventReader EventStreamAsync(string watcherName, CancellationToken token = default);
|
||||
Task<IEnumerable<string>> ListSinksAsync(CancellationToken token = default);
|
||||
Task<IReadOnlyList<string>> ListSinksAsync(CancellationToken token = default);
|
||||
Task<string> RegisterFileSinkAsync(string targetPath, CancellationToken token = default);
|
||||
Task<bool> RemoveFileSinkAsync(string targetPath, CancellationToken token = default);
|
||||
}
|
||||
|
|
96
src/INetMock.Client/PCAP/Client/PcapApiClient.cs
Normal file
96
src/INetMock.Client/PCAP/Client/PcapApiClient.cs
Normal file
|
@ -0,0 +1,96 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Google.Protobuf.WellKnownTypes;
|
||||
using Grpc.Core;
|
||||
using Grpc.Net.Client;
|
||||
using INetMock.Client.Rpc;
|
||||
|
||||
namespace INetMock.Client.PCAP.Client
|
||||
{
|
||||
public class PcapApiClient : IPcapApiClient
|
||||
{
|
||||
private readonly PCAPService.PCAPServiceClient _pcapServiceClient;
|
||||
|
||||
public PcapApiClient(string address, GrpcChannelOptions? options = null)
|
||||
: this(new Uri(address), options)
|
||||
{
|
||||
}
|
||||
|
||||
public PcapApiClient(Uri address, GrpcChannelOptions? options = null)
|
||||
: this(GrpcChannel.ForAddress(address, options ?? new GrpcChannelOptions()))
|
||||
{
|
||||
}
|
||||
|
||||
public PcapApiClient(ChannelBase channel)
|
||||
{
|
||||
_pcapServiceClient = new PCAPService.PCAPServiceClient(channel);
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<RecordingDevice>> ListAvailableDevicesAsync(CancellationToken token = default)
|
||||
{
|
||||
var devices = await _pcapServiceClient.ListAvailableDevicesAsync(new(), Metadata.Empty, null, token);
|
||||
return devices.AvailableDevices
|
||||
.Select(d => new RecordingDevice(
|
||||
d.Name,
|
||||
d.Addresses
|
||||
.Select(addr => new IPAddress(addr.Span))
|
||||
.ToList()
|
||||
)
|
||||
)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<Subscription>> ListActiveRecordingsAsync(CancellationToken token = default)
|
||||
{
|
||||
var recordings = await _pcapServiceClient.ListActiveRecordingsAsync(new(), Metadata.Empty, null, token);
|
||||
return recordings.Subscriptions.Select(SplitKeyIntoSubscription).ToList();
|
||||
}
|
||||
|
||||
public async Task<string> StartPcapFileRecordingAsync(RecordingRequest request,
|
||||
CancellationToken token = default)
|
||||
{
|
||||
var clientRequest = new StartPCAPFileRecordingRequest
|
||||
{
|
||||
Device = request.Device,
|
||||
Promiscuous = request.Promiscuous,
|
||||
TargetPath = request.TargetPath,
|
||||
ReadTimeout = Duration.FromTimeSpan(request.ReadTimeout)
|
||||
};
|
||||
|
||||
var result = await _pcapServiceClient.StartPCAPFileRecordingAsync(
|
||||
clientRequest,
|
||||
Metadata.Empty,
|
||||
null,
|
||||
token);
|
||||
|
||||
return result.ResolvedPath;
|
||||
}
|
||||
|
||||
public async Task<bool> StopPcapFileRecord(string consumerKey, CancellationToken token = default)
|
||||
{
|
||||
var clientRequest = new StopPCAPFileRecordRequest {ConsumerKey = consumerKey};
|
||||
var result = await _pcapServiceClient.StopPCAPFileRecordAsync(
|
||||
clientRequest,
|
||||
Metadata.Empty,
|
||||
null,
|
||||
token
|
||||
);
|
||||
return result.Removed;
|
||||
}
|
||||
|
||||
private static Subscription SplitKeyIntoSubscription(string key)
|
||||
{
|
||||
var splitIndex = key.IndexOf(':');
|
||||
if (splitIndex < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
return new Subscription(key.Substring(0, splitIndex), key.Substring(splitIndex), key);
|
||||
}
|
||||
}
|
||||
}
|
29
src/INetMock.Client/PCAP/IPcapApiClient.cs
Normal file
29
src/INetMock.Client/PCAP/IPcapApiClient.cs
Normal file
|
@ -0,0 +1,29 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace INetMock.Client.PCAP
|
||||
{
|
||||
public record RecordingDevice(string Name, IReadOnlyList<IPAddress> Addresses);
|
||||
|
||||
public record Subscription(string ConsumerName, string Device, string ConsumerKey);
|
||||
public record RecordingRequest(string Device, string TargetPath, bool Promiscuous = false)
|
||||
{
|
||||
public RecordingRequest(string device, string targetPath, bool promiscuous, TimeSpan readTimeout) : this(device,
|
||||
targetPath, promiscuous)
|
||||
{
|
||||
ReadTimeout = readTimeout;
|
||||
}
|
||||
|
||||
public TimeSpan ReadTimeout { get; } = TimeSpan.FromSeconds(30);
|
||||
}
|
||||
|
||||
public interface IPcapApiClient
|
||||
{
|
||||
Task<IReadOnlyList<RecordingDevice>> ListAvailableDevicesAsync(CancellationToken token = default);
|
||||
Task<IReadOnlyList<Subscription>> ListActiveRecordingsAsync(CancellationToken token = default);
|
||||
Task<string> StartPcapFileRecordingAsync(RecordingRequest request, CancellationToken token = default);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue