Skip to content

Commit

Permalink
Fix #198
Browse files Browse the repository at this point in the history
Fix #200
  • Loading branch information
RoLex committed Mar 8, 2023
1 parent e942b12 commit e8b89c6
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 54 deletions.
89 changes: 51 additions & 38 deletions src/cbanlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ cBanList::cBanList(cServerDC *s):
AddCol("note_op", "varchar(255)", "", true, mModel.mNoteOp);
AddCol("note_usr", "varchar(255)", "", true, mModel.mNoteUsr);
AddCol("share_size", "varchar(18)", "", true, mModel.mShare);
mMySQLTable.mExtra = "UNIQUE (ip,nick), ";
mMySQLTable.mExtra += "INDEX nick_index (nick), ";
mMySQLTable.mExtra += "INDEX date_index (date_limit), ";
mMySQLTable.mExtra += "INDEX range_index (range_fr)";
mMySQLTable.mExtra = "unique (`ip`, `nick`), ";
mMySQLTable.mExtra += "index `nick_index` (`nick`), ";
mMySQLTable.mExtra += "index `date_index` (`date_limit`), ";
mMySQLTable.mExtra += "index `range_index` (`range_fr`)";
SetBaseTo(&mModel);
}

Expand All @@ -84,22 +84,22 @@ cUnBanList::cUnBanList(cServerDC* s):
AddPrimaryKey("date_unban");
AddCol("unban_op", "varchar(128)", "", true, mModelUn.mUnNickOp);
AddCol("unban_reason", "varchar(255)", "", true, mModelUn.mUnReason);
mMySQLTable.mExtra = "UNIQUE (ip, nick, date_unban)";
mMySQLTable.mExtra = "unique (`ip`, `nick`, `date_unban`)";
}

cUnBanList::~cUnBanList()
{}

void cBanList::Cleanup()
{
mQuery.OStream() << "delete from " << mMySQLTable.mName << " where date_limit is not null and date_limit < " << (mS->mTime.Sec() - (3600 * 24 * 7));
mQuery.OStream() << "delete from `" << mMySQLTable.mName << "` where `date_limit` is not null and `date_limit` < " << (mS->mTime.Sec() - (3600 * 24 * 7));
mQuery.Query();
mQuery.Clear();
}

void cUnBanList::Cleanup()
{
mQuery.OStream() << "delete from " << mMySQLTable.mName << " where date_unban < " << (cTime().Sec() - (3600 * 24 * 30)); // todo: mS->mTime.Sec()
mQuery.OStream() << "delete from `" << mMySQLTable.mName << "` where `date_unban` < " << (cTime().Sec() - (3600 * 24 * 30)); // todo: mS->mTime.Sec()
mQuery.Query();
mQuery.Clear();
}
Expand Down Expand Up @@ -443,61 +443,74 @@ bool cBanList::AddTestCondition(ostream &os, const string &value, int mask)
string host;
unsigned long num = 0;

