IPCServer.cs 12 KB

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