1e41f4b71Sopenharmony_ci# @ohos.net.vpnExtension (Enhanced VPN Management)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciThis module implements virtual private network (VPN) management, such as starting and stopping a third-party VPN.
4e41f4b71Sopenharmony_ciThird-party VPNs refer to VPN services provided by third parties. They usually support more security and privacy functions and more comprehensive customization options.
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci> **NOTE**
7e41f4b71Sopenharmony_ci> 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.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci## Modules to Import
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci```js
12e41f4b71Sopenharmony_ciimport { vpnExtension } from '@kit.NetworkKit';
13e41f4b71Sopenharmony_ci```
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci## vpnExtension.startVpnExtensionAbility
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_cistartVpnExtensionAbility(want: Want): Promise\<void>
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ciEnables the VPN extension ability.
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core.
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci**Model restriction**: This API can be used only in the stage model.
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci**Parameters**
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci| Name| Type                               | Mandatory| Description              |
28e41f4b71Sopenharmony_ci| ------ | ----------------------------------- | ---- | ------------------ |
29e41f4b71Sopenharmony_ci| want   | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes  | Want information.|
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci**Return value**
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci| Type          | Description                   |
34e41f4b71Sopenharmony_ci| -------------- | ----------------------- |
35e41f4b71Sopenharmony_ci| Promise\<void> | Promise that returns no value.|
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci**Error codes**
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci| ID| Error Message                              |
40e41f4b71Sopenharmony_ci| --------- | -------------------------------------- |
41e41f4b71Sopenharmony_ci| 401       | If the input parameter is not valid parameter.|
42e41f4b71Sopenharmony_ci| 16000001  | The specified ability does not exist.  |
43e41f4b71Sopenharmony_ci| 16000002  | Incorrect ability type.                |
44e41f4b71Sopenharmony_ci| 16000006  | Cross-user operations are not allowed. |
45e41f4b71Sopenharmony_ci| 16000008  | The crowdtesting application expires.  |
46e41f4b71Sopenharmony_ci| 16000011  | The context does not exist.            |
47e41f4b71Sopenharmony_ci| 16000050  | Internal error.                        |
48e41f4b71Sopenharmony_ci| 16200001  | The caller has been released.          |
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci**Example**
51e41f4b71Sopenharmony_ciStage model:
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ci```ts
54e41f4b71Sopenharmony_ciimport { common, Want } from '@kit.AbilityKit';
55e41f4b71Sopenharmony_ciimport { vpnExtension } from '@kit.NetworkKit';
56e41f4b71Sopenharmony_ci
57e41f4b71Sopenharmony_cilet context = getContext(this) as common.VpnExtensionContext;
58e41f4b71Sopenharmony_cilet want: Want = {
59e41f4b71Sopenharmony_ci  deviceId: "",
60e41f4b71Sopenharmony_ci  bundleName: "com.example.myvpndemo",
61e41f4b71Sopenharmony_ci  abilityName: "MyVpnExtAbility",
62e41f4b71Sopenharmony_ci};
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci@Entry
65e41f4b71Sopenharmony_ci@Component
66e41f4b71Sopenharmony_cistruct Index {
67e41f4b71Sopenharmony_ci  @State message: string = 'Hello World'
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci  build() {
70e41f4b71Sopenharmony_ci    Row() {
71e41f4b71Sopenharmony_ci      Column() {
72e41f4b71Sopenharmony_ci        Text(this.message)
73e41f4b71Sopenharmony_ci          .fontSize(50)
74e41f4b71Sopenharmony_ci          .fontWeight(FontWeight.Bold).onClick(() => {
75e41f4b71Sopenharmony_ci          console.info("btn click") })
76e41f4b71Sopenharmony_ci        Button('Start Extension').onClick(() => {
77e41f4b71Sopenharmony_ci          vpnExtension.startVpnExtensionAbility(want);
78e41f4b71Sopenharmony_ci        }).width('70%').fontSize(45).margin(16)
79e41f4b71Sopenharmony_ci        }.width('100%')
80e41f4b71Sopenharmony_ci    }.height('100%')
81e41f4b71Sopenharmony_ci  }
82e41f4b71Sopenharmony_ci}
83e41f4b71Sopenharmony_ci```
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_ci## vpnExtension.stopVpnExtensionAbility
86e41f4b71Sopenharmony_ci
87e41f4b71Sopenharmony_cistopVpnExtensionAbility(want: Want): Promise\<void>
88e41f4b71Sopenharmony_ci
89e41f4b71Sopenharmony_ciStops the VPN extension ability.
90e41f4b71Sopenharmony_ci
91e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core.
92e41f4b71Sopenharmony_ci
93e41f4b71Sopenharmony_ci**Model restriction**: This API can be used only in the stage model.
94e41f4b71Sopenharmony_ci
95e41f4b71Sopenharmony_ci**Parameters**
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ci| Name| Type                               | Mandatory| Description            |
98e41f4b71Sopenharmony_ci| ------ | ----------------------------------- | ---- | ---------------- |
99e41f4b71Sopenharmony_ci| want   | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | Yes  | Want information.|
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_ci**Return value**
102e41f4b71Sopenharmony_ci
103e41f4b71Sopenharmony_ci| Type          | Description                   |
104e41f4b71Sopenharmony_ci| -------------- | ----------------------- |
105e41f4b71Sopenharmony_ci| Promise\<void> | Promise that returns no value.|
106e41f4b71Sopenharmony_ci
107e41f4b71Sopenharmony_ci**Error codes**
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci| ID| Error Message                              |
110e41f4b71Sopenharmony_ci| --------- | -------------------------------------- |
111e41f4b71Sopenharmony_ci| 401       | If the input parameter is not valid parameter.|
112e41f4b71Sopenharmony_ci| 16000001  | The specified ability does not exist.  |
113e41f4b71Sopenharmony_ci| 16000002  | Incorrect ability type.                |
114e41f4b71Sopenharmony_ci| 16000006  | Cross-user operations are not allowed. |
115e41f4b71Sopenharmony_ci| 16000011  | The context does not exist.            |
116e41f4b71Sopenharmony_ci| 16000050  | Internal error.                        |
117e41f4b71Sopenharmony_ci| 16200001  | The caller has been released.          |
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_ci**Example**
120e41f4b71Sopenharmony_ciStage model:
121e41f4b71Sopenharmony_ci
122e41f4b71Sopenharmony_ci```ts
123e41f4b71Sopenharmony_ciimport { common, Want } from '@kit.AbilityKit';
124e41f4b71Sopenharmony_ciimport { vpnExtension } from '@kit.NetworkKit';
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_cilet context = getContext(this) as common.VpnExtensionContext;
127e41f4b71Sopenharmony_cilet want: Want = {
128e41f4b71Sopenharmony_ci  deviceId: "",
129e41f4b71Sopenharmony_ci  bundleName: "com.example.myvpndemo",
130e41f4b71Sopenharmony_ci  abilityName: "MyVpnExtAbility",
131e41f4b71Sopenharmony_ci};
132e41f4b71Sopenharmony_ci
133e41f4b71Sopenharmony_ci@Entry
134e41f4b71Sopenharmony_ci@Component
135e41f4b71Sopenharmony_cistruct Index {
136e41f4b71Sopenharmony_ci  @State message: string = 'Hello World'
137e41f4b71Sopenharmony_ci
138e41f4b71Sopenharmony_ci  build() {
139e41f4b71Sopenharmony_ci    Row() {
140e41f4b71Sopenharmony_ci      Column() {
141e41f4b71Sopenharmony_ci        Text(this.message)
142e41f4b71Sopenharmony_ci          .fontSize(50)
143e41f4b71Sopenharmony_ci          .fontWeight(FontWeight.Bold).onClick(() => {
144e41f4b71Sopenharmony_ci          console.info("btn click") })
145e41f4b71Sopenharmony_ci        Button('Start Extension').onClick(() => {
146e41f4b71Sopenharmony_ci          vpnExtension.startVpnExtensionAbility(want);
147e41f4b71Sopenharmony_ci        }).width('70%').fontSize(45).margin(16)
148e41f4b71Sopenharmony_ci        Button('Stop Extension').onClick(() => {
149e41f4b71Sopenharmony_ci          console.info("btn end")
150e41f4b71Sopenharmony_ci          vpnExtension.stopVpnExtensionAbility(want);
151e41f4b71Sopenharmony_ci        }).width('70%').fontSize(45).margin(16)
152e41f4b71Sopenharmony_ci
153e41f4b71Sopenharmony_ci        }.width('100%')
154e41f4b71Sopenharmony_ci    }.height('100%')
155e41f4b71Sopenharmony_ci  }
156e41f4b71Sopenharmony_ci}
157e41f4b71Sopenharmony_ci```
158e41f4b71Sopenharmony_ci
159e41f4b71Sopenharmony_ci
160e41f4b71Sopenharmony_ci## vpnExtension.createVpnConnection
161e41f4b71Sopenharmony_ci
162e41f4b71Sopenharmony_cicreateVpnConnection(context: VpnExtensionContext): VpnConnection
163e41f4b71Sopenharmony_ci
164e41f4b71Sopenharmony_ciCreates a **VpnConnection** object.
165e41f4b71Sopenharmony_ci
166e41f4b71Sopenharmony_ci> **NOTE**
167e41f4b71Sopenharmony_ci>
168e41f4b71Sopenharmony_ci> Before calling **createVpnConnection**, call **startVpnExtensionAbility** to enable the VPN function.
169e41f4b71Sopenharmony_ci
170e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Vpn
171e41f4b71Sopenharmony_ci
172e41f4b71Sopenharmony_ci**Model restriction**: This API can be used only in the stage model.
173e41f4b71Sopenharmony_ci
174e41f4b71Sopenharmony_ci**Parameters**
175e41f4b71Sopenharmony_ci
176e41f4b71Sopenharmony_ci| Name | Type                                                        | Mandatory| Description          |
177e41f4b71Sopenharmony_ci| ------- | ------------------------------------------------------------ | ---- | -------------- |
178e41f4b71Sopenharmony_ci| context | [VpnExtensionContext](js-apis-inner-application-VpnExtensionContext.md) | Yes  | Specified context.|
179e41f4b71Sopenharmony_ci
180e41f4b71Sopenharmony_ci**Return value**
181e41f4b71Sopenharmony_ci
182e41f4b71Sopenharmony_ci| Type                           | Description                   |
183e41f4b71Sopenharmony_ci| :------------------------------ | :---------------------- |
184e41f4b71Sopenharmony_ci| [VpnConnection](#vpnconnection) | **VpnConnection** object.|
185e41f4b71Sopenharmony_ci
186e41f4b71Sopenharmony_ci**Error codes**
187e41f4b71Sopenharmony_ci
188e41f4b71Sopenharmony_ci| ID| Error Message        |
189e41f4b71Sopenharmony_ci| --------- | ---------------- |
190e41f4b71Sopenharmony_ci| 401       | Parameter error. |
191e41f4b71Sopenharmony_ci
192e41f4b71Sopenharmony_ci**Example**
193e41f4b71Sopenharmony_ciStage model:
194e41f4b71Sopenharmony_ci
195e41f4b71Sopenharmony_ci```ts
196e41f4b71Sopenharmony_ciimport { vpnExtension, VpnExtensionAbility } from '@kit.NetworkKit';
197e41f4b71Sopenharmony_ciimport { common, Want } from '@kit.AbilityKit';
198e41f4b71Sopenharmony_ci
199e41f4b71Sopenharmony_cilet context: vpnExtension.VpnExtensionContext;
200e41f4b71Sopenharmony_ciexport default class MyVpnExtAbility extends VpnExtensionAbility {
201e41f4b71Sopenharmony_ci  onCreate(want: Want) {
202e41f4b71Sopenharmony_ci    let VpnConnection : vpnExtension.VpnConnection = vpnExtension.createVpnConnection(context);
203e41f4b71Sopenharmony_ci    console.info("vpn createVpnConnection: " + JSON.stringify(VpnConnection));
204e41f4b71Sopenharmony_ci  }
205e41f4b71Sopenharmony_ci}
206e41f4b71Sopenharmony_ci```
207e41f4b71Sopenharmony_ci
208e41f4b71Sopenharmony_ci## VpnConnection
209e41f4b71Sopenharmony_ci
210e41f4b71Sopenharmony_ciDefines a **VpnConnection** object. Before calling **VpnConnection** APIs, you need to create a VPN connection object by calling **vpnExt.createVpnConnection**.
211e41f4b71Sopenharmony_ci
212e41f4b71Sopenharmony_ci### create
213e41f4b71Sopenharmony_ci
214e41f4b71Sopenharmony_cicreate(config: VpnConfig): Promise\<number\>
215e41f4b71Sopenharmony_ci
216e41f4b71Sopenharmony_ciCreates a VPN based on the specified configuration. This API uses a promise to return the result.
217e41f4b71Sopenharmony_ci
218e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Vpn
219e41f4b71Sopenharmony_ci
220e41f4b71Sopenharmony_ci**Parameters**
221e41f4b71Sopenharmony_ci
222e41f4b71Sopenharmony_ci| Name| Type                   | Mandatory| Description                     |
223e41f4b71Sopenharmony_ci| ------ | ----------------------- | ---- | ------------------------- |
224e41f4b71Sopenharmony_ci| config | [VpnConfig](#vpnconfig) | Yes  | VPN configuration.|
225e41f4b71Sopenharmony_ci
226e41f4b71Sopenharmony_ci**Return value**
227e41f4b71Sopenharmony_ci
228e41f4b71Sopenharmony_ci| Type             | Description                                                          |
229e41f4b71Sopenharmony_ci| ----------------- | -------------------------------------------------------------- |
230e41f4b71Sopenharmony_ci| Promise\<number\> | Promise used to return the result, which is the file descriptor of the vNIC.|
231e41f4b71Sopenharmony_ci
232e41f4b71Sopenharmony_ci**Error codes**
233e41f4b71Sopenharmony_ci
234e41f4b71Sopenharmony_ci| ID| Error Message                                        |
235e41f4b71Sopenharmony_ci| --------- | ------------------------------------------------ |
236e41f4b71Sopenharmony_ci| 401       | Parameter error.                                 |
237e41f4b71Sopenharmony_ci| 2200001   | Invalid parameter value.                         |
238e41f4b71Sopenharmony_ci| 2200002   | Operation failed. Cannot connect to service.     |
239e41f4b71Sopenharmony_ci| 2200003   | System internal error.                           |
240e41f4b71Sopenharmony_ci| 2203001   | VPN creation denied, please check the user type. |
241e41f4b71Sopenharmony_ci| 2203002   | VPN exist already, please execute destroy first. |
242e41f4b71Sopenharmony_ci
243e41f4b71Sopenharmony_ci**Example**
244e41f4b71Sopenharmony_ci
245e41f4b71Sopenharmony_ci```js
246e41f4b71Sopenharmony_ciimport { vpnExtension, VpnExtensionAbility } from '@kit.NetworkKit';
247e41f4b71Sopenharmony_ciimport { common, Want } from '@kit.AbilityKit';
248e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit';
249e41f4b71Sopenharmony_ci
250e41f4b71Sopenharmony_cilet context: vpnExtension.VpnExtensionContext;
251e41f4b71Sopenharmony_ciexport default class MyVpnExtAbility extends VpnExtensionAbility {
252e41f4b71Sopenharmony_ci  private tunIp: string = '10.0.0.5';
253e41f4b71Sopenharmony_ci  private blockedAppName: string = 'com.example.myvpndemo';
254e41f4b71Sopenharmony_ci  onCreate(want: Want) {
255e41f4b71Sopenharmony_ci    let VpnConnection : vpnExtension.VpnConnection = vpnExtension.createVpnConnection(context);
256e41f4b71Sopenharmony_ci    console.info("vpn createVpnConnection: " + JSON.stringify(VpnConnection));
257e41f4b71Sopenharmony_ci    this.SetupVpn();
258e41f4b71Sopenharmony_ci  }
259e41f4b71Sopenharmony_ci  SetupVpn() {
260e41f4b71Sopenharmony_ci        class Address {
261e41f4b71Sopenharmony_ci            address: string;
262e41f4b71Sopenharmony_ci            family: number;
263e41f4b71Sopenharmony_ci
264e41f4b71Sopenharmony_ci            constructor(address: string, family: number) {
265e41f4b71Sopenharmony_ci                this.address = address;
266e41f4b71Sopenharmony_ci                this.family = family;
267e41f4b71Sopenharmony_ci            }
268e41f4b71Sopenharmony_ci        }
269e41f4b71Sopenharmony_ci
270e41f4b71Sopenharmony_ci        class AddressWithPrefix {
271e41f4b71Sopenharmony_ci            address: Address;
272e41f4b71Sopenharmony_ci            prefixLength: number;
273e41f4b71Sopenharmony_ci
274e41f4b71Sopenharmony_ci            constructor(address: Address, prefixLength: number) {
275e41f4b71Sopenharmony_ci                this.address = address;
276e41f4b71Sopenharmony_ci                this.prefixLength = prefixLength;
277e41f4b71Sopenharmony_ci            }
278e41f4b71Sopenharmony_ci        }
279e41f4b71Sopenharmony_ci
280e41f4b71Sopenharmony_ci        class Config {
281e41f4b71Sopenharmony_ci            addresses: AddressWithPrefix[];
282e41f4b71Sopenharmony_ci            mtu: number;
283e41f4b71Sopenharmony_ci            dnsAddresses: string[];
284e41f4b71Sopenharmony_ci            trustedApplications: string[];
285e41f4b71Sopenharmony_ci            blockedApplications: string[];
286e41f4b71Sopenharmony_ci
287e41f4b71Sopenharmony_ci            constructor(
288e41f4b71Sopenharmony_ci                tunIp: string,
289e41f4b71Sopenharmony_ci                blockedAppName: string
290e41f4b71Sopenharmony_ci            ) {
291e41f4b71Sopenharmony_ci                this.addresses = [
292e41f4b71Sopenharmony_ci                    new AddressWithPrefix(new Address(tunIp, 1), 24)
293e41f4b71Sopenharmony_ci                ];
294e41f4b71Sopenharmony_ci                this.mtu = 1400;
295e41f4b71Sopenharmony_ci                this.dnsAddresses = ["114.114.114.114"];
296e41f4b71Sopenharmony_ci                this.trustedApplications = [];
297e41f4b71Sopenharmony_ci                this.blockedApplications = [blockedAppName];
298e41f4b71Sopenharmony_ci            }
299e41f4b71Sopenharmony_ci        }
300e41f4b71Sopenharmony_ci
301e41f4b71Sopenharmony_ci        let config = new Config(this.tunIp, this.blockedAppName);
302e41f4b71Sopenharmony_ci
303e41f4b71Sopenharmony_ci        try {
304e41f4b71Sopenharmony_ci            let VpnConnection : vpnExtension.VpnConnection = vpnExtension.createVpnConnection(context);
305e41f4b71Sopenharmony_ci            VpnConnection.create(config).then((data) => {
306e41f4b71Sopenharmony_ci                hilog.error(0x0000, 'developTag', 'tunfd: %{public}s', JSON.stringify(data) ?? '');
307e41f4b71Sopenharmony_ci            })
308e41f4b71Sopenharmony_ci        } catch (error) {
309e41f4b71Sopenharmony_ci            hilog.error(0x0000, 'developTag', 'vpn setUp fail: %{public}s', JSON.stringify(error) ?? '');
310e41f4b71Sopenharmony_ci        }
311e41f4b71Sopenharmony_ci    }
312e41f4b71Sopenharmony_ci}
313e41f4b71Sopenharmony_ci```
314e41f4b71Sopenharmony_ci
315e41f4b71Sopenharmony_ci### protect
316e41f4b71Sopenharmony_ci
317e41f4b71Sopenharmony_ciprotect(socketFd: number): Promise\<void\>
318e41f4b71Sopenharmony_ci
319e41f4b71Sopenharmony_ciProtects sockets against a VPN connection. The data sent through sockets is directly transmitted over the physical network and therefore the traffic does not traverse through the VPN. This API uses a promise to return the result.
320e41f4b71Sopenharmony_ci
321e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Vpn
322e41f4b71Sopenharmony_ci
323e41f4b71Sopenharmony_ci**Parameters**
324e41f4b71Sopenharmony_ci
325e41f4b71Sopenharmony_ci| Name  | Type  | Mandatory| Description                                                                                       |
326e41f4b71Sopenharmony_ci| -------- | ------ | ---- | ------------------------------------------------------------------------------------------- |
327e41f4b71Sopenharmony_ci| socketFd | number | Yes  | Socket file descriptor. It can be obtained through [getSocketFd](js-apis-socket.md#getsocketfd10-1).|
328e41f4b71Sopenharmony_ci
329e41f4b71Sopenharmony_ci**Return value**
330e41f4b71Sopenharmony_ci
331e41f4b71Sopenharmony_ci| Type           | Description                                                 |
332e41f4b71Sopenharmony_ci| --------------- | ----------------------------------------------------- |
333e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result. If the operation is successful, the operation result is returned. If the operation fails, an error message is returned.|
334e41f4b71Sopenharmony_ci
335e41f4b71Sopenharmony_ci**Error codes**
336e41f4b71Sopenharmony_ci
337e41f4b71Sopenharmony_ci| ID| Error Message                                    |
338e41f4b71Sopenharmony_ci| --------- | -------------------------------------------- |
339e41f4b71Sopenharmony_ci| 401       | Parameter error.                             |
340e41f4b71Sopenharmony_ci| 2200001   | Invalid parameter value.                     |
341e41f4b71Sopenharmony_ci| 2200002   | Operation failed. Cannot connect to service. |
342e41f4b71Sopenharmony_ci| 2200003   | System internal error.                       |
343e41f4b71Sopenharmony_ci| 2203004   | Invalid socket file descriptor.              |
344e41f4b71Sopenharmony_ci
345e41f4b71Sopenharmony_ci**Example**
346e41f4b71Sopenharmony_ci
347e41f4b71Sopenharmony_ci```js
348e41f4b71Sopenharmony_ciimport { vpnExtension, VpnExtensionAbility } from '@kit.NetworkKit';
349e41f4b71Sopenharmony_ciimport { common, Want } from '@kit.AbilityKit';
350e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit';
351e41f4b71Sopenharmony_ci
352e41f4b71Sopenharmony_cilet g_tunnelFd = -1;
353e41f4b71Sopenharmony_cilet context: vpnExtension.VpnExtensionContext;
354e41f4b71Sopenharmony_ciexport default class MyVpnExtAbility extends VpnExtensionAbility {
355e41f4b71Sopenharmony_ci  private vpnServerIp: string = '192.168.31.13';
356e41f4b71Sopenharmony_ci  onCreate(want: Want) {
357e41f4b71Sopenharmony_ci    let VpnConnection : vpnExtension.VpnConnection = vpnExtension.createVpnConnection(context);
358e41f4b71Sopenharmony_ci    console.info("vpn createVpnConnection: " + JSON.stringify(VpnConnection));
359e41f4b71Sopenharmony_ci    this.CreateTunnel();
360e41f4b71Sopenharmony_ci    this.Protect();
361e41f4b71Sopenharmony_ci  }
362e41f4b71Sopenharmony_ci  CreateTunnel() {
363e41f4b71Sopenharmony_ci      g_tunnelFd = 8888;
364e41f4b71Sopenharmony_ci  }
365e41f4b71Sopenharmony_ci  Protect() {
366e41f4b71Sopenharmony_ci        hilog.info(0x0000, 'developTag', '%{public}s', 'vpn Protect');
367e41f4b71Sopenharmony_ci        let VpnConnection : vpnExtension.VpnConnection = vpnExtension.createVpnConnection(context);
368e41f4b71Sopenharmony_ci        VpnConnection.protect(g_tunnelFd).then(() => {
369e41f4b71Sopenharmony_ci            hilog.info(0x0000, 'developTag', '%{public}s', 'vpn Protect Success');
370e41f4b71Sopenharmony_ci        }).catch((err : Error) => {
371e41f4b71Sopenharmony_ci            hilog.error(0x0000, 'developTag', 'vpn Protect Failed %{public}s', JSON.stringify(err) ?? '');
372e41f4b71Sopenharmony_ci        })
373e41f4b71Sopenharmony_ci  }
374e41f4b71Sopenharmony_ci}
375e41f4b71Sopenharmony_ci```
376e41f4b71Sopenharmony_ci
377e41f4b71Sopenharmony_ci### destroy
378e41f4b71Sopenharmony_ci
379e41f4b71Sopenharmony_cidestroy(): Promise\<void\>
380e41f4b71Sopenharmony_ci
381e41f4b71Sopenharmony_ciDestroys a VPN. This API uses a promise to return the result.
382e41f4b71Sopenharmony_ci
383e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Vpn
384e41f4b71Sopenharmony_ci
385e41f4b71Sopenharmony_ci**Return value**
386e41f4b71Sopenharmony_ci
387e41f4b71Sopenharmony_ci| Type           | Description                                                 |
388e41f4b71Sopenharmony_ci| --------------- | ----------------------------------------------------- |
389e41f4b71Sopenharmony_ci| Promise\<void\> | Promise used to return the result. If the operation is successful, the operation result is returned. If the operation fails, an error message is returned.|
390e41f4b71Sopenharmony_ci
391e41f4b71Sopenharmony_ci**Error codes**
392e41f4b71Sopenharmony_ci
393e41f4b71Sopenharmony_ci| ID| Error Message                                    |
394e41f4b71Sopenharmony_ci| --------- | -------------------------------------------- |
395e41f4b71Sopenharmony_ci| 401       | Parameter error.                             |
396e41f4b71Sopenharmony_ci| 2200002   | Operation failed. Cannot connect to service. |
397e41f4b71Sopenharmony_ci| 2200003   | System internal error.                       |
398e41f4b71Sopenharmony_ci
399e41f4b71Sopenharmony_ci**Example**
400e41f4b71Sopenharmony_ci
401e41f4b71Sopenharmony_ci```js
402e41f4b71Sopenharmony_ciimport { vpnExtension, VpnExtensionAbility } from '@kit.NetworkKit';
403e41f4b71Sopenharmony_ciimport { common, Want } from '@kit.AbilityKit';
404e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
405e41f4b71Sopenharmony_ci
406e41f4b71Sopenharmony_cilet context: vpnExtension.VpnExtensionContext;
407e41f4b71Sopenharmony_ciexport default class MyVpnExtAbility extends VpnExtensionAbility {
408e41f4b71Sopenharmony_ci  onCreate(want: Want) {
409e41f4b71Sopenharmony_ci    let VpnConnection : vpnExtension.VpnConnection = vpnExtension.createVpnConnection(context);
410e41f4b71Sopenharmony_ci    console.info("vpn createVpnConnection: " + JSON.stringify(VpnConnection));
411e41f4b71Sopenharmony_ci    VpnConnection.destroy().then(() => {
412e41f4b71Sopenharmony_ci      console.info("destroy success.");
413e41f4b71Sopenharmony_ci    }).catch((error : BusinessError) => {
414e41f4b71Sopenharmony_ci      console.error("destroy fail" + JSON.stringify(error));
415e41f4b71Sopenharmony_ci    });
416e41f4b71Sopenharmony_ci  }
417e41f4b71Sopenharmony_ci}
418e41f4b71Sopenharmony_ci```
419e41f4b71Sopenharmony_ci
420e41f4b71Sopenharmony_ci## VpnConfig
421e41f4b71Sopenharmony_ci
422e41f4b71Sopenharmony_ciDefines the VPN configuration.
423e41f4b71Sopenharmony_ci
424e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Vpn
425e41f4b71Sopenharmony_ci
426e41f4b71Sopenharmony_ci| Name               | Type                                                          | Mandatory| Description                               |
427e41f4b71Sopenharmony_ci| ------------------- | -------------------------------------------------------------- | ---- | ----------------------------------- |
428e41f4b71Sopenharmony_ci| addresses           | Array\<[LinkAddress](js-apis-net-connection.md#linkaddress)\> | Yes  | IP address of the vNIC.           |
429e41f4b71Sopenharmony_ci| routes              | Array\<[RouteInfo](js-apis-net-connection.md#routeinfo)\>     | No  | Route information of the vNIC.           |
430e41f4b71Sopenharmony_ci| dnsAddresses        | Array\<string\>                                                | No  | IP address of the DNS server.               |
431e41f4b71Sopenharmony_ci| searchDomains       | Array\<string\>                                                | No  | List of DNS search domains.                 |
432e41f4b71Sopenharmony_ci| mtu                 | number                                                         | No  | Maximum transmission unit (MTU), in bytes.    |
433e41f4b71Sopenharmony_ci| isIPv4Accepted      | boolean                                                        | No  | Whether IPv4 is supported. The default value is **true**.     |
434e41f4b71Sopenharmony_ci| isIPv6Accepted      | boolean                                                        | No  | Whether IPv6 is supported. The default value is **false**.    |
435e41f4b71Sopenharmony_ci| isInternal          | boolean                                                        | No  | Whether the built-in VPN is supported. The default value is **false**.  |
436e41f4b71Sopenharmony_ci| isBlocking          | boolean                                                        | No  | Whether the blocking mode is used. The default value is **false**.      |
437e41f4b71Sopenharmony_ci| trustedApplications | Array\<string\>                                                | No  | List of trusted applications, which are represented by bundle names of the string type. |
438e41f4b71Sopenharmony_ci| blockedApplications | Array\<string\>                                                | No  | List of blocked applications, which are represented by bundle names of the string type. |
439e41f4b71Sopenharmony_ci
440e41f4b71Sopenharmony_ci**Example**
441e41f4b71Sopenharmony_ci
442e41f4b71Sopenharmony_ci```js
443e41f4b71Sopenharmony_ciimport { vpnExtension} from '@kit.NetworkKit';
444e41f4b71Sopenharmony_ci
445e41f4b71Sopenharmony_cilet vpnConfig: vpnExtension.VpnConfig = {
446e41f4b71Sopenharmony_ci  addresses: [],
447e41f4b71Sopenharmony_ci  routes: [{
448e41f4b71Sopenharmony_ci    interface: "eth0",
449e41f4b71Sopenharmony_ci    destination: {
450e41f4b71Sopenharmony_ci      address: {
451e41f4b71Sopenharmony_ci        address:'',
452e41f4b71Sopenharmony_ci        family:1,
453e41f4b71Sopenharmony_ci        port:8080
454e41f4b71Sopenharmony_ci      },
455e41f4b71Sopenharmony_ci      prefixLength:1
456e41f4b71Sopenharmony_ci    },
457e41f4b71Sopenharmony_ci    gateway: {
458e41f4b71Sopenharmony_ci      address:'',
459e41f4b71Sopenharmony_ci      family:1,
460e41f4b71Sopenharmony_ci      port:8080
461e41f4b71Sopenharmony_ci    },
462e41f4b71Sopenharmony_ci    hasGateway: true,
463e41f4b71Sopenharmony_ci    isDefaultRoute: true,
464e41f4b71Sopenharmony_ci  }],
465e41f4b71Sopenharmony_ci  mtu: 1400,
466e41f4b71Sopenharmony_ci  dnsAddresses: ["223.5.5.5", "223.6.6.6"],
467e41f4b71Sopenharmony_ci  trustedApplications: [],
468e41f4b71Sopenharmony_ci  blockedApplications: [],
469e41f4b71Sopenharmony_ci}
470e41f4b71Sopenharmony_cilet context: vpnExtension.VpnExtensionContext;
471e41f4b71Sopenharmony_ci
472e41f4b71Sopenharmony_cifunction vpnCreate(){
473e41f4b71Sopenharmony_ci  let VpnConnection: vpnExtension.VpnConnection = vpnExtension.createVpnConnection(context);
474e41f4b71Sopenharmony_ci  VpnConnection.create(vpnConfig).then((data) => {
475e41f4b71Sopenharmony_ci    console.info("vpn create " + JSON.stringify(data));
476e41f4b71Sopenharmony_ci  })
477e41f4b71Sopenharmony_ci}
478e41f4b71Sopenharmony_ci```
479