PipeIPCServer.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. using H.Formatters;
  2. using H.Pipes;
  3. using H.Pipes.AccessControl;
  4. using H.Pipes.Args;
  5. using InABox.API;
  6. using InABox.Clients;
  7. using InABox.Core;
  8. using InABox.IPC.Shared;
  9. using InABox.Server.WebSocket;
  10. using System;
  11. using System.Collections.Concurrent;
  12. using System.Collections.Generic;
  13. using System.Diagnostics;
  14. using System.IO.Pipes;
  15. using System.Linq;
  16. using System.Reflection;
  17. using System.Security.Principal;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. namespace Piping
  21. {
  22. using PipeResponse = PipeRequest;
  23. delegate void PipePollEvent(PipeNotifyState.Session session);
  24. class PipeNotifyState
  25. {
  26. public class Session
  27. {
  28. public PipeConnection<PipeResponse?> Connection { get; }
  29. public Guid SessionID { get; }
  30. public Platform Platform { get; }
  31. public Session(PipeConnection<PipeResponse?> connection, Guid sessionID, Platform platform)
  32. {
  33. Connection = connection;
  34. SessionID = sessionID;
  35. Platform = platform;
  36. }
  37. }
  38. public ConcurrentDictionary<Guid, Session> SessionMap = new();
  39. public event PipePollEvent? OnPoll;
  40. public void Poll(Session session)
  41. {
  42. OnPoll?.Invoke(session);
  43. }
  44. }
  45. class PipeIPCNotifier : Notifier
  46. {
  47. PipeNotifyState NotifyState { get; set; }
  48. public PipeIPCNotifier(PipeNotifyState notifyState)
  49. {
  50. NotifyState = notifyState;
  51. NotifyState.OnPoll += NotifyState_OnPoll;
  52. }
  53. private void NotifyState_OnPoll(PipeNotifyState.Session session)
  54. {
  55. Notify.Poll(session.SessionID);
  56. }
  57. protected override IEnumerable<Guid> GetSessions(Platform platform)
  58. {
  59. return NotifyState.SessionMap.Where(x => x.Value.Platform == platform).Select(x => x.Key);
  60. }
  61. protected override IEnumerable<Guid> GetUserSessions(Guid userID)
  62. {
  63. return CredentialsCache.GetUserSessions(userID);
  64. }
  65. protected override void NotifyAll<TNotification>(TNotification notification)
  66. {
  67. foreach(var session in NotifyState.SessionMap.Values)
  68. {
  69. session.Connection.WriteAsync(PipeRequest.Notification(notification)).ContinueWith(task =>
  70. {
  71. if(task.Exception != null)
  72. {
  73. Logger.Send(LogType.Error, "", $"Error in notification: {CoreUtils.FormatException(task.Exception)}");
  74. }
  75. });
  76. }
  77. }
  78. protected override void NotifySession<TNotification>(Guid sessionID, TNotification notification)
  79. {
  80. if(NotifyState.SessionMap.TryGetValue(sessionID, out var session))
  81. {
  82. session.Connection.WriteAsync(PipeRequest.Notification(notification)).ContinueWith(task =>
  83. {
  84. if (task.Exception != null)
  85. {
  86. Logger.Send(LogType.Error, "", $"Error in notification: {CoreUtils.FormatException(task.Exception)}");
  87. }
  88. });
  89. }
  90. }
  91. protected override void NotifySession(Guid sessionID, Type TNotification, BaseObject notification)
  92. {
  93. if (NotifyState.SessionMap.TryGetValue(sessionID, out var session))
  94. {
  95. session.Connection.WriteAsync(PipeRequest.Notification(TNotification, notification)).ContinueWith(task =>
  96. {
  97. if (task.Exception != null)
  98. {
  99. Logger.Send(LogType.Error, "", $"Error in notification: {CoreUtils.FormatException(task.Exception)}");
  100. }
  101. });
  102. }
  103. }
  104. }
  105. public class PipeIPCServer : IDisposable
  106. {
  107. PipeServer<PipeRequest> Server;
  108. PipeNotifyState NotifyState = new();
  109. public PipeIPCServer(string name)
  110. {
  111. Server = new PipeServer<PipeRequest>(name);
  112. Notify.AddNotifier(new PipeIPCNotifier(NotifyState));
  113. var pipeSecurity = new PipeSecurity();
  114. pipeSecurity.AddAccessRule(new PipeAccessRule(new SecurityIdentifier(WellKnownSidType.LocalSid, null), PipeAccessRights.ReadWrite, System.Security.AccessControl.AccessControlType.Allow));
  115. pipeSecurity.AddAccessRule(new PipeAccessRule(new SecurityIdentifier(WellKnownSidType.LocalServiceSid, null), PipeAccessRights.ReadWrite, System.Security.AccessControl.AccessControlType.Allow));
  116. pipeSecurity.AddAccessRule(new PipeAccessRule(new SecurityIdentifier(WellKnownSidType.LocalSystemSid, null), PipeAccessRights.ReadWrite, System.Security.AccessControl.AccessControlType.Allow));
  117. Server.SetPipeSecurity(pipeSecurity);
  118. Server.ClientConnected += Server_ClientConnected;
  119. Server.ClientDisconnected += Server_ClientDisconnected;
  120. Server.MessageReceived += Server_MessageReceived;
  121. Server.ExceptionOccurred += Server_ExceptionOccurred;
  122. }
  123. private void Server_ExceptionOccurred(object? sender, H.Pipes.Args.ExceptionEventArgs e)
  124. {
  125. Logger.Send(LogType.Error, "", $"Exception Occurred: {e.Exception.Message}");
  126. }
  127. public void Start()
  128. {
  129. Server.StartAsync().Wait();
  130. }
  131. private static List<Type>? _persistentRemotable;
  132. private static Type? GetEntity(string entityName)
  133. {
  134. _persistentRemotable ??= CoreUtils.TypeList(
  135. e => e.IsSubclassOf(typeof(Entity)) &&
  136. e.GetInterfaces().Contains(typeof(IRemotable)) &&
  137. e.GetInterfaces().Contains(typeof(IPersistent))).ToList();
  138. return _persistentRemotable.FirstOrDefault(x => x.Name == entityName);
  139. }
  140. private static Type? GetResponseType(Method method, string? entityName)
  141. {
  142. if(entityName != null)
  143. {
  144. var entityType = GetEntity(entityName);
  145. if(entityType != null)
  146. {
  147. var response = method switch
  148. {
  149. Method.Query => typeof(QueryResponse<>).MakeGenericType(entityType),
  150. Method.Delete => typeof(DeleteResponse<>).MakeGenericType(entityType),
  151. Method.MultiDelete => typeof(MultiDeleteResponse<>).MakeGenericType(entityType),
  152. Method.Save => typeof(SaveResponse<>).MakeGenericType(entityType),
  153. Method.MultiSave => typeof(MultiSaveResponse<>).MakeGenericType(entityType),
  154. _ => null
  155. };
  156. if (response != null) return response;
  157. }
  158. }
  159. return method switch
  160. {
  161. Method.QueryMultiple => typeof(MultiQueryResponse),
  162. Method.Validate => typeof(ValidateResponse),
  163. Method.Check2FA => typeof(Check2FAResponse),
  164. _ => null
  165. };
  166. }
  167. private class RequestData
  168. {
  169. public ConnectionMessageEventArgs<PipeRequest?> e { get; }
  170. public RequestData(ConnectionMessageEventArgs<PipeResponse?> e)
  171. {
  172. this.e = e;
  173. }
  174. }
  175. private PipeResponse QueryMultiple(PipeRequest request, RequestData data)
  176. {
  177. var response = RestService.QueryMultiple(request.GetRequest<MultiQueryRequest>(), true);
  178. return request.Respond(response);
  179. }
  180. private PipeResponse Validate(PipeRequest request, RequestData data)
  181. {
  182. var validateRequest = request.GetRequest<ValidateRequest>();
  183. var response = RestService.Validate(validateRequest);
  184. if(response.Session != Guid.Empty)
  185. {
  186. var newSession = new PipeNotifyState.Session(data.e.Connection, response.Session, validateRequest.Credentials.Platform);
  187. NotifyState.SessionMap[response.Session] = newSession;
  188. NotifyState.Poll(newSession);
  189. }
  190. return request.Respond(response);
  191. }
  192. private PipeResponse Ping(PipeRequest request, RequestData data) => request.Respond(new PingResponse().Status(StatusCode.OK));
  193. private PipeResponse Info(PipeRequest request, RequestData data)
  194. {
  195. var response = RestService.Info(request.GetRequest<InfoRequest>());
  196. return request.Respond(response);
  197. }
  198. private PipeResponse Check2FA(PipeRequest request, RequestData data)
  199. {
  200. var response = RestService.Check2FA(request.GetRequest<Check2FARequest>());
  201. return request.Respond(response);
  202. }
  203. private PipeResponse Query<T>(PipeRequest request, RequestData data) where T : Entity, new()
  204. {
  205. var response = RestService<T>.List(request.GetRequest<QueryRequest<T>>());
  206. return request.Respond(response);
  207. }
  208. private PipeResponse Save<T>(PipeRequest request, RequestData data) where T : Entity, new()
  209. {
  210. var response = RestService<T>.Save(request.GetRequest<SaveRequest<T>>());
  211. return request.Respond(response);
  212. }
  213. private PipeResponse MultiSave<T>(PipeRequest request, RequestData data) where T : Entity, new()
  214. {
  215. var response = RestService<T>.MultiSave(request.GetRequest<MultiSaveRequest<T>>());
  216. return request.Respond(response);
  217. }
  218. private PipeResponse Delete<T>(PipeRequest request, RequestData data) where T : Entity, new()
  219. {
  220. var response = RestService<T>.Delete(request.GetRequest<DeleteRequest<T>>());
  221. return request.Respond(response);
  222. }
  223. private PipeResponse MultiDelete<T>(PipeRequest request, RequestData data) where T : Entity, new()
  224. {
  225. var response = RestService<T>.MultiDelete(request.GetRequest<MultiDeleteRequest<T>>());
  226. return request.Respond(response);
  227. }
  228. private static readonly MethodInfo QueryMethod = GetMethod(nameof(Query));
  229. private static readonly MethodInfo SaveMethod = GetMethod(nameof(Save));
  230. private static readonly MethodInfo MultiSaveMethod = GetMethod(nameof(MultiSave));
  231. private static readonly MethodInfo DeleteMethod = GetMethod(nameof(Delete));
  232. private static readonly MethodInfo MultiDeleteMethod = GetMethod(nameof(MultiDelete));
  233. private static readonly MethodInfo QueryMultipleMethod = GetMethod(nameof(QueryMultiple));
  234. private static readonly MethodInfo ValidateMethod = GetMethod(nameof(Validate));
  235. private static readonly MethodInfo Check2FAMethod = GetMethod(nameof(Check2FA));
  236. private static readonly MethodInfo PingMethod = GetMethod(nameof(Ping));
  237. private static readonly MethodInfo InfoMethod = GetMethod(nameof(Info));
  238. private static MethodInfo GetMethod(string name) =>
  239. typeof(PipeIPCServer).GetMethod(name, BindingFlags.NonPublic | BindingFlags.Instance)
  240. ?? throw new Exception($"Invalid method '{name}'");
  241. private void Server_MessageReceived(object? sender, ConnectionMessageEventArgs<PipeRequest?> e)
  242. {
  243. Task.Run(() =>
  244. {
  245. var start = DateTime.Now;
  246. try
  247. {
  248. if (e.Message == null) throw new Exception($"Invalid message");
  249. var method = e.Message.Method switch
  250. {
  251. Method.Query => QueryMethod,
  252. Method.QueryMultiple => QueryMultipleMethod,
  253. Method.Delete => DeleteMethod,
  254. Method.MultiDelete => MultiDeleteMethod,
  255. Method.Save => SaveMethod,
  256. Method.MultiSave => MultiSaveMethod,
  257. Method.Check2FA => Check2FAMethod,
  258. Method.Validate => ValidateMethod,
  259. Method.Ping => PingMethod,
  260. Method.Info => InfoMethod,
  261. Method.None or _ => throw new Exception($"Invalid method '{e.Message.Method}'")
  262. };
  263. if (e.Message.Type != null)
  264. {
  265. var entityType = GetEntity(e.Message.Type) ?? throw new Exception($"No entity '{e.Message.Type}'");
  266. method = method.MakeGenericMethod(entityType);
  267. }
  268. var response = method.Invoke(this, new object[] { e.Message, new RequestData(e) }) as PipeResponse;
  269. e.Connection.WriteAsync(response).ContinueWith(task =>
  270. {
  271. if (task.Exception != null)
  272. {
  273. Logger.Send(LogType.Error, "", $"Error in response: {CoreUtils.FormatException(task.Exception)}");
  274. }
  275. });
  276. }
  277. catch (Exception err)
  278. {
  279. Logger.Send(LogType.Error, "", err.Message);
  280. if (e.Message != null)
  281. {
  282. var responseType = GetResponseType(e.Message.Method, e.Message.Type);
  283. if (responseType != null)
  284. {
  285. var response = (Activator.CreateInstance(responseType) as Response)!;
  286. response.Status = StatusCode.Error;
  287. response.Messages.Add(err.Message);
  288. e.Connection.WriteAsync(e.Message.Respond(response)).ContinueWith(task =>
  289. {
  290. if (task.Exception != null)
  291. {
  292. Logger.Send(LogType.Error, "", $"Error in response: {CoreUtils.FormatException(task.Exception)}");
  293. }
  294. });
  295. }
  296. }
  297. }
  298. });
  299. }
  300. private void Server_ClientDisconnected(object? sender, H.Pipes.Args.ConnectionEventArgs<PipeRequest> e)
  301. {
  302. Logger.Send(LogType.Information, "", "Client Disconnected");
  303. var sessionID = NotifyState.SessionMap.Where(x => x.Value.Connection == e.Connection).FirstOrDefault().Key;
  304. NotifyState.SessionMap.TryRemove(sessionID, out var session);
  305. e.Connection.DisposeAsync();
  306. }
  307. private void Server_ClientConnected(object? sender, H.Pipes.Args.ConnectionEventArgs<PipeRequest> e)
  308. {
  309. Logger.Send(LogType.Information, "", "Client Connected");
  310. }
  311. public void Dispose()
  312. {
  313. Server.DisposeAsync().AsTask().Wait();
  314. }
  315. ~PipeIPCServer()
  316. {
  317. Dispose();
  318. }
  319. }
  320. }