openharmony 鸿蒙 js-apis-net-vpnExtension-sys

2025-06-12 浏览 (2)

@ohos.net.vpnExtension (Enhanced VPN Management) (System API)

This module implements virtual private network (VPN) management, such as starting and stopping a third-party VPN. Third-party VPNs refer to VPN services provided by third parties. They usually support more security and privacy functions and more comprehensive customization options.

NOTE The initial APIs of this module are supported since API version 11. Newly added APIs will be marked with a superscript to indicate their earliest API version. This topic describes only system APIs provided by the module. For details about its public APIs, see @ohos.net.vpnExtension (VPN Extension).

Modules to Import

import { vpnExtension } from '@kit.NetworkKit';

vpnExtension.setAlwaysOnVpnEnabled

setAlwaysOnVpnEnabled(enable: boolean, bundleName: string): Promise<void>

Enables or disables the always on mode.

System API: This is a system API.

Required permissions: ohos.permission.MANAGE_VPN

System capability: SystemCapability.Communication.NetManager.Vpn

Model restriction: This API can be used only in the stage model.

Parameters

NameTypeMandatoryDescription
enablebooleanYesWhether to enable the always on mode.
bundleNamestringYesBundle name of the application (generally a third-party application).

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.

Example Stage model:

import { vpnExtension } from '@kit.NetworkKit';
import { Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

let want: Want = {
  deviceId: "",
  bundleName: 'com.example.myvpndemo',
  abilityName: 'MyVpnExtAbility',
};

vpnExtension.setAlwaysOnVpnEnabled(true, want.bundleName).then(() => {
  console.info('setAlwaysOnVpnEnabled success.');
}).catch((err : BusinessError) => {
  console.error('setAlwaysOnVpnEnabled fail, err-> ${JSON.stringify(err)}');
});

vpnExtension.isAlwaysOnVpnEnabled

isAlwaysOnVpnEnabled(bundleName: string): Promise<boolean>

Obtains the status of the always on mode.

System API: This is a system API.

Required permissions: ohos.permission.MANAGE_VPN

System capability: SystemCapability.Communication.NetManager.Vpn

Model restriction: This API can be used only in the stage model.

Parameters

NameTypeMandatoryDescription
bundleNamestringYesBundle name of the application (generally a third-party application).

Return value

TypeDescription
Promise<boolean>Promise used to return the result.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.

Example Stage model:

import { vpnExtension } from '@kit.NetworkKit';
import { Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

let want: Want = {
  deviceId: "",
  bundleName: 'com.example.myvpndemo',
  abilityName: 'MyVpnExtAbility',
};

vpnExtension.isAlwaysOnVpnEnabled(want.bundleName).then((data : boolean) => {
  console.info('isAlwaysOnVpnEnabled success.');
}).catch((err : BusinessError) => {
  console.error('setAlwaysOnVpnEnabled fail, err-> ${JSON.stringify(err)}');
});

vpnExtension.updateVpnAuthorizedState

updateVpnAuthorizedState(bundleName: string): boolean

Updates the VPN pop-up authorization status.

System API: This is a system API.

Required permissions: ohos.permission.MANAGE_VPN

System capability: SystemCapability.Communication.NetManager.Vpn

Model restriction: This API can be used only in the stage model.

Parameters

NameTypeMandatoryDescription
bundleNamestringYesBundle name of the application (generally a third-party application).

Return value

TypeDescription
booleanBoolean value indicating whether the VPN pop-up authorization status is successfully updated.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.

Example Stage model:

import { vpnExtension } from '@kit.NetworkKit';
import { Want } from '@kit.AbilityKit';

let want: Want = {
  deviceId: "",
  bundleName: 'com.example.myvpndemo',
  abilityName: 'MyVpnExtAbility',
};

let result: boolean = vpnExtension.updateVpnAuthorizedState(want.bundleName);
console.log("Result: "+ result);

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Network Kit

harmony 鸿蒙NetConn_ConnectionProperties

harmony 鸿蒙NetConn_HttpProxy

harmony 鸿蒙NetConn_NetAddr

harmony 鸿蒙NetConn_NetCapabilities

harmony 鸿蒙NetConn_NetConnCallback

harmony 鸿蒙NetConn_NetHandle

harmony 鸿蒙NetConn_NetHandleList

harmony 鸿蒙NetConn_NetSpecifier

harmony 鸿蒙NetConn_Route

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