18e745fdaSopenharmony_ci/* 28e745fdaSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 38e745fdaSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 48e745fdaSopenharmony_ci * you may not use this file except in compliance with the License. 58e745fdaSopenharmony_ci * You may obtain a copy of the License at 68e745fdaSopenharmony_ci * 78e745fdaSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 88e745fdaSopenharmony_ci * 98e745fdaSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 108e745fdaSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 118e745fdaSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 128e745fdaSopenharmony_ci * See the License for the specific language governing permissions and 138e745fdaSopenharmony_ci * limitations under the License. 148e745fdaSopenharmony_ci */ 158e745fdaSopenharmony_ci 168e745fdaSopenharmony_ci#ifndef OPENVPN_CTL_H 178e745fdaSopenharmony_ci#define OPENVPN_CTL_H 188e745fdaSopenharmony_ci 198e745fdaSopenharmony_ci#include "cJSON.h" 208e745fdaSopenharmony_ci#include "netsys_controller.h" 218e745fdaSopenharmony_ci#include "net_vpn_impl.h" 228e745fdaSopenharmony_ci#include "openvpn_config.h" 238e745fdaSopenharmony_ci 248e745fdaSopenharmony_ci#define VPN_PIDDIR "/data/service/el1/public/vpn" 258e745fdaSopenharmony_ci 268e745fdaSopenharmony_cinamespace OHOS { 278e745fdaSopenharmony_cinamespace NetManagerStandard { 288e745fdaSopenharmony_ciusing namespace NetsysNative; 298e745fdaSopenharmony_ci 308e745fdaSopenharmony_ciconstexpr const char *OPENVPN_NODE_ROOT = "openvpn"; 318e745fdaSopenharmony_ciconstexpr const char *OPENVPN_NODE_MTU = "mtu"; 328e745fdaSopenharmony_ciconstexpr const char *OPENVPN_NODE_ADDRESS = "address"; 338e745fdaSopenharmony_ciconstexpr const char *OPENVPN_NODE_NETMASK = "netmask"; 348e745fdaSopenharmony_ciconstexpr const char *OPENVPN_NODE_CONFIG = "config"; 358e745fdaSopenharmony_ciconstexpr const char *OPENVPN_NODE_STATE = "state"; 368e745fdaSopenharmony_ciconstexpr const char *OPENVPN_NODE_UPDATE_STATE = "updateState"; 378e745fdaSopenharmony_ciconstexpr const char *OPENVPN_NODE_SETUP_VPN_TUN = "setupVpnTun"; 388e745fdaSopenharmony_ciconstexpr const char *OPENVPN_MASK_TAG = "***"; 398e745fdaSopenharmony_ci 408e745fdaSopenharmony_cienum OpenvpnStateCode : int32_t { 418e745fdaSopenharmony_ci OPENVPN_STATE_UNKNOWN = 1, 428e745fdaSopenharmony_ci OPENVPN_STATE_SETUP, 438e745fdaSopenharmony_ci OPENVPN_STATE_STARTED, 448e745fdaSopenharmony_ci OPENVPN_STATE_CONNECTED, 458e745fdaSopenharmony_ci OPENVPN_STATE_DISCONNECTED, 468e745fdaSopenharmony_ci OPENVPN_STATE_ERROR_PRIVATE_KEY = 200, 478e745fdaSopenharmony_ci OPENVPN_STATE_ERROR_CLIENT_CRT, 488e745fdaSopenharmony_ci OPENVPN_STATE_ERROR_CA_CAT, 498e745fdaSopenharmony_ci OPENVPN_STATE_ERROR_TIME_OUT, 508e745fdaSopenharmony_ci}; 518e745fdaSopenharmony_ci 528e745fdaSopenharmony_ciclass OpenvpnCtl : public NetVpnImpl { 538e745fdaSopenharmony_cipublic: 548e745fdaSopenharmony_ci OpenvpnCtl(sptr<VpnConfig> config, const std::string &pkg, int32_t userId, std::vector<int32_t> &activeUserIds); 558e745fdaSopenharmony_ci ~OpenvpnCtl() = default; 568e745fdaSopenharmony_ci 578e745fdaSopenharmony_ci bool IsInternalVpn() override; 588e745fdaSopenharmony_ci int32_t SetUp() override; 598e745fdaSopenharmony_ci int32_t Destroy() override; 608e745fdaSopenharmony_ci int32_t GetConnectedSysVpnConfig(sptr<SysVpnConfig> &sysVpnConfig) override; 618e745fdaSopenharmony_ci int32_t NotifyConnectStage(const std::string &stage, const int32_t &result) override; 628e745fdaSopenharmony_ci bool IsSystemVpn() override; 638e745fdaSopenharmony_ci sptr<OpenvpnConfig> openvpnConfig_; 648e745fdaSopenharmony_ci 658e745fdaSopenharmony_ciprivate: 668e745fdaSopenharmony_ci const std::string OPENVPN_CONFIG_FILE = VPN_PIDDIR "/config.ovpn"; 678e745fdaSopenharmony_ci const std::string OPENVPN_ASKPASS_FILE = VPN_PIDDIR "/askpass"; 688e745fdaSopenharmony_ci const std::string OPENVPN_ASKPASS_PARAM = "askpass " + std::string(OPENVPN_ASKPASS_FILE); 698e745fdaSopenharmony_ci int32_t openvpnState_ = OPENVPN_STATE_UNKNOWN; 708e745fdaSopenharmony_ci void UpdateOpenvpnState(const int32_t state); 718e745fdaSopenharmony_ci int32_t StartOpenvpn(); 728e745fdaSopenharmony_ci std::string MaskOpenvpnMessage(const std::string &msg); 738e745fdaSopenharmony_ci int32_t HandleClientMessage(const std::string &msg); 748e745fdaSopenharmony_ci int32_t SetUpVpnTun(); 758e745fdaSopenharmony_ci void UpdateConfig(cJSON* jConfig); 768e745fdaSopenharmony_ci void UpdateState(cJSON* state); 778e745fdaSopenharmony_ci void StopOpenvpn(); 788e745fdaSopenharmony_ci}; 798e745fdaSopenharmony_ci} // namespace NetManagerStandard 808e745fdaSopenharmony_ci} // namespace OHOS 818e745fdaSopenharmony_ci#endif // OPENVPN_CTL_H