示例#1
0
        public FwErrorCode AddPort(int nPortNumber, NET_FW_IP_PROTOCOL_ ipProtocol, string strRegisterName)
        {
            if (_mFirewallProfile == null)
            {
                return(FwErrorCode.FwErrInitialized);
            }

            bool        bEnablePort = true;
            FwErrorCode nError      = IsPortEnabled(nPortNumber, ipProtocol, ref bEnablePort);

            if (nError != FwErrorCode.FwNoerror)
            {
                return(nError);
            }

            // Only add the port, if it isn't added to the collection
            if (bEnablePort == false)
            {
                // Retrieve the collection of globally open ports
                INetFwOpenPorts fwOpenPorts = _mFirewallProfile.GloballyOpenPorts;
                if (fwOpenPorts == null)
                {
                    return(FwErrorCode.FwErrGlobalOpenPorts);
                }

                // Create an instance of an open port
                Type typeFwPort = Type.GetTypeFromCLSID(new Guid("{0CA545C6-37AD-4A6C-BF92-9F7610067EF5}"));
                var  fwOpenPort = (INetFwOpenPort)Activator.CreateInstance(typeFwPort);

                // Set the port number
                fwOpenPort.Port = nPortNumber;

                // Set the IP Protocol
                fwOpenPort.Protocol = ipProtocol;

                // Set the registered name
                fwOpenPort.Name = strRegisterName;

                try
                {
                    fwOpenPorts.Add(fwOpenPort);
                }
                catch
                {
                    return(FwErrorCode.FwErrAddToCollection);
                }
            }
            else
            {
                return(FwErrorCode.FwErrSamePortExist);
            }

            return(FwErrorCode.FwNoerror);
        }
示例#2
0
        public FwErrorCode AddApplication(string strProcessImageFileName, string strRegisterName)
        {
            if (_mFirewallProfile == null)
            {
                return(FwErrorCode.FwErrInitialized);
            }

            if (strProcessImageFileName.Length == 0 || strRegisterName.Length == 0)
            {
                return(FwErrorCode.FwErrInvalidArg);
            }

            // First of all, check the application is already authorized;
            bool        bAppEnable = true;
            FwErrorCode nError     = IsAppEnabled(strProcessImageFileName, ref bAppEnable);

            if (nError != FwErrorCode.FwNoerror)
            {
                return(nError);
            }

            // Only add the application if it isn't authorized
            if (bAppEnable == false)
            {
                // Retrieve the authorized application collection
                INetFwAuthorizedApplications fwApps = _mFirewallProfile.AuthorizedApplications;

                if (fwApps == null)
                {
                    return(FwErrorCode.FwErrAuthApplications);
                }

                // Create an instance of an authorized application
                Type typeFwApp = Type.GetTypeFromCLSID(new Guid("{EC9846B3-2762-4A6B-A214-6ACB603462D2}"));

                var fwApp = (INetFwAuthorizedApplication)Activator.CreateInstance(typeFwApp);

                // Set the process image file name
                fwApp.ProcessImageFileName = strProcessImageFileName;
                fwApp.Name = strRegisterName;

                try
                {
                    fwApps.Add(fwApp);
                }
                catch
                {
                    return(FwErrorCode.FwErrAddToCollection);
                }
            }

            return(FwErrorCode.FwNoerror);
        }
示例#3
0
        public FwErrorCode RemoveApplication(string strProcessImageFileName)
        {
            if (_mFirewallProfile == null)
            {
                return(FwErrorCode.FwErrInitialized);
            }
            if (strProcessImageFileName.Length == 0)
            {
                return(FwErrorCode.FwErrInvalidArg);
            }

            bool        bAppEnable = true;
            FwErrorCode nError     = IsAppEnabled(strProcessImageFileName, ref bAppEnable);

            if (nError != FwErrorCode.FwNoerror)
            {
                return(nError);
            }

            // Only remove the application if it is authorized
            if (bAppEnable)
            {
                // Retrieve the authorized application collection
                INetFwAuthorizedApplications fwApps = _mFirewallProfile.AuthorizedApplications;
                if (fwApps == null)
                {
                    return(FwErrorCode.FwErrAuthApplications);
                }

                try
                {
                    fwApps.Remove(strProcessImageFileName);
                }
                catch
                {
                    return(FwErrorCode.FwErrRemoveFromCollection);
                }
            }

            return(FwErrorCode.FwNoerror);
        }
示例#4
0
        public FwErrorCode TurnOnWindowsFirewall()
        {
            if (_mFirewallProfile == null)
            {
                return(FwErrorCode.FwErrInitialized);
            }

            // Check whether the firewall is off
            FwErrorCode ret = IsWindowsFirewallOn(out bool bFwOn);

            if (ret != FwErrorCode.FwNoerror)
            {
                return(ret);
            }

            // If it is off now, turn it on
            if (!bFwOn)
            {
                _mFirewallProfile.FirewallEnabled = true;
            }

            return(FwErrorCode.FwNoerror);
        }
示例#5
0
        public FwErrorCode RemovePort(int nPortNumber, NET_FW_IP_PROTOCOL_ ipProtocol)
        {
            if (_mFirewallProfile == null)
            {
                return(FwErrorCode.FwErrInitialized);
            }

            bool        bEnablePort = false;
            FwErrorCode nError      = IsPortEnabled(nPortNumber, ipProtocol, ref bEnablePort);

            if (nError != FwErrorCode.FwNoerror)
            {
                return(nError);
            }

            // Only remove the port, if it is on the collection
            if (bEnablePort)
            {
                // Retrieve the collection of globally open ports
                INetFwOpenPorts fwOpenPorts = _mFirewallProfile.GloballyOpenPorts;
                if (fwOpenPorts == null)
                {
                    return(FwErrorCode.FwErrGlobalOpenPorts);
                }

                try
                {
                    fwOpenPorts.Remove(nPortNumber, ipProtocol);
                }
                catch
                {
                    return(FwErrorCode.FwErrRemoveFromCollection);
                }
            }

            return(FwErrorCode.FwNoerror);
        }