Disconnect unauthed connections after a timeout of 60 secs

git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@1067 726aef4b-f618-498e-8847-2d620e286838
This commit is contained in:
psychon
2008-05-24 17:09:25 +00:00
parent 1e64901f0e
commit e1bf2d21ca
3 changed files with 55 additions and 0 deletions

View File

@@ -12,6 +12,7 @@
#include "DCCSock.h"
#include "IRCSock.h"
#include "Server.h"
#include "Timers.h"
#include "User.h"
CClient::~CClient() {
@@ -23,6 +24,9 @@ CClient::~CClient() {
m_pUser->AddBytesRead(GetBytesRead());
m_pUser->AddBytesWritten(GetBytesWritten());
}
if (m_pTimeout) {
CZNC::Get().GetManager().DelCronByAddr(m_pTimeout);
}
}
void CClient::ReadLine(const CString& sData) {
@@ -1828,6 +1832,11 @@ void CClientAuth::RefuseLogin(const CString& sReason) {
}
void CClient::RefuseLogin(const CString& sReason) {
if (m_pTimeout) {
m_pTimeout->Stop();
m_pTimeout = NULL;
}
PutStatus("Bad username and/or password.");
PutClient(":irc.znc.com 464 " + GetNick() + " :" + sReason);
Close();
@@ -1843,6 +1852,11 @@ void CClient::AcceptLogin(CUser& User) {
m_sPass = "";
m_pUser = &User;
if (m_pTimeout) {
m_pTimeout->Stop();
m_pTimeout = NULL;
}
if (!m_pUser->IsHostAllowed(GetRemoteIP())) {
PutClient(":irc.znc.com 463 " + GetNick() + " :Your host ("
+ GetRemoteIP() + ") is not allowed");
@@ -1860,6 +1874,20 @@ void CClient::AcceptLogin(CUser& User) {
MODULECALL(OnUserAttached(), m_pUser, this, );
}
void CClient::StartLoginTimeout() {
m_pTimeout = new CClientTimeout(this);
CZNC::Get().GetManager().AddCron(m_pTimeout);
}
void CClient::LoginTimeout() {
PutClient("ERROR :Closing link [Timeout]");
Close(Csock::CLT_AFTERWRITE);
if (m_pTimeout) {
m_pTimeout->Stop();
m_pTimeout = NULL;
}
}
void CClient::Connected() {
DEBUG_ONLY(cout << GetSockName() << " == Connected();" << endl);
SetTimeout(900); // Now that we are connected, let nature take its course
@@ -1895,6 +1923,7 @@ void CClient::IRCDisconnected() {
Csock* CClient::GetSockObj(const CString& sHost, unsigned short uPort) {
CClient* pSock = new CClient(sHost, uPort);
pSock->StartLoginTimeout();
return pSock;
}

View File

@@ -17,6 +17,7 @@ class CZNC;
class CUser;
class CIRCSock;
class CClient;
class CClientTimeout;
// !Forward Declarations
class CAuthBase {
@@ -77,6 +78,7 @@ public:
void InitClient() {
m_pUser = NULL;
m_pTimeout = NULL;
m_pIRCSock = NULL;
m_bGotPass = false;
m_bGotNick = false;
@@ -89,6 +91,8 @@ public:
void AcceptLogin(CUser& User);
void RefuseLogin(const CString& sReason);
void StartLoginTimeout();
void LoginTimeout();
CString GetNick(bool bAllowIRCNick = true) const;
CString GetNickMask() const;
@@ -123,6 +127,7 @@ public:
void SetNick(const CString& s);
CUser* GetUser() const { return m_pUser; }
private:
protected:
bool m_bGotPass;
bool m_bGotNick;
@@ -136,6 +141,7 @@ protected:
CIRCSock* m_pIRCSock;
unsigned int m_uKeepNickCounter;
CSmartPtr<CAuthBase> m_spAuth;
CClientTimeout* m_pTimeout;
};
#endif // !_CLIENT_H

View File

@@ -94,4 +94,24 @@ protected:
CUser* m_pUser;
};
class CClientTimeout : public CCron {
public:
CClientTimeout(CClient* pClient) : CCron() {
m_pClient = pClient;
SetName("CClientTimeout::UNKNOWN");
StartMaxCycles(60, 1);
}
virtual ~CClientTimeout() {}
protected:
virtual void RunJob() {
if (m_pClient) {
m_pClient->LoginTimeout();
m_pClient = NULL;
}
}
CClient* m_pClient;
};
#endif // !_TIMERS_H