Skip to content

Commit

Permalink
chore(health): update storage check
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed Jan 21, 2025
1 parent 2c040ec commit 4d9104c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
20 changes: 14 additions & 6 deletions src/GZCTF/Services/HealthCheck/StorageHealthCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,29 @@ namespace GZCTF.Services.HealthCheck;

public class StorageHealthCheck(IBlobStorage blobStorage) : IHealthCheck
{
static readonly string Filename = $"{nameof(StorageHealthCheck)}_{Guid.CreateVersion7():N}";

public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context,
CancellationToken cancellationToken = default)
{
try
{
var time = DateTime.UtcNow;
const string filename = "GZCTF_HealthCheck";
string random = Guid.NewGuid().ToString();
await blobStorage.WriteTextAsync(filename, random, cancellationToken: cancellationToken);
await blobStorage.WriteTextAsync(Filename, random, cancellationToken: cancellationToken);

if (!await blobStorage.ExistsAsync(filename, cancellationToken: cancellationToken) ||
await blobStorage.ReadTextAsync(filename, cancellationToken: cancellationToken) != random)
return HealthCheckResult.Unhealthy();
try
{
if (!await blobStorage.ExistsAsync(Filename, cancellationToken: cancellationToken) ||
await blobStorage.ReadTextAsync(Filename, cancellationToken: cancellationToken) != random)
return HealthCheckResult.Unhealthy();
}
finally
{
await blobStorage.DeleteAsync(Filename, cancellationToken: cancellationToken);
}

return DateTime.UtcNow - time > TimeSpan.FromSeconds(10)
return DateTime.UtcNow - time > TimeSpan.FromSeconds(5)
? HealthCheckResult.Degraded()
: HealthCheckResult.Healthy();
}
Expand Down
40 changes: 21 additions & 19 deletions src/GZCTF/Utils/RecordableNetworkStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ public class RecordableNetworkStreamOptions
/// </summary>
public sealed class RecordableNetworkStream : NetworkStream
{
static readonly PhysicalAddress DummyPhysicalAddress = PhysicalAddress.Parse("00-11-00-11-00-11");
static readonly IPEndPoint Host = new(0, 65535);

readonly CaptureFileWriterDevice? _device;
readonly IBlobStorage? _storage;
readonly string _tempFile = string.Empty;
readonly PhysicalAddress _dummyPhysicalAddress = PhysicalAddress.Parse("00-11-00-11-00-11");
readonly IPEndPoint _host = new(0, 65535);
readonly RecordableNetworkStreamOptions _options;

bool _disposed;
Expand All @@ -66,7 +67,7 @@ public RecordableNetworkStream(Socket socket, byte[]? metadata, IBlobStorage sto
_device.Open();

if (metadata is not null)
WriteCapturedData(_host, _options.Source, metadata);
WriteCapturedData(Host, _options.Source, metadata);
}

public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
Expand All @@ -90,19 +91,19 @@ public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationTo
}

/// <summary>
/// 向文件写入一条流量记录
/// Write the captured data to the file
/// </summary>
/// <param name="source">源地址</param>
/// <param name="dest">目的地址</param>
/// <param name="buffer">数据</param>
/// <param name="source">Source address</param>
/// <param name="dest">Destination address</param>
/// <param name="buffer">Data buffer</param>
void WriteCapturedData(IPEndPoint source, IPEndPoint dest, ReadOnlyMemory<byte> buffer)
{
var udp = new UdpPacket((ushort)source.Port, (ushort)dest.Port)
{
PayloadDataSegment = new ByteArraySegment(buffer.ToArray())
};

var packet = new EthernetPacket(_dummyPhysicalAddress, _dummyPhysicalAddress, EthernetType.IPv6)
var packet = new EthernetPacket(DummyPhysicalAddress, DummyPhysicalAddress, EthernetType.IPv6)
{
PayloadPacket = new IPv6Packet(source.Address, dest.Address) { PayloadPacket = udp }
};
Expand All @@ -114,21 +115,22 @@ void WriteCapturedData(IPEndPoint source, IPEndPoint dest, ReadOnlyMemory<byte>

public override async ValueTask DisposeAsync()
{
if (!_disposed)
{
_device?.Close();
_device?.Dispose();
if (_disposed)
return;

// move temp file to storage with specified path
if (_options.EnableCapture && !string.IsNullOrEmpty(_options.BlobPath) && _storage is not null)
{
await _storage.WriteFileAsync(_options.BlobPath, _tempFile);
File.Delete(_tempFile);
}
_device?.Close();
_device?.Dispose();

await base.DisposeAsync();
// move temp file to storage with specified path
if (_options.EnableCapture && !string.IsNullOrEmpty(_options.BlobPath) && _storage is not null)
{
await _storage.WriteFileAsync(_options.BlobPath, _tempFile);
File.Delete(_tempFile);
}

await base.DisposeAsync();
GC.SuppressFinalize(this);

_disposed = true;
}
}

0 comments on commit 4d9104c

Please sign in to comment.