Browse Source

Added flag for whether server is running HTTPS; added some timeout handling for IPCClient

Kenric Nugteren 2 years ago
parent
commit
044fa07966

+ 2 - 0
InABox.Core/Client/Request.cs

@@ -414,6 +414,8 @@ namespace InABox.Clients
         public string? ColorScheme { get; set; }
         public byte[]? Logo { get; set; }
         public string Version { get; set; }
+
+        public bool IsHTTPS { get; set; }
     }
     
     public class InfoResponse : Response

+ 2 - 0
InABox.Server/Rest/RestListener.cs

@@ -533,6 +533,7 @@ namespace InABox.API
         public static void InitCertificate(ushort port, X509Certificate2 certificate)
         {
             RestListener.certificate = certificate;
+            RestService.IsHTTPS = true;
             host?.Bind(IPAddress.Any, port, certificate);
         }
         public static void InitCertificate(ushort port, string certificateFile)
@@ -542,6 +543,7 @@ namespace InABox.API
 
         public static void InitPort(ushort port)
         {
+            RestService.IsHTTPS = false;
             host?.Bind(IPAddress.Any, port);
         }
 

+ 4 - 1
InABox.Server/RestService.cs

@@ -9,6 +9,8 @@ namespace InABox.API
     {
         public static bool CheckPasswordExpiration { get; set; } = true;
 
+        public static bool IsHTTPS { get; set; } = false;
+
         protected static void GarbageCollection()
         {
             //DateTime now = DateTime.Now;
@@ -227,7 +229,8 @@ namespace InABox.API
                 {
                     ColorScheme = DbFactory.ColorScheme,
                     Version = CoreUtils.GetVersion(),
-                    Logo = DbFactory.Logo
+                    Logo = DbFactory.Logo,
+                    IsHTTPS = IsHTTPS
                 }
             };
             response.Status = StatusCode.OK;

+ 5 - 2
inabox.client.ipc/IPCClient.cs

@@ -10,7 +10,7 @@ using System.Threading.Tasks;
 
 namespace InABox.Client.IPC
 {
-    internal class IPCClient : IDisposable
+    public class IPCClient : IDisposable
     {
         private PipeClient<PipeRequest> Client;
 
@@ -70,7 +70,10 @@ namespace InABox.Client.IPC
 
             try
             {
-                ev.Wait(timeout);
+                if (!ev.Wait(timeout))
+                {
+                    return PipeRequest.Error(RequestError.TIMEOUT);
+                }
             }
             catch (Exception e)
             {

+ 1 - 1
inabox.client.ipc/IPCClientFactory.cs

@@ -6,7 +6,7 @@ using System.Threading.Tasks;
 
 namespace InABox.Client.IPC
 {
-    internal static class IPCClientFactory
+    public static class IPCClientFactory
     {
         private static Dictionary<string, IPCClient> Clients = new();
 

+ 6 - 6
inabox.client.ipc/PipeIPCClient.cs

@@ -25,6 +25,7 @@ namespace InABox.Client.IPC
         public PipeIPCClient(string pipeName)
         {
             Client = IPCClientFactory.GetClient(pipeName);
+            Timeout = TimeSpan.FromSeconds(300);
         }
 
         private static string[]? _types;
@@ -36,21 +37,20 @@ namespace InABox.Client.IPC
                 .ToArray();
             return _types;
         }
-        
+
         public override DatabaseInfo Info()
         {
             try
             {
                 var request = new InfoRequest();
-                PrepareRequest(request,false);
+                PrepareRequest(request, false);
                 var response = Send(PipeRequest.Info(request)).GetResponse<InfoResponse>();
                 return response.Info;
             }
-            catch (Exception e)
+            catch (Exception)
             {
                 return new DatabaseInfo();
             }
-
         }
         
         private void PrepareRequest(Request request, bool doCredentials = true)
@@ -70,9 +70,9 @@ namespace InABox.Client.IPC
             Request.BeforeRequest?.Invoke(request);
         }
 
-        private PipeRequest Send(PipeRequest request, int timeout = 300000)
+        private PipeRequest Send(PipeRequest request, int? timeout = null)
         {
-            return Client.Send(request, timeout);
+            return Client.Send(request, timeout ?? Convert.ToInt32(Timeout.TotalMilliseconds));
         }
 
         protected override bool DoCheck2FA(string code, Guid? session)

+ 6 - 1
inabox.ipc.shared/PipeRequest.cs

@@ -13,7 +13,8 @@ namespace InABox.IPC.Shared
     {
         NONE,
         DISCONNECTED,
-        UNKNOWN
+        UNKNOWN,
+        TIMEOUT
     }
 
     public enum Method
@@ -78,6 +79,10 @@ namespace InABox.IPC.Shared
                     response.Status = StatusCode.Error;
                     response.Messages.Add("Unknown Error");
                     break;
+                case RequestError.TIMEOUT:
+                    response.Status = StatusCode.Error;
+                    response.Messages.Add("Timeout");
+                    break;
             }
             return response;
         }