harmony 鸿蒙@ohos.enterprise.networkManager(网络管理)

2023-06-24 浏览 (811)

@ohos.enterprise.networkManager(网络管理)

本模块提供设备网络管理能力,包括查询设备IP地址、MAC地址信息等。

说明:

本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

本模块接口仅可在Stage模型下使用。

本模块接口仅对设备管理应用开放,需将设备管理应用激活后调用,实现相应功能。

导入模块

import networkManager from '@ohos.enterprise.networkManager';

networkManager.getAllNetworkInterfaces

getAllNetworkInterfaces(admin: Want, callback: AsyncCallback<Array<string>>): void

指定设备管理应用获取所有激活的网络接口。使用callback异步回调。

需要权限: ohos.permission.ENTERPRISE_GET_NETWORK_INFO

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

参数:

参数名类型必填说明
adminWant设备管理应用。
callbackAsyncCallback<Array<string>>回调函数。当接口调用成功,err为null,data为网络接口名称数组,否则err为错误对象。

错误码

以下错误码的详细介绍请参见企业设备管理错误码

错误码ID错误信息
9200001the application is not an administrator of the device.
9200002the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};

networkManager.getAllNetworkInterfaces(wantTemp, (err, result) => {
  if (err) {
    console.error(`Failed to get all network interfaces. Code: ${err.code}, message: ${err.message}`);
    return;
  }
  console.info(`Succeeded in getting all network interfaces, result : ${JSON.stringify(result)}`);
});

networkManager.getAllNetworkInterfaces

getAllNetworkInterfaces(admin: Want): Promise<Array<string>>

指定设备管理应用获取所有激活的网络接口。使用Promise异步回调。

需要权限: ohos.permission.ENTERPRISE_GET_NETWORK_INFO

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

参数:

参数名类型必填说明
adminWant设备管理应用。

返回值:

类型说明
Promise<Array<string>>Promise结果,返回网络接口名称数组。

错误码

以下错误码的详细介绍请参见企业设备管理错误码

错误码ID错误信息
9200001the application is not an administrator of the device.
9200002the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};

networkManager.getAllNetworkInterfaces(wantTemp).then((result) => {
  console.info(`Succeeded in getting all network interfaces, result : ${JSON.stringify(result)}`);
}).catch((err: BusinessError) => {
  console.error(`Failed to get all network interfaces. Code: ${err.code}, message: ${err.message}`);
});

networkManager.getIpAddress

getIpAddress(admin: Want, networkInterface: string, callback: AsyncCallback<string>): void

指定设备管理应用根据网络接口获取设备IP地址。使用callback异步回调。

需要权限: ohos.permission.ENTERPRISE_GET_NETWORK_INFO

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

参数:

参数名类型必填说明
adminWant设备管理应用。
networkInterfacestring指定网络接口。
callbackAsyncCallback<string>回调函数。当接口调用成功,err为null,data为IP地址,否则err为错误对象。

错误码

以下错误码的详细介绍请参见企业设备管理错误码

错误码ID错误信息
9200001the application is not an administrator of the device.
9200002the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};

networkManager.getIpAddress(wantTemp, 'eth0', (err, result) => {
  if (err) {
    console.error(`Failed to get ip address. Code: ${err.code}, message: ${err.message}`);
    return;
  }
  console.info(`Succeeded in getting ip address, result : ${result}`);
});

networkManager.getIpAddress

getIpAddress(admin: Want, networkInterface: string): Promise<string>

指定设备管理应用根据网络接口获取设备IP地址。使用Promise异步回调。

需要权限: ohos.permission.ENTERPRISE_GET_NETWORK_INFO

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

参数:

参数名类型必填说明
adminWant设备管理应用。
networkInterfacestring指定网络接口。

返回值:

类型说明
Promise<string>Promise结果,返回设备IP地址。

错误码

以下错误码的详细介绍请参见企业设备管理错误码

错误码ID错误信息
9200001the application is not an administrator of the device.
9200002the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};

