Skip to content

Commit

Permalink
Moved messages to convention based.
Browse files Browse the repository at this point in the history
  • Loading branch information
NTDLS committed Jun 12, 2024
1 parent e501288 commit 2c1919c
Show file tree
Hide file tree
Showing 7 changed files with 412 additions and 347 deletions.
354 changes: 27 additions & 327 deletions NetProxy.Service/NpServiceManager.cs

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions NetProxy.Service/NpServiceManagerMessageHandlerBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using NTDLS.NullExtensions;
using NTDLS.ReliableMessaging;

namespace NetProxy.Service
{
internal class NpServiceManagerMessageHandlerBase
{
public NpServiceManager EnforceLoginAndGetServiceManager(RmContext context)
{
var serviceManager = (context.Endpoint.Parameter as NpServiceManager).EnsureNotNull();
if (serviceManager.AuthenticatedConnections.Contains(context.ConnectionId) == false)
{
throw new Exception("Login has not been completed.");
}
return serviceManager;
}

}
}
166 changes: 166 additions & 0 deletions NetProxy.Service/NpServiceManagerNotificationHandlers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
using NetProxy.Library.Payloads.ReliableMessages.Notifications;
using NetProxy.Library.Utilities;
using NetProxy.Service.Proxy;
using NTDLS.NullExtensions;
using NTDLS.ReliableMessaging;

namespace NetProxy.Service
{
internal class NpServiceManagerNotificationHandlers : NpServiceManagerMessageHandlerBase, IRmMessageHandler
{
public void OnNotificationRegisterLogin(RmContext context, NotificationRegisterLogin notification)
{
var serviceManager = (context.Endpoint.Parameter as NpServiceManager).EnsureNotNull();

try
{
lock (serviceManager.Configuration.EnsureNotNull())
{
if (serviceManager.Configuration.Users.Collection.Where(o =>
o.UserName.ToLower() == notification.UserName.ToLower() && o.PasswordHash.ToLower() == notification.PasswordHash.ToLower()).Any())
{
serviceManager.AuthenticatedConnections.Add(context.ConnectionId);

Singletons.Logging.Write(NpLogging.Severity.Verbose,
$"Registered connection: {context.ConnectionId}, User: {notification.UserName} (Logged in users {serviceManager.AuthenticatedConnections.Count}).");
}
else
{
Singletons.Logging.Write(NpLogging.Severity.Verbose,
$"Failed to register connection: {context.ConnectionId}, User: {notification.UserName} (Logged in users {serviceManager.AuthenticatedConnections.Count}).");
}
}
}
catch (Exception ex)
{
Singletons.Logging.Write("An error occurred while logging in.", ex);
}
}

public void OnNotificationPersistUserList(RmContext context, NotificationPersistUserList notification)
{
var serviceManager = EnforceLoginAndGetServiceManager(context);

try
{
lock (serviceManager.Configuration.EnsureNotNull())
{
serviceManager.Configuration.Users.Collection.Clear();

foreach (var user in notification.Collection)
{
serviceManager.Configuration.Users.Add(user);
}
serviceManager.SaveConfiguration();
}
}
catch (Exception ex)
{
Singletons.Logging.Write("Failed to save user list.", ex);
}

}

public void OnNotificationUpsertProxy(RmContext context, NotificationUpsertProxy notification)
{
var serviceManager = EnforceLoginAndGetServiceManager(context);

try
{

lock (serviceManager.Configuration.EnsureNotNull())
{
var existingProxy = serviceManager.GetProxyById(notification.ProxyConfiguration.Id);

if (existingProxy != null)
{
existingProxy.Stop();
serviceManager.RemoveProxy(existingProxy);
}

var newProxy = new NpProxy(notification.ProxyConfiguration);

serviceManager.AddProxy(newProxy);

serviceManager.SaveConfiguration();

if (newProxy.Configuration.AutoStart)
{
newProxy.Start();
}
}
}
catch (Exception ex)
{
Singletons.Logging.Write("Failed to upsert proxy.", ex);
}

}

public void OnNotificationDeleteProxy(RmContext context, NotificationDeleteProxy notification)
{
var serviceManager = EnforceLoginAndGetServiceManager(context);

try
{

lock (serviceManager.Configuration.EnsureNotNull())
{
var existingProxy = serviceManager.GetProxyById(notification.Id);

if (existingProxy != null)
{
existingProxy.Stop();
serviceManager.RemoveProxy(existingProxy);
}
serviceManager.SaveConfiguration();
}
}
catch (Exception ex)
{
Singletons.Logging.Write("Failed to delete proxy.", ex);
}
}

public void OnNotificationStopProxy(RmContext context, NotificationStopProxy notification)
{
var serviceManager = EnforceLoginAndGetServiceManager(context);

try
{
lock (serviceManager.Configuration.EnsureNotNull())
{
var existingProxy = serviceManager.GetProxyById(notification.Id);

existingProxy?.Stop();
}
}
catch (Exception ex)
{
Singletons.Logging.Write("Failed to stop proxy.", ex);
}
}

public void OnNotificationStartProxy(RmContext context, NotificationStartProxy notification)
{
var serviceManager = EnforceLoginAndGetServiceManager(context);

try
{
lock (serviceManager.Configuration.EnsureNotNull())
{
var existingProxy = serviceManager.GetProxyById(notification.Id);

if (existingProxy?.Start() != true)
{
serviceManager.Notify(context.ConnectionId, new NotificationMessage("Failed to start proxy."));
}
}
}
catch (Exception ex)
{
Singletons.Logging.Write("Failed to start proxy.", ex);
}
}
}
}
175 changes: 175 additions & 0 deletions NetProxy.Service/NpServiceManagerQueryHandlers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
using NetProxy.Library.Payloads;
using NetProxy.Library.Payloads.ReliableMessages.Queries;
using NetProxy.Library.Utilities;
using NTDLS.NullExtensions;
using NTDLS.ReliableMessaging;

