Skip to content

Commit

Permalink
Code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
NTDLS committed Nov 4, 2024
1 parent 2554986 commit 6d9a127
Show file tree
Hide file tree
Showing 31 changed files with 146 additions and 255 deletions.
26 changes: 25 additions & 1 deletion NetTunnel.Library/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
namespace NetTunnel.Library
using NetTunnel.Library.Payloads;

namespace NetTunnel.Library
{
public static class Constants
{
public const string FriendlyName = "NetTunnel";
public const string EventSourceName = "NetTunnel";

public enum NtHTTPHeaderResult
{
WaitOnData,
Present,
NotPresent
}

public enum NtEdgeStatus
{
Normal, //This is a persistent connection.
New, //This is a new connection.
Expire //The connection is gone.
}

public class NtEdgeState
{
public NtEdgeStatus Status { get; set; }
public DirectionalKey TunnelKey { get; set; } = new();
public DirectionalKey EndpointKey { get; set; } = new();
public Guid EdgeId { get; set; }
}

public enum NtTrafficType
{
Raw,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class UOSQueryRequestKeyExchange : IRmQuery<UOSQueryReplyKeyExchangeReply

public UOSQueryRequestKeyExchange()
{
NegotiationToken = new byte[0];
NegotiationToken = Array.Empty<byte>();
}

public UOSQueryRequestKeyExchange(byte[] negotiationToken)
Expand All @@ -24,7 +24,7 @@ public class UOSQueryReplyKeyExchangeReply : IRmQueryReply

public UOSQueryReplyKeyExchangeReply()
{
NegotiationToken = new byte[0];
NegotiationToken = Array.Empty<byte>();
}

public UOSQueryReplyKeyExchangeReply(Guid connectionId, byte[] negotiationToken)
Expand Down
28 changes: 12 additions & 16 deletions NetTunnel.Library/ServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,27 @@ namespace NetTunnel.Library
/// </summary>
public class ServiceClient
{
private readonly ILogger _logger;
private readonly int _port;
private readonly ServiceConfiguration _configuration;
private readonly string _address;
private readonly int _port;
private readonly string _userName;
private readonly string _passwordHash;
private readonly string _userName;

public NtUserRole Role { get; private set; } = NtUserRole.Undefined;

public string Address { get { return _address; } }
public bool IsLoggedIn { get; private set; } = false;
public delegate void ConnectedEvent(RmContext context);
public delegate void DisconnectedEvent(RmContext context);
public delegate void ExceptionEvent(RmContext? context, Exception ex, IRmPayload? payload);
public event ConnectedEvent? OnConnected;
public event DisconnectedEvent? OnDisconnected;
public event ExceptionEvent? OnException;
/// <summary>
/// The id of the service that we are logged into.
/// </summary>
public Guid ServiceId { get; private set; }
public NtUserRole Role { get; private set; } = NtUserRole.Undefined;
public RmClient Client { get; private set; }
public bool IsLoggedIn { get; private set; } = false;
public ILogger _logger;

public event ExceptionEvent? OnException;
public delegate void ExceptionEvent(RmContext? context, Exception ex, IRmPayload? payload);

public event ConnectedEvent? OnConnected;
public delegate void ConnectedEvent(RmContext context);

public event DisconnectedEvent? OnDisconnected;
public delegate void DisconnectedEvent(RmContext context);
public string Address { get { return _address; } }

public ServiceClient(ILogger logger, ServiceConfiguration configuration, RmClient client, string address, int port, string userName, string passwordHash)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ internal class ServiceNotificationHandlers : ServiceHandlerBase, IRmMessageHandl
/// The remote service is letting us know that they are about to start using the cryptography provider,
/// so we need to apply the one that we have ready on this end.
/// </summary>
/// <param name="context"></param>
/// <param name="notification"></param>
public void OnNotify(RmContext context, UOSNotificationApplyCryptography notification)
{
try
Expand Down Expand Up @@ -57,8 +55,6 @@ public void OnNotify(RmContext context, S2SNotificationEndpointConnect notificat
/// The remote service is letting us know that edge data has been received on an endpoint and is giving
/// it to the local service to that it can be delivered to the associated endpoint edge connection.
/// </summary>
/// <param name="context"></param>
/// <param name="notification"></param>
public void OnNotify(RmContext context, S2SNotificationEndpointDataExchange notification)
{
try
Expand All @@ -78,8 +74,6 @@ public void OnNotify(RmContext context, S2SNotificationEndpointDataExchange noti
/// <summary>
/// The remote service is letting us know that it is deleting the given tunnel.
/// </summary>
/// <param name="context"></param>
/// <param name="notification"></param>
public void OnNotify(RmContext context, S2SNotificationTunnelDeletion notification)
{
try
Expand All @@ -98,8 +92,6 @@ public void OnNotify(RmContext context, S2SNotificationTunnelDeletion notificati
/// <summary>
/// The remote service is letting us know that it is deleting the given endpoint.
/// </summary>
/// <param name="context"></param>
/// <param name="notification"></param>
public void OnNotify(RmContext context, S2SNotificationEndpointDeletion notification)
{
try
Expand All @@ -118,8 +110,6 @@ public void OnNotify(RmContext context, S2SNotificationEndpointDeletion notifica
/// <summary>
/// The remote service is letting us know that an edge connection has been terminated to the given endpoint.
/// </summary>
/// <param name="context"></param>
/// <param name="notification"></param>
public void OnNotify(RmContext context, S2SNotificationEndpointDisconnect notification)
{
try
Expand All @@ -138,8 +128,6 @@ public void OnNotify(RmContext context, S2SNotificationEndpointDisconnect notifi
/// <summary>
/// The UI is requesting that the local service disconnect the given endpoint edge connection.
/// </summary>
/// <param name="context"></param>
/// <param name="notification"></param>
public void OnNotify(RmContext context, UINotifyTerminateEndpointEdgeConnection notification)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ internal class ServiceQueryHandlersForServiceToService : ServiceHandlerBase, IRm
/// <summary>
/// The remote service is requesting that this service respond to a ping request.
/// </summary>
/// <param name="context"></param>
/// <param name="query"></param>
/// <returns></returns>
public S2SQueryPingReply OnQuery(RmContext context, S2SQueryPing query)
{
try
Expand All @@ -44,9 +41,6 @@ public S2SQueryPingReply OnQuery(RmContext context, S2SQueryPing query)
/// The remote outbound service is asking this service to register the given tunnel.
/// The service will connect the tunnel and send back the endpoints for the logged in user.
/// </summary>
/// <param name="context"></param>
/// <param name="query"></param>
/// <returns></returns>
public S2SQueryRegisterTunnelReply OnQuery(RmContext context, S2SQueryRegisterTunnel query)
{
try
Expand Down Expand Up @@ -94,9 +88,6 @@ public S2SQueryRegisterTunnelReply OnQuery(RmContext context, S2SQueryRegisterTu
/// <summary>
/// A new endpoint has been added to the remote service, so that remote service is asking this service to add the endpoint locally too.
/// </summary>
/// <param name="context"></param>
/// <param name="query"></param>
/// <returns></returns>
public S2SQueryUpsertEndpointReply OnQuery(RmContext context, S2SQueryUpsertEndpoint query)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ internal class ServiceQueryHandlersForServiceToServiceOrUI : ServiceHandlerBase,
/// sends the NotificationApplyCryptography notification. This is because if we apply the cryptography now, then
/// the reply will be encrypted before the remote service has the data it needs to decrypt it.
/// </summary>
/// <param name="context"></param>
/// <param name="query"></param>
/// <returns></returns>
public UOSQueryReplyKeyExchangeReply OnQuery(RmContext context, UOSQueryRequestKeyExchange query)
{
try
Expand All @@ -48,9 +45,6 @@ public UOSQueryReplyKeyExchangeReply OnQuery(RmContext context, UOSQueryRequestK
/// <summary>
/// A UI client or remote service is requesting to login.
/// </summary>
/// <param name="context"></param>
/// <param name="query"></param>
/// <returns></returns>
public UOSQueryLoginReply OnQuery(RmContext context, UOSQueryLogin query)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ internal class ServiceQueryHandlersForUI : ServiceHandlerBase, IRmMessageHandler
/// <summary>
/// A UI client is requesting a list of tunnels.
/// </summary>
/// <param name="context"></param>
/// <param name="query"></param>
/// <returns></returns>
public UIQueryGetTunnelsReply UIQueryGetTunnels(RmContext context, UIQueryGetTunnels query)
{
try
Expand All @@ -40,9 +37,6 @@ public UIQueryGetTunnelsReply UIQueryGetTunnels(RmContext context, UIQueryGetTun
/// <summary>
/// A user is asking the service to create a tunnel with the given configuration.
/// </summary>
/// <param name="context"></param>
/// <param name="query"></param>
/// <returns></returns>
public UIQueryCreateTunnelReply UIQueryCreateTunnel(RmContext context, UIQueryCreateTunnel query)
{
try
Expand Down Expand Up @@ -70,9 +64,6 @@ public UIQueryCreateTunnelReply UIQueryCreateTunnel(RmContext context, UIQueryCr
/// A user is asking that a new endpoint be added to the local tunnel and also sent through the connected tunnel
/// to the other service on the other end so that it can also add associated endpoint to its tunnel.
/// </summary>
/// <param name="context"></param>
/// <param name="query"></param>
/// <returns></returns>
public UIQueryDistributeUpsertEndpointReply UIQueryDistributeUpsertEndpoint(RmContext context, UIQueryDistributeUpsertEndpoint query)
{
try
Expand All @@ -99,9 +90,6 @@ public UIQueryDistributeUpsertEndpointReply UIQueryDistributeUpsertEndpoint(RmCo
/// <summary>
/// A user is asking the service to upsert an endpoint associated with a user.
/// </summary>
/// <param name="context"></param>
/// <param name="query"></param>
/// <returns></returns>
public UIQueryUpsertUserEndpointReply UIQueryUpsertUserEndpoint(RmContext context, UIQueryUpsertUserEndpoint query)
{
try
Expand All @@ -128,9 +116,6 @@ public UIQueryUpsertUserEndpointReply UIQueryUpsertUserEndpoint(RmContext contex
/// <summary>
/// The UI is requesting general tunnel statistics.
/// </summary>
/// <param name="context"></param>
/// <param name="query"></param>
/// <returns></returns>
public UIQueryGetTunnelStatisticsReply UIQueryGetTunnelStatistics(RmContext context, UIQueryGetTunnelStatistics query)
{
try
Expand All @@ -154,9 +139,6 @@ public UIQueryGetTunnelStatisticsReply UIQueryGetTunnelStatistics(RmContext cont
/// <summary>
/// The UI is requesting that a tunnel be disconnected, but not deleted from the associated remote service.
/// </summary>
/// <param name="context"></param>
/// <param name="query"></param>
/// <returns></returns>
public UIQueryDisconnectTunnelReply UIQueryDisconnectTunnel(RmContext context, UIQueryDisconnectTunnel query)
{
try
Expand All @@ -180,9 +162,6 @@ public UIQueryDisconnectTunnelReply UIQueryDisconnectTunnel(RmContext context, U
/// <summary>
/// The UI is requesting that a tunnel be deleted.
/// </summary>
/// <param name="context"></param>
/// <param name="query"></param>
/// <returns></returns>
public UIQueryDeleteTunnelReply UIQueryDeleteTunnel(RmContext context, UIQueryDeleteTunnel query)
{
try
Expand Down Expand Up @@ -210,9 +189,6 @@ public UIQueryDeleteTunnelReply UIQueryDeleteTunnel(RmContext context, UIQueryDe
/// <summary>
/// The UI is requesting a list of all users.
/// </summary>
/// <param name="context"></param>
/// <param name="query"></param>
/// <returns></returns>
public UIQueryGetUsersReply UIQueryGetUsers(RmContext context, UIQueryGetUsers query)
{
try
Expand Down Expand Up @@ -240,9 +216,6 @@ public UIQueryGetUsersReply UIQueryGetUsers(RmContext context, UIQueryGetUsers q
/// <summary>
/// The UI is requesting to delete a given user.
/// </summary>
/// <param name="context"></param>
/// <param name="query"></param>
/// <returns></returns>
public UIQueryDeleteUserReply UIQueryDeleteUser(RmContext context, UIQueryDeleteUser query)
{
try
Expand All @@ -269,9 +242,6 @@ public UIQueryDeleteUserReply UIQueryDeleteUser(RmContext context, UIQueryDelete
/// <summary>
/// The UI is requesting that a user be altered with the given information (password, role, etc.)
/// </summary>
/// <param name="context"></param>
/// <param name="query"></param>
/// <returns></returns>
public UIQueryEditUserReply UIQueryEditUser(RmContext context, UIQueryEditUser query)
{
Singletons.Logger.Verbose(NTDLS.Helpers.Text.SeperateCamelCase(MethodBase.GetCurrentMethod()?.Name ?? string.Empty));
Expand All @@ -298,9 +268,6 @@ public UIQueryEditUserReply UIQueryEditUser(RmContext context, UIQueryEditUser q
/// <summary>
/// The UI is requesting that a user be created with the given information.
/// </summary>
/// <param name="context"></param>
/// <param name="query"></param>
/// <returns></returns>
public UIQueryCreateUserReply UIQueryCreateUser(RmContext context, UIQueryCreateUser query)
{
Singletons.Logger.Verbose(NTDLS.Helpers.Text.SeperateCamelCase(MethodBase.GetCurrentMethod()?.Name ?? string.Empty));
Expand All @@ -327,9 +294,6 @@ public UIQueryCreateUserReply UIQueryCreateUser(RmContext context, UIQueryCreate
/// <summary>
/// The UI is requesting the local service configuration.
/// </summary>
/// <param name="context"></param>
/// <param name="query"></param>
/// <returns></returns>
public UIQueryGetServiceConfigurationReply UIQueryGetServiceConfiguration(RmContext context, UIQueryGetServiceConfiguration query)
{
Singletons.Logger.Verbose(NTDLS.Helpers.Text.SeperateCamelCase(MethodBase.GetCurrentMethod()?.Name ?? string.Empty));
Expand Down Expand Up @@ -357,9 +321,6 @@ public UIQueryGetServiceConfigurationReply UIQueryGetServiceConfiguration(RmCont
/// <summary>
/// The UI is requesting that the local service configuration be changed.
/// </summary>
/// <param name="context"></param>
/// <param name="query"></param>
/// <returns></returns>
public UIQueryPutServiceConfigurationReply UIQueryPutServiceConfiguration(RmContext context, UIQueryPutServiceConfiguration query)
{
Singletons.Logger.Verbose(NTDLS.Helpers.Text.SeperateCamelCase(MethodBase.GetCurrentMethod()?.Name ?? string.Empty));
Expand Down Expand Up @@ -387,9 +348,6 @@ public UIQueryPutServiceConfigurationReply UIQueryPutServiceConfiguration(RmCont
/// The UI is requesting that an endpoint be deleted and that we let the
/// remote service connected to the tunnel know so it can do the same.
/// </summary>
/// <param name="context"></param>
/// <param name="query"></param>
/// <returns></returns>
public UIQueryDeleteEndpointReply UIQueryDeleteEndpoint(RmContext context, UIQueryDeleteEndpoint query)
{
Singletons.Logger.Verbose(NTDLS.Helpers.Text.SeperateCamelCase(MethodBase.GetCurrentMethod()?.Name ?? string.Empty));
Expand All @@ -416,9 +374,6 @@ public UIQueryDeleteEndpointReply UIQueryDeleteEndpoint(RmContext context, UIQue
/// <summary>
/// The UI is requesting that a tunnel be started.
/// </summary>
/// <param name="context"></param>
/// <param name="query"></param>
/// <returns></returns>
public UIQueryStartTunnelReply UIQueryStartTunnel(RmContext context, UIQueryStartTunnel query)
{
Singletons.Logger.Verbose(NTDLS.Helpers.Text.SeperateCamelCase(MethodBase.GetCurrentMethod()?.Name ?? string.Empty));
Expand All @@ -440,9 +395,6 @@ public UIQueryStartTunnelReply UIQueryStartTunnel(RmContext context, UIQueryStar
/// <summary>
/// The UI is requesting that a tunnel be stopped.
/// </summary>
/// <param name="context"></param>
/// <param name="query"></param>
/// <returns></returns>
public UIQueryStopTunnelReply UIQueryStopTunnel(RmContext context, UIQueryStopTunnel query)
{
Singletons.Logger.Verbose(NTDLS.Helpers.Text.SeperateCamelCase(MethodBase.GetCurrentMethod()?.Name ?? string.Empty));
Expand All @@ -464,9 +416,6 @@ public UIQueryStopTunnelReply UIQueryStopTunnel(RmContext context, UIQueryStopTu
/// <summary>
/// The UI is requesting general tunnel properties.
/// </summary>
/// <param name="context"></param>
/// <param name="query"></param>
/// <returns></returns>
public UIQueryGetTunnelPropertiesReply UIQueryGetTunnelProperties(RmContext context, UIQueryGetTunnelProperties query)
{
Singletons.Logger.Debug(NTDLS.Helpers.Text.SeperateCamelCase(MethodBase.GetCurrentMethod()?.Name ?? string.Empty));
Expand All @@ -490,9 +439,6 @@ public UIQueryGetTunnelPropertiesReply UIQueryGetTunnelProperties(RmContext cont
/// <summary>
/// The UI is requesting general endpoint properties.
/// </summary>
/// <param name="context"></param>
/// <param name="query"></param>
/// <returns></returns>
public UIQueryGetEndpointPropertiesReply UIQueryGetEndpointProperties(RmContext context, UIQueryGetEndpointProperties query)
{
Singletons.Logger.Debug(NTDLS.Helpers.Text.SeperateCamelCase(MethodBase.GetCurrentMethod()?.Name ?? string.Empty));
Expand All @@ -516,9 +462,6 @@ public UIQueryGetEndpointPropertiesReply UIQueryGetEndpointProperties(RmContext
/// <summary>
/// The UI is requesting a list of edge connections to a given endpoint.
/// </summary>
/// <param name="context"></param>
/// <param name="query"></param>
/// <returns></returns>
public UIQueryGetEndpointEdgeConnectionsReply UIQueryGetEndpointEdgeConnections(RmContext context, UIQueryGetEndpointEdgeConnections query)
{
Singletons.Logger.Debug(NTDLS.Helpers.Text.SeperateCamelCase(MethodBase.GetCurrentMethod()?.Name ?? string.Empty));
Expand Down
Loading

0 comments on commit 6d9a127

Please sign in to comment.