networkManager.getIpAddress(wantTemp, 'eth0').then((result) => {
  console.info(`Succeeded in getting ip address, result : ${result}`);
}).catch((err: BusinessError) => {
  console.error(`Failed to get ip address. Code: ${err.code}, message: ${err.message}`);
});

networkManager.getMac

getMac(admin: Want, networkInterface: string, callback: AsyncCallback<string>): void

指定设备管理应用根据网络接口获取设备MAC地址。使用callback异步回调。

需要权限: ohos.permission.ENTERPRISE_GET_NETWORK_INFO

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

参数:

参数名类型必填说明
adminWant设备管理应用。
networkInterfacestring指定网络接口。
callbackAsyncCallback<string>回调函数。当接口调用成功,err为null,data为设备MAC地址,否则err为错误对象。

错误码

以下错误码的详细介绍请参见企业设备管理错误码

错误码ID错误信息
9200001the application is not an administrator of the device.
9200002the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};

networkManager.getMac(wantTemp, 'eth0', (err, result) => {
  if (err) {
    console.error(`Failed to get mac. Code: ${err.code}, message: ${err.message}`);
    return;
  }
  console.info(`Succeeded in getting mac, result : ${result}`);
});

networkManager.getMac

getMac(admin: Want, networkInterface: string): Promise<string>;

指定设备管理应用根据网络接口获取设备MAC地址。使用Promise异步回调。

需要权限: ohos.permission.ENTERPRISE_GET_NETWORK_INFO

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

参数:

参数名类型必填说明
adminWant设备管理应用。
networkInterfacestring指定网络接口。

返回值:

类型说明
Promise<string>Promise结果,返回设备MAC地址。

错误码

以下错误码的详细介绍请参见企业设备管理错误码

错误码ID错误信息
9200001the application is not an administrator of the device.
9200002the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};

networkManager.getMac(wantTemp, 'eth0').then((result) => {
  console.info(`Succeeded in getting mac, result : ${result}`);
}).catch((err: BusinessError) => {
  console.error(`Failed to get mac. Code: ${err.code}, message: ${err.message}`);
});

networkManager.isNetworkInterfaceDisabled

isNetworkInterfaceDisabled(admin: Want, networkInterface: string, callback: AsyncCallback<boolean>): void

指定设备管理应用查询指定网络接口是否被禁用。使用callback异步回调。

需要权限: ohos.permission.ENTERPRISE_GET_NETWORK_INFO

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

参数:

参数名类型必填说明
adminWant设备管理应用。
networkInterfacestring指定网络接口。
callbackAsyncCallback<boolean>回调函数。当接口调用成功,err为null,data为指定网络接口是否被禁用,true表示该网络接口被禁用,false表示该网络接口未被禁用,否则err为错误对象。

错误码

以下错误码的详细介绍请参见企业设备管理错误码

错误码ID错误信息
9200001the application is not an administrator of the device.
9200002the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};

networkManager.isNetworkInterfaceDisabled(wantTemp, 'eth0', (err, result) => {
  if (err) {
    console.error(`Failed to query network interface is disabled or not. Code: ${err.code}, message: ${err.message}`);
    return;
  }
  console.info(`Succeeded in querying network interface is disabled or not, result : ${result}`);
});

networkManager.isNetworkInterfaceDisabled

isNetworkInterfaceDisabled(admin: Want, networkInterface: string): Promise<boolean>

指定设备管理应用查询指定网络接口是否被禁用。使用Promise异步回调。

需要权限: ohos.permission.ENTERPRISE_GET_NETWORK_INFO

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

参数:

参数名类型必填说明
adminWant设备管理应用。
networkInterfacestring指定网络接口。

返回值:

类型说明
Promise<boolean>Promise结果,返回指定网络接口是否被禁用,true表示该网络接口被禁用,false表示该网络接口未被禁用。

错误码

以下错误码的详细介绍请参见企业设备管理错误码

错误码ID错误信息
9200001the application is not an administrator of the device.
9200002the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};

