Skip to content

Commit

Permalink
feat(repeater): refrain from utilizing non standard ports
Browse files Browse the repository at this point in the history
closes #165
  • Loading branch information
derevnjuk committed Oct 3, 2023
1 parent 8573f3a commit 7354585
Show file tree
Hide file tree
Showing 41 changed files with 1,163 additions and 523 deletions.
3 changes: 1 addition & 2 deletions src/SecTester.Repeater/Api/RepeaterIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace SecTester.Repeater.Api;

internal record RepeaterIdentity(string Id, string Name)
internal record RepeaterIdentity(string Id)
{
public string Id { get; } = Id ?? throw new ArgumentNullException(nameof(Id));
public string Name { get; } = Name ?? throw new ArgumentNullException(nameof(Name));
}
58 changes: 58 additions & 0 deletions src/SecTester.Repeater/Bus/DefaultRepeaterBusFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SecTester.Core;
using SecTester.Core.Utils;
using SocketIO.Serializer.MessagePack;
using SocketIOClient;

namespace SecTester.Repeater.Bus;

public class DefaultRepeaterBusFactory : IRepeaterBusFactory
{
private readonly Configuration _config;
private readonly ILoggerFactory _loggerFactory;
private readonly IServiceScopeFactory _scopeFactory;

public DefaultRepeaterBusFactory(Configuration config, ILoggerFactory loggerFactory, IServiceScopeFactory scopeFactory)
{
_config = config ?? throw new ArgumentNullException(nameof(config));
_loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
_scopeFactory = scopeFactory ?? throw new ArgumentNullException(nameof(scopeFactory));
}

public IRepeaterBus Create(string repeaterId)
{
if (_config.Credentials is null)
{
throw new InvalidOperationException(
"Please provide credentials to establish a connection with the bus."
);
}

var url = new Uri(_config.Api);
var options = new SocketIOOptions
{
Path = "/api/ws/v1",
ReconnectionAttempts = 20,
ReconnectionDelayMax = 86400000,
ConnectionTimeout = TimeSpan.FromSeconds(10),
Auth = new List<KeyValuePair<string, string>>
{
new("token", _config.Credentials.Token), new("domain", repeaterId)
},
};

var client = new SocketIOClient.SocketIO(url, options)
{
Serializer = new SocketIOMessagePackSerializer()
};
var wrapper = new SocketIoClientWrapper(client);

var scope = _scopeFactory.CreateAsyncScope();
var timerProvider = scope.ServiceProvider.GetRequiredService<ITimerProvider>();

return new SocketIoRepeaterBus(url, wrapper, timerProvider, _loggerFactory.CreateLogger<IRepeaterBus>());
}
}
38 changes: 0 additions & 38 deletions src/SecTester.Repeater/Bus/DefaultRepeaterEventBusFactory.cs

This file was deleted.

15 changes: 15 additions & 0 deletions src/SecTester.Repeater/Bus/IRepeaterBus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace SecTester.Repeater.Bus;

public interface IRepeaterBus : IAsyncDisposable
{
event Func<IncomingRequest, Task<OutgoingResponse>> RequestReceived;
event Action<Exception> ErrorOccurred;
event Action<Version> UpgradeAvailable;

Task Connect();
Task Deploy(string repeaterId, Runtime? runtime = null, CancellationToken? cancellationToken = null);
}
6 changes: 6 additions & 0 deletions src/SecTester.Repeater/Bus/IRepeaterBusFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace SecTester.Repeater.Bus;

public interface IRepeaterBusFactory
{
IRepeaterBus Create(string repeaterId);
}
8 changes: 0 additions & 8 deletions src/SecTester.Repeater/Bus/IRepeaterEventBusFactory.cs

This file was deleted.

14 changes: 14 additions & 0 deletions src/SecTester.Repeater/Bus/ISocketIoClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Threading.Tasks;

namespace SecTester.Repeater.Bus;

