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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
use crate::errors::WindowsFirewallError;
use crate::{
FirewallRule, FirewallRuleUpdate, add_rule, add_rule_if_not_exists, add_rule_or_update,
enable_rule, remove_rule, rule_exists, update_rule,
};
impl FirewallRule {
/// Adds a new firewall rule to the system.
///
/// This function creates and adds a new firewall rule based on the current instance's properties.
/// The rule is registered with the Windows Firewall, applying the specified settings such as
/// name, direction, action, and other parameters.
///
/// # Arguments
///
/// This function does not take any arguments, as it operates on the current instance's fields.
///
/// # Returns
///
/// This function returns a [`Result<(), WindowsFirewallError>`](WindowsFirewallError). If the rule is added successfully,
/// it returns `Ok(())`. In case of an error (e.g., COM initialization failure or failure to add rule),
/// it returns a [`WindowsFirewallError`].
///
/// # Errors
///
/// This function may return a [`WindowsFirewallError`] if there is a failure during:
/// - COM initialization [`WindowsFirewallError::CoInitializeExFailed`].
/// - Adding the firewall rule.
///
/// # Security
///
/// ⚠️ This function requires **administrative privileges**.
pub fn add(&self) -> Result<(), WindowsFirewallError> {
add_rule(self)
}
/// Adds a new firewall rule to the system only if a rule with the same name doesn't exist.
///
/// This function first checks if a rule with the given name exists, and if not,
/// adds the new rule to the Windows Firewall.
///
/// # Arguments
///
/// This function does not take any arguments, as it operates on the current instance's fields.
///
/// # Returns
///
/// This function returns a [`Result<bool, WindowsFirewallError>`](WindowsFirewallError). If the rule is added successfully,
/// it returns `Ok(true)`. If the rule already exists, it returns `Ok(false)`. In case of an error
/// (e.g., COM initialization failure or failure to add rule), it returns a [`WindowsFirewallError`].
///
/// # Errors
///
/// This function may return a [`WindowsFirewallError`] if there is a failure during:
/// - COM initialization [`WindowsFirewallError::CoInitializeExFailed`].
/// - Checking if the rule exists.
/// - Adding the firewall rule.
///
/// # Security
///
/// ⚠️ This function requires **administrative privileges**.
pub fn add_if_not_exists(&self) -> Result<bool, WindowsFirewallError> {
add_rule_if_not_exists(self)
}
/// Adds a new firewall rule to the system or updates an existing rule with the same name.
///
/// This function first checks if a rule with the given name exists. If it does, the function updates
/// the existing rule with the new settings. If the rule does not exist, it adds a new rule to the
/// Windows Firewall.
///
/// # Arguments
///
/// This function does not take any arguments, as it operates on the current instance's fields.
///
/// # Returns
///
/// This function returns a [`Result<bool, WindowsFirewallError>`](WindowsFirewallError). If the rule is added
/// successfully, it returns `Ok(true)`. If the rule already exists and was updated, it returns `Ok(false)`.
/// In case of an error (e.g., COM initialization failure, failure to add or update the rule), it returns a
/// [`WindowsFirewallError`].
///
/// # Errors
///
/// This function may return a [`WindowsFirewallError`] if there is a failure during:
/// - COM initialization [`WindowsFirewallError::CoInitializeExFailed`].
/// - Fetching the existing rule or adding the new rule.
/// - Updating the firewall rule.
///
/// # Security
///
/// ⚠️ This function requires **administrative privileges**.
pub fn add_or_update(&self) -> Result<bool, WindowsFirewallError> {
add_rule_or_update(self)
}
/// Deletes an existing firewall rule from the system.
///
/// This function removes a firewall rule identified by its name. Once deleted, the rule
/// can no longer be applied, and any associated settings are lost.
///
/// # Arguments
///
/// This function does not take any arguments, as it operates on the current instance's [`name`](FirewallRule::name) field.
///
/// # Returns
///
/// This function returns a [`Result<(), WindowsFirewallError>`](WindowsFirewallError). If the rule is removed successfully,
/// it returns `Ok(())`. In case of an error (e.g., COM initialization failure, rule not found),
/// it returns a [`WindowsFirewallError`].
///
/// # Errors
///
/// This function may return a [`WindowsFirewallError`] if there is a failure during:
/// - COM initialization [`WindowsFirewallError::CoInitializeExFailed`].
/// - Removing the rule.
///
/// # Security
///
/// ⚠️ This function requires **administrative privileges**.
pub fn remove(self) -> Result<(), WindowsFirewallError> {
remove_rule(&self.name)?;
Ok(())
}
/// Updates an existing firewall rule with new settings.
///
/// This function modifies an existing firewall rule based on the provided [`settings`](FirewallRuleUpdate).
/// It updates various properties such as name, direction, action, protocol, and addresses.
/// If the protocol is ICMP (IPv4 or IPv6), the function ensures that local and remote ports
/// are cleared, as they are not applicable to ICMP.
///
/// # Arguments
///
/// * `settings` - A reference to a [`FirewallRuleUpdate`] struct containing the new
/// configuration parameters for the firewall rule.
///
/// # Returns
///
/// This function returns a [`Result<(), WindowsFirewallError>`](WindowsFirewallError). If the rule is updated successfully,
/// it returns `Ok(())`. In case of an error (e.g., COM initialization failure, rule not found, or failure
/// to update the rule), it returns a [`WindowsFirewallError`].
///
/// # Errors
///
/// This function may return a [`WindowsFirewallError`] if there is a failure during:
/// - COM initialization [`WindowsFirewallError::CoInitializeExFailed`].
/// - Fetching the rule.
///
/// # Security
///
/// ⚠️ This function requires **administrative privileges**.
pub fn update(&mut self, settings: &FirewallRuleUpdate) -> Result<(), WindowsFirewallError> {
update_rule(&self.name, settings)?;
if let Some(name) = &settings.name {
self.name = name.clone();
}
if let Some(direction) = &settings.direction {
self.direction = *direction;
}
if let Some(enabled) = settings.enabled {
self.enabled = enabled;
}
if let Some(action) = &settings.action {
self.action = *action;
}
if let Some(description) = &settings.description {
self.description = Some(description.clone());
}
if let Some(application_name) = &settings.application_name {
self.application_name = Some(application_name.clone());
}
if let Some(service_name) = &settings.service_name {
self.service_name = Some(service_name.clone());
}
if let Some(protocol) = &settings.protocol {
if !protocol.is_tcp_or_udp() {
self.local_ports = None;
self.remote_ports = None;
}
if !protocol.is_icmp() {
self.icmp_types_and_codes = None;
}
self.protocol = Some(*protocol);
}
if let Some(local_ports) = &settings.local_ports {
self.local_ports = Some(local_ports.clone());
}
if let Some(remote_ports) = &settings.remote_ports {
self.remote_ports = Some(remote_ports.clone());
}
if let Some(local_addresses) = &settings.local_addresses {
self.local_addresses = Some(local_addresses.clone());
}
if let Some(remote_addresses) = &settings.remote_addresses {
self.remote_addresses = Some(remote_addresses.clone());
}
if let Some(icmp_types_and_codes) = &settings.icmp_types_and_codes {
self.icmp_types_and_codes = Some(icmp_types_and_codes.clone());
}
if let Some(interfaces) = &settings.interfaces {
self.interfaces = Some(interfaces.clone());
}
if let Some(interface_types) = &settings.interface_types {
self.interface_types = Some(interface_types.clone());
}
if let Some(grouping) = &settings.grouping {
self.grouping = Some(grouping.clone());
}
if let Some(profiles) = &settings.profiles {
self.profiles = Some(*profiles);
}
if let Some(edge_traversal) = &settings.edge_traversal {
self.edge_traversal = Some(*edge_traversal);
}
Ok(())
}
/// Enables or disables an existing firewall rule.
///
/// This function modifies the state of a firewall rule based on the `enable` parameter.
/// If `enable` is `true`, the rule is enabled; otherwise, it is disabled. The function
/// updates the `enabled` field accordingly.
///
/// # Arguments
///
/// * `enable` - A boolean indicating whether to enable (`true`) or disable (`false`) the rule.
///
/// # Returns
///
/// This function returns a [`Result<(), WindowsFirewallError>`](WindowsFirewallError). If the rule is updated successfully,
/// it returns `Ok(())`. If an error occurs (e.g., COM initialization failure, rule not found),
/// it returns a [`WindowsFirewallError`].
///
/// # Errors
///
/// This function may return a [`WindowsFirewallError`] if there is a failure during:
/// - COM initialization [`WindowsFirewallError::CoInitializeExFailed`].
/// - Fetching the rule.
/// - Enabling or disabling the rule.
///
/// # Security
///
/// ⚠️ This function requires **administrative privileges**.
pub fn enable(&mut self, enable: bool) -> Result<(), WindowsFirewallError> {
enable_rule(&self.name, enable)?;
self.enabled = enable;
Ok(())
}
/// Checks if a firewall rule with the given name exists.
///
/// This function initializes COM, creates a firewall policy object, and checks if a rule
/// with the specified name exists in the Windows Firewall rules list.
///
/// # Arguments
///
/// This function does not take any arguments, as it operates on the current instance's [`name`](FirewallRule::name) field.
///
/// # Returns
///
/// This function returns a [`Result<bool, WindowsFirewallError>`](WindowsFirewallError). If the rule exists, it returns `Ok(true)`,
/// otherwise it returns `Ok(false)`. In case of an error (e.g., COM initialization failure or issue
/// with firewall policy), it returns a [`WindowsFirewallError`].
///
/// # Errors
///
/// This function may return a [`WindowsFirewallError`] in case of failures during COM initialization
/// or while interacting with the firewall policy object.
///
/// # Security
///
/// This function does not require administrative privileges.
pub fn exists(&self) -> Result<bool, WindowsFirewallError> {
rule_exists(&self.name)
}
}