networkManager.isNetworkInterfaceDisabled(wantTemp, 'eth0').then((result) => {
  console.info(`Succeeded in querying network interface is disabled or not, result : ${result}`);
}).catch((err: BusinessError) => {
  console.error(`Failed to query network interface is disabled or not. Code: ${err.code}, message: ${err.message}`);
});

networkManager.setNetworkInterfaceDisabled

setNetworkInterfaceDisabled(admin: Want, networkInterface: string, isDisabled: boolean, callback: AsyncCallback<void>): void

指定设备管理应用禁止设备使用指定网络。使用callback异步回调。

需要权限: ohos.permission.ENTERPRISE_SET_NETWORK

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

参数:

参数名类型必填说明
adminWant设备管理应用。
networkInterfacestring指定网络接口。
isDisabledbooleantrue表示禁用该网络接口,false表示开启该网络接口。
callbackAsyncCallback<void>回调函数。当接口调用成功,err为null,否则err为错误对象。

错误码

以下错误码的详细介绍请参见企业设备管理错误码

错误码ID错误信息
9200001the application is not an administrator of the device.
9200002the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};

networkManager.setNetworkInterfaceDisabled(wantTemp, 'eth0', true, (err) => {
  if (err) {
    console.error(`Failed to set network interface disabled. Code: ${err.code}, message: ${err.message}`);
    return;
  }
  console.info(`Succeeded in setting network interface disabled`);
});

networkManager.setNetworkInterfaceDisabled

setNetworkInterfaceDisabled(admin: Want, networkInterface: string, isDisabled: boolean): Promise<void>

指定设备管理应用禁止设备使用指定网络。使用Promise异步回调。

需要权限: ohos.permission.ENTERPRISE_SET_NETWORK

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

参数:

参数名类型必填说明
adminWant设备管理应用。
networkInterfacestring指定网络接口。
isDisabledbooleantrue表示禁用该网络接口,false表示开启该网络接口。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。当禁用网络接口失败时抛出错误对象

错误码

以下错误码的详细介绍请参见企业设备管理错误码

错误码ID错误信息
9200001the application is not an administrator of the device.
9200002the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};

networkManager.setNetworkInterfaceDisabled(wantTemp, 'eth0', true).then(() => {
  console.info(`Succeeded in setting network interface disabled`);
}).catch((err: BusinessError) => {
  console.error(`Failed to set network interface disabled. Code: ${err.code}, message: ${err.message}`);
});

networkManager.setGlobalProxy

setGlobalProxy(admin: Want, httpProxy: connection.HttpProxy, callback: AsyncCallback<void>): void

指定设备管理应用设置网络全局代理,使用callback异步回调。

需要权限: ohos.permission.ENTERPRISE_MANAGE_NETWORK

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

参数:

参数名类型必填说明
adminWant设备管理应用。
httpProxyconnection.HttpProxy网络全局Http代理配置信息。
callbackAsyncCallback<void>回调函数。当接口调用成功,err为null,否则err为错误对象。

错误码

以下错误码的详细介绍请参见企业设备管理错误码

错误码ID错误信息
9200001the application is not an administrator of the device.
9200002the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
import connection from '@ohos.net.connection';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
let exclusionStr: string = "192.168,baidu.com"
let exclusionArray: Array<string> = exclusionStr.split(',');
let httpProxy: connection.HttpProxy = {
  host: "192.168.xx.xxx",
  port: 8080,
  exclusionList: exclusionArray
};

networkManager.setGlobalProxy(wantTemp, httpProxy, (err) => {
  if (err) {
    console.error(`Failed to set network global proxy. Code: ${err.code}, message: ${err.message}`);
    return;
  }
  console.info(`Succeeded in setting network global proxy`);
});

networkManager.setGlobalProxy

setGlobalProxy(admin: Want, httpProxy: connection.HttpProxy): Promise<void>

指定设备管理应用设置网络全局代理,使用Promise异步回调。

需要权限: ohos.permission.ENTERPRISE_MANAGE_NETWORK

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

参数:

