diff --git a/common/include/constants_dinput.h b/common/include/constants_dinput.h index 48dc40c..02e52eb 100644 --- a/common/include/constants_dinput.h +++ b/common/include/constants_dinput.h @@ -133,6 +133,8 @@ namespace DistributedInput { /* The input device is external (not built-in). */ constexpr uint32_t INPUT_DEVICE_CLASS_EXTERNAL = 0x80000000; + const std::string LOCAL_DEV_ID = "localNodeDevice"; + enum class EHandlerMsgType { DINPUT_SINK_EVENT_HANDLER_MSG = 1, DINPUT_SOURCE_EVENT_HANDLER_MSG = 2 diff --git a/common/include/test/whitelistunittest/white_list_test.cpp b/common/include/test/whitelistunittest/white_list_test.cpp index a90492c..579c1dd 100644 --- a/common/include/test/whitelistunittest/white_list_test.cpp +++ b/common/include/test/whitelistunittest/white_list_test.cpp @@ -41,27 +41,6 @@ void WhiteListTest::TearDownTestCase() { } -HWTEST_F(WhiteListTest, Init01, testing::ext::TestSize.Level0) -{ - std::string deviceId = "whitelist"; - int32_t ret = WhiteListUtil::GetInstance().Init(deviceId); - EXPECT_EQ(SUCCESS, ret); -} - -HWTEST_F(WhiteListTest, Init02, testing::ext::TestSize.Level0) -{ - std::string deviceId; - int32_t ret = WhiteListUtil::GetInstance().Init(deviceId); - EXPECT_EQ(ERR_DH_INPUT_WHILTELIST_INIT_FAIL, ret); -} - -HWTEST_F(WhiteListTest, Init03, testing::ext::TestSize.Level0) -{ - std::string deviceId = "test"; - int32_t ret = WhiteListUtil::GetInstance().Init(deviceId); - EXPECT_EQ(SUCCESS, ret); -} - HWTEST_F(WhiteListTest, SyncWhiteList01, testing::ext::TestSize.Level0) { // 11|22,33|44,55,66 @@ -366,12 +345,6 @@ HWTEST_F(WhiteListTest, ClearWhiteList03, testing::ext::TestSize.Level0) int32_t ret = WhiteListUtil::GetInstance().ClearWhiteList(deviceId); EXPECT_EQ(SUCCESS, ret); } - -HWTEST_F(WhiteListTest, UnInit01, testing::ext::TestSize.Level0) -{ - int32_t ret = WhiteListUtil::GetInstance().UnInit(); - EXPECT_EQ(SUCCESS, ret); } } } -} \ No newline at end of file diff --git a/common/include/white_list_util.cpp b/common/include/white_list_util.cpp index 93233f0..d424512 100644 --- a/common/include/white_list_util.cpp +++ b/common/include/white_list_util.cpp @@ -30,14 +30,20 @@ namespace DistributedInput { namespace { const char* const SPLIT_LINE = "|"; const char* const SPLIT_COMMA = ","; + const int32_t COMB_KEY_VEC_MIN_LEN = 2; + const int32_t LAST_KEY_ACTION_LEN = 1; + const int32_t LAST_KEY_LEN = 1; } WhiteListUtil::WhiteListUtil() { + DHLOGI("Ctor WhiteListUtil, addr: %p", this); + Init(); } WhiteListUtil::~WhiteListUtil() { + DHLOGI("Dtor WhiteListUtil, addr: %p", this); } WhiteListUtil &WhiteListUtil::GetInstance(void) @@ -46,18 +52,9 @@ WhiteListUtil &WhiteListUtil::GetInstance(void) return instance; } -int32_t WhiteListUtil::Init(const std::string &deviceId) +int32_t WhiteListUtil::Init() { - DHLOGI("start, deviceId=%s", GetAnonyString(deviceId).c_str()); - ClearWhiteList(); const char* const whiteListFilePath = "/etc/dinput_business_event_whitelist.cfg"; - - if (deviceId.empty()) { - // device id error - DHLOGE("%s error, deviceId empty", __func__); - return ERR_DH_INPUT_WHILTELIST_INIT_FAIL; - } - std::ifstream inFile(whiteListFilePath, std::ios::in | std::ios::binary); if (!inFile.is_open()) { // file open error @@ -104,11 +101,9 @@ int32_t WhiteListUtil::Init(const std::string &deviceId) } inFile.close(); + SyncWhiteList(LOCAL_DEV_ID, vecWhiteList); - std::lock_guard lock(mutex_); - mapDeviceWhiteList_[deviceId] = vecWhiteList; - - DHLOGI("success, deviceId=%s", GetAnonyString(deviceId).c_str()); + DHLOGI("Local WhiteListUtil init success"); return DH_SUCCESS; } @@ -155,9 +150,60 @@ int32_t WhiteListUtil::SyncWhiteList(const std::string &deviceId, const TYPE_WHI std::lock_guard lock(mutex_); mapDeviceWhiteList_[deviceId] = vecWhiteList; + for (auto combKeys : vecWhiteList) { + GetCombKeysHash(combKeys, combKeysHashMap_[deviceId]); + } + return DH_SUCCESS; } +void WhiteListUtil::GetCombKeysHash(TYPE_COMBINATION_KEY_VEC combKeys, std::unordered_set &targetSet) +{ + if (combKeys.size() < COMB_KEY_VEC_MIN_LEN) { + DHLOGE("white list item length invalid"); + return; + } + + TYPE_KEY_CODE_VEC lastKeyAction = combKeys.back(); + if (lastKeyAction.size() != LAST_KEY_ACTION_LEN) { + DHLOGE("last key action invalid"); + return; + } + combKeys.pop_back(); + TYPE_KEY_CODE_VEC lastKey = combKeys.back(); + if (lastKey.size() != LAST_KEY_LEN) { + DHLOGE("last key invalid"); + return; + } + combKeys.pop_back(); + + std::unordered_set hashSets; + WhiteListItemHash hash; + GetAllComb(combKeys, hash, combKeys.size(), hashSets); + + for (auto &hash : hashSets) { + targetSet.insert(hash + std::to_string(lastKey[0]) + std::to_string(lastKeyAction[0])); + } +} + +void WhiteListUtil::GetAllComb(TYPE_COMBINATION_KEY_VEC vecs, WhiteListItemHash hash, + int32_t targetLen, std::unordered_set &hashSets) +{ + for (int32_t i = 0; i < vecs.size(); i++) { + TYPE_KEY_CODE_VEC nowVec = vecs[i]; + for (int32_t code : nowVec) { + WhiteListItemHash newHash = { hash.hash + std::to_string(code), hash.len + 1 }; + TYPE_COMBINATION_KEY_VEC leftVecs = vecs; + leftVecs.erase(leftVecs.begin() + i); + GetAllComb(leftVecs, newHash, targetLen, hashSets); + } + } + + if (hash.len == targetLen) { + hashSets.insert(hash.hash); + } +} + int32_t WhiteListUtil::ClearWhiteList(const std::string &deviceId) { DHLOGI("deviceId=%s", GetAnonyString(deviceId).c_str()); @@ -203,56 +249,38 @@ bool WhiteListUtil::CheckSubVecData(const TYPE_COMBINATION_KEY_VEC::iterator &it return bIsMatching; } +std::string WhiteListUtil::GetBusinessEventHash(const BusinessEvent &event) +{ + std::string hash = ""; + for (auto &p : event.pressedKeys) { + hash += std::to_string(p); + } + hash += std::to_string(event.keyCode); + hash += std::to_string(event.keyAction); + + return hash; +} + bool WhiteListUtil::IsNeedFilterOut(const std::string &deviceId, const BusinessEvent &event) { DHLOGI("start, deviceId=%s", GetAnonyString(deviceId).c_str()); std::lock_guard lock(mutex_); - if (mapDeviceWhiteList_.empty()) { + if (combKeysHashMap_.empty()) { DHLOGE("%s called, whilte list is empty!", __func__); return false; } - TYPE_DEVICE_WHITE_LIST_MAP::const_iterator iter = mapDeviceWhiteList_.find(deviceId); - if (iter == mapDeviceWhiteList_.end()) { + auto iter = combKeysHashMap_.find(deviceId); + if (iter == combKeysHashMap_.end()) { DHLOGE("%s called, not find by deviceId!", __func__); return false; } - TYPE_KEY_CODE_VEC vecKeyCode = event.pressedKeys; - vecKeyCode.push_back(event.keyCode); - vecKeyCode.push_back(event.keyAction); - if (vecKeyCode.empty()) { - DHLOGE("%s called, vecKeyCode is empty!", __func__); - return false; - } + std::string hash = GetBusinessEventHash(event); + DHLOGI("Searched business event hash: %s", hash.c_str()); - bool bIsMatching = false; - TYPE_WHITE_LIST_VEC vecWhiteList = iter->second; - for (TYPE_WHITE_LIST_VEC::iterator iter1 = vecWhiteList.begin(); iter1 != vecWhiteList.end(); ++iter1) { - if (vecKeyCode.size() != iter1->size()) { - DHLOGI( - "%s called, vecKeyCodeSize=%d, iter1Size=%d", - __func__, vecKeyCode.size(), iter1->size()); - continue; - } - - TYPE_COMBINATION_KEY_VEC::iterator iter2 = iter1->begin(); - TYPE_KEY_CODE_VEC::iterator iter3 = vecKeyCode.begin(); - for (; iter2 != iter1->end() && iter3 != vecKeyCode.end(); ++iter2, ++iter3) { - bIsMatching = false; - bIsMatching = CheckSubVecData(iter2, iter3); - if (!bIsMatching) { - break; - } - } - if (bIsMatching) { - break; - } - } - - DHLOGI("%s called, bIsMatching=%d", __func__, bIsMatching); - return bIsMatching; + return combKeysHashMap_[deviceId].find(hash) != combKeysHashMap_[deviceId].end(); } } // namespace DistributedInput } // namespace DistributedHardware diff --git a/common/include/white_list_util.h b/common/include/white_list_util.h index bb942d8..ab1e535 100644 --- a/common/include/white_list_util.h +++ b/common/include/white_list_util.h @@ -17,6 +17,7 @@ #define WHITE_LIST_UTIL_H #include +#include #include "constants_dinput.h" @@ -27,17 +28,37 @@ using TYPE_KEY_CODE_VEC = std::vector; using TYPE_COMBINATION_KEY_VEC = std::vector; using TYPE_WHITE_LIST_VEC = std::vector; using TYPE_DEVICE_WHITE_LIST_MAP = std::map; + +struct WhiteListItemHash { + std::string hash; + + // the keys num + int32_t len; + + WhiteListItemHash() : hash(""), len(0) {} + + WhiteListItemHash(std::string hash, int32_t len) : hash(hash), len(len) {} +}; + class WhiteListUtil { public: static WhiteListUtil &GetInstance(void); - int32_t Init(const std::string &deviceId); - int32_t UnInit(void); int32_t SyncWhiteList(const std::string &deviceId, const TYPE_WHITE_LIST_VEC &vecWhiteList); int32_t ClearWhiteList(const std::string &deviceId); int32_t ClearWhiteList(void); int32_t GetWhiteList(const std::string &deviceId, TYPE_WHITE_LIST_VEC &vecWhiteList); + + /* + * check is event in white list of deviceId + * + * Return: + * true: event in white list of this device + * false: event not in white list of this device, or white list of this device not exist + */ bool IsNeedFilterOut(const std::string &deviceId, const BusinessEvent &event); private: + int32_t Init(); + int32_t UnInit(void); WhiteListUtil(); ~WhiteListUtil(); WhiteListUtil(const WhiteListUtil &other) = delete; @@ -46,8 +67,13 @@ private: TYPE_COMBINATION_KEY_VEC &vecCombinationKey) const; bool CheckSubVecData(const TYPE_COMBINATION_KEY_VEC::iterator &iter2, const TYPE_KEY_CODE_VEC::iterator &iter3) const; + void GetCombKeysHash(TYPE_COMBINATION_KEY_VEC combKeys, std::unordered_set &targetSet); + void GetAllComb(TYPE_COMBINATION_KEY_VEC vecs, WhiteListItemHash hash, + int32_t targetLen, std::unordered_set &hashSets); + std::string GetBusinessEventHash(const BusinessEvent &event); private: TYPE_DEVICE_WHITE_LIST_MAP mapDeviceWhiteList_; + std::map> combKeysHashMap_; std::mutex mutex_; }; } // namespace DistributedInput diff --git a/dfx_utils/include/hidumper.h b/dfx_utils/include/hidumper.h index 0124b09..2584769 100644 --- a/dfx_utils/include/hidumper.h +++ b/dfx_utils/include/hidumper.h @@ -61,7 +61,6 @@ public: const std::string& peerSessionName, const SessionStatus& sessionStatus); void SetSessionStatus(const std::string& remoteDevId, const SessionStatus& sessionStatus); void DeleteSessionInfo(const std::string& remoteDevId); - std::string GetLocalDeviceId(); private: explicit HiDumper() = default; ~HiDumper() = default; diff --git a/interfaces/ipc/include/distributed_input_client.h b/interfaces/ipc/include/distributed_input_client.h index 4a6f99f..02740ee 100644 --- a/interfaces/ipc/include/distributed_input_client.h +++ b/interfaces/ipc/include/distributed_input_client.h @@ -115,9 +115,6 @@ private: private: static std::shared_ptr instance; - bool m_bIsAlreadyInitWhiteList = false; - const std::string localDevId_ = "localNodeDevice"; - DInputServerType serverType = DInputServerType::NULL_SERVER_TYPE; DInputDeviceType inputTypes_ = DInputDeviceType::NONE; diff --git a/interfaces/ipc/src/distributed_input_client.cpp b/interfaces/ipc/src/distributed_input_client.cpp index b7cf57f..d5d333f 100644 --- a/interfaces/ipc/src/distributed_input_client.cpp +++ b/interfaces/ipc/src/distributed_input_client.cpp @@ -123,7 +123,6 @@ int32_t DistributedInputClient::ReleaseSource() serverType = DInputServerType::NULL_SERVER_TYPE; inputTypes_ = DInputDeviceType::NONE; - m_bIsAlreadyInitWhiteList = false; sinkTypeCallback = nullptr; sourceTypeCallback = nullptr; addWhiteListCallback = nullptr; @@ -139,9 +138,8 @@ int32_t DistributedInputClient::ReleaseSink() } serverType = DInputServerType::NULL_SERVER_TYPE; inputTypes_ = DInputDeviceType::NONE; - m_bIsAlreadyInitWhiteList = false; sinkTypeCallback = nullptr; - WhiteListUtil::GetInstance().ClearWhiteList(localDevId_); + WhiteListUtil::GetInstance().ClearWhiteList(LOCAL_DEV_ID); return DinputSAManager::GetInstance().dInputSinkProxy_->Release(); } @@ -294,25 +292,7 @@ int32_t DistributedInputClient::StopRemoteInput( bool DistributedInputClient::IsNeedFilterOut(const std::string& deviceId, const BusinessEvent& event) { DHLOGI("%s called, deviceId: %s", __func__, GetAnonyString(deviceId).c_str()); - if (serverType == DInputServerType::NULL_SERVER_TYPE) { - DHLOGE("No sa start using."); - return true; - } - - if (serverType == DInputServerType::SINK_SERVER_TYPE) { - if (m_bIsAlreadyInitWhiteList) { - return WhiteListUtil::GetInstance().IsNeedFilterOut(localDevId_, event); - } - - if (WhiteListUtil::GetInstance().Init(localDevId_) != DH_SUCCESS) { - return false; - } - m_bIsAlreadyInitWhiteList = true; - - return WhiteListUtil::GetInstance().IsNeedFilterOut(localDevId_, event); - } - - return !WhiteListUtil::GetInstance().IsNeedFilterOut(deviceId, event); + return WhiteListUtil::GetInstance().IsNeedFilterOut(deviceId, event); } DInputServerType DistributedInputClient::IsStartDistributedInput(const uint32_t& inputType) @@ -371,7 +351,8 @@ void DistributedInputClient::AddWhiteListInfos( { nlohmann::json inputData = nlohmann::json::parse(strJson); size_t jsonSize = inputData.size(); - DHLOGI("AddWhiteListInfosCb OnResult json size:%d.\n", jsonSize); + DHLOGI("AddWhiteListInfosCb OnResult deviceId: %s, json str: %s, json size:%d.\n", + GetAnonyString(deviceId).c_str(), GetAnonyString(strJson).c_str(), jsonSize); TYPE_WHITE_LIST_VEC vecWhiteList = inputData; WhiteListUtil::GetInstance().SyncWhiteList(deviceId, vecWhiteList); } diff --git a/services/sink/sinkmanager/include/distributed_input_sink_manager.h b/services/sink/sinkmanager/include/distributed_input_sink_manager.h index d545361..d488006 100644 --- a/services/sink/sinkmanager/include/distributed_input_sink_manager.h +++ b/services/sink/sinkmanager/include/distributed_input_sink_manager.h @@ -73,10 +73,6 @@ public: void SetStartTransFlag(const DInputServerType flag); - bool GetInitWhiteListFlag(); - - void SetInitWhiteListFlag(bool isInit); - uint32_t GetInputTypes(); void SetInputTypes(const uint32_t& inputTypess); @@ -90,7 +86,6 @@ public: int32_t Dump(int32_t fd, const std::vector& args) override; private: - bool isAlreadyInitWhiteList_ = false; ServiceSinkRunningState serviceRunningState_ = ServiceSinkRunningState::STATE_NOT_START; DInputServerType isStartTrans_ = DInputServerType::NULL_SERVER_TYPE; std::shared_ptr statuslistener_; diff --git a/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp b/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp index 2e27a89..96d2562 100644 --- a/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp +++ b/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp @@ -79,18 +79,6 @@ void DistributedInputSinkManager::DInputSinkListener::onPrepareRemoteInput( // send prepare result and if result ok, send white list TYPE_WHITE_LIST_VEC vecFilter; - if (sinkManagerObj_->GetInitWhiteListFlag() == false) { - if (WhiteListUtil::GetInstance().Init(deviceId) != DH_SUCCESS) { - DHLOGE("%s called, init white list fail!", __func__); - jsonStr[DINPUT_SOFTBUS_KEY_RESP_VALUE] = true; - jsonStr[DINPUT_SOFTBUS_KEY_WHITE_LIST] = ""; - smsg = jsonStr.dump(); - DistributedInputSinkTransport::GetInstance().RespPrepareRemoteInput(sessionId, smsg); - return; - } - sinkManagerObj_->SetInitWhiteListFlag(true); - } - WhiteListUtil::GetInstance().GetWhiteList(deviceId, vecFilter); if (vecFilter.empty() || vecFilter[0].empty() || vecFilter[0][0].empty()) { DHLOGE("onPrepareRemoteInput called, white list is null."); @@ -338,16 +326,6 @@ void DistributedInputSinkManager::SetStartTransFlag(const DInputServerType flag) isStartTrans_ = flag; } -bool DistributedInputSinkManager::GetInitWhiteListFlag() -{ - return isAlreadyInitWhiteList_; -} - -void DistributedInputSinkManager::SetInitWhiteListFlag(bool isInit) -{ - isAlreadyInitWhiteList_ = isInit; -} - uint32_t DistributedInputSinkManager::GetInputTypes() { return static_cast(inputTypes_);