Example #1
0
        public static FirewallStatus IsFirewallEnabled()
        {
            INetFwPolicy2 fwPolicy2 = GetFwPolicy2();

            FirewallStatus firewallStatus  = FirewallStatus.Enabled;
            var            currentProfiles = fwPolicy2.CurrentProfileTypes;

            if (Convert.ToBoolean(currentProfiles & (int)NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PRIVATE) && !fwPolicy2.FirewallEnabled[NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PRIVATE])
            {
                firewallStatus = FirewallStatus.PrivateDisabled;
            }

            if (Convert.ToBoolean(currentProfiles & (int)NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC) && !fwPolicy2.FirewallEnabled[NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC])
            {
                if (firewallStatus == FirewallStatus.PrivateDisabled)
                {
                    firewallStatus = FirewallStatus.AllDisabled;
                }
                else
                {
                    firewallStatus = FirewallStatus.PublicDisabled;
                }
            }

            return(firewallStatus);
        }
Example #2
0
        public static bool EnableFirewall()
        {
            FirewallStatus state = Status(FirewallDomain.Domain);

            if (state == RemoteDesktop.FirewallStatus.Enabled)
            {
                return(true);
            }
            try {
                int exitcode = -1;
                Cmd.RunClose("netsh", "advfirewall set allprofiles state on", ref exitcode);
                bool r = exitcode == 0;
                if (r)
                {
                    Logger.OkDebugLine("enable firewall ok");
                }
                else
                {
                    Logger.WarnDebugLine("enable firewall failed, exitcode=" + exitcode);
                }
                return(r);
            }
            catch (Exception e) {
                Logger.ErrorDebugLine("enable firewall failed,因为异常", e);
                return(false);
            }
        }
Example #3
0
        public void DisableFirewallTest()
        {
            Firewall.DisableFirewall();
            FirewallStatus state = Firewall.Status(FirewallDomain.Domain);

            Assert.AreEqual(FirewallStatus.Disabled, state);
            state = Firewall.Status(FirewallDomain.Private);
            Assert.AreEqual(FirewallStatus.Disabled, state);
            state = Firewall.Status(FirewallDomain.Public);
            Assert.AreEqual(FirewallStatus.Disabled, state);
        }
Example #4
0
 public void EnableFirewallTest()
 {
     lock (_locker) {
         Firewall.EnableFirewall();
         FirewallStatus state = Firewall.Status(FirewallDomain.Domain);
         Assert.AreEqual(FirewallStatus.Enabled, state);
         state = Firewall.Status(FirewallDomain.Private);
         Assert.AreEqual(FirewallStatus.Enabled, state);
         state = Firewall.Status(FirewallDomain.Public);
         Assert.AreEqual(FirewallStatus.Enabled, state);
     }
 }
Example #5
0
        public static void AddRdpRule()
        {
            FirewallStatus state = Status(FirewallDomain.Domain);

            if (state == RemoteDesktop.FirewallStatus.Disabled)
            {
                return;
            }
            try {
                OpenPort($"{RdpRuleName}_TCP", RdpTcpPort, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP, RdpScope);
                OpenPort($"{RdpRuleName}_UDP", RdpUdpPort, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP, RdpScope);
            }
            catch (Exception e) {
                Logger.ErrorDebugLine(e);
            }
        }