参数名类型必填说明
adminWant设备管理应用。
httpProxyconnection.HttpProxy网络全局Http代理配置信息。
isDisabledbooleantrue表示禁用该网络接口,false表示开启该网络接口。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。当设置网络全局代理失败时抛出错误对象

错误码

以下错误码的详细介绍请参见企业设备管理错误码

错误码ID错误信息
9200001the application is not an administrator of the device.
9200002the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';
import connection from '@ohos.net.connection';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
let exclusionStr: string = "192.168,baidu.com"
let exclusionArray: Array<string> = exclusionStr.split(',');
let httpProxy: connection.HttpProxy = {
  host: "192.168.xx.xxx",
  port: 8080,
  exclusionList: exclusionArray
};

networkManager.setGlobalProxy(wantTemp, httpProxy).then(() => {
  console.info(`Succeeded in setting network global proxy`);
}).catch((err: BusinessError) => {
  console.error(`Failed to set network global proxy. Code: ${err.code}, message: ${err.message}`);
});

networkManager.getGlobalProxy

getGlobalProxy(admin: Want, callback: AsyncCallback<connection.HttpProxy>): void

指定设备管理应用获取网络全局代理,使用callback异步回调。

需要权限: ohos.permission.ENTERPRISE_MANAGE_NETWORK

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

参数:

参数名类型必填说明
adminWant设备管理应用。
callbackAsyncCallback<connection.HttpProxy>回调函数。当接口调用成功,err为null,否则err为错误对象。

错误码

以下错误码的详细介绍请参见企业设备管理错误码

错误码ID错误信息
9200001the application is not an administrator of the device.
9200002the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};

networkManager.getGlobalProxy(wantTemp, (err, result) => {
  if (err) {
    console.error(`Failed to get network global proxy. Code: ${err.code}, message: ${err.message}`);
    return;
  }
  console.info(`Succeeded in getting network global proxy, result : ${JSON.stringify(result)}`);
});

networkManager.getGlobalProxy

getGlobalProxy(admin: Want): Promise<connection.HttpProxy>

指定设备管理应用获取网络全局代理,使用Promise异步回调。

需要权限: ohos.permission.ENTERPRISE_MANAGE_NETWORK

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

参数:

参数名类型必填说明
adminWant设备管理应用。

返回值:

类型说明
Promise<connection.HttpProxy>Promise对象,返回网络全局Http代理配置信息。

错误码

以下错误码的详细介绍请参见企业设备管理错误码

错误码ID错误信息
9200001the application is not an administrator of the device.
9200002the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};

networkManager.getGlobalProxy(wantTemp).then(() => {
  console.info(`Succeeded in getting network global proxy`);
}).catch((err: BusinessError) => {
  console.error(`Failed to get network global proxy. Code: ${err.code}, message: ${err.message}`);
});

networkManager.addIptablesFilterRule

addIptablesFilterRule(admin: Want, filterRule: AddFilterRule, callback: AsyncCallback<void>): void

指定设备管理应用为设备添加网络包过滤规则。使用callback异步回调。

需要权限: ohos.permission.ENTERPRISE_MANAGE_NETWORK

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

参数:

参数名类型必填说明
adminWant设备管理应用。
filterRuleAddFilterRule添加网络包过滤规则。
callbackAsyncCallback<void>回调函数。当接口调用成功,err为null,否则err为错误对象。

错误码

以下错误码的详细介绍请参见企业设备管理错误码

错误码ID错误信息
9200001the application is not an administrator of the device.
9200002the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
let filterRule: networkManager.AddFilterRule = {
  "ruleNo": 1,
  "srcAddr": "192.168.1.1-192.188.22.66",
  "destAddr": "10.1.1.1",
  "srcPort": "8080",
  "destPort": "8080",
  "uid": "9696",
  "method": networkManager.AddMethod.APPEND,
  "direction": networkManager.Direction.OUTPUT,
  "action": networkManager.Action.DENY,
  "protocol": networkManager.Protocol.UDP,
}

