-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
412 additions
and
347 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
166
NetProxy.Service/NpServiceManagerNotificationHandlers.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.