Exemple #1
0
        public string Add(long hubKey, long apiKey, Transform transform, TimeSpan?cacheRefreshInterval, string securityKey, SelectQuery selectQuery)
        {
            if (!_hubs.TryGetValue(hubKey, out var apiKeys))
            {
                apiKeys = new ConcurrentDictionary <long, string>();
                _hubs.TryAdd(hubKey, apiKeys);
            }

            if (apiKeys.TryGetValue(apiKey, out var _))
            {
                throw new LiveDataException("The Api is already activated.");
            }

            if (securityKey == null)
            {
                securityKey = EncryptString.GenerateRandomKey(25);
            }

            // strip non alphanumeric chars from the key to ensure url compatibility.
            var arr = securityKey.ToCharArray();

            arr = Array.FindAll(arr, c => char.IsLetterOrDigit(c) ||
                                char.IsWhiteSpace(c) ||
                                c == '-');
            securityKey = new string(arr);

            if (apiKeys.TryAdd(apiKey, securityKey))
            {
                var apiData = new ApiData()
                {
                    ApiStatus   = EApiStatus.Activated,
                    HubKey      = hubKey,
                    ApiKey      = apiKey,
                    SecurityKey = securityKey,
                    Transform   = transform,
                    SelectQuery = selectQuery
                };

                if (cacheRefreshInterval != null)
                {
                    apiData.SetCacheResetTimer(cacheRefreshInterval.Value);
                }

                if (_liveApis.TryAdd(securityKey, apiData))
                {
                    ApiUpdate(new ApiData()
                    {
                        ApiStatus = EApiStatus.Activated, ApiKey = apiKey, HubKey = hubKey, SecurityKey = securityKey
                    });
                    return(securityKey);
                }
                else
                {
                    // cleanup the added key if an issue occurs.
                    apiKeys.Remove(apiKey, out var _);
                }
            }

            throw new LiveDataException("The Api could not be activated.");
        }
Exemple #2
0
        public SharedSettings(IConfiguration configuration, ILogger <SharedSettings> logger, IHost host, IManagedTasks managedTasks, IMemoryCache memoryCache)
        {
            _logger       = logger;
            _host         = host;
            _managedTasks = managedTasks;
            _memoryCache  = memoryCache;

            SessionEncryptionKey = EncryptString.GenerateRandomKey();
            RemoteSettings       = configuration.Get <RemoteSettings>();

            var url = RemoteSettings.AppSettings.WebServer;

            if (url.Substring(url.Length - 1) != "/")
            {
                url += "/";
            }
            BaseUrl = url;

            _apiUri = url + "api/";

            CookieContainer = new CookieContainer();
            var handler = new HttpClientHandler
            {
                CookieContainer = CookieContainer
            };

            //Login to the web server to receive an authenticated cookie.
            _httpClient     = new HttpClient(handler);
            _loginSemaphore = new SemaphoreSlim(1, 1);
        }
        private async Task <MemoryStream> CreateAppSettingsFile(RemoteAgentSettings settings, CancellationToken cancellationToken)
        {
            var remoteSettings = new RemoteSettings();

            // copy any settings across.
            settings.RemoteApplicationSettings.CopyProperties(remoteSettings.AppSettings);
            settings.RemoteApplicationSettings.CopyProperties(remoteSettings.Network);
            settings.RemoteApplicationSettings.CopyProperties(remoteSettings.Permissions);
            settings.RemoteApplicationSettings.CopyProperties(remoteSettings.Plugins);
            settings.RemoteApplicationSettings.CopyProperties(remoteSettings.Alerts);
            //TODO these should be copied by the copy properties.
            remoteSettings.Permissions.AllowedHubs  = settings.RemoteApplicationSettings.AllowedHubs;
            remoteSettings.Permissions.AllowedPaths = settings.RemoteApplicationSettings.AllowedPaths;
            settings.RemoteApplicationSettings.CopyProperties(remoteSettings.Privacy);
            remoteSettings.NamingStandards.LoadDefault();

            remoteSettings.AppSettings.UserPrompt = false;
            remoteSettings.AppSettings.WebServer  = (HttpContext.Request.IsHttps ? "https://" : "http://") +
                                                    HttpContext.Request.Host + HttpContext.Request.PathBase.Value;
            remoteSettings.AppSettings.RemoteAgentId = Guid.NewGuid().ToString();

            var user = await _operations.RepositoryManager.GetUserAsync(User, cancellationToken);

            var dbRemoteAgent = new DexihRemoteAgent()
            {
                RemoteAgentId = remoteSettings.AppSettings.RemoteAgentId,
                RestrictIp    = false,
                Name          = remoteSettings.AppSettings.Name,
                UserId        = user.Id
            };

            if (settings.EmbedUserName)
            {
                remoteSettings.AppSettings.UserToken = await _operations.RepositoryManager.GenerateRemoteUserTokenAsync(user, remoteSettings.AppSettings.RemoteAgentId, cancellationToken);

                var hashedToken = SecureHash.CreateHash(remoteSettings.AppSettings.UserToken);
                dbRemoteAgent.HashedToken       = hashedToken;
                remoteSettings.AppSettings.User = user.UserName;
            }

            if (settings.RemoteApplicationSettings.AutoGenerateCertificate)
            {
                remoteSettings.Network.CertificateFilename = "dexih.pfx";
                remoteSettings.Network.CertificatePassword = EncryptString.GenerateRandomKey();
            }

            remoteSettings.Logging.LogLevel.Default   = settings.LogLevel;
            remoteSettings.Logging.LogLevel.System    = settings.LogLevel;
            remoteSettings.Logging.LogLevel.Microsoft = settings.LogLevel;

            await _operations.RepositoryManager.SaveRemoteAgent(user.Id, dbRemoteAgent, cancellationToken);

            var json = JsonSerializer.Serialize(remoteSettings, new JsonSerializerOptions()
            {
                WriteIndented = true
            });
            var appSettingsStream = new MemoryStream(Encoding.ASCII.GetBytes(json));

            return(appSettingsStream);
        }