networkManager.addIptablesFilterRule(wantTemp, filterRule, (err) => {
  if (err) {
    console.error(`Failed to set iptables filter rule. Code: ${err.code}, message: ${err.message}`);
    return;
  }
  console.info(`Succeeded in setting iptables filter rule`);
});

networkManager.addIptablesFilterRule

addIptablesFilterRule(admin: Want, filterRule: AddFilterRule): Promise<void>

指定设备管理应用为设备添加网络包过滤规则。使用Promise异步回调。

需要权限: ohos.permission.ENTERPRISE_MANAGE_NETWORK

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

参数:

参数名类型必填说明
adminWant设备管理应用。
filterRuleAddFilterRule添加网络包过滤规则。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。当添加网络包过滤规则失败时抛出错误对象。

错误码

以下错误码的详细介绍请参见企业设备管理错误码

错误码ID错误信息
9200001the application is not an administrator of the device.
9200002the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
let filterRule: networkManager.AddFilterRule = {
  "ruleNo": 1,
  "srcAddr": "192.168.1.1-192.188.22.66",
  "destAddr": "10.1.1.1",
  "srcPort": "8080",
  "destPort": "8080",
  "uid": "9696",
  "method": networkManager.AddMethod.APPEND,
  "direction": networkManager.Direction.OUTPUT,
  "action": networkManager.Action.DENY,
  "protocol": networkManager.Protocol.UDP,
}

networkManager.addIptablesFilterRule(wantTemp, filterRule).then(() => {
  console.info(`Succeeded in setting iptables filter rule`);
}).catch((err: BusinessError) => {
  console.error(`Failed to set iptables filter rule. Code: ${err.code}, message: ${err.message}`);
});

networkManager.removeIptablesFilterRule

removeIptablesFilterRule(admin: Want, filterRule: RemoveFilterRule, callback: AsyncCallback<void>): void

指定设备管理应用移除网络包过滤规则。使用callback异步回调。

需要权限: ohos.permission.ENTERPRISE_MANAGE_NETWORK

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

参数:

参数名类型必填说明
adminWant设备管理应用。
filterRuleRemoveFilterRule移除网络包过滤规则。
callbackAsyncCallback<void>回调函数。当接口调用成功,err为null,否则err为错误对象。

错误码

以下错误码的详细介绍请参见企业设备管理错误码

错误码ID错误信息
9200001the application is not an administrator of the device.
9200002the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
let filterRule: networkManager.RemoveFilterRule = {
  "srcAddr": "192.168.1.1-192.188.22.66",
  "destAddr": "10.1.1.1",
  "srcPort": "8080",
  "destPort": "8080",
  "uid": "9696",
  "direction": networkManager.Direction.OUTPUT,
  "action": networkManager.Action.DENY,
  "protocol": networkManager.Protocol.UDP,
}

networkManager.removeIptablesFilterRule(wantTemp, filterRule, (err) => {
  if (err) {
    console.error(`Failed to remove iptables filter rule. Code: ${err.code}, message: ${err.message}`);
    return;
  }
  console.info(`Succeeded in removing iptables filter rule`);
});

networkManager.removeIptablesFilterRule

removeIptablesFilterRule(admin: Want, filterRule: RemoveFilterRule): Promise<void>

指定设备管理应用移除网络包过滤规则。使用Promise异步回调。

需要权限: ohos.permission.ENTERPRISE_MANAGE_NETWORK

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

参数:

参数名类型必填说明
adminWant设备管理应用。
filterRuleRemoveFilterRule移除网络包过滤规则。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。当移除网络包过滤规则失败时抛出错误对象。

错误码

以下错误码的详细介绍请参见企业设备管理错误码

错误码ID错误信息
9200001the application is not an administrator of the device.
9200002the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
let filterRule: networkManager.RemoveFilterRule = {
  "srcAddr": "192.168.1.1-192.188.22.66",
  "destAddr": "10.1.1.1",
  "srcPort": "8080",
  "destPort": "8080",
  "uid": "9696",
  "direction": networkManager.Direction.OUTPUT,
  "action": networkManager.Action.DENY,
  "protocol": networkManager.Protocol.UDP,
}

