private static void ConfigureFirewall(ApplicationConfiguration configuration, bool silent, bool remove)
{
Utils.Trace(Utils.TraceMasks.Information, "Configuring firewall.");
// check for ports to open/close.
StringCollection baseAddresses = new StringCollection();
if (configuration.ServerConfiguration != null)
{
baseAddresses = configuration.ServerConfiguration.BaseAddresses;
}
if (configuration.DiscoveryServerConfiguration != null)
{
baseAddresses = configuration.DiscoveryServerConfiguration.BaseAddresses;
}
// remove access.
if (remove)
{
try
{
ConfigUtils.RemoveFirewallAccess(Application.ExecutablePath, baseAddresses);
}
catch (Exception e)
{
Utils.Trace(e, "Unexpected error while removing firewall access.");
}
return;
}
// enable access.
try
{
// check if firewall needs configuration.
if (!ConfigUtils.CheckFirewallAccess(Application.ExecutablePath, baseAddresses))
{
bool configure = true;
if (!silent)
{
string message = "The firewall has not been configured to allow external access to the server. Configure firewall?";
if (MessageBox.Show(message, configuration.ApplicationName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
{
configure = false;
}
}
if (configure)
{
ConfigUtils.SetFirewallAccess(configuration.ApplicationName, Application.ExecutablePath, baseAddresses);
}
}
}
catch (Exception e)
{
Utils.Trace(e, "Unexpected error while checking or changing the firewall configuration.");
}
}