• 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 */
15import parameter from '@ohos.systemParameterEnhance';
16import LogUtil from '../../../../../../../common/utils/src/main/ets/default/baseUtil/LogUtil';
17
18const MODULE_TAG: string = 'setting_vpn:VpnTypeModel:';
19
20/**
21 * support vpn type & displayname
22 */
23export class VpnTypeModel {
24  static readonly TYPE_IKEV2_IPSEC_MSCHAPv2: number = 1; // vpn.SysVpnType.IKEV2_IPSEC_MSCHAPv2;
25  static readonly TYPE_IKEV2_IPSEC_PSK: number = 2; // vpn.SysVpnType.IKEV2_IPSEC_PSK;
26  static readonly TYPE_IKEV2_IPSEC_RSA: number = 3; // vpn.SysVpnType.IKEV2_IPSEC_RSA;
27  static readonly TYPE_L2TP_IPSEC_PSK: number = 4; // vpn.SysVpnType.L2TP_IPSEC_PSK;
28  static readonly TYPE_L2TP_IPSEC_RSA: number = 5; // vpn.SysVpnType.L2TP_IPSEC_RSA;
29  static readonly TYPE_IPSEC_XAUTH_PSK: number = 6; // vpn.SysVpnType.IPSEC_XAUTH_PSK;
30  static readonly TYPE_IPSEC_XAUTH_RSA: number = 7; // vpn.SysVpnType.IPSEC_XAUTH_RSA;
31  static readonly TYPE_IPSEC_HYBRID_RSA: number = 8; // vpn.SysVpnType.IPSEC_HYBRID_RSA;
32  static readonly TYPE_OPENVPN: number = 9; // vpn.SysVpnType.OPENVPN;
33
34  private supportVpnTypes: number[] = [];
35  private static instance: VpnTypeModel;
36
37  public static getInstance(): VpnTypeModel {
38    if (!this.instance) {
39      this.instance = new VpnTypeModel();
40    }
41    return this.instance;
42  }
43
44  constructor() {
45    let supportStr: string = '';
46    try {
47      supportStr = parameter.getSync('const.product.supportVpn', '');
48    } catch (err) {
49      LogUtil.error(MODULE_TAG + `getSupportStr is failed , err : ${JSON.stringify(err)} `);
50    }
51    if (supportStr !== '') {
52      supportStr.split(',').forEach((vpnTypeStr) => {
53        let vpnType: number = Number(vpnTypeStr);
54        switch (vpnType) {
55          case VpnTypeModel.TYPE_IKEV2_IPSEC_MSCHAPv2:
56          case VpnTypeModel.TYPE_IKEV2_IPSEC_PSK:
57          case VpnTypeModel.TYPE_IKEV2_IPSEC_RSA:
58          case VpnTypeModel.TYPE_L2TP_IPSEC_PSK:
59          case VpnTypeModel.TYPE_L2TP_IPSEC_RSA:
60          case VpnTypeModel.TYPE_IPSEC_XAUTH_PSK:
61          case VpnTypeModel.TYPE_IPSEC_XAUTH_RSA:
62          case VpnTypeModel.TYPE_IPSEC_HYBRID_RSA:
63          case VpnTypeModel.TYPE_OPENVPN:
64            this.supportVpnTypes.push(vpnType);
65            break;
66          default:
67            LogUtil.info(MODULE_TAG + supportStr + ` has unknown vpnType:` + vpnType);
68            break;
69        }
70      })
71    }
72    LogUtil.info(MODULE_TAG + `supportVpn ${this.supportVpnTypes}`);
73  }
74
75  isSupportVpn(): boolean {
76    return this.supportVpnTypes.length > 0;
77  }
78
79  getSupportVpnTypes(): number[] {
80    return this.supportVpnTypes;
81  }
82
83  getSupportVpnTypeStrs(): string[] {
84    let types: string[] = [];
85    this.supportVpnTypes.forEach(type => {
86      types.push(this.getVpnTypeStr(type))
87    });
88    return types;
89  }
90
91  getVpnTypeStr(vpnType: number): string {
92    switch (vpnType) {
93      case VpnTypeModel.TYPE_IKEV2_IPSEC_MSCHAPv2: return 'IKEv2/IPSec MSCHAPv2';
94      case VpnTypeModel.TYPE_IKEV2_IPSEC_PSK: return 'IKEv2/IPSec PSK';
95      case VpnTypeModel.TYPE_IKEV2_IPSEC_RSA: return 'IKEv2/IPSec RSA';
96      case VpnTypeModel.TYPE_L2TP_IPSEC_PSK: return 'L2TP/IPSec PSK';
97      case VpnTypeModel.TYPE_L2TP_IPSEC_RSA: return 'L2TP/IPSec RSA';
98      case VpnTypeModel.TYPE_IPSEC_XAUTH_PSK: return 'IPSec Xauth PSK';
99      case VpnTypeModel.TYPE_IPSEC_XAUTH_RSA: return 'IPSec Xauth RSA';
100      case VpnTypeModel.TYPE_IPSEC_HYBRID_RSA: return 'IPSec Hybrid RSA';
101      case VpnTypeModel.TYPE_OPENVPN: return 'OpenVpn';
102      default :
103        LogUtil.warn(MODULE_TAG + 'getVpnTypeStr unknown vpnType:' + vpnType);
104        return '';
105    }
106  }
107}