networkManager.removeIptablesFilterRule(wantTemp, filterRule).then(() => {
  console.info(`Succeeded in removing iptables filter rule`);
}).catch((err: BusinessError) => {
  console.error(`Failed to remove iptables filter rule. Code: ${err.code}, message: ${err.message}`);
});

networkManager.listIptablesFilterRules

listIptablesFilterRules(admin: Want, callback: AsyncCallback<string>): void

指定设备管理应用获取网络包过滤规则。使用callback异步回调。

需要权限: ohos.permission.ENTERPRISE_MANAGE_NETWORK

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

参数:

参数名类型必填说明
adminWant设备管理应用。
callbackAsyncCallback<string>回调函数。当接口调用成功,err为null,否则err为错误对象。

错误码

以下错误码的详细介绍请参见企业设备管理错误码

错误码ID错误信息
9200001the application is not an administrator of the device.
9200002the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};

networkManager.listIptablesFilterRules(wantTemp, (err, result) => {
  if (err) {
    console.error(`Failed to get iptables filter rule. Code: ${err.code}, message: ${err.message}`);
    return;
  }
  console.info(`Succeeded in getting iptables filter rule, result : ${result}`);
});

networkManager.listIptablesFilterRules

listIptablesFilterRules(admin: Want): Promise<string>

指定设备管理应用获取网络包过滤规则。使用Promise异步回调。

需要权限: ohos.permission.ENTERPRISE_MANAGE_NETWORK

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

参数:

参数名类型必填说明
adminWant设备管理应用。

返回值:

类型说明
Promise<string>Promise对象,返回网络包过滤规则。

错误码

以下错误码的详细介绍请参见企业设备管理错误码

错误码ID错误信息
9200001the application is not an administrator of the device.
9200002the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};

networkManager.listIptablesFilterRules(wantTemp).then((result) => {
  console.info(`Succeeded in getting iptables filter rule, result: ${result}`);
}).catch((err: BusinessError) => {
  console.error(`Failed to remove iptables filter rule. Code: ${err.code}, message: ${err.message}`);
});

AddFilterRule

添加网络包过滤规则

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

名称类型必填说明
ruleNonumber规则序号。
srcAddrstringip源地址。
destAddrstringip目标地址。
srcPortstringip源端口。
destPortstringip目标端口。
uidstring应用uid。
methodAddMethod添加策略。
directionDirection规则链。
actionAction接收或者丢弃数据包。
protocolProtocol网络协议。

RemoveFilterRule

移除网络包过滤规则

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

名称类型必填说明
srcAddrstringip源地址。
destAddrstringip目标地址。
srcPortstringip源端口。
destPortstringip目标端口。
uidstring应用uid。
directionDirection规则链。
actionAction接收或者丢弃数据包。
protocolProtocol网络协议。

AddMethod

添加网络包方法。

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

名称说明
APPEND0追加。
INSERT1插入。

Direction

规则链。

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

名称说明
INPUT0输入链。
OUTPUT1输出链。

Action

数据包的行为。

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

名称说明
ALLOW0接收数据包。
DENY1丢弃数据包。

Protocol

网络协议。

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

名称说明
ALL0全部网络协议。
TCP1网络协议TCP。
UDP2网络协议UDP。
ICMP3网络协议ICMP。

你可能感兴趣的鸿蒙文章

harmony 鸿蒙接口

harmony 鸿蒙系统公共事件定义(待停用)

harmony 鸿蒙系统公共事件定义

harmony 鸿蒙开发说明

harmony 鸿蒙企业设备管理概述(仅对系统应用开放)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager模块)

harmony 鸿蒙@ohos.distributedBundle (分布式包管理)

harmony 鸿蒙@ohos.bundle (Bundle模块)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (企业设备管理扩展能力)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/46f179dd019f458b9ea88bd7cb3f67e8