1# @ohos.net.vpnExtension (Enhanced VPN Management) (System API)
2
3This module implements virtual private network (VPN) management, such as starting and stopping a third-party VPN.
4Third-party VPNs refer to VPN services provided by third parties. They usually support more security and privacy functions and more comprehensive customization options.
5
6> **NOTE**
7> 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.
8> This topic describes only system APIs provided by the module. For details about its public APIs, see [@ohos.net.vpnExtension (VPN Extension)](js-apis-net-vpnExtension.md).
9
10## Modules to Import
11
12```js
13import { vpnExtension } from '@kit.NetworkKit';
14```
15
16
17## vpnExtension.setAlwaysOnVpnEnabled
18
19setAlwaysOnVpnEnabled(enable: boolean, bundleName: string): Promise\<void>
20
21Enables or disables the **always on** mode.
22
23**System API**: This is a system API.
24
25**Required permissions**: ohos.permission.MANAGE_VPN
26
27**System capability**: SystemCapability.Communication.NetManager.Vpn
28
29**Model restriction**: This API can be used only in the stage model.
30
31**Parameters**
32
33| Name    | Type   | Mandatory| Description                                                   |
34| ---------- | ------- | ---- | ------------------------------------------------------- |
35| enable     | boolean | Yes  | Whether to enable the **always on** mode.                                  |
36| bundleName | string  | Yes  | Bundle name of the application (generally a third-party application).|
37
38**Return value**
39
40| Type          | Description                   |
41| -------------- | ----------------------- |
42| Promise\<void> | Promise that returns no value.|
43
44**Error codes**
45
46| ID| Error Message                                |
47| --------- | ---------------------------------------- |
48| 201       | Permission denied.                       |
49| 202       | Non-system applications use system APIs. |
50| 401       | Parameter error.                         |
51
52**Example**
53Stage model:
54
55```ts
56import { vpnExtension } from '@kit.NetworkKit';
57import { Want } from '@kit.AbilityKit';
58import { BusinessError } from '@kit.BasicServicesKit';
59
60let want: Want = {
61  deviceId: "",
62  bundleName: 'com.example.myvpndemo',
63  abilityName: 'MyVpnExtAbility',
64};
65
66vpnExtension.setAlwaysOnVpnEnabled(true, want.bundleName).then(() => {
67  console.info('setAlwaysOnVpnEnabled success.');
68}).catch((err : BusinessError) => {
69  console.error('setAlwaysOnVpnEnabled fail, err-> ${JSON.stringify(err)}');
70});
71```
72
73## vpnExtension.isAlwaysOnVpnEnabled
74
75isAlwaysOnVpnEnabled(bundleName: string): Promise\<boolean>
76
77Obtains the status of the **always on** mode.
78
79**System API**: This is a system API.
80
81**Required permissions**: ohos.permission.MANAGE_VPN
82
83**System capability**: SystemCapability.Communication.NetManager.Vpn
84
85**Model restriction**: This API can be used only in the stage model.
86
87**Parameters**
88
89| Name    | Type  | Mandatory| Description                                                   |
90| ---------- | ------ | ---- | ------------------------------------------------------- |
91| bundleName | string | Yes  | Bundle name of the application (generally a third-party application).|
92
93**Return value**
94
95| Type             | Description                          |
96| ----------------- | ------------------------------ |
97| Promise\<boolean> | Promise used to return the result.|
98
99**Error codes**
100
101| ID| Error Message                                |
102| --------- | ---------------------------------------- |
103| 201       | Permission denied.                       |
104| 202       | Non-system applications use system APIs. |
105| 401       | Parameter error.                         |
106
107**Example**
108Stage model:
109
110```ts
111import { vpnExtension } from '@kit.NetworkKit';
112import { Want } from '@kit.AbilityKit';
113import { BusinessError } from '@kit.BasicServicesKit';
114
115let want: Want = {
116  deviceId: "",
117  bundleName: 'com.example.myvpndemo',
118  abilityName: 'MyVpnExtAbility',
119};
120
121vpnExtension.isAlwaysOnVpnEnabled(want.bundleName).then((data : boolean) => {
122  console.info('isAlwaysOnVpnEnabled success.');
123}).catch((err : BusinessError) => {
124  console.error('setAlwaysOnVpnEnabled fail, err-> ${JSON.stringify(err)}');
125});
126```
127
128## vpnExtension.updateVpnAuthorizedState
129
130updateVpnAuthorizedState(bundleName: string): boolean
131
132Updates the VPN pop-up authorization status.
133
134**System API**: This is a system API.
135
136**Required permissions**: ohos.permission.MANAGE_VPN
137
138**System capability**: SystemCapability.Communication.NetManager.Vpn
139
140**Model restriction**: This API can be used only in the stage model.
141
142**Parameters**
143
144| Name    | Type  | Mandatory| Description                                            |
145| ---------- | ------ | ---- | ------------------------------------------------ |
146| bundleName | string | Yes  | Bundle name of the application (generally a third-party application).|
147
148**Return value**
149
150| Type   | Description                                       |
151| ------- | ------------------------------------------- |
152| boolean | Boolean value indicating whether the VPN pop-up authorization status is successfully updated.|
153
154**Error codes**
155
156| ID| Error Message                                |
157| --------- | ---------------------------------------- |
158| 201       | Permission denied.                       |
159| 202       | Non-system applications use system APIs. |
160| 401       | Parameter error.                         |
161
162**Example**
163Stage model:
164
165```ts
166import { vpnExtension } from '@kit.NetworkKit';
167import { Want } from '@kit.AbilityKit';
168
169let want: Want = {
170  deviceId: "",
171  bundleName: 'com.example.myvpndemo',
172  abilityName: 'MyVpnExtAbility',
173};
174
175let result: boolean = vpnExtension.updateVpnAuthorizedState(want.bundleName);
176console.log("Result: "+ result);
177```
178