IPCServer.cs 14 KB

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