namespace NetProxy.Service
{
internal class NpServiceManagerQueryHandlers : NpServiceManagerMessageHandlerBase, IRmMessageHandler
{
public QueryLoginReply OnQueryLogin(RmContext context, QueryLogin query)
{
var serviceManager = (context.Endpoint.Parameter as NpServiceManager).EnsureNotNull();

var reply = new QueryLoginReply();

try
{
lock (serviceManager.Configuration.EnsureNotNull())
{
if (serviceManager.Configuration.Users.Collection.Where(o =>
o.UserName.Equals(query.UserName, StringComparison.CurrentCultureIgnoreCase)
&& o.PasswordHash.Equals(query.PasswordHash, StringComparison.CurrentCultureIgnoreCase)).Any())
{
serviceManager.AuthenticatedConnections.Add(context.ConnectionId);
Singletons.Logging.Write(NpLogging.Severity.Verbose,
$"Logged in connection: {context.ConnectionId}, User: {query.UserName} (Logged in users {serviceManager.AuthenticatedConnections.Count}).");
}
else
{
Singletons.Logging.Write(NpLogging.Severity.Verbose,
$"Failed login connection: {context.ConnectionId}, User: {query.UserName} (Logged in users {serviceManager.AuthenticatedConnections.Count}).");
}

reply.Result = true;
}
}
catch (Exception ex)
{
Singletons.Logging.Write("An error occurred while logging in.", ex);
reply.Message = ex.Message;
}

return reply;
}

public QueryProxyConfigurationListReply OnQueryProxyConfigurationList(RmContext context, QueryProxyConfigurationList query)
{
var serviceManager = EnforceLoginAndGetServiceManager(context);

var reply = new QueryProxyConfigurationListReply();

try
{
lock (serviceManager.Configuration.EnsureNotNull())
{
foreach (var proxy in serviceManager.GetProxies())
{
var augmentedProxy = new NpProxyGridItem()
{
Id = proxy.Configuration.Id,
Name = proxy.Configuration.Name,
TrafficType = proxy.Configuration.TrafficType,
ProxyType = proxy.Configuration.TrafficType.ToString() + " / " + proxy.Configuration.BindingProtocol.ToString(),
BindingProtocol = proxy.Configuration.BindingProtocol,
Description = proxy.Configuration.Description,
IsRunning = proxy.IsRunning,
ListenPort = proxy.Configuration.ListenPort,
ListenOnAllAddresses = proxy.Configuration.ListenOnAllAddresses,
Bindings = proxy.Configuration.Bindings
};

reply.Collection.Add(augmentedProxy);
}
}

return reply;
}
catch (Exception ex)
{
Singletons.Logging.Write("Failed to get proxy list.", ex);
reply.Message = ex.Message;
}

return reply;
}

public QueryProxyStatisticsReply OnQueryProxyStatistics(RmContext context, QueryProxyStatistics query)
{
var serviceManager = EnforceLoginAndGetServiceManager(context);

var reply = new QueryProxyStatisticsReply();

try
{
lock (serviceManager.Configuration.EnsureNotNull())
{
foreach (var proxy in serviceManager.GetProxies())
{
proxy.Statistics.Use((o) =>
{
var statistics = new NpProxyGridStats()
{
Id = proxy.Configuration.Id,
IsRunning = proxy.IsRunning,
BytesRead = o.BytesRead,
BytesWritten = o.BytesWritten,
TotalConnections = o.TotalConnections,
CurrentConnections = o.CurrentConnections
};
reply.Collection.Add(statistics);
});
}
}

}
catch (Exception ex)
{
Singletons.Logging.Write("Failed to get proxy stats list.", ex);
reply.Message = ex.Message;
}

return reply;
}

public QueryProxyConfigurationReply OnQueryProxyConfiguration(RmContext context, QueryProxyConfiguration query)
{
var serviceManager = EnforceLoginAndGetServiceManager(context);

var reply = new QueryProxyConfigurationReply();

try
{
lock (serviceManager.Configuration.EnsureNotNull())
{
var proxy = serviceManager.GetProxyById(query.Id);
if (proxy != null)
{
reply.ProxyConfiguration = proxy.Configuration;
}
}
}
catch (Exception ex)
{
Singletons.Logging.Write("Failed to get proxy.", ex);
reply.Message = ex.Message;
}

return reply;
}

public QueryUserListReply OnQueryUserList(RmContext context, QueryUserList query)
{
var serviceManager = EnforceLoginAndGetServiceManager(context);

var reply = new QueryUserListReply();

try
{
lock (serviceManager.Configuration.EnsureNotNull())
{
reply.Collection = serviceManager.Configuration.Users.Collection;
}
}
catch (Exception ex)
{
Singletons.Logging.Write("Failed to get user list.", ex);
reply.Message = ex.Message;
}

return reply;
}
}
}
6 changes: 0 additions & 6 deletions NetProxy.Service/Proxy/NpProxyCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ public List<NpProxyConfiguration> CloneConfigurations()
return proxyConfigurations;
}

public NpProxy? this[Guid proxyId]
=> this.Where(o => o.Configuration.Id == proxyId).FirstOrDefault();

//public void Add(NpProxy proxy)
// => this.Add(proxy);

public new void Remove(NpProxy item)
{
NpUtility.TryAndIgnore(item.Stop);
Expand Down
Loading

0 comments on commit 2c1919c

Please sign in to comment.