public interface ISocketIoClient : IDisposable
{
public bool Connected { get; }
public Task Connect();
public Task Disconnect();
public void On(string eventName, Action<ISocketIoResponse> callback);
public void Off(string eventName);
public Task EmitAsync(string eventName, params object[] data);
}
11 changes: 11 additions & 0 deletions src/SecTester.Repeater/Bus/ISocketIoResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Threading;
using System.Threading.Tasks;

namespace SecTester.Repeater.Bus;

public interface ISocketIoResponse
{
public T GetValue<T>(int i = 0);
public Task CallbackAsync(params object[] data);
public Task CallbackAsync(CancellationToken cancellationToken, params object[] data);
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using SecTester.Core.Bus;
using SecTester.Repeater.Runners;

namespace SecTester.Repeater.Bus;

[MessageType(name: "ExecuteScript")]
public record RequestExecutingEvent(Uri Url) : Event, IRequest
public record IncomingRequest(Uri Url) : Event, IRequest
{
public string? Body { get; init; }
[JsonPropertyName("correlation_id_regex")]
public Regex? CorrelationIdRegex { get; init; }
public HttpMethod Method { get; init; } = HttpMethod.Get;
public Protocol Protocol { get; init; } = Protocol.Http;
public Uri Url { get; init; } = Url ?? throw new ArgumentNullException(nameof(Url));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;
using SecTester.Repeater.Runners;

namespace SecTester.Repeater.Bus;

public record RequestExecutingResult : IResponse
public record OutgoingResponse : IResponse
{
[JsonPropertyName("status_code")] public int? StatusCode { get; init; }
public int? StatusCode { get; init; }

public string? Body { get; init; }
public string? Message { get; init; }

[JsonPropertyName("error_code")] public string? ErrorCode { get; init; }
public string? ErrorCode { get; init; }

public Protocol Protocol { get; init; } = Protocol.Http;

Expand Down
6 changes: 0 additions & 6 deletions src/SecTester.Repeater/Bus/RegisterRepeaterCommand.cs

This file was deleted.

3 changes: 0 additions & 3 deletions src/SecTester.Repeater/Bus/RegisterRepeaterPayload.cs

This file was deleted.

3 changes: 0 additions & 3 deletions src/SecTester.Repeater/Bus/RegisterRepeaterResult.cs

This file was deleted.

10 changes: 0 additions & 10 deletions src/SecTester.Repeater/Bus/RepeaterRegisteringError.cs

This file was deleted.

6 changes: 0 additions & 6 deletions src/SecTester.Repeater/Bus/RepeaterStatusEvent.cs

This file was deleted.

30 changes: 0 additions & 30 deletions src/SecTester.Repeater/Bus/RequestExecutingEventListener.cs

This file was deleted.

29 changes: 29 additions & 0 deletions src/SecTester.Repeater/Bus/SocketIoClientWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Threading.Tasks;

namespace SecTester.Repeater.Bus;

public class SocketIoClientWrapper : ISocketIoClient
{
private readonly SocketIOClient.SocketIO _socketIo;

public SocketIoClientWrapper(SocketIOClient.SocketIO socketIo) => _socketIo = socketIo ?? throw new ArgumentNullException(nameof(socketIo));

public void Dispose()
{
_socketIo.Dispose();
GC.SuppressFinalize(this);
}

public bool Connected => _socketIo.Connected;

public Task Connect() => _socketIo.ConnectAsync();

public Task Disconnect() => _socketIo.DisconnectAsync();

public void On(string eventName, Action<ISocketIoResponse> callback) => _socketIo.On(eventName, x => callback(x as ISocketIoResponse));

Check warning on line 24 in src/SecTester.Repeater/Bus/SocketIoClientWrapper.cs

View workflow job for this annotation

GitHub Actions / windows-2019

Possible null reference argument for parameter 'obj' in 'void Action<ISocketIoResponse>.Invoke(ISocketIoResponse obj)'.

public void Off(string eventName) => _socketIo.Off(eventName);

public Task EmitAsync(string eventName, params object[] data) => _socketIo.EmitAsync(eventName, data);
}
Loading

0 comments on commit 7354585

Please sign in to comment.