/// <summary> /// Starts the azure relay engine. /// </summary> public static void StartTunnelRelayEngine() { try { TunnelRelayEngine.InitializePlugins(); ServiceHost = new WebServiceHost(typeof(WCFContract)); ServiceHost.AddServiceEndpoint( typeof(WCFContract), new WebHttpRelayBinding( EndToEndWebHttpSecurityMode.Transport, RelayClientAuthenticationType.None), ApplicationData.Instance.ProxyBaseUrl) .EndpointBehaviors.Add( new TransportClientEndpointBehavior( TokenProvider.CreateSharedAccessSignatureTokenProvider( ApplicationData.Instance.ServiceBusKeyName, ApplicationData.Instance.ServiceBusSharedKey))); ServiceHost.Open(); // Ignore all HTTPs cert errors. We wanna do this after the call to Azure is made so that if Azure call presents wrong cert we bail out. ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback((sender, cert, chain, errs) => true); } catch (Exception ex) { Logger.LogError(CallInfo.Site(), ex, "Failed to start Service host"); throw; } }
/// <summary> /// Logouts this instance. /// </summary> public static void Logout() { Logger.LogInfo(CallInfo.Site(), "Logging out"); Instance = new ApplicationData { RedirectionUrl = "http://localhost:3979/", }; }
/// <summary> /// Initializes the plugin. /// </summary> public void InitializePlugin() { if (!this.isInitialized) { Logger.LogInfo(CallInfo.Site(), "Initializing '{0}'.", this.PluginInstance.PluginName); Task.Run(() => this.PluginInstance.Initialize()); this.isInitialized = true; } }
/// <summary> /// Stops the azure relay engine. /// </summary> public static void StopTunnelRelayEngine() { Logger.LogInfo(CallInfo.Site(), "Shutting down tunnel relay engine"); if (TunnelRelayEngine.ServiceHost != null) { // Serialize settings back to json. File.WriteAllText("appSettings.json", JsonConvert.SerializeObject(ApplicationData.Instance, Formatting.Indented)); // Shutdown the Relay. TunnelRelayEngine.ServiceHost.Close(); } }
/// <summary> /// Initializes static members of the <see cref="ApplicationData"/> class. /// </summary> static ApplicationData() { if (File.Exists("appSettings.json")) { Logger.LogInfo(CallInfo.Site(), "Loading existing settings"); Instance = JsonConvert.DeserializeObject <ApplicationData>(File.ReadAllText("appSettings.json")); } else { Logger.LogInfo(CallInfo.Site(), "Appsettings don't exist. Creating new one."); Instance = new ApplicationData { RedirectionUrl = "http://localhost:3979/", }; } }
/// <summary> /// Unprotects the specified data. /// </summary> /// <param name="data">The data.</param> /// <returns>Unprotected data.</returns> public static byte[] Unprotect(byte[] data) { try { return(ProtectedData.Unprotect(data, null, DataProtectionScope.LocalMachine)); } catch (CryptographicException cryptEx) { Logger.LogError(CallInfo.Site(), cryptEx, "Failed to decryt with Cryptographic exception"); throw; } catch (Exception ex) { Logger.LogError(CallInfo.Site(), ex, "Failed to decrypt"); throw; } }
/// <summary> /// Protects the specified data. /// </summary> /// <param name="data">The data.</param> /// <returns>Protected data</returns> public static byte[] Protect(byte[] data) { try { // Encrypt the data using DataProtectionScope.LocalMachine. The result can be decrypted // only on same machine. return(ProtectedData.Protect(data, null, DataProtectionScope.LocalMachine)); } catch (CryptographicException cryptEx) { Logger.LogError(CallInfo.Site(), cryptEx, "Failed to encrypt with Cryptographic exception"); throw; } catch (Exception ex) { Logger.LogError(CallInfo.Site(), ex, "Failed to encrypt"); throw; } }
/// <summary> /// Logouts this instance. /// </summary> public static void Logout() { Logger.LogInfo(CallInfo.Site(), "Logging out"); Instance = new ApplicationData(); }
/// <summary> /// Initializes the plugins. /// </summary> private static void InitializePlugins() { Plugins = new ObservableCollection <PluginDetails>(); pluginInstances.Add(new HeaderAdditionPlugin()); pluginInstances.Add(new HeaderRemovalPlugin()); string pluginDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Plugins"); if (Directory.Exists(pluginDirectory)) { Directory.EnumerateFiles(pluginDirectory, "*.dll").ToList().ForEach(dll => { try { Assembly assembly = Assembly.LoadFrom(dll); foreach (Type pluginType in assembly.GetExportedTypes().Where(type => type.GetInterfaces().Contains(typeof(ITunnelRelayPlugin)))) { Logger.LogInfo(CallInfo.Site(), "Loading plugin '{0}'", pluginType.FullName); pluginInstances.Add(Activator.CreateInstance(pluginType) as ITunnelRelayPlugin); } } catch (Exception ex) { Logger.LogError(CallInfo.Site(), ex); } }); } pluginInstances.ForEach(plugin => { PluginDetails pluginDetails = new PluginDetails { PluginInstance = plugin, PluginSettings = new ObservableCollection <PluginSettingDetails>(), IsEnabled = ApplicationData.Instance.EnabledPlugins.Contains(plugin.GetType().FullName), }; try { var settingProperties = plugin.GetType().GetProperties().Where(memberInfo => memberInfo.GetCustomAttribute(typeof(PluginSetting)) != null); foreach (var setting in settingProperties) { if (!setting.PropertyType.IsEquivalentTo(typeof(string))) { throw new InvalidDataException("Plugin settings can only be of string datatype"); } var pluginSetting = new PluginSettingDetails { AttributeData = setting.GetCustomAttribute <PluginSetting>(), PluginInstance = plugin, PropertyDetails = setting, }; if (ApplicationData.Instance.PluginSettingsMap.TryGetValue(pluginDetails.PluginInstance.GetType().FullName, out Dictionary <string, string> pluginSettingsVal)) { if (pluginSettingsVal.TryGetValue(pluginSetting.PropertyDetails.Name, out string propertyValue)) { pluginSetting.Value = propertyValue; } } pluginDetails.PluginSettings.Add(pluginSetting); } if (pluginDetails.IsEnabled) { pluginDetails.InitializePlugin(); } } catch (Exception ex) { Logger.LogError(CallInfo.Site(), ex); } Plugins.Add(pluginDetails); }); }