/// <summary> /// Initialize the class and configurations /// </summary> /// <param name="serviceConfigs"></param> private void Initialize(IEnumerable <IServiceConfig> serviceConfigs) { if (serviceConfigs != null) { _vpnConfig = serviceConfigs as List <OpenVPNConfig>; } // Check if configurations are passed. if (_vpnConfig == null || !_vpnConfig.Any()) { throw new InvalidOperationException("OpenVPN init: config list was null."); } // Get the default network interace. var defaultNetworkInterface = _firewall.GetDefaultInterface(); // Context-aware dictionary of OpenVPN configs and their rendered forms. var configDictionary = new Dictionary <OpenVPNConfig, string>(); foreach (var vpnConfig in _vpnConfig) { var renderedConfig = vpnConfig.GetStringConfig().Result; configDictionary.Add(vpnConfig, renderedConfig); } // Iterate through each pending configuration. foreach (var configHolder in configDictionary) { // Get the properly formatted path of where the configuration will be stored.. var onDiskName = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".ovpn"); // Write the configuration to the disk. using (var writer = new StreamWriter(onDiskName)) writer.Write(configHolder.Value); // Keep track of the configuration path. _configsOnDisk.Add(onDiskName); // Log to the console. _logger.LogDebug($"OpenVPN init: wrote config to {onDiskName}, attempting to start 3rd party daemon."); // At this stage, we have the configs ready and on disk. Let us simply bootstrap the processes. StartDaemon(onDiskName); // Create MASQ rules for each config. _firewall.Rules.Masquerade(configHolder.Key.Listener.Network, defaultNetworkInterface.Name); } }