• 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 
16 #ifndef OPENVPN_CTL_H
17 #define OPENVPN_CTL_H
18 
19 #include "cJSON.h"
20 #include "netsys_controller.h"
21 #include "net_vpn_impl.h"
22 #include "openvpn_config.h"
23 
24 #define VPN_PIDDIR "/data/service/el1/public/vpn"
25 
26 namespace OHOS {
27 namespace NetManagerStandard {
28 using namespace NetsysNative;
29 
30 constexpr const char *OPENVPN_NODE_ROOT = "openvpn";
31 constexpr const char *OPENVPN_NODE_MTU = "mtu";
32 constexpr const char *OPENVPN_NODE_ADDRESS = "address";
33 constexpr const char *OPENVPN_NODE_NETMASK = "netmask";
34 constexpr const char *OPENVPN_NODE_CONFIG = "config";
35 constexpr const char *OPENVPN_NODE_STATE = "state";
36 constexpr const char *OPENVPN_NODE_UPDATE_STATE = "updateState";
37 constexpr const char *OPENVPN_NODE_SETUP_VPN_TUN = "setupVpnTun";
38 constexpr const char *OPENVPN_MASK_TAG = "***";
39 
40 enum OpenvpnStateCode : int32_t {
41     OPENVPN_STATE_UNKNOWN = 1,
42     OPENVPN_STATE_SETUP,
43     OPENVPN_STATE_STARTED,
44     OPENVPN_STATE_CONNECTED,
45     OPENVPN_STATE_DISCONNECTED,
46     OPENVPN_STATE_ERROR_PRIVATE_KEY = 200,
47     OPENVPN_STATE_ERROR_CLIENT_CRT,
48     OPENVPN_STATE_ERROR_CA_CAT,
49     OPENVPN_STATE_ERROR_TIME_OUT,
50 };
51 
52 enum OpenVpnConfigType : int32_t {
53     OPENVPN_ASKPASS = 0,
54     OPENVPN_CONF,
55 };
56 
57 class OpenvpnCtl : public NetVpnImpl {
58 public:
59     OpenvpnCtl(sptr<VpnConfig> config, const std::string &pkg, int32_t userId, std::vector<int32_t> &activeUserIds);
60     ~OpenvpnCtl() = default;
61 
62     bool IsInternalVpn() override;
63     int32_t SetUp() override;
64     int32_t Destroy() override;
65     int32_t GetConnectedSysVpnConfig(sptr<SysVpnConfig> &sysVpnConfig) override;
66     int32_t NotifyConnectStage(const std::string &stage, const int32_t &result) override;
67     int32_t GetSysVpnCertUri(const int32_t certType, std::string &certUri) override;
68     bool IsSystemVpn() override;
69     sptr<OpenvpnConfig> openvpnConfig_;
70 
71 private:
72     const std::string  OPENVPN_ASKPASS_FILE = VPN_PIDDIR "/askpass";
73     const std::string  OPENVPN_ASKPASS_PARAM = "askpass " + std::string(OPENVPN_ASKPASS_FILE);
74     int32_t openvpnState_ = OPENVPN_STATE_UNKNOWN;
75     void UpdateOpenvpnState(const int32_t state);
76     int32_t StartOpenvpn();
77     std::string MaskOpenvpnMessage(const std::string &msg);
78     int32_t HandleClientMessage(const std::string &msg);
79     int32_t SetUpVpnTun();
80     void UpdateConfig(cJSON* jConfig);
81     void UpdateState(cJSON* state);
82     void StopOpenvpn();
83 };
84 } // namespace NetManagerStandard
85 } // namespace OHOS
86 #endif // OPENVPN_CTL_H