2c424710创建于 2025年11月17日历史提交
/*
 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "publish_manager.h"

#include <cinttypes>

#include "event_log_wrapper.h"
#include "system_time.h"

namespace OHOS {
namespace EventFwk {
PublishManager::PublishManager()
{}

PublishManager::~PublishManager()
{}

bool PublishManager::CheckIsFloodAttack(pid_t appUid)
{
    EVENT_LOGD(LOG_TAG_CES, "enter");
    std::lock_guard<ffrt::mutex> lock(mutex_);
    bool isAttacked = false;
    int64_t now = SystemTime::GetNowSysTime();
    EVENT_LOGD(LOG_TAG_CES, "dispatch common event by app (uid = %{public}d) at now = %{public}" PRId64, appUid, now);
    auto iter = floodAttackAppStatistics_.find(appUid);
    if (iter == floodAttackAppStatistics_.end()) {
        floodAttackAppStatistics_[appUid].emplace_back(now);
        return isAttacked;
    }
    // Remove expired record
    auto iterVec = iter->second.begin();
    while (iterVec != iter->second.end()) {
        if (now - *iterVec > FLOOD_ATTACK_INTERVAL_MAX) {
            iterVec = iter->second.erase(iterVec);
        } else {
            break;
        }
    }
    if (iter->second.size() + 1 > FLOOD_ATTACK_NUMBER_MAX) {
        EVENT_LOGW(LOG_TAG_CES, "CES was maliciously attacked by app (uid = %{public}d)", appUid);
        isAttacked = true;
    }
    iter->second.emplace_back(now);
    return isAttacked;
}
}  // namespace EventFwk
}  // namespace OHOS