# WinFirewall.Rule

Import: `import com.install4j.api.windows.WinFirewall;`

```java
public static class Rule
```

A Windows firewall rule.

## Methods

| Signature | Returns | Description |
|---|---|---|
| [`setGroupName(String)`](#setgroupname-string) | `Rule` | An optional group name for this rule. |
| [`setDescription(String)`](#setdescription-string) | `Rule` | An optional description for this rule. |
| [`setLocalPorts(String)`](#setlocalports-string) | `Rule` | A list of local port numbers or port ranges separated by commas. |
| [`setLocalAddresses(String)`](#setlocaladdresses-string) | `Rule` | One or more comma-delimited tokens specifying the local addresses from which the application can listen for traffic. |
| [`setRemotePorts(String)`](#setremoteports-string) | `Rule` | A list of remote port numbers or port ranges separated by commas. |
| [`setRemoteAddresses(String)`](#setremoteaddresses-string) | `Rule` | One or more comma-delimited tokens specifying the remote addresses from which the application can listen for traffic. |
| [`setProfileTypes(Collection<ProfileType>)`](#setprofiletypes-collection-profiletype) | `Rule` | The profiles this rule should be added for. |
| [`setOnlyActiveProfiles(boolean)`](#setonlyactiveprofiles-boolean) | `Rule` | Only add for profiles that are currently active. |
| [`setAvoidPublicProfile(boolean)`](#setavoidpublicprofile-boolean) | `Rule` | Only add for public profile if public profile is the only currently active profile. |
| [`setEnabled(boolean)`](#setenabled-boolean) | `Rule` | If this rule should be enabled when created. |

### setGroupName(String)

```java
public Rule setGroupName(String groupName)
```

An optional group name for this rule.


**Parameters:**

- `groupName` — the group name

**Returns:** this rule

### setDescription(String)

```java
public Rule setDescription(String description)
```

An optional description for this rule.


**Parameters:**

- `description` — the description

**Returns:** this rule

### setLocalPorts(String)

```java
public Rule setLocalPorts(String localPorts)
```

A list of local port numbers or port ranges separated by commas.

Example: 80, 8080, 5000-5100

The default is any ports.


**Parameters:**

- `localPorts` — the port list

**Returns:** this rule

### setLocalAddresses(String)

```java
public Rule setLocalAddresses(String localAddresses)
```

One or more comma-delimited tokens specifying the local addresses from which the application can listen for traffic. Valid tokens include:
- "*" or an empty value indicates any local address. If present, this must be the only token included.
- A valid IPv4 or IPv6 address.
- A subnet can be specified using either the subnet mask or network prefix notation. If neither a subnet mask nor a network prefix is specified, the subnet mask defaults to 255.255.255.255.
- An IPv4 or IPv6 address range in the format of "start address - end address" with no spaces included.

The default value is "*".


**Parameters:**

- `localAddresses` — the address string

**Returns:** this rule

### setRemotePorts(String)

```java
public Rule setRemotePorts(String remotePorts)
```

A list of remote port numbers or port ranges separated by commas.

Example: 80, 8080, 5000-5100

The default is any ports.


**Parameters:**

- `remotePorts` — the port list

**Returns:** this rule

### setRemoteAddresses(String)

```java
public Rule setRemoteAddresses(String remoteAddresses)
```

One or more comma-delimited tokens specifying the remote addresses from which the application can listen for traffic. Valid tokens include:
- "*" or an empty value indicates any remote address. If present, this must be the only token included.
- A valid IPv4 or IPv6 address.
- A subnet can be specified using either the subnet mask or network prefix notation. If neither a subnet mask nor a network prefix is specified, the subnet mask defaults to 255.255.255.255.
- An IPv4 or IPv6 address range in the format of "start address - end address" with no spaces included.
- "Defaultgateway"
- "DHCP"
- "DNS"
- "WINS"
- "LocalSubnet" indicates any local address on the local subnet. This token is not case-sensitive.

The default value is "*".


**Parameters:**

- `remoteAddresses` — the address string

**Returns:** this rule

### setProfileTypes(Collection<ProfileType>)

```java
public Rule setProfileTypes(Collection<ProfileType> profileTypes)
```

The profiles this rule should be added for.

The default value is `ProfileType.DOMAIN` and `ProfileType.PRIVATE`


**Parameters:**

- `profileTypes` — a collection of profile types

**Returns:** this rule

### setOnlyActiveProfiles(boolean)

```java
public Rule setOnlyActiveProfiles(boolean onlyActiveProfiles)
```

Only add for profiles that are currently active.

The default value is `false`.


**Parameters:**

- `onlyActiveProfiles` — the value

**Returns:** this object

### setAvoidPublicProfile(boolean)

```java
public Rule setAvoidPublicProfile(boolean avoidPublicProfile)
```

Only add for public profile if public profile is the only currently active profile.

The default value is `false`.


**Parameters:**

- `avoidPublicProfile` — the value

**Returns:** this object

### setEnabled(boolean)

```java
public Rule setEnabled(boolean enabled)
```

If this rule should be enabled when created.

The default value is `true`.


**Parameters:**

- `enabled` — the value

**Returns:** this object

## Constructors

| Signature | Description |
|---|---|
| [`Rule(String, String, RuleDirection, NetworkProtocol)`](#rule-string-string-ruledirection-networkprotocol) | Create a rule with the minimally required parameters. |

### Rule(String, String, RuleDirection, NetworkProtocol)

```java
public Rule(String executablePath, String ruleName, RuleDirection ruleDirection, NetworkProtocol networkProtocol)
```

Create a rule with the minimally required parameters.


**Parameters:**

- `executablePath` — The path to the executable this rule is created for.
- `ruleName` — The name of the firewall rule.
- `ruleDirection` — If the rule is an incoming or outgoing rule.
- `networkProtocol` — If this is a rule for TCP or UDP.