Example #6
0
        public static bool IsRdpRuleExists()
        {
            FirewallStatus state = Status(FirewallDomain.Domain);

            if (state == RemoteDesktop.FirewallStatus.Disabled)
            {
                return(true);
            }
            try {
                INetFwPolicy2 policyManager = GetPolicyManager();
                return(policyManager.Rules.OfType <INetFwRule>().Any(x => x.Name.StartsWith(RdpRuleName)));
            }
            catch (Exception e) {
                Logger.ErrorDebugLine(e);
                return(true);
            }
        }
        public void ChangeProfileCommandUseTheFirewallClientToUpdateTheProfile()
        {
            // Arrange
            var client = new Mock<IFirewallClient>();
            var status = new FirewallStatus
            {
                IsEnabled = true,
            };
            client.Setup(f => f.Status).Returns(status);
            var subject = new MainWindowViewModel(client.Object);
            subject.Initialize();

            // Act
            subject.ChangeProfileCommand.Execute(FilteringProfile.LowFiltering);

            // Assert
            client.Verify(f => f.SetFilteringProfile(FilteringProfile.LowFiltering));
        }
        public void RegistersItselfForFirewallStatusChangeEventsOnLoaded()
        {
            // Arrange
            var client = new Mock<IFirewallClient>();
            var status = new FirewallStatus
            {
                IsEnabled = true,
            };
            client.Setup(f => f.Status).Returns(status);
            var subject = new MainWindowViewModel(client.Object);
            subject.Initialize();

            // Act
            subject.OnLoaded();
            client.Raise(f => f.StatusChanged += null, new FirewallStatus { IsEnabled = false });

            // Assert
            Assert.False(subject.FirewallStatus.IsEnabled);
        }
Example #9
0
        public static void AddMinerClientRule()
        {
            FirewallStatus state = Status(FirewallDomain.Domain);

            if (state == RemoteDesktop.FirewallStatus.Disabled)
            {
                return;
            }
            try {
                OpenPort($"{MinerClientRuleName}_TCP", NTKeyword.MinerClientPort, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP, MinerClientScope);
                OpenPort($"{MinerClientRuleName}_UDP", NTKeyword.MinerClientPort, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP, MinerClientScope);

                OpenPort($"{NTMinerDaemonRuleName}_TCP", NTKeyword.NTMinerDaemonPort, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP, MinerClientScope);
                OpenPort($"{NTMinerDaemonRuleName}_UDP", NTKeyword.NTMinerDaemonPort, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP, MinerClientScope);
            }
            catch (Exception e) {
                Logger.ErrorDebugLine(e);
            }
        }
        public void TheFilteringProfileIsRetriedWhenInitialized()
        {
            // Arrange
            var client = new Mock<IFirewallClient>();
            var status = new FirewallStatus
            {
                IsEnabled = true,
                InboundAction = FirewallAction.Block,
                OutboundAction = FirewallAction.Allow,
                NetworkProfile = NetworkProfile.Private
            };
            client.Setup(f => f.Status).Returns(status);
            var subject = new MainWindowViewModel(client.Object);

            // Act
            subject.Initialize();

            // Assert
            Assert.Equal(status.FilteringProfile, subject.FilteringProfile);
        }
Example #11
0
        public static void RemoveRdpRule()
        {
            FirewallStatus state = Status(FirewallDomain.Domain);

            if (state == RemoteDesktop.FirewallStatus.Disabled)
            {
                return;
            }
            try {
                INetFwOpenPorts openPorts = GetOpenPorts();
                openPorts.Remove(RdpTcpPort, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP);
                openPorts.Remove(RdpUdpPort, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP);

                INetFwPolicy2 policyManager = GetPolicyManager();
                policyManager.Rules.Remove(RdpRuleName);
            }
            catch (Exception e) {
                Logger.ErrorDebugLine(e);
            }
        }
        private void BlockButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(PortTextBox.Text) || string.IsNullOrEmpty(ProtocolTextBox.Text))
            {
                MessageBox.Show("Missing selection. Choose a result from the 'Find ports' section.", "Missing selection", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (string.IsNullOrEmpty(ForeignIPTextBox.Text))
            {
                MessageBox.Show("A foreign IP is required. Use the 'Listen for remote connections' button if the protocol is UDP and choose one of the IP addresses that may appear over time.", "No foreign IP", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            FirewallStatus status = WinFirewall.IsFirewallEnabled();

            if (status == FirewallStatus.PrivateDisabled || status == FirewallStatus.PublicDisabled || status == FirewallStatus.AllDisabled)
            {
                MessageBox.Show("Your windows firewall is not fully enabled which may prevent blocks from being effective. Change your firewall settings through the control panel. Use the 'Firewall Status Check' button for more information.", "Windows Firewall Check", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            WinFirewall.AddFirewallRule(ProtocolTextBox.Text == "UDP" ? NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP : NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP, ForeignIPTextBox.Text, PortTextBox.Text, ProcessPathTextBox.Text);
        }