Exemple #4
0
 public void EncryptPerformance(int iterations)
 {
     for (int i = 0; i < iterations; i++)
     {
         var value = EncryptString.GenerateRandomKey(200);
         var key   = EncryptString.GenerateRandomKey(50);
         EncryptDecrypt(value, key);
     }
 }
Exemple #5
0
        public void RandomKey()
        {
            var key = EncryptString.GenerateRandomKey(50);

            Assert.Equal(50, key.Length);

            // check no bad characters
            key = EncryptString.GenerateRandomKey(50000);

            Assert.False(key.Contains("/"));
            Assert.False(key.Contains("+"));
        }
Exemple #6
0
        public static IConfigurationBuilder AddUserInput(this IConfigurationBuilder configurationBuilder)
        {
            const string defaultWebServer = "https://dexih.dataexpertsgroup.com";

            var configBuild    = configurationBuilder.Build();
            var remoteSettings = configBuild.Get <RemoteSettings>() ?? new RemoteSettings();

            var newSettings = new Dictionary <string, string>();

            if (remoteSettings.RequiresUserInput())
            {
                var detailed = GetBoolInput("Would you like to enter detailed configuration options?", false);

                var sslConfigureComplete = false;

                if (detailed)
                {
                    if (string.IsNullOrEmpty(remoteSettings.AppSettings.EncryptionKey))
                    {
                        Console.WriteLine("Enter the encryption key.");
                        Console.Write("This is used to encrypt/decrypt data marked as secure. [auto-generate]: ");
                        var encryptionKey = Console.ReadLine();
                        if (string.IsNullOrEmpty(encryptionKey))
                        {
                            newSettings["AppSettings:EncryptionKey"] = EncryptString.GenerateRandomKey();
                        }
                        else
                        {
                            newSettings["AppSettings:EncryptionKey"] = encryptionKey;
                        }
                        Console.WriteLine($"New encryption key \"{newSettings["AppSettings:EncryptionKey"]}\".");
                    }
                    else
                    {
                        Console.Write("Enter the encryption key [blank - use current, \"new\" to generate]: ");
                        var encryptionKey = Console.ReadLine();
                        if (string.IsNullOrEmpty(encryptionKey))
                        {
                            Console.WriteLine($"Existing key used: \"{remoteSettings.AppSettings.EncryptionKey}\".");
                        }
                        if (encryptionKey.ToLower() == "new")
                        {
                            newSettings["AppSettings:EncryptionKey"] = EncryptString.GenerateRandomKey();
                            Console.WriteLine($"New Encryption key \"{newSettings["AppSettings:EncryptionKey"]}\".");
                        }
                    }

                    //any critical settings not received, prompt user.
                    var allowDataUpload = GetBoolInput(
                        "Allow files to be uploaded through the agent", remoteSettings.Privacy.AllowDataUpload);
                    newSettings["Privacy:AllowDataUpload"] = allowDataUpload.ToString();

                    var allowDataDownload = GetBoolInput(
                        "Allow files to be downloaded through the agent",
                        remoteSettings.Privacy.AllowDataDownload);
                    newSettings["Privacy:AllowDataDownload"] = allowDataDownload.ToString();

                    if (allowDataDownload || allowDataUpload)
                    {
                        newSettings["Network:EnforceHttps"] = GetBoolInput(
                            "Enforce Https(encrypted) for upload/download of data",
                            remoteSettings.Network.EnforceHttps).ToString();

                        var allowLanAccess = GetBoolInput(
                            "Allow direct connections through the LAN",
                            remoteSettings.Privacy.AllowLanAccess);
                        newSettings["Privacy:AllowLanAccess"] = allowLanAccess.ToString();

                        var allowExternalAccess = GetBoolInput(
                            "Allow direct connections externally through internet (note: ports must be mapped to internet IP)?",
                            remoteSettings.Privacy.AllowExternalAccess);
                        newSettings["Privacy:AllowExternalAccess"] = allowExternalAccess.ToString();

                        if (allowLanAccess || allowExternalAccess)
                        {
                            newSettings["Network:DownloadPort"] = GetNumberInput(
                                "Enter the network port to listen for data upload/download connections",
                                remoteSettings.Network.DownloadPort, false, 1, 65535).ToString();

                            newSettings["Network:EnableUPnP"] = GetBoolInput(
                                "Enable UPnP discovery to map port automatically to the internet?",
                                remoteSettings.Network.EnableUPnP).ToString();
                        }

                        var enforceHttps = GetBoolInput(
                            "Allow connections through a proxy server?",
                            remoteSettings.Network.EnforceHttps);

                        newSettings["Network:EnforceHttps"] = enforceHttps.ToString();

                        var allowProxyAccess = GetBoolInput(
                            "Allow connections externally through a proxy server?",
                            remoteSettings.Privacy.AllowExternalAccess);
                        newSettings["Privacy:AllowProxy"] = allowProxyAccess.ToString();

                        if (allowProxyAccess)
                        {
                            newSettings["Network:ProxyUrl"] = GetStringInput(
                                "Enter a custom proxy url (leave blank for default internet based proxy)",
                                remoteSettings.Network.ProxyUrl, true);
                        }

                        if (enforceHttps &&
                            (allowLanAccess || allowExternalAccess))
                        {
                            var autoGenerateCertificate = GetBoolInput(
                                "Automatically generate a SSL certificate",
                                remoteSettings.Network.AutoGenerateCertificate);

                            newSettings["Network:AutoGenerateCertificate"] = autoGenerateCertificate.ToString();

                            if (autoGenerateCertificate)
                            {
                                newSettings["Network:ExternalDownloadUrl"] = GetStringInput(
                                    "Enter an external url to access the http connection for data upload/download (this can be used when using a proxy server or forwarding the port to another network or internet)",
                                    remoteSettings.Network.ExternalDownloadUrl, true);

                                newSettings["Network:CertificateFilename"] = GetStringInput(
                                    "Enter the path/filename of the SSL (PKF) certificate",
                                    remoteSettings.Network.CertificateFilename, false);
                            }

                            newSettings["Network:CertificatePassword"] = GetStringInput(
                                "Enter password for the SSL certificate",
                                remoteSettings.Network.CertificatePassword, false);

                            sslConfigureComplete = true;
                        }
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(remoteSettings.AppSettings.EncryptionKey))
                    {
                        newSettings["AppSettings:EncryptionKey"] = EncryptString.GenerateRandomKey();
                    }

                    if (remoteSettings.Network.AutoGenerateCertificate)
                    {
                        if (string.IsNullOrEmpty(remoteSettings.Network.CertificateFilename))
                        {
                            newSettings["Network:CertificateFilename"] = "dexih.pfx";
                        }

                        if (string.IsNullOrEmpty(remoteSettings.Network.CertificatePassword))
                        {
                            if (File.Exists(remoteSettings.CertificateFilePath()))
                            {
                                newSettings["Network:CertificatePassword"] = GetStringInput(
                                    $"Enter the password for the certificate with the name {remoteSettings.Network.CertificateFilename}.",
                                    remoteSettings.Network.CertificatePassword, false);
                            }
                            else
                            {
                                newSettings["Network:CertificatePassword"] = EncryptString.GenerateRandomKey();
                            }
                        }
                    }
                }

                var checkSaveSettings = true;

                newSettings["AppSettings:WebServer"] = defaultWebServer;

                Console.Write($"Enter the dexih web server [{remoteSettings.AppSettings.WebServer}]: ");
                var webServer = Console.ReadLine();

                if (!string.IsNullOrEmpty(webServer))
                {
                    newSettings["AppSettings:WebServer"] = webServer;
                }

                Console.Write($"Enter the login email [{remoteSettings.AppSettings.User}]: ");
                var user = Console.ReadLine();
                if (!string.IsNullOrEmpty(user))
                {
                    newSettings["AppSettings:User"] = user;
                }

                Console.Write("Enter the password [leave empty to specify user token]: ");
                ConsoleKeyInfo keyInfo;
                var            pass = "";
                do
                {
                    keyInfo = Console.ReadKey(true);
                    if (keyInfo.Key != ConsoleKey.Backspace && keyInfo.Key != ConsoleKey.Enter)
                    {
                        pass += keyInfo.KeyChar;
                        Console.Write("*");
                    }
                    else
                    {
                        if (keyInfo.Key == ConsoleKey.Backspace && pass.Length > 0)
                        {
                            pass = pass.Substring(0, pass.Length - 1);
                            Console.Write("\b \b");
                        }
                    }
                } while (keyInfo.Key != ConsoleKey.Enter);

                Console.WriteLine();
                newSettings["Runtime:Password"] = pass;

                var remoteAgentId = remoteSettings.AppSettings.RemoteAgentId;

                if (string.IsNullOrEmpty(pass))
                {
                    remoteAgentId = GetStringInput(
                        $"Enter the unique remote agent id (create new tokens at: {newSettings["AppSettings:WebServer"]}/hubs/index/remoteAgents:",
                        remoteSettings.AppSettings.RemoteAgentId, false);

                    newSettings["AppSettings:RemoteAgentId"] = remoteAgentId;

                    newSettings["AppSettings:UserToken"] = GetStringInput(
                        "Enter the user token:",
                        remoteSettings.AppSettings.UserToken, false);
                }
                else
                {
                    // if there is a password, and reset settings has been asked, then remove the user token.
                    if (remoteSettings.AppSettings.UserPrompt)
                    {
                        newSettings["AppSettings:UserToken"] = null;
                    }
                }

                if (string.IsNullOrEmpty(remoteAgentId))
                {
                    newSettings["AppSettings:RemoteAgentId"] = Guid.NewGuid().ToString();
                }

                Console.Write($"Enter a name to describe this remote agent [{Environment.MachineName}]: ");
                newSettings["AppSettings:Name"] = Console.ReadLine();

                if (string.IsNullOrEmpty(remoteSettings.AppSettings.Name))
                {
                    newSettings["AppSettings:Name"] = Environment.MachineName;
                }

                if (!sslConfigureComplete)
                {
                    var certificateFileName = remoteSettings.Network.CertificateFilename;

                    if (remoteSettings.Network.AutoGenerateCertificate && string.IsNullOrEmpty(certificateFileName))
                    {
                        certificateFileName = "dexih.pfx";
                        newSettings["Network:CertificateFilename"] = certificateFileName;
                        checkSaveSettings = true;
                    }

                    if (File.Exists(certificateFileName) && string.IsNullOrEmpty(remoteSettings.Network.CertificatePassword))
                    {
                        newSettings["Network:CertificatePassword"] = GetStringInput(
                            $"An SSL certificate {certificateFileName} was found.  Enter the password for this certificate: ",
                            remoteSettings.Network.CertificatePassword, false);
                        checkSaveSettings = true;
                    }
                }

                if (checkSaveSettings && !remoteSettings.Runtime.SaveSettings)
                {
                    Console.Write("Would you like to save settings (enter yes or no) [no]?: ");
                    var saveResult = Console.ReadLine().ToLower();

                    while (!(saveResult == "yes" || saveResult == "no" || string.IsNullOrEmpty(saveResult)))
                    {
                        Console.Write("Would you like to save settings (enter yes or no) [no]?: ");
                        saveResult = Console.ReadLine().ToLower();
                    }

                    if (saveResult == "no" || saveResult == "no" || string.IsNullOrEmpty(saveResult))
                    {
                        newSettings["Runtime:SaveSettings"] = "false";
                    }
                    else if (saveResult == "yes")
                    {
                        newSettings["Runtime:SaveSettings"] = "true";
                    }
                }
            }

            configurationBuilder.AddInMemoryCollection(newSettings);

            return(configurationBuilder);
        }