Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
The network firewall module provides the following functions:
To ensure app running efficiency, all API calls are asynchronous. For asynchronous APIs, a promise is provided. The following examples use the promise mode. For more modes, see @ohos.net.netFirewall (Network Firewall).
Typical firewall scenarios include:
The following describes the development procedure specific to each application scenario.
Use a network cable to connect the device to a network port.
Import the netFirewall namespace from @kit.NetworkKit.
// Import the netFirewall namespace from @kit.NetworkKit.
import { netFirewall } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';Call setNetFirewallPolicy to enable the firewall.
// IP address type
interface IpType{
family:number;
type:number;
address?:string;
mask?:number;
startIp?:string;
endIp?:string;
}
// Port number
interface IpPort{
startPort:number;
endPort:number;
}
// ...
// Define the firewall policy to enable the firewall and deny inbound traffic while allowing outbound traffic.
let policy: netFirewall.NetFirewallPolicy = {
isOpen: true,
inAction: netFirewall.FirewallRuleAction.RULE_DENY,
outAction: netFirewall.FirewallRuleAction.RULE_ALLOW
};
// Set the firewall policy for user 100.
netFirewall.setNetFirewallPolicy(100, policy).then(() => {
hilog.info(0x0000, 'testTag', `set firewall policy success.`);
}).catch((error : BusinessError) => {
hilog.error(0x0000, 'testTag', `error: set firewall policy failed: ${JSON.stringify(error)}`);
});Call addNetFirewallRule to add firewall rules.
// Initialize firewall rules for specific types of IP addresses.
let ipRule: netFirewall.NetFirewallRule = {
name: 'rule1',
description: 'rule1 description',
direction: netFirewall.NetFirewallRuleDirection.RULE_IN,
action: netFirewall.FirewallRuleAction.RULE_DENY,
type: netFirewall.NetFirewallRuleType.RULE_IP,
isEnabled: true,
appUid: 20001,
localIps: [
{
family: 1,
type: 1,
address: '10.10.1.1',
mask: 32
},{
family: 1,
type: 2,
startIp: '10.20.1.1',
endIp: '10.20.1.10'
}] as IpType[],
remoteIps:[
{
family: 1,
type: 1,
address: '20.10.1.1',
mask: 32
},{
family: 1,
type: 2,
startIp: '20.20.1.1',
endIp: '20.20.1.10'
}] as IpType[],
protocol: 6,
localPorts: [
{
startPort: 1000,
endPort: 1000
},{
startPort: 2000,
endPort: 2001
}] as IpPort[],
remotePorts: [
{
startPort: 443,
endPort: 443
}] as IpPort[],
userId: 100,
interface:'wlan0' // Supported since API version 26.0.0.
};
// Add firewall rules.
netFirewall.addNetFirewallRule(ipRule).then((result: number) => {
// ...
hilog.info(0x0000, 'testTag', `rule Id: ${result}`);
}, (reason: BusinessError) => {
// ...
hilog.error(0x0000, 'testTag', `error: add firewall rule failed: ${JSON.stringify(reason)}`);
});Use a network cable to connect the device to a network port.
Import the netFirewall namespace from @kit.NetworkKit.
// Import the netFirewall namespace from @kit.NetworkKit.
import { netFirewall } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';Call setNetFirewallPolicy to enable the firewall.
interface domain{
isWildcard: boolean;
domain: string;
}
// ...
// Define the firewall policy to enable the firewall and deny inbound traffic while allowing outbound traffic.
let policy: netFirewall.NetFirewallPolicy = {
isOpen: true,
inAction: netFirewall.FirewallRuleAction.RULE_DENY,
outAction: netFirewall.FirewallRuleAction.RULE_ALLOW
};
// Set the firewall policy for user 100.
netFirewall.setNetFirewallPolicy(100, policy).then(() => {
hilog.info(0x0000, 'testTag', `set firewall policy success.`);
}).catch((error : BusinessError) => {
hilog.error(0x0000, 'testTag', `error: set firewall policy failed: ${JSON.stringify(error)}`);
});Call addNetFirewallRule to add firewall rules.
// Initialize firewall rules for specific types of domain names.
let domainRule: netFirewall.NetFirewallRule = {
name: 'rule2',
description: 'rule2 description',
direction: netFirewall.NetFirewallRuleDirection.RULE_IN,
action: netFirewall.FirewallRuleAction.RULE_DENY,
type: netFirewall.NetFirewallRuleType.RULE_DOMAIN,
isEnabled: true,
appUid: 20002,
domains: [
{
isWildcard: false,
domain: 'www.HarmonyOS.cn'
},{
isWildcard: true,
domain: '*.HarmonyOS.cn'
},{
isWildcard: true,
domain: '*w.HarmonyOS.cn' // Supported since API version 26.0.0.
},{
isWildcard: true,
domain: 'www.HarmonyOS.*' // Supported since API version 26.0.0.
},{
isWildcard: true,
domain: 'www.HarmonyOS.c*' // Supported since API version 26.0.0.
}] as domain[],
userId: 100,
interface:'wlan0' // Supported since API version 26.0.0.
};
// Add firewall rules.
netFirewall.addNetFirewallRule(domainRule).then((result: number) => {
// ...
hilog.info(0x0000, 'testTag', `rule Id: ${result}`);
}, (reason: BusinessError) => {
// ...
hilog.error(0x0000, 'testTag', `error: add firewall rule failed: ${JSON.stringify(reason)}`);
});