소스 검색

Added an EncryptedLocalConfiguration; fixed Lookup field adding;

Kenric Nugteren 2 년 전
부모
커밋
5ff9fdf79f

+ 7 - 2
InABox.Client.Remote.Json/HttpJsonClient.cs

@@ -69,9 +69,9 @@ namespace InABox.Client.Remote.Json
             throw new Exception(error);
         }
 
-        public bool Ping()
+        public bool Ping(string endPoint)
         {
-            var req = new RestRequest("", Method.GET)
+            var req = new RestRequest(endPoint, Method.GET)
             {
                 Timeout = 10 * 1000
             };
@@ -93,6 +93,11 @@ namespace InABox.Client.Remote.Json
             return false;
         }
 
+        public bool Ping()
+        {
+            return Ping("");
+        }
+
         public bool TryGetRequest<T>(string command, out T result, [NotNullWhen(false)] out string? error)
         {
             var req = new RestRequest(command, Method.GET)

+ 47 - 8
InABox.Configuration/LocalConfiguration.cs

@@ -43,11 +43,20 @@ namespace InABox.Configuration
             return GetFileName();
         }
 
+        protected virtual string SerializeConfiguration(Dictionary<string, T> config)
+        {
+            return Serialization.Serialize(config, true);
+        }
+        protected virtual Dictionary<string, T>? DeserializeConfiguration(string text)
+        {
+            return Serialization.Deserialize<Dictionary<string, T>>(text);
+        }
+
         private Dictionary<string, T> LoadCurrentConfig(string filename)
         {
             Dictionary<string, T>? config = null;
             if (File.Exists(filename))
-                config = Serialization.Deserialize<Dictionary<string, T>>(File.ReadAllText(filename));
+                config = DeserializeConfiguration(File.ReadAllText(filename));
             return config ?? new Dictionary<string, T>();
         }
 
@@ -69,8 +78,7 @@ namespace InABox.Configuration
             {
                 //Logger.Send(LogType.Information, "", "Local Config: Deserializing Configuration");
 
-                var config =
-                    Serialization.Deserialize<Dictionary<string, T>>(File.ReadAllText(filename)); //Toml.ReadString<T>(File.ReadAllText(FileName));
+                var config = DeserializeConfiguration(File.ReadAllText(filename)); //Toml.ReadString<T>(File.ReadAllText(FileName));
                 if (config != null && config.ContainsKey(Section))
                     //Logger.Send(LogType.Information, "", "Local Config:  Found Section ["+Section+"}");
                     if (config[Section] != null)
@@ -96,7 +104,7 @@ namespace InABox.Configuration
 
             config[Section] = obj;
 
-            var contents = Serialization.Serialize(config, true);
+            var contents = SerializeConfiguration(config);
 
             File.WriteAllText(filename, contents);
 
@@ -113,7 +121,7 @@ namespace InABox.Configuration
                 config[section] = obj;
             }
 
-            var contents = Serialization.Serialize(config, true);
+            var contents = SerializeConfiguration(config);
             File.WriteAllText(filename, contents);
 
             foreach(var (section, obj) in objs)
@@ -131,7 +139,7 @@ namespace InABox.Configuration
                 config.Remove(Section);
             if (config.Any())
             {
-                var contents = Serialization.Serialize(config);
+                var contents = SerializeConfiguration(config);
 
                 File.WriteAllText(filename, contents);
             }
@@ -156,12 +164,43 @@ namespace InABox.Configuration
             if (!File.Exists(filename))
                 return new[] { "" };
 
-            var config =
-                Serialization.Deserialize<Dictionary<string, T>>(File.ReadAllText(filename)); //Toml.ReadString<T>(File.ReadAllText(FileName));
+            var config = DeserializeConfiguration(File.ReadAllText(filename));
             if (config == null)
                 return new[] { "" };
 
             return config.Keys.ToArray();
         }
     }
+
+    public class EncryptedLocalConfiguration<T> : LocalConfiguration<T>
+        where T : LocalConfigurationSettings, new()
+    {
+        private byte[] Key;
+
+        public EncryptedLocalConfiguration(byte[] key, string section = "") : base(section)
+        {
+            Key = key;
+        }
+        public EncryptedLocalConfiguration(byte[] key, string section, string folder) : base(section, folder)
+        {
+            Key = key;
+        }
+        public EncryptedLocalConfiguration(string key, string section = "") : this(ConvertKey(key), section) { }
+        public EncryptedLocalConfiguration(string key, string section, string folder) : this(ConvertKey(key), section, folder) { }
+
+        protected override string SerializeConfiguration(Dictionary<string, T> config)
+        {
+            return Encryption.EncryptV2(base.SerializeConfiguration(config), Key);
+        }
+
+        protected override Dictionary<string, T>? DeserializeConfiguration(string text)
+        {
+            return base.DeserializeConfiguration(Encryption.DecryptV2(text, Key));
+        }
+
+        private static byte[] ConvertKey(string key)
+        {
+            return Convert.FromBase64String(key);
+        }
+    }
 }

