X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ffreenet%2Fintroductionpuzzleinserter.cpp;h=000d973fd49b0f5754659a921402b8dbda7d5c14;hb=59a5414ec47a2932a7802fcd1d98c4d80166564f;hp=1fa13c0f88e6e410e601e2d6e14f81df6dba3e90;hpb=07319f767b4c4f0e06e9957f217d5107b196617b;p=fms.git diff --git a/src/freenet/introductionpuzzleinserter.cpp b/src/freenet/introductionpuzzleinserter.cpp index 1fa13c0..000d973 100644 --- a/src/freenet/introductionpuzzleinserter.cpp +++ b/src/freenet/introductionpuzzleinserter.cpp @@ -3,6 +3,10 @@ #include "../../include/stringfunctions.h" #include "../../include/option.h" #include "../../include/freenet/captcha/simplecaptcha.h" +#ifdef ALTERNATE_CAPTCHA +#include "../../include/freenet/captcha/alternatecaptcha1.h" +#include "../../include/freenet/captcha/alternatecaptcha2.h" +#endif #include "../../include/base64.h" #include @@ -13,12 +17,12 @@ #include #endif -IntroductionPuzzleInserter::IntroductionPuzzleInserter():IIndexInserter() +IntroductionPuzzleInserter::IntroductionPuzzleInserter(SQLite3DB::DB *db):IIndexInserter(db) { Initialize(); } -IntroductionPuzzleInserter::IntroductionPuzzleInserter(FCPv2 *fcp):IIndexInserter(fcp) +IntroductionPuzzleInserter::IntroductionPuzzleInserter(SQLite3DB::DB *db, FCPv2::Connection *fcp):IIndexInserter(db,fcp) { Initialize(); } @@ -33,8 +37,13 @@ void IntroductionPuzzleInserter::CheckForNeededInsert() while(!rs.AtEnd()) { - std::string localidentityidstr; + int localidentityid=0; + std::string localidentityidstr=""; Poco::DateTime now; + float minutesbetweeninserts=0; + minutesbetweeninserts=1440.0/(float)m_maxpuzzleinserts; + Poco::DateTime lastinsert=now; + lastinsert-=Poco::Timespan(0,0,minutesbetweeninserts,0,0); if(rs.GetField(0)) { @@ -47,7 +56,16 @@ void IntroductionPuzzleInserter::CheckForNeededInsert() // identity doesn't have any non-solved puzzles for today - start a new insert if(rs2.Empty()==true) { - StartInsert(rs.GetInt(0)); + // make sure we are on the next day or the appropriate amount of time has elapsed since the last insert + if(m_lastinserted.find(rs.GetInt(0))==m_lastinserted.end() || m_lastinserted[rs.GetInt(0)]<=lastinsert || m_lastinserted[rs.GetInt(0)].day()!=now.day()) + { + StartInsert(rs.GetInt(0)); + m_lastinserted[rs.GetInt(0)]=now; + } + else + { + m_log->trace("IntroductionPuzzleInserter::CheckForNeededInsert waiting to insert puzzle for "+localidentityidstr); + } } rs.Next(); @@ -57,13 +75,26 @@ void IntroductionPuzzleInserter::CheckForNeededInsert() void IntroductionPuzzleInserter::GenerateCaptcha(std::string &encodeddata, std::string &solution) { - SimpleCaptcha captcha; + ICaptcha *cap=0; +#ifdef ALTERNATE_CAPTCHA + if(rand()%2==0) + { + cap=new AlternateCaptcha1(); + } + else + { + cap=new AlternateCaptcha2(); + } + m_log->trace("IntroductionPuzzleInserter::GenerateCaptcha using alternate captcha generator"); +#else + cap=new SimpleCaptcha(); +#endif std::vector puzzle; std::vector puzzlesolution; - captcha.Generate(); - captcha.GetPuzzle(puzzle); - captcha.GetSolution(puzzlesolution); + cap->Generate(); + cap->GetPuzzle(puzzle); + cap->GetSolution(puzzlesolution); encodeddata.clear(); solution.clear(); @@ -71,9 +102,11 @@ void IntroductionPuzzleInserter::GenerateCaptcha(std::string &encodeddata, std:: Base64::Encode(puzzle,encodeddata); solution.insert(solution.begin(),puzzlesolution.begin(),puzzlesolution.end()); + delete cap; + } -const bool IntroductionPuzzleInserter::HandlePutFailed(FCPMessage &message) +const bool IntroductionPuzzleInserter::HandlePutFailed(FCPv2::Message &message) { SQLite3DB::Statement st; std::vector idparts; @@ -99,7 +132,7 @@ const bool IntroductionPuzzleInserter::HandlePutFailed(FCPMessage &message) return true; } -const bool IntroductionPuzzleInserter::HandlePutSuccessful(FCPMessage &message) +const bool IntroductionPuzzleInserter::HandlePutSuccessful(FCPv2::Message &message) { Poco::DateTime now; SQLite3DB::Statement st; @@ -139,23 +172,24 @@ const bool IntroductionPuzzleInserter::HandlePutSuccessful(FCPMessage &message) void IntroductionPuzzleInserter::Initialize() { m_fcpuniquename="IntroductionPuzzleInserter"; + m_maxpuzzleinserts=50; } const bool IntroductionPuzzleInserter::StartInsert(const long &localidentityid) { Poco::DateTime now; - std::string idstring; + std::string idstring=""; long index=0; - std::string indexstr; + std::string indexstr=""; Poco::UUIDGenerator uuidgen; Poco::UUID uuid; - std::string messagebase; + std::string messagebase=""; IntroductionPuzzleXML xml; - std::string encodedpuzzle; - std::string solutionstring; - FCPMessage message; - std::string xmldata; - std::string xmldatasizestr; + std::string encodedpuzzle=""; + std::string solutionstring=""; + FCPv2::Message message; + std::string xmldata=""; + std::string xmldatasizestr=""; std::string privatekey=""; std::string publickey=""; std::string keypart=""; @@ -173,7 +207,7 @@ const bool IntroductionPuzzleInserter::StartInsert(const long &localidentityid) } StringFunctions::Convert(index,indexstr); - if(index<50) + if(indexQuery("SELECT PrivateKey,PublicKey FROM tblLocalIdentity WHERE LocalIdentityID="+idstring+";"); if(rs2.Empty()==false && rs2.GetField(0)!=NULL) @@ -190,9 +224,15 @@ const bool IntroductionPuzzleInserter::StartInsert(const long &localidentityid) } } - Option::Instance()->Get("MessageBase",messagebase); + Option option(m_db); + option.Get("MessageBase",messagebase); GenerateCaptcha(encodedpuzzle,solutionstring); + if(encodedpuzzle.size()==0) + { + m_log->fatal("IntroductionPuzzleInserter::StartInsert could not create introduction puzzle"); + return false; + } try { @@ -218,11 +258,11 @@ const bool IntroductionPuzzleInserter::StartInsert(const long &localidentityid) message["Identifier"]=m_fcpuniquename+"|"+idstring+"|"+indexstr+"|"+xml.GetUUID()+"|"+message["URI"]; message["UploadFrom"]="direct"; message["DataLength"]=xmldatasizestr; - m_fcp->SendMessage(message); - m_fcp->SendRaw(xmldata.c_str(),xmldata.size()); + m_fcp->Send(message); + m_fcp->Send(std::vector(xmldata.begin(),xmldata.end())); // insert to USK - message.Reset(); + message.Clear(); message.SetName("ClientPutComplexDir"); message["URI"]="USK"+privatekey.substr(3)+messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y.%m.%d")+"|IntroductionPuzzle/0/"; message["Identifier"]=m_fcpuniquename+"USK|"+message["URI"]; @@ -230,8 +270,8 @@ const bool IntroductionPuzzleInserter::StartInsert(const long &localidentityid) message["Files.0.Name"]="IntroductionPuzzle.xml"; message["Files.0.UplaodFrom"]="direct"; message["Files.0.DataLength"]=xmldatasizestr; - m_fcp->SendMessage(message); - m_fcp->SendRaw(xmldata.c_str(),xmldata.size()); + m_fcp->Send(message); + m_fcp->Send(std::vector(xmldata.begin(),xmldata.end())); m_db->Execute("INSERT INTO tblIntroductionPuzzleInserts(UUID,Type,MimeType,LocalIdentityID,PuzzleData,PuzzleSolution) VALUES('"+xml.GetUUID()+"','captcha','image/bmp',"+idstring+",'"+encodedpuzzle+"','"+solutionstring+"');"); @@ -241,7 +281,7 @@ const bool IntroductionPuzzleInserter::StartInsert(const long &localidentityid) } else { - m_log->warning("IntroductionPuzzleInserter::StartInsert already inserted 50 puzzles for "+idstring); + m_log->warning("IntroductionPuzzleInserter::StartInsert already inserted max puzzles for "+idstring); } return true;