switch(mask) {
switch (mask) {
case eBF_NICK:
os << "( nick = '"; cConfMySQL::WriteStringConstant(os, value); os << "')";
break;
os << "(`nick` = '"; cConfMySQL::WriteStringConstant(os, value); os << "')";
//os << "(`ip` = '_nickban_' and `nick` = '"; cConfMySQL::WriteStringConstant(os, value); os << "')";
break;

case eBF_IP:
os << "(ip='"; cConfMySQL::WriteStringConstant(os, value); os << "')";
break;
//case (int)eBF_NICK : os << "(ip='_nickban_' AND nick='" << value << "')"; break;
//case (int)eBF_IP : os << "(nick='_ipban_' AND ip='" << value << "')"; break;
os << "(`ip` = '"; cConfMySQL::WriteStringConstant(os, value); os << "')";
//os << "(`nick` = '_ipban_' and `ip` = '"; cConfMySQL::WriteStringConstant(os, value); os << "')";
break;

case eBF_RANGE:
if (!Ip2Num(value, num, false)) {
os << " 0 ";
return false;
}

os << "(`nick` = '_rangeban_' and " << num << " between `range_fr` and `range_to`)";
os << "(`nick` = '_rangeban_' and (" << num << " between `range_fr` and `range_to`))";
break;

case eBF_SHARE :
os << "(nick='_shareban_' AND share_size = '" << value << "')";
break;
case eBF_HOST1 :
if(!this->GetHostSubstring(value, host, 1)) {
case eBF_SHARE:
os << "(`nick` = '_shareban_' and `share_size` = '"; cConfMySQL::WriteStringConstant(os, value); os << "')";
break;

case eBF_HOST1:
if (!this->GetHostSubstring(value, host, 1)) {
os << " 0 ";
return false;
}
os << "(ip='_host1ban_' AND '" << host << "' = nick)";
break;
case eBF_HOST2 :
if(!this->GetHostSubstring(value, host, 2)) {

os << "(`ip` = '_host1ban_' and `nick` = '"; cConfMySQL::WriteStringConstant(os, host); os << "')";
break;

case eBF_HOST2:
if (!this->GetHostSubstring(value, host, 2)) {
os << " 0 ";
return false;
}
os << "(ip='_host2ban_' AND '" << host << "' = nick)";
break;
case eBF_HOST3 :
if(!this->GetHostSubstring(value, host, 3)) {

os << "(`ip` = '_host2ban_' and `nick` = '"; cConfMySQL::WriteStringConstant(os, host); os << "')";
break;

case eBF_HOST3:
if (!this->GetHostSubstring(value, host, 3)) {
os << " 0 ";
return false;
};
os << "(ip='_host3ban_' AND '" << host << "' = nick)";
break;
case eBF_HOSTR1 :
if(!this->GetHostSubstring(value, host, -1)) {

os << "(`ip` = '_host3ban_' and `nick` = '"; cConfMySQL::WriteStringConstant(os, host); os << "')";
break;

case eBF_HOSTR1:
if (!this->GetHostSubstring(value, host, -1)) {
os << " 0 ";
return false;
};
os << "(ip='_hostr1ban_' AND '" << host << "' = nick)";
break;
case eBF_PREFIX :
os << "(ip='_prefixban_' AND nick=LEFT('";cConfMySQL::WriteStringConstant(os, value); os << "',LENGTH(nick)))";
break;
default: return false;

os << "(`ip` = '_hostr1ban_' and `nick` = '"; cConfMySQL::WriteStringConstant(os, host); os << "')";
break;

case eBF_PREFIX:
os << "(`ip` = '_prefixban_' and `nick` = left('"; cConfMySQL::WriteStringConstant(os, value); os << "', length(`nick`)))";
break;

default:
return false;
}

return true;
}

Expand Down
2 changes: 0 additions & 2 deletions src/cconndc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ int cConnDC::Send(string &data, bool AddPipe, bool Flush)
mTimeLastAttempt = Server()->mTime;

if (ret > 0) { // calculate upload bandwidth in real time
SetGeoZone(); // must be called first
//Server()->mUploadZone[mGeoZone].Dump();
Server()->mUploadZone[mGeoZone].Insert(Server()->mTime, ret);
//Server()->mUploadZone[mGeoZone].Dump();
Expand Down Expand Up @@ -603,7 +602,6 @@ void cDCConnFactory::DeleteConn(cAsyncConn * &connection)
#endif

if (conn->GetLSFlag(eLS_ALLOWED)) {
conn->SetGeoZone(); // must be called first
mServer->mUserCountTot--;
mServer->mUserCount[conn->mGeoZone]--;

Expand Down
27 changes: 14 additions & 13 deletions src/cdcproto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,6 @@ int cDCProto::DC_ValidateNick(cMessageDC *msg, cConnDC *conn)
return -1;
}

conn->SetGeoZone(); // must be called first
unsigned int limit = mS->mC.max_users_total; // user limits
unsigned int limit_cc = mS->mC.max_users[conn->mGeoZone];
unsigned int limit_extra = 0;
Expand Down Expand Up @@ -1210,21 +1209,23 @@ int cDCProto::DC_MyINFO(cMessageDC *msg, cConnDC *conn)
conn->mpUser->mMyFlag = myinfo_speed[myinfo_speed.size() - 1]; // set status flags, note: we set it before executing callbacks, so plugins have option to change it before showing user to all

if (conn->GetLSFlag(eLS_LOGIN_DONE) != eLS_LOGIN_DONE) { // user sent myinfo for the first time
cBan Ban(mS);
result = mS->mBanList->TestBan(Ban, conn, conn->mpUser->mNick, eBF_SHARE);
if (conn->mpUser->mShare > 0) { // check share ban if non zero
cBan Ban(mS);
result = mS->mBanList->TestBan(Ban, conn, conn->mpUser->mNick, eBF_SHARE);

if (result && (conn->mpUser->mClass <= eUC_REGUSER)) {
os << ((result == 1) ? _("You are prohibited from entering this hub") : _("You are banned from this hub")) << ":\r\n";
Ban.DisplayUser(os);
mS->DCPublicHS(os.str(), conn);
if (result && (conn->mpUser->mClass <= eUC_REGUSER)) {
os << ((result == 1) ? _("You are prohibited from entering this hub") : _("You are banned from this hub")) << ":\r\n";
Ban.DisplayUser(os);
mS->DCPublicHS(os.str(), conn);

if (conn->Log(1))
conn->LogStream() << "Kicked user due to ban detection" << endl;
if (conn->Log(1))
conn->LogStream() << "Kicked user due to ban detection" << endl;

conn->CloseNice(1000, eCR_KICKED);
delete dc_tag;
dc_tag = NULL;
return -1;
conn->CloseNice(1000, eCR_KICKED);
delete dc_tag;
dc_tag = NULL;
return -1;
}
}

#ifndef WITHOUT_PLUGINS
Expand Down
3 changes: 2 additions & 1 deletion src/cserverdc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,8 @@ int cServerDC::OnNewConn(cAsyncConn *nc)
return -1;
#endif

conn->mLock.append("EXTENDEDPROTOCOL_NMDC_"); // todo: EscapeChars with DCN
conn->SetGeoZone(); // set zone once on connect
conn->mLock.append("EXTENDEDPROTOCOL_NMDC_"); // todo: EscapeChars with DCN when dynamic data added

if (mTLSProxy.size())
conn->mLock.append("TLS_");
Expand Down

4 comments on commit e8b89c6

@pavel-pimenov
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а зачем в SQL поля в таблицах оборачиваешь nick так?

@RoLex
Copy link
Member Author

@RoLex RoLex commented on e8b89c6 Mar 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

привычка такая, во избежании такой проблемы > https://dev.mysql.com/doc/refman/8.0/en/keywords.html

@pavel-pimenov
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А почему не заберешь фикс? принято именно != string::npos + это короче
изображение

@RoLex
Copy link
Member Author

@RoLex RoLex commented on e8b89c6 Mar 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

пропустил эту инфу от тебя. логично, избегает лишнего входа в массив cc_zone[pos]. сделаю

Please sign in to comment.