From 9ef37369891040e0c4f3b49a5b6eec427c2ba970 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Sat, 27 Oct 2012 09:03:49 +0000 Subject: [PATCH] added: to 'ipban' winix function: possibility to remove a ban (or all bans) added: to SessionManager: sorting of the ban list (in the second thread) git-svn-id: svn://ttmath.org/publicrep/winix/trunk@903 e52654a7-88a9-db11-a3e9-0013d4bc506e --- core/ipbancontainer.cpp | 28 +++++++++++++++++++++++++++- core/ipbancontainer.h | 4 +++- core/misc.h | 1 + core/sessionmanager.cpp | 20 ++++++++++++++++++++ core/sessionmanager.h | 4 +++- core/system.cpp | 28 ++++++++++++++++++++++++---- core/system.h | 6 ++++-- functions/ipban.cpp | 21 +++++++++++++++++++++ functions/ipban.h | 4 ++++ functions/login.h | 1 + html/fun_ipban.html | 5 +++++ 11 files changed, 113 insertions(+), 9 deletions(-) diff --git a/core/ipbancontainer.cpp b/core/ipbancontainer.cpp index f42c84e..58e7ab4 100644 --- a/core/ipbancontainer.cpp +++ b/core/ipbancontainer.cpp @@ -16,7 +16,7 @@ IPBanContainer::IPBanContainer() { - is_ipban_tab_sorted = false; + is_ipban_tab_sorted = true; // an empty list is sorted soft_max_size = 100; max_size = 110; } @@ -49,6 +49,7 @@ IPBan & IPBanContainer::AddIP(int ip) RemoveOldRecords(); ipban_tab.push_back(ip_ban); + is_ipban_tab_sorted = false; return ipban_tab.back(); } else @@ -58,6 +59,31 @@ IPBan & IPBanContainer::AddIP(int ip) } +void IPBanContainer::RemoveIP(int ip) +{ + IPBan * ipban = FindIP(ip); + + if( ipban ) + { + size_t index = ipban - &ipban_tab[0]; + ipban_tab.erase(ipban_tab.begin() + index); + } +} + + +bool IPBanContainer::IsSorted() +{ + return is_ipban_tab_sorted; +} + + +void IPBanContainer::Clear() +{ + ipban_tab.clear(); + is_ipban_tab_sorted = true; +} + + // we need to remove some old records for the size of the container // to be less or equal to soft_max_size void IPBanContainer::RemoveOldRecords() diff --git a/core/ipbancontainer.h b/core/ipbancontainer.h index 1247af4..f8f2228 100644 --- a/core/ipbancontainer.h +++ b/core/ipbancontainer.h @@ -27,7 +27,9 @@ public: size_t Size(); IPBan & GetIPBan(size_t index); void SetMaxSize(size_t soft_size, size_t size); - + void RemoveIP(int ip); + void Clear(); + bool IsSorted(); private: diff --git a/core/misc.h b/core/misc.h index 45a109b..ccf2cac 100755 --- a/core/misc.h +++ b/core/misc.h @@ -234,6 +234,7 @@ PT::WTextStream IPToStr(unsigned int ip); PT::WTextStream IPToStr(int ip); + bool IsWhite(wchar_t s); bool IsWhite(const wchar_t * str, bool treat_new_line_as_white = false); bool IsWhite(const std::wstring & str, bool treat_new_line_as_white = false); diff --git a/core/sessionmanager.cpp b/core/sessionmanager.cpp index 583ee5c..127406d 100755 --- a/core/sessionmanager.cpp +++ b/core/sessionmanager.cpp @@ -460,8 +460,16 @@ IPBan & SessionManager::GetIPBan(size_t index) } +void SessionManager::RemoveIPBan(int ip) +{ + ban_tab.RemoveIP(ip); +} +void SessionManager::ClearIPBanList() +{ + ban_tab.Clear(); +} /* @@ -488,6 +496,7 @@ SessionContainer::Iterator i; { Lock(); + CheckWheterIPListIsSorted(); CheckSession(i); exit = synchro->was_stop_signal; @@ -496,6 +505,17 @@ SessionContainer::Iterator i; } +// objects locked +void SessionManager::CheckWheterIPListIsSorted() +{ + if( !ban_tab.IsSorted() ) + { + log << log4 << "SM: sorting the ban list" << logend; + ban_tab.Sort(); + } +} + + // it's called from the other thread (with Lock and Unlock) void SessionManager::CheckSession(SessionContainer::Iterator & i) { diff --git a/core/sessionmanager.h b/core/sessionmanager.h index 129ca23..f955c2f 100755 --- a/core/sessionmanager.h +++ b/core/sessionmanager.h @@ -62,7 +62,8 @@ public: IPBan & AddIPToBanList(int ip); size_t BanListSize(); IPBan & GetIPBan(size_t index); - + void RemoveIPBan(int ip); + void ClearIPBanList(); private: @@ -96,6 +97,7 @@ private: void CheckSession(SessionContainer::Iterator & i); bool IsSessionOutdated(const Session & s) const; void DeleteSession(Session * del_session); + void CheckWheterIPListIsSorted(); }; diff --git a/core/system.cpp b/core/system.cpp index d723f1f..f1d552a 100755 --- a/core/system.cpp +++ b/core/system.cpp @@ -324,20 +324,40 @@ void System::RedirectWithFunctionAndParamsTo(const std::wstring & url) } -void System::RedirectToLastDir() +void System::RedirectToLastDir(const wchar_t * postfix) { if( !cur->request->dir_tab.empty() ) - RedirectTo( *cur->request->dir_tab.back() ); + RedirectTo( *cur->request->dir_tab.back(), postfix ); } -void System::RedirectToLastItem() +void System::RedirectToLastItem(const wchar_t * postfix) { if( cur->request->last_item ) - RedirectTo( *cur->request->last_item ); + RedirectTo( *cur->request->last_item, postfix ); } +void System::RedirectToLastFunction(const wchar_t * postfix) +{ + RedirectToLastDir(); + + if( cur->request->is_item ) + { + cur->request->redirect_to += cur->request->item.url; + cur->request->redirect_to += '/'; + } + + if( cur->request->function ) + cur->request->redirect_to += cur->request->function->fun.url; + + if( postfix ) + { + cur->request->redirect_to += '/'; + cur->request->redirect_to += postfix; + } +} + bool System::CanChangeUser(const Item & item, long new_user_id) { diff --git a/core/system.h b/core/system.h index b698df7..e855dd8 100755 --- a/core/system.h +++ b/core/system.h @@ -96,8 +96,10 @@ public: void RedirectTo(const std::wstring & url); void RedirectWithFunctionAndParamsTo(const wchar_t * url); void RedirectWithFunctionAndParamsTo(const std::wstring & url); - void RedirectToLastDir(); - void RedirectToLastItem(); // redirect to an item if exists or to the last directory + void RedirectToLastDir(const wchar_t * postfix = 0); + void RedirectToLastItem(const wchar_t * postfix = 0); // redirect to an item if exists or to the last directory + void RedirectToLastFunction(const wchar_t * postfix = 0); + bool CanChangeUser(const Item & item, long new_user_id); bool CanChangeGroup(const Item & item, long new_group_id); diff --git a/functions/ipban.cpp b/functions/ipban.cpp index 8b07281..9528c0f 100755 --- a/functions/ipban.cpp +++ b/functions/ipban.cpp @@ -7,8 +7,13 @@ * */ +#include +#include +#include +#include #include "ipban.h" #include "functions.h" +#include "core/sessionmanager.h" @@ -28,15 +33,31 @@ bool IPBanFun::HasAccess() } + void IPBanFun::MakePost() { } + void IPBanFun::MakeGet() { + if( cur->request->IsParam(L"removeip") ) + { + if( cur->request->ParamValue(L"removeip") == L"all" ) + { + session_manager->ClearIPBanList(); + } + else + { + AssignString(cur->request->ParamValue(L"removeip"), tmp_ip_str); + int ip = (int)inet_addr(tmp_ip_str.c_str()); + session_manager->RemoveIPBan(ip); + } + system->RedirectToLastFunction(); + } } diff --git a/functions/ipban.h b/functions/ipban.h index 3f92949..7e282a8 100755 --- a/functions/ipban.h +++ b/functions/ipban.h @@ -28,6 +28,10 @@ public: void MakePost(); void MakeGet(); +private: + + std::string tmp_ip_str; + }; diff --git a/functions/login.h b/functions/login.h index d3b5e4b..1648cf5 100755 --- a/functions/login.h +++ b/functions/login.h @@ -49,6 +49,7 @@ private: std::string pass_decrypted; std::wstring pass_hashed; std::wstring salt; + }; diff --git a/html/fun_ipban.html b/html/fun_ipban.html index 1773d2c..d21a8b4 100644 --- a/html/fun_ipban.html +++ b/html/fun_ipban.html @@ -34,6 +34,11 @@ [end] + +

+ Remove all IP's from the list +

+ [else]

There are not any IP addresses banned at the moment.

[end]