• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2* Copyright (c) 2024 Huawei Device Co., Ltd.
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14*/
15
16import Want from '@ohos.app.ability.Want';
17import VpnExtensionAbility from '@ohos.app.ability.VpnExtensionAbility';
18import vpn_client from 'libvpn_client.so';
19import vpnExt from '@ohos.net.vpnExtension';
20import hilog from '@ohos.hilog';
21
22const TAG: string = "[MyVpnExtAbility]";
23let g_tunFd = -1;
24let g_tunnelFd = -1;
25
26export default class MyVpnExtAbility extends VpnExtensionAbility {
27  private VpnConnection: vpnExt.VpnConnection;
28  private vpnServerIp: string = '192.168.31.13';
29  private tunIp: string = '10.0.0.5';
30  private blockedAppName: string = 'com.example.myvpndemo';
31
32  onCreate(want: Want) {
33    console.info(TAG, `onCreate, want: ${want.abilityName}`);
34    this.VpnConnection = vpnExt.createVpnConnection(this.context);
35    console.info("createVpnConnection success");
36    this.CreateTunnel();
37    this.Protect();
38    this.SetupVpn();
39  }
40
41  onRequest(want: Want, startId: number) {
42    console.info(TAG, `onRequest, want: ${want.abilityName}`);
43  }
44
45  onConnect(want: Want) {
46    console.info(TAG, `onConnect, want: ${want.abilityName}`);
47    let abilityName: string = want.parameters.abilityName.toString();
48    let bundleName: string = want.parameters.bundleName.toString();
49    return null;
50  }
51
52  onDisconnect(want: Want) {
53    console.info(TAG, `onDisconnect, want: ${want.abilityName}`);
54  }
55
56  onDestroy() {
57    this.Destroy();
58    console.info(TAG, `onDestroy`);
59  }
60
61  CreateTunnel() {
62    console.info("CreateTunnel");
63    g_tunnelFd = vpn_client.tcpConnect(this.vpnServerIp, 8888);
64  }
65
66  Protect() {
67    hilog.info(0x0000, 'developTag', '%{public}s', 'vpn Protect');
68    this.VpnConnection.protect(g_tunnelFd).then(() => {
69      hilog.info(0x0000, 'developTag', '%{public}s', 'vpn Protect Success');
70    }).catch((err: Error) => {
71      hilog.error(0x0000, 'developTag', 'vpn Protect Failed %{public}s', JSON.stringify(err) ?? '');
72    })
73  }
74
75  SetupVpn() {
76    hilog.info(0x0000, 'developTag', '%{public}s', 'vpn SetupVpn');
77
78    class Address {
79      address: string;
80      family: number;
81
82      constructor(address: string, family: number) {
83        this.address = address;
84        this.family = family;
85      }
86    }
87
88    class AddressWithPrefix {
89      address: Address;
90      prefixLength: number;
91
92      constructor(address: Address, prefixLength: number) {
93        this.address = address;
94        this.prefixLength = prefixLength;
95      }
96    }
97
98    class Config {
99      addresses: AddressWithPrefix[];
100      mtu: number;
101      dnsAddresses: string[];
102      trustedApplications: string[];
103      blockedApplications: string[];
104
105      constructor(
106        tunIp: string,
107        blockedAppName: string
108      ) {
109        this.addresses = [
110          new AddressWithPrefix(new Address(tunIp, 1), 24)
111        ];
112        this.mtu = 1400;
113        this.dnsAddresses = ["114.114.114.114"];
114        this.trustedApplications = [];
115        this.blockedApplications = [blockedAppName];
116      }
117    }
118
119    let config = new Config(this.tunIp, this.blockedAppName);
120
121    try {
122      this.VpnConnection.create(config).then((data) => {
123        g_tunFd = data;
124        hilog.error(0x0000, 'developTag', 'tunfd: %{public}s', JSON.stringify(data) ?? '');
125        vpn_client.startVpn(g_tunFd, g_tunnelFd);
126      })
127    } catch (error) {
128      hilog.error(0x0000, 'developTag', 'vpn setUp fail: %{public}s', JSON.stringify(error) ?? '');
129    }
130  }
131
132  Destroy() {
133    hilog.info(0x0000, 'developTag', '%{public}s', 'vpn Destroy');
134    vpn_client.stopVpn(g_tunnelFd);
135    this.VpnConnection.destroy().then(() => {
136      hilog.info(0x0000, 'developTag', '%{public}s', 'vpn Destroy Success');
137    }).catch((err : Error) => {
138      hilog.error(0x0000, 'developTag', 'vpn Destroy Failed: %{public}s', JSON.stringify(err) ?? '');
139    })
140  }
141}