|
| 1 | +using System; |
| 2 | + |
| 3 | +namespace TaskSchedulerConfig |
| 4 | +{ |
| 5 | + class Firewall |
| 6 | + { |
| 7 | + internal static bool OldFirewall = Environment.OSVersion.Version.Major == 5; |
| 8 | + |
| 9 | + public enum Rule |
| 10 | + { |
| 11 | + FileAndPrinterSharing, |
| 12 | + RemoteTaskManagement |
| 13 | + } |
| 14 | + |
| 15 | + public Firewall(string server) |
| 16 | + { |
| 17 | + Rules = new RulesContainer(this); |
| 18 | + Type NetFwMgrType = Type.GetTypeFromProgID(OldFirewall ? "HNetCfg.FwMgr" : "HNetCfg.FwPolicy2", server, false); |
| 19 | + Instance = Activator.CreateInstance(NetFwMgrType); |
| 20 | + } |
| 21 | + |
| 22 | + public bool Enabled |
| 23 | + { |
| 24 | + get |
| 25 | + { |
| 26 | + if (OldFirewall) |
| 27 | + return Instance.LocalPolicy.CurrentProfile.FirewallEnabled; |
| 28 | + else |
| 29 | + { |
| 30 | + const int NET_FW_PROFILE2_DOMAIN = 1; |
| 31 | + const int NET_FW_PROFILE2_PRIVATE = 2; |
| 32 | + const int NET_FW_PROFILE2_PUBLIC = 4; |
| 33 | + |
| 34 | + bool result = false; |
| 35 | + int CurrentProfiles = Instance.CurrentProfileTypes; |
| 36 | + |
| 37 | + // The returned 'CurrentProfiles' bit mask can have more than 1 bit set if multiple profiles are active or current at the same time |
| 38 | + if ((CurrentProfiles & NET_FW_PROFILE2_DOMAIN) != 0 && Instance.FirewallEnabled(NET_FW_PROFILE2_DOMAIN)) |
| 39 | + result = true; |
| 40 | + if ((CurrentProfiles & NET_FW_PROFILE2_PRIVATE) != 0 && Instance.FirewallEnabled(NET_FW_PROFILE2_PRIVATE)) |
| 41 | + result = true; |
| 42 | + if ((CurrentProfiles & NET_FW_PROFILE2_PUBLIC) != 0 && Instance.FirewallEnabled(NET_FW_PROFILE2_PUBLIC)) |
| 43 | + result = true; |
| 44 | + return result; |
| 45 | + } |
| 46 | + } |
| 47 | + set |
| 48 | + { |
| 49 | + if (OldFirewall) |
| 50 | + { |
| 51 | + Instance.LocalPolicy.CurrentProfile.FirewallEnabled = value; |
| 52 | + } |
| 53 | + else |
| 54 | + { |
| 55 | + int CurrentProfiles = Instance.CurrentProfileTypes; |
| 56 | + Instance.set_FirewallEnabled(CurrentProfiles, value); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public RulesContainer Rules { get; } |
| 62 | + |
| 63 | + public dynamic Instance { get; } |
| 64 | + |
| 65 | + public class RulesContainer |
| 66 | + { |
| 67 | + private const int NET_FW_SERVICE_FILE_AND_PRINT = 0; |
| 68 | + private const string FPS = "File and Printer Sharing"; |
| 69 | + private const string RSTM = "Remote Scheduled Tasks Management"; |
| 70 | + |
| 71 | + private Firewall firewall; |
| 72 | + |
| 73 | + internal RulesContainer(Firewall f) { firewall = f; } |
| 74 | + |
| 75 | + public bool this[Rule rule] |
| 76 | + { |
| 77 | + get |
| 78 | + { |
| 79 | + if (Firewall.OldFirewall) |
| 80 | + { |
| 81 | + switch (rule) |
| 82 | + { |
| 83 | + case Rule.FileAndPrinterSharing: |
| 84 | + return firewall.Instance.LocalPolicy.CurrentProfile.Services.Item(NET_FW_SERVICE_FILE_AND_PRINT).Enabled; |
| 85 | + default: |
| 86 | + throw new IndexOutOfRangeException("Unrecognized rule"); |
| 87 | + } |
| 88 | + } |
| 89 | + else |
| 90 | + { |
| 91 | + switch (rule) |
| 92 | + { |
| 93 | + case Rule.FileAndPrinterSharing: |
| 94 | + return firewall.Instance.IsRuleGroupCurrentlyEnabled(FPS); |
| 95 | + case Rule.RemoteTaskManagement: |
| 96 | + return firewall.Instance.IsRuleGroupCurrentlyEnabled(RSTM); |
| 97 | + default: |
| 98 | + throw new IndexOutOfRangeException("Unrecognized rule"); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + set |
| 103 | + { |
| 104 | + if (Firewall.OldFirewall) |
| 105 | + { |
| 106 | + switch (rule) |
| 107 | + { |
| 108 | + case Rule.FileAndPrinterSharing: |
| 109 | + firewall.Instance.LocalPolicy.CurrentProfile.Services.Item(NET_FW_SERVICE_FILE_AND_PRINT).Enabled = value; |
| 110 | + break; |
| 111 | + default: |
| 112 | + throw new IndexOutOfRangeException("Unrecognized rule"); |
| 113 | + } |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + switch (rule) |
| 118 | + { |
| 119 | + case Rule.FileAndPrinterSharing: |
| 120 | + firewall.Instance.EnableRuleGroup(firewall.Instance.CurrentProfileTypes, FPS, value); |
| 121 | + break; |
| 122 | + case Rule.RemoteTaskManagement: |
| 123 | + firewall.Instance.EnableRuleGroup(firewall.Instance.CurrentProfileTypes, RSTM, value); |
| 124 | + break; |
| 125 | + default: |
| 126 | + throw new IndexOutOfRangeException("Unrecognized rule"); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments