CookieManager.cpp 9.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
/*
 * MIT License
 *
 * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "Util/util.h"
#include "Util/MD5.h"
#include "Common/config.h"
#include "CookieManager.h"

//////////////////////////////CookieData////////////////////////////////////
CookieData::CookieData(const CookieManager::Ptr &manager,
                       const string &cookie,
                       const string &uid,
                       uint64_t max_elapsed,
                       const string &path){
    _uid = uid;
    _max_elapsed = max_elapsed;
    _cookie_uuid = cookie;
    _path = path;
    _manager = manager;
    manager->onAddCookie(path,uid,cookie);
}

CookieData::~CookieData() {
    auto strongManager = _manager.lock();
    if(strongManager){
        strongManager->onDelCookie(_path,_uid,_cookie_uuid);
    }
}
string CookieData::getCookie(const string &cookie_name) const {
    return (StrPrinter << cookie_name << "=" << _cookie_uuid << ";expires=" << cookieExpireTime() << ";path=" << _path);
}

bool CookieData::isExpired() {
    return _ticker.elapsedTime() > _max_elapsed * 1000;
}

void CookieData::updateTime() {
    _ticker.resetTime();
}
const string & CookieData::getUid() const{
    return _uid;
}
string CookieData::cookieExpireTime() const{
    char buf[64];
    time_t tt = time(NULL) + _max_elapsed;
    strftime(buf, sizeof buf, "%a, %b %d %Y %H:%M:%S GMT", gmtime(&tt));
    return buf;
}

const string& CookieData::getCookie() const {
    return _cookie_uuid;
}

const string& CookieData::getPath() const{
    return _path;
}

//////////////////////////////CookieManager////////////////////////////////////
INSTANCE_IMP(CookieManager);

CookieData::Ptr CookieManager::addCookie(const string &uidIn, int max_client ,uint64_t max_elapsed, const string &path) {
    lock_guard<recursive_mutex> lck(_mtx_cookie);
    auto cookie = _geneator.obtain();
    auto uid = uidIn.empty() ? cookie : uidIn;
    auto oldCookie = getOldestCookie(uid, max_client , path);
    if(!oldCookie.empty()){
        //假如该账号已经登录了,那么删除老的cookie。
        //目的是实现单账号多地登录时挤占登录
        delCookie(oldCookie,path);
    }
    CookieData::Ptr data(new CookieData(shared_from_this(),cookie,uid,max_elapsed,path));
    //保存该账号下的新cookie
    _map_cookie[path][cookie] = data;
    return data;
}

bool CookieManager::delCookie(const CookieData::Ptr &cookie) {
    if(!cookie){
        return false;
    }
    return delCookie(cookie->getPath(),cookie->getCookie());
}

bool CookieManager::delCookie(const string &path , const string &cookie) {
    lock_guard<recursive_mutex> lck(_mtx_cookie);
    auto it = _map_cookie.find(path);
    if(it == _map_cookie.end()){
        return false;
    }
    return it->second.erase(cookie);
}

CookieData::Ptr CookieManager::getCookie(const string &cookie, const string &path) {
    lock_guard<recursive_mutex> lck(_mtx_cookie);
    auto it_path = _map_cookie.find(path);
    if(it_path == _map_cookie.end()){
        //该path下没有任何cookie
        return nullptr;
    }
    auto it_cookie = it_path->second.find(cookie);
    if(it_cookie == it_path->second.end()){
        //该path下没有对应的cookie
        return nullptr;
    }
    if(it_cookie->second->isExpired()){
        //cookie过期
        it_path->second.erase(it_cookie);
        return nullptr;
    }
    return it_cookie->second;
}

CookieData::Ptr CookieManager::getCookie(const StrCaseMap &http_header, const string &cookie_name, const string &path) {
    auto it = http_header.find("Cookie");
    if (it == http_header.end()) {
        return nullptr;
    }
    auto cookie = FindField(it->second.data(), (cookie_name + "=").data(), ";");
    if (!cookie.size()) {
        cookie = FindField(it->second.data(), (cookie_name + "=").data(), nullptr);
    }
    if(cookie.empty()){
        return nullptr;
    }
    return CookieManager::Instance().getCookie(cookie, path);
}

CookieManager::CookieManager() {
    //定时删除过期的cookie,防止内存膨胀
    _timer = std::make_shared<Timer>(10,[this](){
        onManager();
        return true;
    }, nullptr);
}

CookieManager::~CookieManager() {
    _timer.reset();
}

void CookieManager::onManager() {
    lock_guard<recursive_mutex> lck(_mtx_cookie);
    //先遍历所有path
    for(auto it_path = _map_cookie.begin() ; it_path != _map_cookie.end() ;){
        //再遍历该path下的所有cookie
        for (auto it_cookie = it_path->second.begin() ; it_cookie != it_path->second.end() ; ){
            if(it_cookie->second->isExpired()){
                //cookie过期,移除记录
                WarnL << it_cookie->second->getUid() << " cookie过期";
                it_cookie = it_path->second.erase(it_cookie);
                continue;
            }
            ++it_cookie;
        }

        if(it_path->second.empty()){
            //该path下没有任何cooki记录,移除之
            WarnL << "该path下没有任何cooki记录:" << it_path->first;
            it_path = _map_cookie.erase(it_path);
            continue;
        }
        ++it_path;
    }
}

void CookieManager::onAddCookie(const string &path,const string &uid,const string &cookie){
    //添加新的cookie,我们记录下这个uid下有哪些cookie,目的是实现单账号多地登录时挤占登录
    lock_guard<recursive_mutex> lck(_mtx_cookie);
    //相同用户下可以存在多个cookie(意味多地登录),这些cookie根据登录时间的早晚依次排序
    _map_uid_to_cookie[path][uid][getCurrentMillisecond()] = cookie;
}
void CookieManager::onDelCookie(const string &path,const string &uid,const string &cookie){
    lock_guard<recursive_mutex> lck(_mtx_cookie);
    //回收随机字符串
    _geneator.release(cookie);

    auto it_path = _map_uid_to_cookie.find(path);
    if(it_path == _map_uid_to_cookie.end()){
        //该path下未有任意用户登录
        return;
    }
    auto it_uid = it_path->second.find(uid);
    if(it_uid == it_path->second.end()){
        //该用户尚未登录
        return;
    }

    //遍历同一名用户下的所有客户端,移除命中的客户端
    for(auto it_cookie = it_uid->second.begin() ; it_cookie != it_uid->second.end() ; ++it_cookie ){
        if(it_cookie->second != cookie) {
            //不是该cookie
            continue;
        }
        //移除该用户名下的某个cookie,这个设备cookie将失效
        it_uid->second.erase(it_cookie);

        if(it_uid->second.size() != 0) {
            break;
        }

        //该用户名下没有任何设备在线,移除之
        it_path->second.erase(it_uid);

        if(it_path->second.size() != 0) {
            break;
        }
        //该path下未有任何用户在线,移除之
        _map_uid_to_cookie.erase(it_path);
        break;
    }

}

string CookieManager::getOldestCookie(const string &uid, int max_client,const string &path){
    lock_guard<recursive_mutex> lck(_mtx_cookie);
    auto it_path = _map_uid_to_cookie.find(path);
    if(it_path == _map_uid_to_cookie.end()){
        //该路径下未有任意cookie
        return "";
    }
    auto it_uid = it_path->second.find(uid);
    if(it_uid == it_path->second.end()){
        //该用户从未登录过
        return "";
    }
    if(it_uid->second.size() < MAX(1,max_client)){
        //同一名用户下,客户端个数还没达到限制个数
        return "";
    }
    //客户端个数超过限制,移除最先登录的客户端
    return it_uid->second.begin()->second;
}

/////////////////////////////////CookieGeneator////////////////////////////////////
string CookieGeneator::obtain(){
    //获取唯一的防膨胀的随机字符串
    while (true){
        auto str = obtain_l();
        if(_obtained.find(str) == _obtained.end()){
            //没有重复
            _obtained.emplace(str);
            return str;
        }
    }
}
void CookieGeneator::release(const string &str){
    //从防膨胀库中移除
    _obtained.erase(str);
}

string CookieGeneator::obtain_l(){
    //12个伪随机字节 + 4个递增的整形字节,然后md5即为随机字符串
    auto str = makeRandStr(12,false);
    str.append((char *)&_index, sizeof(_index));
    ++_index;
    return MD5(str).hexdigest();
}