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 class OpenvpnCtl : public NetVpnImpl {
53 public:
54     OpenvpnCtl(sptr<VpnConfig> config, const std::string &pkg, int32_t userId, std::vector<int32_t> &activeUserIds);
55     ~OpenvpnCtl() = default;
56 
57     bool IsInternalVpn() override;
58     int32_t SetUp() override;
59     int32_t Destroy() override;
60     int32_t GetConnectedSysVpnConfig(sptr<SysVpnConfig> &sysVpnConfig) override;
61     int32_t NotifyConnectStage(const std::string &stage, const int32_t &result) override;
62     bool IsSystemVpn() override;
63     sptr<OpenvpnConfig> openvpnConfig_;
64 
65 private:
66     const std::string  OPENVPN_CONFIG_FILE = VPN_PIDDIR "/config.ovpn";
67     const std::string  OPENVPN_ASKPASS_FILE = VPN_PIDDIR "/askpass";
68     const std::string  OPENVPN_ASKPASS_PARAM = "askpass " + std::string(OPENVPN_ASKPASS_FILE);
69     int32_t openvpnState_ = OPENVPN_STATE_UNKNOWN;
70     void UpdateOpenvpnState(const int32_t state);
71     int32_t StartOpenvpn();
72     std::string MaskOpenvpnMessage(const std::string &msg);
73     int32_t HandleClientMessage(const std::string &msg);
74     int32_t SetUpVpnTun();
75     void UpdateConfig(cJSON* jConfig);
76     void UpdateState(cJSON* state);
77     void StopOpenvpn();
78 };
79 } // namespace NetManagerStandard
80 } // namespace OHOS
81 #endif // OPENVPN_CTL_H