X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ffreenet%2Fintroductionpuzzleinserter.cpp;h=000d973fd49b0f5754659a921402b8dbda7d5c14;hb=59a5414ec47a2932a7802fcd1d98c4d80166564f;hp=9dca8e095ccfc1e9d79677f8da5e0e9702c781b8;hpb=964f55fd550fc711c0320ce6a24ad713040695d0;p=fms.git diff --git a/src/freenet/introductionpuzzleinserter.cpp b/src/freenet/introductionpuzzleinserter.cpp index 9dca8e0..000d973 100644 --- a/src/freenet/introductionpuzzleinserter.cpp +++ b/src/freenet/introductionpuzzleinserter.cpp @@ -3,71 +3,98 @@ #include "../../include/stringfunctions.h" #include "../../include/option.h" #include "../../include/freenet/captcha/simplecaptcha.h" -#include "../../include/uuidgenerator.h" +#ifdef ALTERNATE_CAPTCHA +#include "../../include/freenet/captcha/alternatecaptcha1.h" +#include "../../include/freenet/captcha/alternatecaptcha2.h" +#endif #include "../../include/base64.h" +#include +#include +#include + #ifdef XMEM #include #endif -IntroductionPuzzleInserter::IntroductionPuzzleInserter() +IntroductionPuzzleInserter::IntroductionPuzzleInserter(SQLite3DB::DB *db):IIndexInserter(db) { Initialize(); } -IntroductionPuzzleInserter::IntroductionPuzzleInserter(FCPv2 *fcp):IFCPConnected(fcp) +IntroductionPuzzleInserter::IntroductionPuzzleInserter(SQLite3DB::DB *db, FCPv2::Connection *fcp):IIndexInserter(db,fcp) { Initialize(); } void IntroductionPuzzleInserter::CheckForNeededInsert() { - // select all local ids that aren't single use and that aren't currently inserting a puzzle - SQLite3DB::Recordset rs=m_db->Query("SELECT LocalIdentityID FROM tblLocalIdentity WHERE SingleUse='false' AND InsertingPuzzle='false' AND PrivateKey IS NOT NULL AND PrivateKey <> '' ORDER BY LastInsertedPuzzle;"); - - while(!rs.AtEnd()) + // only do 1 insert at a time + if(m_inserting.size()==0) { - std::string localidentityidstr; - DateTime now; - now.SetToGMTime(); - - if(rs.GetField(0)) - { - localidentityidstr=rs.GetField(0); - } - - // if this identity has any non-solved puzzles for today, we don't need to insert a new puzzle - SQLite3DB::Recordset rs2=m_db->Query("SELECT UUID FROM tblIntroductionPuzzleInserts WHERE Day='"+now.Format("%Y-%m-%d")+"' AND FoundSolution='false' AND LocalIdentityID="+localidentityidstr+";"); - - // identity doesn't have any non-solved puzzles for today - start a new insert - if(rs2.Empty()==true) + // select all local ids that aren't single use and that aren't currently inserting a puzzle and are publishing a trust list + SQLite3DB::Recordset rs=m_db->Query("SELECT LocalIdentityID FROM tblLocalIdentity WHERE PublishTrustList='true' AND SingleUse='false' AND PrivateKey IS NOT NULL AND PrivateKey <> '' ORDER BY LastInsertedPuzzle;"); + + while(!rs.AtEnd()) { - StartInsert(rs.GetInt(0)); + 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)) + { + localidentityidstr=rs.GetField(0); + } + + // if this identity has any non-solved puzzles for today, we don't need to insert a new puzzle + SQLite3DB::Recordset rs2=m_db->Query("SELECT UUID FROM tblIntroductionPuzzleInserts WHERE Day='"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"' AND FoundSolution='false' AND LocalIdentityID="+localidentityidstr+";"); + + // identity doesn't have any non-solved puzzles for today - start a new insert + if(rs2.Empty()==true) + { + // 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(); } - - rs.Next(); } } -void IntroductionPuzzleInserter::FCPConnected() -{ - m_db->Execute("UPDATE tblLocalIdentity SET InsertingPuzzle='false';"); -} - -void IntroductionPuzzleInserter::FCPDisconnected() -{ - -} - 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(); @@ -75,36 +102,11 @@ void IntroductionPuzzleInserter::GenerateCaptcha(std::string &encodeddata, std:: Base64::Encode(puzzle,encodeddata); solution.insert(solution.begin(),puzzlesolution.begin(),puzzlesolution.end()); -} - -const bool IntroductionPuzzleInserter::HandleMessage(FCPMessage &message) -{ - - if(message["Identifier"].find("IntroductionPuzzleInserter")==0) - { - - // ignore URIGenerated message - if(message.GetName()=="URIGenerated") - { - return true; - } - - if(message.GetName()=="PutSuccessful") - { - return HandlePutSuccessful(message); - } + delete cap; - if(message.GetName()=="PutFailed") - { - return HandlePutFailed(message); - } - - } - - return false; } -const bool IntroductionPuzzleInserter::HandlePutFailed(FCPMessage &message) +const bool IntroductionPuzzleInserter::HandlePutFailed(FCPv2::Message &message) { SQLite3DB::Statement st; std::vector idparts; @@ -113,99 +115,87 @@ const bool IntroductionPuzzleInserter::HandlePutFailed(FCPMessage &message) StringFunctions::Split(message["Identifier"],"|",idparts); StringFunctions::Convert(idparts[1],localidentityid); - st=m_db->Prepare("UPDATE tblLocalIdentity SET InsertingPuzzle='false' WHERE LocalIdentityID=?;"); - st.Bind(0,localidentityid); - st.Step(); - st.Finalize(); - - // if fatal error or collision - mark index - if(message["Fatal"]=="true" || message["Code"]=="9") + // non USK + if(idparts[0]==m_fcpuniquename) { - m_db->Execute("UPDATE tblIntroductionPuzzleInserts SET Day='"+idparts[5]+"', InsertIndex="+idparts[2]+", FoundSolution='true' WHERE UUID='"+idparts[3]+"';"); - } + // if fatal error or collision - mark index + if(message["Fatal"]=="true" || message["Code"]=="9") + { + m_db->Execute("UPDATE tblIntroductionPuzzleInserts SET Day='"+idparts[5]+"', InsertIndex="+idparts[2]+", FoundSolution='true' WHERE UUID='"+idparts[3]+"';"); + } - m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IntroductionPuzzleInserter::HandlePutFailed failed to insert puzzle "+idparts[3]); + RemoveFromInsertList(localidentityid); + + m_log->debug("IntroductionPuzzleInserter::HandlePutFailed failed to insert puzzle "+idparts[3]); + } return true; } -const bool IntroductionPuzzleInserter::HandlePutSuccessful(FCPMessage &message) +const bool IntroductionPuzzleInserter::HandlePutSuccessful(FCPv2::Message &message) { - DateTime now; + Poco::DateTime now; SQLite3DB::Statement st; std::vector idparts; long localidentityid; long insertindex; - now.SetToGMTime(); StringFunctions::Split(message["Identifier"],"|",idparts); - StringFunctions::Convert(idparts[1],localidentityid); - StringFunctions::Convert(idparts[2],insertindex); - - st=m_db->Prepare("UPDATE tblIntroductionPuzzleInserts SET Day=?, InsertIndex=? WHERE UUID=?;"); - st.Bind(0,idparts[5]); - st.Bind(1,insertindex); - st.Bind(2,idparts[3]); - st.Step(); - st.Finalize(); - - st=m_db->Prepare("UPDATE tblLocalIdentity SET InsertingPuzzle='false', LastInsertedPuzzle=? WHERE LocalIdentityID=?;"); - st.Bind(0,now.Format("%Y-%m-%d %H:%M:%S")); - st.Bind(1,localidentityid); - st.Step(); - st.Finalize(); - - m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IntroductionPuzzleInserter::HandlePutSuccessful inserted puzzle "+idparts[3]); - return true; -} - -void IntroductionPuzzleInserter::Initialize() -{ - m_lastchecked.SetToGMTime(); -} + // non USK + if(idparts[0]==m_fcpuniquename) + { + StringFunctions::Convert(idparts[1],localidentityid); + StringFunctions::Convert(idparts[2],insertindex); -void IntroductionPuzzleInserter::Process() -{ + st=m_db->Prepare("UPDATE tblIntroductionPuzzleInserts SET Day=?, InsertIndex=? WHERE UUID=?;"); + st.Bind(0,idparts[5]); + st.Bind(1,insertindex); + st.Bind(2,idparts[3]); + st.Step(); + st.Finalize(); - DateTime now; + st=m_db->Prepare("UPDATE tblLocalIdentity SET LastInsertedPuzzle=? WHERE LocalIdentityID=?;"); + st.Bind(0,Poco::DateTimeFormatter::format(now,"%Y-%m-%d %H:%M:%S")); + st.Bind(1,localidentityid); + st.Step(); + st.Finalize(); - now.SetToGMTime(); + RemoveFromInsertList(localidentityid); - if(m_lastchecked<(now-(1.0/1440.0))) - { - CheckForNeededInsert(); - m_lastchecked=now; + m_log->debug("IntroductionPuzzleInserter::HandlePutSuccessful inserted puzzle "+idparts[3]); } + return true; } -void IntroductionPuzzleInserter::RegisterWithThread(FreenetMasterThread *thread) +void IntroductionPuzzleInserter::Initialize() { - thread->RegisterFCPConnected(this); - thread->RegisterFCPMessageHandler(this); - thread->RegisterPeriodicProcessor(this); + m_fcpuniquename="IntroductionPuzzleInserter"; + m_maxpuzzleinserts=50; } -void IntroductionPuzzleInserter::StartInsert(const long localidentityid) +const bool IntroductionPuzzleInserter::StartInsert(const long &localidentityid) { - DateTime now; - std::string idstring; + Poco::DateTime now; + std::string idstring=""; long index=0; - std::string indexstr; - UUIDGenerator uuid; - std::string messagebase; + std::string indexstr=""; + Poco::UUIDGenerator uuidgen; + Poco::UUID uuid; + std::string messagebase=""; IntroductionPuzzleXML xml; - std::string encodedpuzzle; - std::string solutionstring; - FCPMessage message; - std::string xmldata; - std::string xmldatasizestr; - std::string privatekey; + std::string encodedpuzzle=""; + std::string solutionstring=""; + FCPv2::Message message; + std::string xmldata=""; + std::string xmldatasizestr=""; + std::string privatekey=""; + std::string publickey=""; + std::string keypart=""; StringFunctions::Convert(localidentityid,idstring); - now.SetToGMTime(); - SQLite3DB::Recordset rs=m_db->Query("SELECT MAX(InsertIndex) FROM tblIntroductionPuzzleInserts WHERE Day='"+now.Format("%Y-%m-%d")+"' AND LocalIdentityID="+idstring+";"); + SQLite3DB::Recordset rs=m_db->Query("SELECT MAX(InsertIndex) FROM tblIntroductionPuzzleInserts WHERE Day='"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"' AND LocalIdentityID="+idstring+";"); if(rs.Empty() || rs.GetField(0)==NULL) { @@ -217,35 +207,83 @@ void IntroductionPuzzleInserter::StartInsert(const long localidentityid) } StringFunctions::Convert(index,indexstr); - SQLite3DB::Recordset rs2=m_db->Query("SELECT PrivateKey FROM tblLocalIdentity WHERE LocalIdentityID="+idstring+";"); - if(rs2.Empty()==false && rs2.GetField(0)!=NULL) + if(indexGet("MessageBase",messagebase); - - GenerateCaptcha(encodedpuzzle,solutionstring); + SQLite3DB::Recordset rs2=m_db->Query("SELECT PrivateKey,PublicKey FROM tblLocalIdentity WHERE LocalIdentityID="+idstring+";"); + if(rs2.Empty()==false && rs2.GetField(0)!=NULL) + { + privatekey=rs2.GetField(0); + if(rs2.GetField(1)) + { + publickey=rs2.GetField(1); + } + if(publickey.size()>=50) + { + // remove - and ~ + keypart=StringFunctions::Replace(StringFunctions::Replace(publickey.substr(4,43),"-",""),"~",""); + } + } - xml.SetType("captcha"); - xml.SetUUID(uuid.Generate()); - xml.SetPuzzleData(encodedpuzzle); - xml.SetMimeType("bitmap/image"); + Option option(m_db); + option.Get("MessageBase",messagebase); - xmldata=xml.GetXML(); - StringFunctions::Convert(xmldata.size(),xmldatasizestr); + GenerateCaptcha(encodedpuzzle,solutionstring); + if(encodedpuzzle.size()==0) + { + m_log->fatal("IntroductionPuzzleInserter::StartInsert could not create introduction puzzle"); + return false; + } - message.SetName("ClientPut"); - message["URI"]=privatekey+messagebase+"|"+now.Format("%Y-%m-%d")+"|IntroductionPuzzle|"+indexstr+".xml"; - message["Identifier"]="IntroductionPuzzleInserter|"+idstring+"|"+indexstr+"|"+xml.GetUUID()+"|"+message["URI"]; - message["UploadFrom"]="direct"; - message["DataLength"]=xmldatasizestr; - m_fcp->SendMessage(message); - m_fcp->SendRaw(xmldata.c_str(),xmldata.size()); + try + { + uuid=uuidgen.createRandom(); + } + catch(...) + { + m_log->fatal("IntroductionPuzzleInserter::StartInsert could not create UUID"); + } - m_db->Execute("UPDATE tblLocalIdentity SET InsertingPuzzle='true' WHERE LocalIdentityID="+idstring+";"); - m_db->Execute("INSERT INTO tblIntroductionPuzzleInserts(UUID,Type,MimeType,LocalIdentityID,PuzzleData,PuzzleSolution) VALUES('"+xml.GetUUID()+"','captcha','image/bmp',"+idstring+",'"+encodedpuzzle+"','"+solutionstring+"');"); + xml.SetType("captcha"); + std::string uuidstr=uuid.toString(); + StringFunctions::UpperCase(uuidstr,uuidstr); + xml.SetUUID(uuidstr+"@"+keypart); + xml.SetPuzzleData(encodedpuzzle); + xml.SetMimeType("image/bmp"); + + xmldata=xml.GetXML(); + StringFunctions::Convert(xmldata.size(),xmldatasizestr); + + message.SetName("ClientPut"); + message["URI"]=privatekey+messagebase+"|"+Poco::DateTimeFormatter::format(now,"%Y-%m-%d")+"|IntroductionPuzzle|"+indexstr+".xml"; + message["Identifier"]=m_fcpuniquename+"|"+idstring+"|"+indexstr+"|"+xml.GetUUID()+"|"+message["URI"]; + message["UploadFrom"]="direct"; + message["DataLength"]=xmldatasizestr; + m_fcp->Send(message); + m_fcp->Send(std::vector(xmldata.begin(),xmldata.end())); + + // insert to USK + 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"]; + message["DefaultName"]="IntroductionPuzzle.xml"; + message["Files.0.Name"]="IntroductionPuzzle.xml"; + message["Files.0.UplaodFrom"]="direct"; + message["Files.0.DataLength"]=xmldatasizestr; + 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+"');"); + + m_inserting.push_back(localidentityid); + + m_log->debug("IntroductionPuzzleInserter::StartInsert started insert for id "+idstring); + } + else + { + m_log->warning("IntroductionPuzzleInserter::StartInsert already inserted max puzzles for "+idstring); + } - m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"IntroductionPuzzleInserter::StartInsert started insert for id "+idstring); + return true; }