+ 5 - 0
InABox.Core/CoreUtils.cs

@@ -364,6 +364,11 @@ namespace InABox.Core
                 return type;
             throw new Exception($"Entity {entityname} does not exist!");
         }
+        public static Type? GetEntityOrNull(string entityname)
+        {
+            TryGetEntity(entityname, out var type);
+            return type;
+        }
 
         //private static List<Type> TypeCache = null;
 

+ 6 - 5
InABox.Core/DatabaseSchema/CustomProperty.cs

@@ -8,7 +8,7 @@ namespace InABox.Core
     [UserTracking(typeof(User))]
     public class CustomProperty : Entity, IProperty, IPersistent, IRemotable, ISequenceable, ILicense<CoreLicense>
     {
-        public Type _class;
+        public Type? _class;
         private BaseEditor? _editor;
 
         private Func<object, object> _getter;
@@ -60,7 +60,7 @@ namespace InABox.Core
             get => _class != null ? _class.EntityName() : "";
             set
             {
-                _class = CoreUtils.GetEntity(value);
+                CoreUtils.TryGetEntity(value, out _class);
                 CheckExpressions();
             }
         }
@@ -86,7 +86,7 @@ namespace InABox.Core
                 _type = value;
                 try
                 {
-                    type = CoreUtils.GetEntity(_type);
+                    type = CoreUtils.GetEntityOrNull(_type) ?? typeof(object);
                 }
                 catch
                 {
@@ -196,7 +196,9 @@ namespace InABox.Core
 
         public Expression Expression()
         {
-            return CoreUtils.CreateIndexExpression(CoreUtils.GetEntity(Class), "UserProperties", Name);
+            if (_class is null)
+                throw new Exception("No class for CustomProperty");
+            return CoreUtils.CreateIndexExpression(_class, "UserProperties", Name);
         }
 
         public Func<object, object> Getter()
@@ -222,7 +224,6 @@ namespace InABox.Core
                 return;
             try
             {
-                var _class = CoreUtils.GetEntity(Class);
                 _getter = Expressions.Getter(_class, "UserProperties", Name);
                 _setter = Expressions.Setter(_class, "UserProperties", Name);
             }

+ 1 - 1
InABox.Core/DatabaseSchema/StandardProperty.cs

@@ -55,7 +55,7 @@ namespace InABox.Core
                 Type propType;
                 try
                 {
-                    propType = CoreUtils.GetEntity(_type);
+                    propType = CoreUtils.GetEntityOrNull(_type) ?? typeof(object);
                 }
                 catch
                 {

+ 2 - 4
InABox.Core/DigitalForms/Layouts/Fields/DFLayoutLookupField/DFLayoutLookupFieldProperties.cs

@@ -44,11 +44,9 @@ namespace InABox.Core
 
         public override IEnumerable<CoreColumn> GetAdditionalColumns()
         {
-            var entity = CoreUtils.GetEntity(LookupType);
-            if(entity is null)
-            {
+            if(!CoreUtils.TryGetEntity(LookupType, out var entity))
                 return Enumerable.Empty<CoreColumn>();
-            }
+
             return AdditionalPropertiesList.Select(x =>
             {
                 if (CoreUtils.TryGetProperty(entity, x, out var property))

+ 2 - 2
InABox.DynamicGrid/FormDesigner/DynamicVariableGrid.cs

@@ -30,7 +30,7 @@ namespace InABox.DynamicGrid
                         if (edit is FilterEditorControl filter)
                         {
                             filter.FilterType = value is string str ?
-                                CoreUtils.GetEntity(str) :
+                                CoreUtils.GetEntityOrNull(str) :
                                 null;
                         }
                     }
@@ -67,7 +67,7 @@ namespace InABox.DynamicGrid
             {
                 var properties = items[0] as DFLayoutLookupFieldProperties;
                 var lookupType = properties.LookupType;
-                var entityType = CoreUtils.GetEntity(lookupType);
+                var entityType = CoreUtils.GetEntityOrNull(lookupType);
                 fe.Type = entityType;
             }
         }

+ 1 - 4
InABox.Logging/NamedPipeLogger.cs

@@ -175,10 +175,7 @@ namespace InABox.Logging
         }
         public override void Stop()
         {
-            if (_pipe.IsStarted)
-            {
-                _pipe.StopAsync().Wait();
-            }
+            _pipe.StopAsync().Wait();
         }
 
         protected override void DoSend(string message)