IPCServer.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 System.IO.Pipes;
  8. using System.Reflection;
  9. using System.Security.Principal;
  10. namespace InABox.IPC
  11. {
  12. public class IPCServer : IDisposable
  13. {
  14. PipeServer<IPCMessage> Server;
  15. IPCPushState PushState = new();
  16. public IPCServer(string name)
  17. {
  18. Server = new PipeServer<IPCMessage>(name);
  19. #if WINDOWS
  20. SetPipeSecurity();
  21. #endif
  22. Server.ClientConnected += Server_ClientConnected;
  23. Server.ClientDisconnected += Server_ClientDisconnected;
  24. Server.MessageReceived += Server_MessageReceived;
  25. Server.ExceptionOccurred += Server_ExceptionOccurred;
  26. }
  27. private void SetPipeSecurity()
  28. {
  29. #pragma warning disable CA1416
  30. var pipeSecurity = new PipeSecurity();
  31. pipeSecurity.AddAccessRule(new PipeAccessRule(new SecurityIdentifier(WellKnownSidType.LocalSid, null), PipeAccessRights.ReadWrite,
  32. System.Security.AccessControl.AccessControlType.Allow));
  33. pipeSecurity.AddAccessRule(new PipeAccessRule(new SecurityIdentifier(WellKnownSidType.LocalServiceSid, null), PipeAccessRights.ReadWrite,
  34. System.Security.AccessControl.AccessControlType.Allow));
  35. pipeSecurity.AddAccessRule(new PipeAccessRule(new SecurityIdentifier(WellKnownSidType.LocalSystemSid, null), PipeAccessRights.ReadWrite,
  36. System.Security.AccessControl.AccessControlType.Allow));
  37. Server.SetPipeSecurity(pipeSecurity);
  38. #pragma warning restore CA1416
  39. }
  40. private void Server_ExceptionOccurred(object? sender, H.Pipes.Args.ExceptionEventArgs e)
  41. {
  42. Logger.Send(LogType.Error, "", $"Exception Occurred: {e.Exception.Message}");
  43. }
  44. public void Start()
  45. {
  46. Server.StartAsync().Wait();
  47. }
  48. private static List<Type>? _persistentRemotable;
  49. private static Type? GetEntity(string entityName)
  50. {
  51. _persistentRemotable ??= CoreUtils.TypeList(
  52. e => e.IsSubclassOf(typeof(Entity)) &&
  53. e.GetInterfaces().Contains(typeof(IRemotable)) &&
  54. e.GetInterfaces().Contains(typeof(IPersistent))).ToList();
  55. return _persistentRemotable.FirstOrDefault(x => x.Name == entityName);
  56. }
  57. private static Type? GetResponseType(Method method, string? entityName)
  58. {
  59. if(entityName != null)
  60. {
  61. var entityType = GetEntity(entityName);
  62. if(entityType != null)
  63. {
  64. var response = method switch
  65. {
  66. Method.Query => typeof(QueryResponse<>).MakeGenericType(entityType),
  67. Method.Delete => typeof(DeleteResponse<>).MakeGenericType(entityType),
  68. Method.MultiDelete => typeof(MultiDeleteResponse<>).MakeGenericType(entityType),
  69. Method.Save => typeof(SaveResponse<>).MakeGenericType(entityType),
  70. Method.MultiSave => typeof(MultiSaveResponse<>).MakeGenericType(entityType),
  71. _ => null
  72. };
  73. if (response != null) return response;
  74. }
  75. }
  76. return method switch
  77. {
  78. Method.QueryMultiple => typeof(MultiQueryResponse),
  79. Method.Validate => typeof(ValidateResponse),
  80. Method.Check2FA => typeof(Check2FAResponse),
  81. _ => null
  82. };
  83. }
  84. private class RequestData
  85. {
  86. public ConnectionMessageEventArgs<IPCMessage?> e { get; }
  87. public RequestData(ConnectionMessageEventArgs<IPCMessage?> e)
  88. {
  89. this.e = e;
  90. }
  91. }
  92. private IPCMessage QueryMultiple(IPCMessage request, RequestData data)
  93. {
  94. var response = RestService.QueryMultiple(request.GetRequest<MultiQueryRequest>(), true);
  95. return request.Respond(response);
  96. }
  97. private IPCMessage Validate(IPCMessage request, RequestData data)
  98. {
  99. var response = RestService.Validate(request.GetRequest<ValidateRequest>());
  100. return request.Respond(response);
  101. }
  102. private IPCMessage Ping(IPCMessage request, RequestData data) => request.Respond(new PingResponse().Status(StatusCode.OK));
  103. private IPCMessage Info(IPCMessage request, RequestData data)
  104. {
  105. var response = RestService.Info(request.GetRequest<InfoRequest>());
  106. return request.Respond(response);
  107. }
  108. private IPCMessage Check2FA(IPCMessage request, RequestData data)
  109. {
  110. var response = RestService.Check2FA(request.GetRequest<Check2FARequest>());
  111. return request.Respond(response);
  112. }
  113. private IPCMessage Query<T>(IPCMessage request, RequestData data) where T : Entity, new()
  114. {
  115. var response = RestService<T>.List(request.GetRequest<QueryRequest<T>>());
  116. return request.Respond(response);
  117. }
  118. private IPCMessage Save<T>(IPCMessage request, RequestData data) where T : Entity, new()
  119. {
  120. var response = RestService<T>.Save(request.GetRequest<SaveRequest<T>>());
  121. return request.Respond(response);
  122. }
  123. private IPCMessage MultiSave<T>(IPCMessage request, RequestData data) where T : Entity, new()
  124. {
  125. var response = RestService<T>.MultiSave(request.GetRequest<MultiSaveRequest<T>>());
  126. return request.Respond(response);
  127. }
  128. private IPCMessage Delete<T>(IPCMessage request, RequestData data) where T : Entity, new()
  129. {
  130. var response = RestService<T>.Delete(request.GetRequest<DeleteRequest<T>>());
  131. return request.Respond(response);
  132. }
  133. private IPCMessage MultiDelete<T>(IPCMessage request, RequestData data) where T : Entity, new()
  134. {
  135. var response = RestService<T>.MultiDelete(request.GetRequest<MultiDeleteRequest<T>>());
  136. return request.Respond(response);
  137. }
  138. private static readonly MethodInfo QueryMethod = GetMethod(nameof(Query));
  139. private static readonly MethodInfo SaveMethod = GetMethod(nameof(Save));
  140. private static readonly MethodInfo MultiSaveMethod = GetMethod(nameof(MultiSave));
  141. private static readonly MethodInfo DeleteMethod = GetMethod(nameof(Delete));
  142. private static readonly MethodInfo MultiDeleteMethod = GetMethod(nameof(MultiDelete));
  143. private static readonly MethodInfo QueryMultipleMethod = GetMethod(nameof(QueryMultiple));
  144. private static readonly MethodInfo ValidateMethod = GetMethod(nameof(Validate));
  145. private static readonly MethodInfo Check2FAMethod = GetMethod(nameof(Check2FA));
  146. private static readonly MethodInfo PingMethod = GetMethod(nameof(Ping));
  147. private static readonly MethodInfo InfoMethod = GetMethod(nameof(Info));
  148. private static MethodInfo GetMethod(string name) =>
  149. typeof(IPCServer).GetMethod(name, BindingFlags.NonPublic | BindingFlags.Instance)
  150. ?? throw new Exception($"Invalid method '{name}'");
  151. private void Server_MessageReceived(object? sender, H.Pipes.Args.ConnectionMessageEventArgs<IPCMessage?> e)
  152. {
  153. Task.Run(() =>
  154. {
  155. var start = DateTime.Now;
  156. try
  157. {
  158. if (e.Message == null) throw new Exception($"Invalid message");
  159. var method = e.Message.Method switch
  160. {
  161. Method.Query => QueryMethod,
  162. Method.QueryMultiple => QueryMultipleMethod,
  163. Method.Delete => DeleteMethod,
  164. Method.MultiDelete => MultiDeleteMethod,
  165. Method.Save => SaveMethod,
  166. Method.MultiSave => MultiSaveMethod,
  167. Method.Check2FA => Check2FAMethod,
  168. Method.Validate => ValidateMethod,
  169. Method.Ping => PingMethod,
  170. Method.Info => InfoMethod,
  171. Method.None or _ => throw new Exception($"Invalid method '{e.Message.Method}'")
  172. };
  173. if (e.Message.Type != null)
  174. {
  175. var entityType = GetEntity(e.Message.Type) ?? throw new Exception($"No entity '{e.Message.Type}'");
  176. method = method.MakeGenericMethod(entityType);
  177. }
  178. var response = method.Invoke(this, new object[] { e.Message, new RequestData(e) }) as IPCMessage;
  179. e.Connection.WriteAsync(response).ContinueWith(task =>
  180. {
  181. if (task.Exception != null)
  182. {
  183. Logger.Send(LogType.Error, "", $"Error in response: {CoreUtils.FormatException(task.Exception)}");
  184. }
  185. });
  186. }
  187. catch (Exception err)
  188. {
  189. Logger.Send(LogType.Error, "", err.Message);
  190. if (e.Message != null)
  191. {
  192. var responseType = GetResponseType(e.Message.Method, e.Message.Type);
  193. if (responseType != null)
  194. {
  195. var response = (Activator.CreateInstance(responseType) as Response)!;
  196. response.Status = StatusCode.Error;
  197. response.Messages.Add(err.Message);
  198. e.Connection.WriteAsync(e.Message.Respond(response)).ContinueWith(task =>
  199. {
  200. if (task.Exception != null)
  201. {
  202. Logger.Send(LogType.Error, "", $"Error in response: {CoreUtils.FormatException(task.Exception)}");
  203. }
  204. });
  205. }
  206. }
  207. }
  208. });
  209. }
  210. private void Server_ClientDisconnected(object? sender, H.Pipes.Args.ConnectionEventArgs<IPCMessage> e)
  211. {
  212. Logger.Send(LogType.Information, "", "Client Disconnected");
  213. var sessionID = PushState.SessionMap.Where(x => x.Value.Connection == e.Connection).FirstOrDefault().Key;
  214. PushState.SessionMap.TryRemove(sessionID, out var session);
  215. e.Connection.DisposeAsync();
  216. }
  217. private void Server_ClientConnected(object? sender, H.Pipes.Args.ConnectionEventArgs<IPCMessage> e)
  218. {
  219. Logger.Send(LogType.Information, "", "Client Connected");
  220. }
  221. public void Dispose()
  222. {
  223. Server.DisposeAsync().AsTask().Wait();
  224. }
  225. ~IPCServer()
  226. {
  227. Dispose();
  228. }
  229. }
  230. }