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