|
@@ -1,13 +1,14 @@
|
|
|
using H.Formatters;
|
|
|
using H.Pipes;
|
|
|
using H.Pipes.AccessControl;
|
|
|
+using H.Pipes.Args;
|
|
|
using InABox.API;
|
|
|
using InABox.Clients;
|
|
|
using InABox.Core;
|
|
|
using InABox.IPC.Shared;
|
|
|
-using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
|
-using Microsoft.CodeAnalysis.Operations;
|
|
|
+using InABox.Server.WebSocket;
|
|
|
using System;
|
|
|
+using System.Collections.Concurrent;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Diagnostics;
|
|
|
using System.IO.Pipes;
|
|
@@ -21,14 +22,116 @@ namespace Piping
|
|
|
{
|
|
|
using PipeResponse = PipeRequest;
|
|
|
|
|
|
+ delegate void PipePollEvent(PipeNotifyState.Session session);
|
|
|
+
|
|
|
+ class PipeNotifyState
|
|
|
+ {
|
|
|
+ public class Session
|
|
|
+ {
|
|
|
+ public PipeConnection<PipeResponse?> Connection { get; }
|
|
|
+
|
|
|
+ public Guid SessionID { get; }
|
|
|
+
|
|
|
+ public Platform Platform { get; }
|
|
|
+
|
|
|
+ public Session(PipeConnection<PipeResponse?> connection, Guid sessionID, Platform platform)
|
|
|
+ {
|
|
|
+ Connection = connection;
|
|
|
+ SessionID = sessionID;
|
|
|
+ Platform = platform;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public ConcurrentDictionary<Guid, Session> SessionMap = new();
|
|
|
+
|
|
|
+ public event PipePollEvent? OnPoll;
|
|
|
+
|
|
|
+ public void Poll(Session session)
|
|
|
+ {
|
|
|
+ OnPoll?.Invoke(session);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ class PipeIPCNotifier : Notifier
|
|
|
+ {
|
|
|
+ PipeNotifyState NotifyState { get; set; }
|
|
|
+
|
|
|
+ public PipeIPCNotifier(PipeNotifyState notifyState)
|
|
|
+ {
|
|
|
+ NotifyState = notifyState;
|
|
|
+ NotifyState.OnPoll += NotifyState_OnPoll;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void NotifyState_OnPoll(PipeNotifyState.Session session)
|
|
|
+ {
|
|
|
+ Notify.Poll(session.SessionID);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override IEnumerable<Guid> GetSessions(Platform platform)
|
|
|
+ {
|
|
|
+ return NotifyState.SessionMap.Where(x => x.Value.Platform == platform).Select(x => x.Key);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override IEnumerable<Guid> GetUserSessions(Guid userID)
|
|
|
+ {
|
|
|
+ return CredentialsCache.GetUserSessions(userID);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override void NotifyAll<TNotification>(TNotification notification)
|
|
|
+ {
|
|
|
+ foreach(var session in NotifyState.SessionMap.Values)
|
|
|
+ {
|
|
|
+ session.Connection.WriteAsync(PipeRequest.Notification(notification)).ContinueWith(task =>
|
|
|
+ {
|
|
|
+ if(task.Exception != null)
|
|
|
+ {
|
|
|
+ Logger.Send(LogType.Error, "", $"Error in notification: {CoreUtils.FormatException(task.Exception)}");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override void NotifySession<TNotification>(Guid sessionID, TNotification notification)
|
|
|
+ {
|
|
|
+ if(NotifyState.SessionMap.TryGetValue(sessionID, out var session))
|
|
|
+ {
|
|
|
+ session.Connection.WriteAsync(PipeRequest.Notification(notification)).ContinueWith(task =>
|
|
|
+ {
|
|
|
+ if (task.Exception != null)
|
|
|
+ {
|
|
|
+ Logger.Send(LogType.Error, "", $"Error in notification: {CoreUtils.FormatException(task.Exception)}");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override void NotifySession(Guid sessionID, Type TNotification, BaseObject notification)
|
|
|
+ {
|
|
|
+ if (NotifyState.SessionMap.TryGetValue(sessionID, out var session))
|
|
|
+ {
|
|
|
+ session.Connection.WriteAsync(PipeRequest.Notification(TNotification, notification)).ContinueWith(task =>
|
|
|
+ {
|
|
|
+ if (task.Exception != null)
|
|
|
+ {
|
|
|
+ Logger.Send(LogType.Error, "", $"Error in notification: {CoreUtils.FormatException(task.Exception)}");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public class PipeIPCServer : IDisposable
|
|
|
{
|
|
|
PipeServer<PipeRequest> Server;
|
|
|
|
|
|
+ PipeNotifyState NotifyState = new();
|
|
|
+
|
|
|
public PipeIPCServer(string name)
|
|
|
{
|
|
|
Server = new PipeServer<PipeRequest>(name);
|
|
|
|
|
|
+ Notify.AddNotifier(new PipeIPCNotifier(NotifyState));
|
|
|
+
|
|
|
var pipeSecurity = new PipeSecurity();
|
|
|
pipeSecurity.AddAccessRule(new PipeAccessRule(new SecurityIdentifier(WellKnownSidType.LocalSid, null), PipeAccessRights.ReadWrite, System.Security.AccessControl.AccessControlType.Allow));
|
|
|
pipeSecurity.AddAccessRule(new PipeAccessRule(new SecurityIdentifier(WellKnownSidType.LocalServiceSid, null), PipeAccessRights.ReadWrite, System.Security.AccessControl.AccessControlType.Allow));
|
|
@@ -91,76 +194,95 @@ namespace Piping
|
|
|
};
|
|
|
}
|
|
|
|
|
|
- private static PipeResponse QueryMultiple(PipeRequest request)
|
|
|
+ private class RequestData
|
|
|
+ {
|
|
|
+ public ConnectionMessageEventArgs<PipeRequest?> e { get; }
|
|
|
+
|
|
|
+ public RequestData(ConnectionMessageEventArgs<PipeResponse?> e)
|
|
|
+ {
|
|
|
+ this.e = e;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private PipeResponse QueryMultiple(PipeRequest request, RequestData data)
|
|
|
{
|
|
|
var response = RestService.QueryMultiple(request.GetRequest<MultiQueryRequest>(), true);
|
|
|
return request.Respond(response);
|
|
|
}
|
|
|
|
|
|
- private static PipeResponse Validate(PipeRequest request)
|
|
|
+ private PipeResponse Validate(PipeRequest request, RequestData data)
|
|
|
{
|
|
|
- var response = RestService.Validate(request.GetRequest<ValidateRequest>());
|
|
|
+ var validateRequest = request.GetRequest<ValidateRequest>();
|
|
|
+ var response = RestService.Validate(validateRequest);
|
|
|
+
|
|
|
+ if(response.Session != Guid.Empty)
|
|
|
+ {
|
|
|
+ var newSession = new PipeNotifyState.Session(data.e.Connection, response.Session, validateRequest.Credentials.Platform);
|
|
|
+ NotifyState.SessionMap[response.Session] = newSession;
|
|
|
+ NotifyState.Poll(newSession);
|
|
|
+ }
|
|
|
+
|
|
|
return request.Respond(response);
|
|
|
}
|
|
|
|
|
|
- private static PipeResponse Ping(PipeRequest request) => request.Respond(new PingResponse().Status(StatusCode.OK));
|
|
|
+ private PipeResponse Ping(PipeRequest request, RequestData data) => request.Respond(new PingResponse().Status(StatusCode.OK));
|
|
|
|
|
|
- private static PipeResponse Info(PipeRequest request)
|
|
|
+ private PipeResponse Info(PipeRequest request, RequestData data)
|
|
|
{
|
|
|
var response = RestService.Info(request.GetRequest<InfoRequest>());
|
|
|
return request.Respond(response);
|
|
|
}
|
|
|
|
|
|
- private static PipeResponse Check2FA(PipeRequest request)
|
|
|
+ private PipeResponse Check2FA(PipeRequest request, RequestData data)
|
|
|
{
|
|
|
var response = RestService.Check2FA(request.GetRequest<Check2FARequest>());
|
|
|
return request.Respond(response);
|
|
|
}
|
|
|
|
|
|
- private static PipeResponse Query<T>(PipeRequest request) where T : Entity, new()
|
|
|
+ private PipeResponse Query<T>(PipeRequest request, RequestData data) where T : Entity, new()
|
|
|
{
|
|
|
var response = RestService<T>.List(request.GetRequest<QueryRequest<T>>());
|
|
|
return request.Respond(response);
|
|
|
}
|
|
|
|
|
|
- private static PipeResponse Save<T>(PipeRequest request) where T : Entity, new()
|
|
|
+ private PipeResponse Save<T>(PipeRequest request, RequestData data) where T : Entity, new()
|
|
|
{
|
|
|
var response = RestService<T>.Save(request.GetRequest<SaveRequest<T>>());
|
|
|
return request.Respond(response);
|
|
|
}
|
|
|
- private static PipeResponse MultiSave<T>(PipeRequest request) where T : Entity, new()
|
|
|
+ private PipeResponse MultiSave<T>(PipeRequest request, RequestData data) where T : Entity, new()
|
|
|
{
|
|
|
var response = RestService<T>.MultiSave(request.GetRequest<MultiSaveRequest<T>>());
|
|
|
return request.Respond(response);
|
|
|
}
|
|
|
|
|
|
- private static PipeResponse Delete<T>(PipeRequest request) where T : Entity, new()
|
|
|
+ private PipeResponse Delete<T>(PipeRequest request, RequestData data) where T : Entity, new()
|
|
|
{
|
|
|
var response = RestService<T>.Delete(request.GetRequest<DeleteRequest<T>>());
|
|
|
return request.Respond(response);
|
|
|
}
|
|
|
- private static PipeResponse MultiDelete<T>(PipeRequest request) where T : Entity, new()
|
|
|
+ private PipeResponse MultiDelete<T>(PipeRequest request, RequestData data) where T : Entity, new()
|
|
|
{
|
|
|
var response = RestService<T>.MultiDelete(request.GetRequest<MultiDeleteRequest<T>>());
|
|
|
return request.Respond(response);
|
|
|
}
|
|
|
|
|
|
- private static MethodInfo QueryMethod = GetMethod(nameof(Query));
|
|
|
- private static MethodInfo SaveMethod = GetMethod(nameof(Save));
|
|
|
- private static MethodInfo MultiSaveMethod = GetMethod(nameof(MultiSave));
|
|
|
- private static MethodInfo DeleteMethod = GetMethod(nameof(Delete));
|
|
|
- private static MethodInfo MultiDeleteMethod = GetMethod(nameof(MultiDelete));
|
|
|
- private static MethodInfo QueryMultipleMethod = GetMethod(nameof(QueryMultiple));
|
|
|
- private static MethodInfo ValidateMethod = GetMethod(nameof(Validate));
|
|
|
- private static MethodInfo Check2FAMethod = GetMethod(nameof(Check2FA));
|
|
|
- private static MethodInfo PingMethod = GetMethod(nameof(Ping));
|
|
|
- private static MethodInfo InfoMethod = GetMethod(nameof(Info));
|
|
|
+ private static readonly MethodInfo QueryMethod = GetMethod(nameof(Query));
|
|
|
+ private static readonly MethodInfo SaveMethod = GetMethod(nameof(Save));
|
|
|
+ private static readonly MethodInfo MultiSaveMethod = GetMethod(nameof(MultiSave));
|
|
|
+ private static readonly MethodInfo DeleteMethod = GetMethod(nameof(Delete));
|
|
|
+ private static readonly MethodInfo MultiDeleteMethod = GetMethod(nameof(MultiDelete));
|
|
|
+ private static readonly MethodInfo QueryMultipleMethod = GetMethod(nameof(QueryMultiple));
|
|
|
+ private static readonly MethodInfo ValidateMethod = GetMethod(nameof(Validate));
|
|
|
+ private static readonly MethodInfo Check2FAMethod = GetMethod(nameof(Check2FA));
|
|
|
+ private static readonly MethodInfo PingMethod = GetMethod(nameof(Ping));
|
|
|
+ private static readonly MethodInfo InfoMethod = GetMethod(nameof(Info));
|
|
|
|
|
|
private static MethodInfo GetMethod(string name) =>
|
|
|
- typeof(PipeIPCServer).GetMethod(name, BindingFlags.NonPublic | BindingFlags.Static)
|
|
|
+ typeof(PipeIPCServer).GetMethod(name, BindingFlags.NonPublic | BindingFlags.Instance)
|
|
|
?? throw new Exception($"Invalid method '{name}'");
|
|
|
|
|
|
- private void Server_MessageReceived(object? sender, H.Pipes.Args.ConnectionMessageEventArgs<PipeRequest?> e)
|
|
|
+ private void Server_MessageReceived(object? sender, ConnectionMessageEventArgs<PipeRequest?> e)
|
|
|
{
|
|
|
Task.Run(() =>
|
|
|
{
|
|
@@ -190,7 +312,7 @@ namespace Piping
|
|
|
method = method.MakeGenericMethod(entityType);
|
|
|
}
|
|
|
|
|
|
- var response = method.Invoke(null, new object[] { e.Message }) as PipeResponse;
|
|
|
+ var response = method.Invoke(this, new object[] { e.Message, new RequestData(e) }) as PipeResponse;
|
|
|
e.Connection.WriteAsync(response);
|
|
|
}
|
|
|
catch (Exception err)
|
|
@@ -214,6 +336,10 @@ namespace Piping
|
|
|
private void Server_ClientDisconnected(object? sender, H.Pipes.Args.ConnectionEventArgs<PipeRequest> e)
|
|
|
{
|
|
|
Logger.Send(LogType.Information, "", "Client Disconnected");
|
|
|
+
|
|
|
+ var sessionID = NotifyState.SessionMap.Where(x => x.Value.Connection == e.Connection).FirstOrDefault().Key;
|
|
|
+ NotifyState.SessionMap.TryRemove(sessionID, out var session);
|
|
|
+
|
|
|
e.Connection.DisposeAsync();
|
|
|
}
|
|
|
|